From 2c0fcdd857a73172daafe5f4ae12f07b2a7ed9e3 Mon Sep 17 00:00:00 2001 From: Oleg Kizeev Date: Fri, 28 Feb 2025 22:11:11 +0700 Subject: [PATCH] Named Arguments in Callbacks --- CHANGELOG.md | 1 + src/main/grammars/SlintParser.bnf | 5 ++- testData/ParsingTestData.slint | 9 ++++ testData/ParsingTestData.txt | 73 ++++++++++++++++++++++++++++++- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c017df8..0b83941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Features - Property Change Callbacks +- Named Arguments in Callbacks ### Fixed diff --git a/src/main/grammars/SlintParser.bnf b/src/main/grammars/SlintParser.bnf index 92ed600..1383df7 100644 --- a/src/main/grammars/SlintParser.bnf +++ b/src/main/grammars/SlintParser.bnf @@ -208,9 +208,12 @@ private PropertyDefinitionInit::= (':' | '<=>') Expression private CallbackDefinition ::= PURE? CALLBACK Identifier (CallbackParametersDeclaration | CallbackBindingDeclaration)? ';' { pin=3 } -private CallbackParametersDeclaration ::= '(' Type? (',' (Type | &')'))* ')' CallbackReturnDeclaration? { +private CallbackParametersDeclaration ::= '(' CallbackNamedParameter? (',' (CallbackNamedParameter | &')'))* ')' CallbackReturnDeclaration? { pin(".*")=1 } +private CallbackNamedParameter ::= Identifier ':' Type { + pin=3 +} private CallbackReturnDeclaration ::= '->' Type { pin=2 } private CallbackBindingDeclaration ::= '<=>' PropertyExpression diff --git a/testData/ParsingTestData.slint b/testData/ParsingTestData.slint index f0c7101..47d232c 100644 --- a/testData/ParsingTestData.slint +++ b/testData/ParsingTestData.slint @@ -126,4 +126,13 @@ export component Example inherits Window { } t := Text {} } +} + +// Named Arguments in Callbacks +export component NamedCallbackExample inherits Rectangle { + // Declare a callback with named argument + callback hello(foo: int, bar: string); + // The names can be overridden with + // anything when setting a handler + hello(aa, bb) => { /* ... */ } } \ No newline at end of file diff --git a/testData/ParsingTestData.txt b/testData/ParsingTestData.txt index e2193db..8ec8a2c 100644 --- a/testData/ParsingTestData.txt +++ b/testData/ParsingTestData.txt @@ -1,4 +1,4 @@ -SLINT_FILE(0,3330) +SLINT_FILE(0,3627) SlintImportDefinitionImpl(IMPORT_DEFINITION)(0,85) PsiElement(import)('import')(0,6) PsiWhiteSpace(' ')(6,7) @@ -1105,4 +1105,73 @@ SLINT_FILE(0,3330) PsiWhiteSpace('\n ')(3322,3327) PsiElement(})('}')(3327,3328) PsiWhiteSpace('\n')(3328,3329) - PsiElement(})('}')(3329,3330) \ No newline at end of file + PsiElement(})('}')(3329,3330) + PsiWhiteSpace('\n\n')(3330,3332) + PsiComment(LINE_COMMENT)('// Named Arguments in Callbacks')(3332,3363) + PsiWhiteSpace('\n')(3363,3364) + PsiElement(export)('export')(3364,3370) + PsiWhiteSpace(' ')(3370,3371) + SlintComponentDefinitionImpl(COMPONENT_DEFINITION)(3371,3627) + PsiElement(component)('component')(3371,3380) + PsiWhiteSpace(' ')(3380,3381) + SlintComponentNameImpl(COMPONENT_NAME)(3381,3401) + PsiElement(IDENTIFIER)('NamedCallbackExample')(3381,3401) + PsiWhiteSpace(' ')(3401,3402) + PsiElement(inherits)('inherits')(3402,3410) + PsiWhiteSpace(' ')(3410,3411) + SlintComponentNameImpl(COMPONENT_NAME)(3411,3420) + PsiElement(IDENTIFIER)('Rectangle')(3411,3420) + PsiWhiteSpace(' ')(3420,3421) + SlintComponentBodyImpl(COMPONENT_BODY)(3421,3627) + PsiElement({)('{')(3421,3422) + PsiWhiteSpace('\n ')(3422,3427) + PsiComment(LINE_COMMENT)('// Declare a callback with named argument')(3427,3468) + PsiWhiteSpace('\n ')(3468,3473) + PsiElement(callback)('callback')(3473,3481) + PsiWhiteSpace(' ')(3481,3482) + PsiElement(IDENTIFIER)('hello')(3482,3487) + PsiElement(()('(')(3487,3488) + PsiElement(IDENTIFIER)('foo')(3488,3491) + PsiElement(BAD_CHARACTER)(':')(3491,3492) + PsiWhiteSpace(' ')(3492,3493) + SlintTypeImpl(TYPE)(3493,3496) + SlintBuiltinTypeImpl(BUILTIN_TYPE)(3493,3496) + PsiElement(IDENTIFIER)('int')(3493,3496) + PsiElement(,)(',')(3496,3497) + PsiWhiteSpace(' ')(3497,3498) + PsiElement(IDENTIFIER)('bar')(3498,3501) + PsiElement(BAD_CHARACTER)(':')(3501,3502) + PsiWhiteSpace(' ')(3502,3503) + SlintTypeImpl(TYPE)(3503,3509) + SlintBuiltinTypeImpl(BUILTIN_TYPE)(3503,3509) + PsiElement(IDENTIFIER)('string')(3503,3509) + PsiElement())(')')(3509,3510) + PsiElement(;)(';')(3510,3511) + PsiWhiteSpace('\n ')(3511,3516) + PsiComment(LINE_COMMENT)('// The names can be overridden with')(3516,3551) + PsiWhiteSpace('\n ')(3551,3556) + PsiComment(LINE_COMMENT)('// anything when setting a handler')(3556,3590) + PsiWhiteSpace('\n ')(3590,3595) + PsiElement(IDENTIFIER)('hello')(3595,3600) + PsiElement(()('(')(3600,3601) + SlintPropertyExpressionImpl(PROPERTY_EXPRESSION)(3601,3603) + SlintPropertyExpressionCompositeImpl(PROPERTY_EXPRESSION_COMPOSITE)(3601,3603) + PsiElement(IDENTIFIER)('aa')(3601,3603) + PsiElement(,)(',')(3603,3604) + PsiWhiteSpace(' ')(3604,3605) + SlintPropertyExpressionImpl(PROPERTY_EXPRESSION)(3605,3607) + SlintPropertyExpressionCompositeImpl(PROPERTY_EXPRESSION_COMPOSITE)(3605,3607) + PsiElement(IDENTIFIER)('bb')(3605,3607) + PsiElement())(')')(3607,3608) + PsiWhiteSpace(' ')(3608,3609) + PsiElement(=)('=')(3609,3610) + PsiElement(>)('>')(3610,3611) + PsiWhiteSpace(' ')(3611,3612) + SlintCallbackBodyImpl(CALLBACK_BODY)(3612,3625) + PsiElement({)('{')(3612,3613) + PsiWhiteSpace(' ')(3613,3614) + PsiComment(DOC_COMMENT)('/* ... */')(3614,3623) + PsiWhiteSpace(' ')(3623,3624) + PsiElement(})('}')(3624,3625) + PsiWhiteSpace('\n')(3625,3626) + PsiElement(})('}')(3626,3627) \ No newline at end of file