From 36fadf3a301f7c2c9d9bde4b45bb4f97ae6da2f7 Mon Sep 17 00:00:00 2001 From: Jonas Wielage Date: Thu, 7 Nov 2024 16:39:59 +0100 Subject: [PATCH] GET / SET in grammar --- .../main/java/org/sonar/php/parser/PHPGrammar.java | 9 ++++++++- .../java/org/sonar/php/parser/PHPLexicalGrammar.java | 7 +++++++ .../main/java/org/sonar/php/parser/TreeFactory.java | 11 ++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java b/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java index 6c850da25..96ef90817 100644 --- a/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java +++ b/php-frontend/src/main/java/org/sonar/php/parser/PHPGrammar.java @@ -661,7 +661,7 @@ public PropertyHookTree PROPERTY_HOOK() { b.zeroOrMore(ATTRIBUTE_GROUP()), b.optional(b.token(PHPKeyword.FINAL)), b.optional(b.token(PHPPunctuator.AMPERSAND)), - NAME_IDENTIFIER_OR_KEYWORD(), + PROPERTY_HOOK_FUNCTION_NAME(), b.optional(PARAMETER_LIST()), b.optional(b.token(DOUBLEARROW)), b.firstOf( @@ -670,6 +670,13 @@ public PropertyHookTree PROPERTY_HOOK() { EXPRESSION_STATEMENT()))); } + public InternalSyntaxToken PROPERTY_HOOK_FUNCTION_NAME() { + return b.nonterminal(PHPLexicalGrammar.PROPERTY_HOOK_FUNCTION_NAME).is( + b.firstOf( + b.token(PHPLexicalGrammar.GET), + b.token(PHPLexicalGrammar.SET))); + } + /** * [ END ] Declaration */ diff --git a/php-frontend/src/main/java/org/sonar/php/parser/PHPLexicalGrammar.java b/php-frontend/src/main/java/org/sonar/php/parser/PHPLexicalGrammar.java index e16a77f90..4a213d148 100644 --- a/php-frontend/src/main/java/org/sonar/php/parser/PHPLexicalGrammar.java +++ b/php-frontend/src/main/java/org/sonar/php/parser/PHPLexicalGrammar.java @@ -65,6 +65,7 @@ public enum PHPLexicalGrammar implements GrammarRuleKey { PROPERTY_HOOK_LIST, PROPERTY_HOOK, + PROPERTY_HOOK_FUNCTION_NAME, TRAIT_METHOD_REFERENCE_FULLY_QUALIFIED, TRAIT_METHOD_REFERENCE, @@ -255,6 +256,9 @@ public enum PHPLexicalGrammar implements GrammarRuleKey { CLONE, PRINT, + GET, + SET, + SELF, PARENT, @@ -353,6 +357,9 @@ public static void lexical(LexerlessGrammarBuilder b) { b.rule(CLONE).is(word(b, "CLONE")).skip(); b.rule(PRINT).is(word(b, "PRINT")).skip(); + b.rule(GET).is(word(b, "GET")).skip(); + b.rule(SET).is(word(b, "SET")).skip(); + b.rule(SELF).is(word(b, "SELF")).skip(); b.rule(PARENT).is(word(b, "PARENT")).skip(); diff --git a/php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java b/php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java index 8d44259c2..7702b20b1 100644 --- a/php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java +++ b/php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java @@ -711,27 +711,20 @@ public PropertyHookTree propertyHook( Optional> attributes, Optional modifier, Optional referenceToken, - NameIdentifierTree name, + InternalSyntaxToken name, Optional parameters, Optional doubleArrowToken, Tree body) { - throwOnUnrecognizedPropertyHookName(name); return new PropertyHookTreeImpl( attributes.or(Collections.emptyList()), modifier.orNull(), referenceToken.orNull(), - name, + new NameIdentifierTreeImpl(name), parameters.orNull(), doubleArrowToken.orNull(), body); } - private static void throwOnUnrecognizedPropertyHookName(NameIdentifierTree name) { - if (!"get".equals(name.text()) && !"set".equals(name.text())) { - throw new RecognitionException(((PHPTree) name).getLine(), "Declared property hook must be named \"get\" or \"set\""); - } - } - /** * [ END ] Declarations */