[php] add Semgrep grammar augmentation (was empty)#583
Draft
brandonspark wants to merge 2 commits into
Draft
Conversation
Wire up `semgrep_ellipsis`, `semgrep_deep_ellipsis`, `semgrep_variadic_metavariable` (`$...ARGS`), and `semgrep_metavar_ident` into the previously-empty semgrep-php grammar so PHP patterns can use metavariables in non-variable positions and ellipses in any expression, statement, member, parameter, argument, and match-arm position. PHP's native `$FOO` parses as a `variable_name`, so we only added a metavariable token for *identifier* positions (class/interface/trait/ enum/function/method names, type references, base/interface clauses, and attribute names). The variable-variable form `$$F` keeps its native meaning since `semgrep_metavar_ident` is never accepted in variable positions. Closes LANG-474, LANG-475. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The PHP Semgrep grammar augmentation file at
lang/semgrep-grammars/src/semgrep-php/grammar.jswas a no-op (entirerulesblock commented out). PHP variables natively start with$so simple$X-style metavariable patterns parsed as PHP variables, but every other Semgrep construct (ellipsis in non-variadic positions,$...ARGS, metavar-as-class/function/type/attribute name) failed.This PR builds out the PHP augmentation, modeled on
semgrep-javaandsemgrep-kotlin. Closes LANG-474, LANG-475.Companion release PR: semgrep/semgrep-php#4
What's added
semgrep_ellipsis(...) wired into expression, statement, member-declaration (class/interface/trait), enum-body, formal-parameter, match-arm positions.semgrep_deep_ellipsis(<... expr ...>) in expression position.semgrep_variadic_metavariable($...ARGS) in formal-parameter and argument positions.semgrep_metavar_ident($FOOin identifier position) wired into class/interface/trait/enum/function/method names, named types, base/interface clauses, and attribute names.$$Flexer interactionPHP's variable-variable form
$$Fkeeps its native meaning (dynamic_variable_name($variable_name(F))) becausesemgrep_metavar_identis only accepted in identifier positions, not variable positions. This means the LANG-475 "metavar property name with$$F" sub-case (class $C { public $T $$F = $V; }) is NOT addressed in this PR —$$Fcontinues to be parsed as variable-variable. That edge case can be tackled in a follow-up if needed, since this PR already unblocks the much larger set of patterns covered in LANG-474's triage. A regression test verifies$$Fstill parses correctly.Test plan
make build && make testinlang/semgrep-grammars/src/semgrep-php/(all 105 tests pass: 81 inherited + 24 new semgrep tests, including a regression test that$$Fstill parses as variable-variable).semgrep/semgrep-phpto release the regenerated parser: [php] add Semgrep grammar augmentation (was empty) semgrep-php#4.🤖 Generated with Claude Code