Skip to content

Commit

Permalink
GET / SET in grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-wielage-sonarsource committed Nov 7, 2024
1 parent 6501c51 commit 36fadf3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -670,6 +670,13 @@ public PropertyHookTree PROPERTY_HOOK() {
EXPRESSION_STATEMENT())));
}

public InternalSyntaxToken PROPERTY_HOOK_FUNCTION_NAME() {
return b.<InternalSyntaxToken>nonterminal(PHPLexicalGrammar.PROPERTY_HOOK_FUNCTION_NAME).is(
b.firstOf(
b.token(PHPLexicalGrammar.GET),
b.token(PHPLexicalGrammar.SET)));
}

/**
* [ END ] Declaration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -255,6 +256,9 @@ public enum PHPLexicalGrammar implements GrammarRuleKey {
CLONE,
PRINT,

GET,
SET,

SELF,
PARENT,

Expand Down Expand Up @@ -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();

Expand Down
11 changes: 2 additions & 9 deletions php-frontend/src/main/java/org/sonar/php/parser/TreeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,27 +711,20 @@ public PropertyHookTree propertyHook(
Optional<List<AttributeGroupTree>> attributes,
Optional<SyntaxToken> modifier,
Optional<InternalSyntaxToken> referenceToken,
NameIdentifierTree name,
InternalSyntaxToken name,
Optional<ParameterListTree> parameters,
Optional<InternalSyntaxToken> 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
*/
Expand Down

0 comments on commit 36fadf3

Please sign in to comment.