Debian Codegraph @

lezer-php/src/php.grammar

raw
1 @precedence {
2 pair,
3 namespace,
4 member,
5 scope,
6 new,
7 call,
8 cast,
9
10 else @right,
11
12 optionalType,
13 unionType @left,
14 intersectionType @left,
15
16 incdec @left,
17 instanceof @left,
18 unary @left,
19 exponent @right,
20 mult @left,
21 plus @left,
22 shift @left,
23 concat @left,
24 compare @left,
25 equal @left,
26 binAnd @left,
27 binXor @left,
28 binOr @left,
29 logAnd @left,
30 logOr @left,
31 qq @right,
32
33 conditional @left,
34 update @right,
35
36 and @left,
37 xor @left,
38 or @left,
39
40 yield @right
41 }
42
43 @skip {} {
44 @top Template {
45 TextInterpolation { Text? PhpOpen } (LineComment | BlockComment | whitespace | TextInterpolation)* topStatements |
46 Text?
47 }
48 }
49
50 @top Program {
51 statement*
52 }
53
54 topStatements { statement* eof }
55
56 statement[@isGroup=Statement] {
57 EmptyStatement { ";" } |
58 Block |
59 LabelStatement { Name ":" } |
60 ExpressionStatement { expression semicolon } |
61 IfStatement {
62 if ParenthesizedExpression (
63 statement (!else elseif ParenthesizedExpression statement)* (!else else statement)? |
64 ColonBlock (elseif ParenthesizedExpression ColonBlock)* (else ColonBlock)? endif semicolon
65 )
66 } |
67 SwitchStatement {
68 switch ParenthesizedExpression (
69 Block { "{" (CaseStatement | DefaultStatement)* "}" } |
70 ColonBlock { ":" (CaseStatement | DefaultStatement)* } endswitch semicolon
71 )
72 } |
73 WhileStatement {
74 while ParenthesizedExpression (trailingStatement | ColonBlock endwhile semicolon)
75 } |
76 DoStatement {
77 do statement while ParenthesizedExpression semicolon
78 } |
79 ForStatement {
80 for ForSpec { "(" expressions? ";" expressions? ";" expressions? ")" }
81 (trailingStatement | ColonBlock endfor semicolon)
82 } |
83 ForeachStatement {
84 foreach ForSpec { "(" expression as (Pair { expression "=>" expression } | expression) ")" }
85 (trailingStatement | ColonBlock endforeach semicolon)
86 } |
87 GotoStatement {
88 goto Name semicolon
89 } |
90 ContinueStatement {
91 continue expression? semicolon
92 } |
93 BreakStatement {
94 break expression? semicolon
95 } |
96 ReturnStatement {
97 return expression? semicolon
98 } |
99 TryStatement {
100 try Block (catch CatchDeclarator { "(" type VariableName? ")" } Block | finally Block)+
101 } |
102 DeclareStatement {
103 declare "(" Name "=" literal ")" (trailingStatement | ColonBlock enddeclare semicolon)
104 } |
105 EchoStatement {
106 echo expressions semicolon
107 } |
108 UnsetStatement {
109 unset "(" commaSep1<expression> ")" semicolon
110 } |
111 ConstDeclaration {
112 Attributes* Visibility? const commaSep1<ConstDeclarator> semicolon
113 } |
114 FunctionDefinition {
115 Attributes* functionDefinitionHeader Block
116 } |
117 ClassDeclaration {
118 Attributes* (final | abstract)? class Name BaseClause? ClassInterfaceClause? DeclarationList
119 } |
120 InterfaceDeclaration {
121 interface Name BaseClause? DeclarationList
122 } |
123 TraitDeclaration {
124 trait Name DeclarationList
125 } |
126 EnumDeclaration {
127 Attributes* enum Name (":" type)? ClassInterfaceClause? EnumBody
128 } |
129 NamespaceDefinition {
130 namespace (qualifiedName semicolon | qualifiedName? Block)
131 } |
132 NamespaceUseDeclaration {
133 use (function | const)?
134 (commaSep1<qualifiedName (as Name)?> |
135 ("\\" NamespaceNameInner | NamespaceName) !namespace "\\" (NamespaceNameInner "\\")* UseGroup)
136 semicolon
137 } |
138 GlobalDeclaration {
139 global commaSep1<expression> semicolon
140 } |
141 FunctionStaticDeclaration {
142 static commaSep1<VariableDeclarator> semicolon
143 }
144 }
145
146 qualifiedName {
147 Name |
148 QualifiedName {
149 "\\" (NamespaceNameInner !namespace "\\")* Name { rawName } |
150 NamespaceName !namespace "\\" (NamespaceNameInner "\\")* Name { rawName }
151 }
152 }
153
154 NamespaceName { name }
155
156 NamespaceNameInner[@name=NamespaceName] { rawName }
157
158 UseGroup {
159 "{" commaSep1<UseClause { (function | const)? qualifiedName (as Name)? }> "}"
160 }
161
162 BaseClause {
163 extends commaSep1<qualifiedName>
164 }
165
166 EnumBody {
167 "{" (EnumCase | memberDeclaration)* "}"
168 }
169
170 EnumCase {
171 Attributes* case Name ("=" (String | Integer))? semicolon
172 }
173
174 DeclarationList {
175 "{" memberDeclaration* "}"
176 }
177
178 ClassInterfaceClause {
179 implements commaSep1<qualifiedName>
180 }
181
182 memberDeclaration {
183 ConstDeclaration { Attributes* modifier* const type? commaSep1<ConstDeclarator> semicolon } |
184 PropertyDeclaration { Attributes* modifier* type? commaSep1<VariableDeclarator> (semicolon | PropertyHooks) } |
185 MethodDeclaration |
186 UseDeclaration
187 }
188
189 PropertyHooks { "{" PropertyHook* "}" }
190 PropertyHook { Name ParamList? (Block | "=>" expression semicolon) }
191
192 modifier {
193 var |
194 Visibility |
195 static |
196 final |
197 abstract |
198 readonly
199 }
200
201 VariableDeclarator { VariableName ("=" expression)? }
202
203 ConstDeclarator[@name=VariableDeclarator] { Name "=" expression }
204
205 MethodDeclaration {
206 Attributes* modifier* functionDefinitionHeader (Block | semicolon)
207 }
208
209 UseDeclaration {
210 use commaSep1<qualifiedName> (UseList | semicolon)
211 }
212
213 UseList {
214 "{" ((UseInsteadOfClause | UseAsClause) semicolon)* "}"
215 }
216
217 UseInsteadOfClause {
218 (ScopedExpression | Name) insteadof Name
219 }
220
221 UseAsClause {
222 (ScopedExpression | Name) as (Visibility? Name | Visibility Name?)
223 }
224
225 functionDefinitionHeader {
226 function "&"? Name ParamList (":" type)?
227 }
228
229 ParamList {
230 "(" commaSep<parameter> ")"
231 }
232
233 parameter {
234 Parameter {
235 Attributes* type? "&"? VariableName ("=" expression)?
236 } |
237 VariadicParameter {
238 Attributes* type? "&"? "..." VariableName
239 } |
240 PropertyParameter {
241 Visibility type? VariableName ("=" expression | PropertyHooks)?
242 }
243 }
244
245 type[@isGroup=Type] {
246 UnionType { type (!unionType LogicOp<"|"> type)+ } |
247 IntersectionType { type (!intersectionType "&" type)+ } |
248 OptionalType { !optionalType LogicOp<"?"> type } |
249 NamedType { qualifiedName | null | array }
250 }
251
252 literal {
253 Integer |
254 Float |
255 string |
256 Boolean |
257 null
258 }
259
260 expressions {
261 expression |
262 SequenceExpression { expression ("," expression)+ }
263 }
264
265 ColonBlock { ":" statement* }
266
267 trailingStatement {
268 statement |
269 EmptyStatement { automaticSemicolon }
270 }
271
272 MatchArm { (default | commaSep1<expression>) "=>" expression }
273
274 CaseStatement { case expression (":" | ";") statement* }
275
276 DefaultStatement { default (":" | ";") statement* }
277
278 Block { "{" statement* "}" }
279
280 expression[@isGroup=Expression] {
281 ConditionalExpression { expression !conditional LogicOp<"?"> expression? ":" expression } |
282 MatchExpression { match ParenthesizedExpression MatchBlock { "{" commaSep1<MatchArm> "}" } } |
283 AssignmentExpression { variable !update "=" "&"? expression } |
284 UpdateExpression { variable !update UpdateOp expression } |
285 YieldExpression { yield (!yield arrayElement | from expression)? } |
286 BinaryExpression |
287 IncludeExpression { (include | include_once) expression } |
288 RequireExpression { (require | require_once) expression } |
289 CloneExpression { clone expression } |
290 UnaryExpression {
291 ControlOp<"@"> expression |
292 (ArithOp<"+" | "-"> | LogicOp<"~" | "!">) !unary expression
293 } |
294 qualifiedName |
295 PrintIntrinsic { print expression } |
296 FunctionExpression {
297 static? function "&"? ParamList (use UseList { "(" commaSep<"&"? VariableName> ")" })? (":" type)? Block
298 } |
299 ArrowFunction { static? fn "&"? ParamList (":" type)? "=>" expression } |
300 NewExpression {
301 new expression (!new ArgList)? |
302 new readonly? class ArgList? BaseClause? ClassInterfaceClause? DeclarationList
303 } |
304 IncDecExpression[@name=UpdateExpression] {
305 expression !incdec ArithOp<"++" | "--"> |
306 ArithOp<"++" | "--"> !incdec expression
307 } |
308 ShellExpression |
309 ParenthesizedExpression |
310 ThrowExpression { throw expression } |
311 variable |
312 literal
313 }
314
315 variable {
316 ArrayExpression {
317 array ValueList { "(" ","? commaSep<arrayElement> ")" } |
318 "[" ","? commaSep<arrayElement> "]"
319 } |
320 ListExpression {
321 list ValueList { "(" ","? commaSep<expression | Pair { expression !pair "=>" expression }> ")" }
322 } |
323 SubscriptExpression { expression !member "[" expression? "]" } |
324 MemberExpression { expression !member ("->" | "?->") memberName } |
325 DynamicVariable |
326 VariableName |
327 CallExpression { expression !call ArgList } |
328 CastExpression { castOpen type ")" !cast expression } |
329 ScopedExpression
330 }
331
332 DynamicVariable { "$" (VariableName | DynamicVariable) | "${" expression "}" }
333
334 ParenthesizedExpression { "(" expression ")" }
335
336 ScopedExpression { expression !scope "::" ClassMemberName }
337
338 ClassMemberName {
339 Name | VariableName | DynamicMemberName { "{" expression "}" }
340 }
341
342 ArgList {
343 "(" commaSep<argument> ")"
344 }
345
346 argument {
347 NamedArgument { Name ":" expression } |
348 SpreadArgument { "..." expression? } |
349 expression
350 }
351
352 memberName {
353 MemberName | VariableName | "{" expression "}"
354 }
355
356 arrayElement {
357 "&"? expression |
358 VariadicUnpacking { "..." expression } |
359 Pair { expression !pair "=>" "&"? expression }
360 }
361
362 Attributes {
363 "#[" commaSep1<Attribute> "]"
364 }
365
366 Attribute {
367 qualifiedName ArgList?
368 }
369
370 @skip {} {
371 TextInterpolation { PhpClose Text? (PhpOpen | eof) }
372
373 Text[isolate] { textElement+ }
374
375 InterpolatedString[@name=String] {
376 startInterpolatedString (
377 interpolatedStringContent |
378 EscapeSequence |
379 interpolatedExpression |
380 Interpolation
381 )* endInterpolatedString
382 }
383
384 // The afterInterpolation token isn't actually real, but used as a
385 // signal (via canShift) to the tokenizer to indicate it shouldn't
386 // consume '[' or '->' tokens.
387 interpolatedExpression {
388 VariableName |
389 MemberExpression {
390 interpolatedExpression afterInterpolation? ("?->" | "->") Name
391 } |
392 SubscriptExpression {
393 interpolatedExpression afterInterpolation?
394 "[" (Integer | UnaryExpression { ArithOp<"-"> Integer } | Name | VariableName) "]"
395 }
396 }
397 }
398
399 Interpolation { ("{" | "${") expression "}" }
400
401 string {
402 InterpolatedString |
403 HeredocString |
404 String
405 }
406
407 BinaryExpression {
408 expression !instanceof instanceof expression |
409 expression !qq LogicOp<"??"> expression |
410 expression !and and expression |
411 expression !or or expression |
412 expression !xor xor expression |
413 expression !logOr LogicOp<"||"> expression |
414 expression !logAnd LogicOp<"&&"> expression |
415 expression !binOr BitOp<"|"> expression |
416 expression !binXor BitOp<"^"> expression |
417 expression !binAnd BitOp { "&" } expression |
418 expression !equal CompareOp<"==" | "===" | "!=" | "!==" | "<>" | "<=>"> expression |
419 expression !compare CompareOp<"<" | ">" | "<=" | ">="> expression |
420 expression !shift BitOp<"<<" | ">>"> expression |
421 expression !plus ArithOp<"+" | "-"> expression |
422 expression !concat ConcatOp<"."> expression |
423 expression !mult ArithOp<"*" | "/" | "%"> expression |
424 expression !exponent ArithOp<"**"> expression
425 }
426
427 Name { name }
428
429 MemberName[@name=Name] { rawName }
430
431 semicolon {
432 automaticSemicolon |
433 ";"
434 }
435
436 @skip { LineComment | BlockComment | whitespace | TextInterpolation }
437
438 @external specialize {name} keywords from "./tokens" {
439 abstract[@name=abstract], and[@name=LogicOp], array[@name=array], as[@name=as], Boolean,
440 break[@name=break], case[@name=case], catch[@name=catch],
441 clone[@name=clone], const[@name=const], continue[@name=continue], default[@name=default],
442 declare[@name=declare], do[@name=do], echo[@name=echo], else[@name=else], elseif[@name=elseif],
443 enddeclare[@name=enddeclare], endfor[@name=endfor], endforeach[@name=endforeach],
444 endif[@name=endif], endswitch[@name=endswitch], endwhile[@name=endwhile], enum[@name=enum],
445 extends[@name=extends], final[@name=final], finally[@name=finally], fn[@name=fn], for[@name=for],
446 foreach[@name=foreach], from[@name=from], function[@name=function], global[@name=global],
447 goto[@name=goto], if[@name=if], implements[@name=implements], include[@name=include],
448 include_once[@name=include_once], instanceof[@name=LogicOp], insteadof[@name=insteadof],
449 interface[@name=interface], list[@name=list], match[@name=match], namespace[@name=namespace],
450 new[@name=new], null[@name=null], or[@name=LogicOp], print[@name=print], readonly[@name=readonly],
451 require[@name=require], require_once[@name=require_once],
452 return[@name=return], switch[@name=switch], throw[@name=throw],
453 trait[@name=trait], try[@name=try], unset[@name=unset], use[@name=use], var[@name=var], Visibility,
454 while[@name=while], xor[@name=LogicOp], yield[@name=yield]
455 }
456
457 commaSep<content> { "" | content ("," content?)* }
458 commaSep1<content> { content ("," content?)* }
459
460 static[@dynamicPrecedence=1] { @extend[@name=static]<name, "static" | "STATIC"> }
461 class { @extend[@name=class]<name, "class" | "CLASS"> }
462
463 @external tokens expression from "./tokens" {
464 castOpen[@name="("],
465 HeredocString
466 }
467
468 @external tokens interpolated from "./tokens" {
469 interpolatedStringContent,
470 EscapeSequence,
471 afterInterpolation
472 }
473
474 @external tokens semicolon from "./tokens" { automaticSemicolon }
475
476 @tokens {
477 whitespace { $[ \t\n\r⁠​ ]+ }
478
479 PhpOpen[closedBy=phpClose] { "<?" ($[pP] $[hH] $[pP] | "=")? }
480
481 PhpClose[openedBy=phpOpen] { "?>" }
482
483 textElement {
484 "\n" | ![\n<] textElement? | "<" ("\n" | @eof | ![\n\?] textElement?)
485 }
486
487 digit { $[0-9] }
488 hex { $[0-9A-Fa-f] }
489 oct { $[0-7] }
490
491 separatedDigits { ("_" digit+)* }
492
493 exponent { $[eE] $[+-]? digit+ separatedDigits }
494
495 Float {
496 digit+ separatedDigits ("." digit* separatedDigits exponent? | exponent) |
497 "." digit+ separatedDigits exponent?
498 }
499
500 Integer {
501 digit+ separatedDigits |
502 "0" $[oO] oct+ ("_" oct+)* |
503 "0" $[xX] hex+ ("_" hex+)* |
504 "0" $[bB] $[01]+ ("_" $[01]+)*
505 }
506
507 @precedence { Float, ConcatOp<".">, Integer }
508
509 ShellExpression { "`" ("\\" _ | ![`\\])* "`" }
510
511 UpdateOp { ("**" | "*" | "/" | "%" | "+" | "-" | "." | "<<" | ">>" | "&" | "^" | "|" | "??") "=" }
512
513 CompareOp<term> { term }
514 ArithOp<term> { term }
515 LogicOp<term> { term }
516 BitOp<term> { term }
517 ControlOp<term> { term }
518 ConcatOp<term> { term }
519
520 startInterpolatedString { $[bB]? '"' }
521 endInterpolatedString { '"' }
522
523 @precedence { startInterpolatedString, name }
524
525 String[isolate] { $[bB]? "'" (![\\'] | "\\" _)* "'"? }
526
527 @precedence { String, name }
528
529 letter { $[_a-zA-Z\u00A1-\u00ff] }
530
531 name { letter (letter | @digit)* }
532
533 rawName { name }
534
535 VariableName { "$" name }
536
537 @precedence { VariableName, "${", "$" }
538
539 LineComment[isolate] { ("//" | "#") lineCommentRest? }
540 lineCommentRest { ![\r\n?] lineCommentRest? | "?" lineCommentQuestion }
541 lineCommentQuestion { $[\r\n] | ![>\r\n] lineCommentRest? }
542
543 @precedence { "#[", LineComment, ArithOp<"*" | "/" | "%"> }
544
545 BlockComment[isolate] { "/*" blockCommentRest }
546 blockCommentRest { ![*] blockCommentRest | "*" blockCommentAfterStar }
547 blockCommentAfterStar { "/" | "*" blockCommentAfterStar | ![/*] blockCommentRest }
548
549 "${" "{" "}" "#[" "[" "]" "(" ")"
550 ";" ":" "::" "," "\\" "?->" "->" "=>" "&" "$" "..."
551 "="[@name=AssignOp]
552 }
553
554 @external tokens eofToken from "./tokens" { eof }
555
556 @external propSource phpHighlighting from "./highlight"
557
558 @detectDelim