-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree-sitter-php.ebnf
770 lines (573 loc) · 19.1 KB
/
tree-sitter-php.ebnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//
// From tree-sitter-php/php/src/grammar.json
//
//
// EBNF to generate railroad diagram at
// (IPV6) https://www.bottlecaps.de/rr/ui
// (IPV4) https://rr.red-dove.com/ui
//
program ::=
text? ( php_tag _statement* )?
php_tag ::=
'<\'?([pP][hH][pP]|'=')?
text_interpolation ::=
'?>' text? ( php_tag | _eof )
text ::=
( '<' | [^ #x09#x0A#x0B#x0C#x0D<][^<]* )+
_statement ::=
empty_statement
| compound_statement
| named_label_statement
| expression_statement
| if_statement
| switch_statement
| while_statement
| do_statement
| for_statement
| foreach_statement
| goto_statement
| continue_statement
| break_statement
| return_statement
| try_statement
| declare_statement
| echo_statement
| unset_statement
| const_declaration
| function_definition
| class_declaration
| interface_declaration
| trait_declaration
| enum_declaration
| namespace_definition
| namespace_use_declaration
| global_declaration
| function_static_declaration
empty_statement ::=
';'
reference_modifier ::=
'&'
function_static_declaration ::=
'static' static_variable_declaration ( ',' static_variable_declaration )* _semicolon
static_variable_declaration ::=
variable_name ( '=' _expression )?
global_declaration ::=
'global' _variable_name ( ',' _variable_name )* _semicolon
namespace_definition ::=
'namespace' ( namespace_name _semicolon | namespace_name? compound_statement )
namespace_use_declaration ::=
'use' ( 'function' | 'const' )? ( namespace_use_clause ( ',' namespace_use_clause )* | '\\' ? namespace_name '\\' namespace_use_group ) _semicolon
namespace_use_clause ::=
( name | _reserved_identifier | qualified_name ) namespace_aliasing_clause?
qualified_name ::=
namespace_name_as_prefix name
namespace_name_as_prefix ::=
'\\'
| '\\' ? namespace_name '\\'
| 'namespace' '\\'
| 'namespace' '\\' ? namespace_name '\\'
namespace_name ::=
name ( '\\' name )*
namespace_aliasing_clause ::=
'as' name
namespace_use_group ::=
'{' namespace_use_group_clause ( ',' namespace_use_group_clause )* '}'
namespace_use_group_clause ::=
( 'function' | 'const' )? namespace_name namespace_aliasing_clause?
trait_declaration ::=
'trait' name declaration_list
interface_declaration ::=
'interface' name base_clause? declaration_list
base_clause ::=
'extends' ( name | _reserved_identifier | qualified_name ) ( ',' ( name | _reserved_identifier | qualified_name ) )*
enum_declaration ::=
attribute_list? 'enum' name ( ':' ( 'string' | 'int' ) )? class_interface_clause? enum_declaration_list
enum_declaration_list ::=
'{' _enum_member_declaration* '}'
_enum_member_declaration ::=
enum_case
| method_declaration
| use_declaration
enum_case ::=
attribute_list? 'case' name ( '=' ( _string | integer ) )? _semicolon
class_declaration ::=
attribute_list? ( final_modifier | abstract_modifier )? readonly_modifier? 'class' name base_clause? class_interface_clause? declaration_list _semicolon?
declaration_list ::=
'{' _member_declaration* '}'
final_modifier ::=
'final'
abstract_modifier ::=
'abstract'
readonly_modifier ::=
'readonly'
class_interface_clause ::=
'implements' ( name | _reserved_identifier | qualified_name ) ( ',' ( name | _reserved_identifier | qualified_name ) )*
_member_declaration ::=
_class_const_declaration
| property_declaration
| method_declaration
| use_declaration
const_declaration ::=
_const_declaration
_class_const_declaration ::=
attribute_list? final_modifier? _const_declaration
_const_declaration ::=
visibility_modifier? 'const' _type? const_element ( ',' const_element )* _semicolon
property_declaration ::=
attribute_list? _modifier+ _type? property_element ( ',' property_element )* _semicolon
_modifier ::=
var_modifier | visibility_modifier | static_modifier | final_modifier | abstract_modifier | readonly_modifier
property_element ::=
variable_name property_initializer?
property_initializer ::=
'=' _expression
method_declaration ::=
attribute_list? _modifier* _function_definition_header ( compound_statement | _semicolon )
var_modifier ::=
'var'
static_modifier ::=
'static'
use_declaration ::=
'use' ( name | _reserved_identifier | qualified_name ) ( ',' ( name | _reserved_identifier | qualified_name ) )* ( use_list | _semicolon )
use_list ::=
'{' ( ( use_instead_of_clause | use_as_clause ) _semicolon )* '}'
use_instead_of_clause ::=
( class_constant_access_expression | name ) 'insteadof' name
use_as_clause ::=
( class_constant_access_expression | name ) 'as' ( visibility_modifier? name | visibility_modifier name? )
visibility_modifier ::=
'public'
| 'protected'
| 'private'
function_definition ::=
attribute_list? _function_definition_header compound_statement
_function_definition_header ::=
'function' reference_modifier? ( name | _reserved_identifier ) formal_parameters _return_type?
_arrow_function_header ::=
attribute_list? static_modifier? 'fn' reference_modifier? formal_parameters _return_type?
arrow_function ::=
_arrow_function_header '=>' _expression
formal_parameters ::=
'(' ( ( simple_parameter | variadic_parameter | property_promotion_parameter ) ( ',' ( simple_parameter | variadic_parameter | property_promotion_parameter ) )* )? ','? ')'
property_promotion_parameter ::=
attribute_list? visibility_modifier readonly_modifier? _type? variable_name ( '=' _expression )?
simple_parameter ::=
attribute_list? _type? reference_modifier? variable_name ( '=' _expression )?
variadic_parameter ::=
attribute_list? _type? reference_modifier? '...' variable_name
_type ::=
_types
| union_type
| intersection_type
| disjunctive_normal_form_type
_types ::=
optional_type
| named_type
| primitive_type
named_type ::=
name
| qualified_name
optional_type ::=
'?' ( named_type | primitive_type )
bottom_type ::=
'never'
union_type ::=
_types ( '|' _types )*
intersection_type ::=
_types ( '&' _types )*
disjunctive_normal_form_type ::=
( '(' intersection_type ')' | _types ) ( '|' ( '(' intersection_type ')' | _types ) )*
primitive_type ::=
'array'
| 'callable'
| 'iterable'
| 'bool'
| 'float'
| 'int'
| 'string'
| 'void'
| 'mixed'
| 'static'
| 'false'
| 'null'
| 'true'
cast_type ::=
'array'
| 'binary'
| 'bool'
| 'boolean'
| 'double'
| 'int'
| 'integer'
| 'float'
| 'object'
| 'real'
| 'string'
| 'unset'
_return_type ::=
':' ( _type | bottom_type )
const_element ::=
( name | _reserved_identifier ) '=' _expression
echo_statement ::=
'echo' _expressions _semicolon
unset_statement ::=
'unset' '(' _variable ( ',' _variable )* ')' _semicolon
declare_statement ::=
'declare' '(' declare_directive ')' ( _statement | ':' _statement* 'enddeclare' _semicolon | _semicolon )
declare_directive ::=
( 'ticks' | 'encoding' | 'strict_types' ) '=' _literal
_literal ::=
integer
| float
| _string
| boolean
| null
float ::=
[0-9]*('_'[0-9]+)*(('.'[0-9]*('_'[0-9]+)*)?([eE][\+-]?[0-9]+('_'[0-9]+)*)|('.'[0-9]*('_'[0-9]+)*)([eE][\+-]?[0-9]+('_'[0-9]+)*)?)
try_statement ::=
'try' compound_statement ( catch_clause | finally_clause )+
catch_clause ::=
'catch' '(' type_list variable_name? ')' compound_statement
type_list ::=
named_type ( '|' named_type )*
finally_clause ::=
'finally' compound_statement
goto_statement ::=
'goto' name _semicolon
continue_statement ::=
'continue' _expression? _semicolon
break_statement ::=
'break' _expression? _semicolon
integer ::=
( [1-9][0-9]*('_'[0-9]+)* | '0'[oO]?[0-7]*('_'[0-7]+)* | '0'[xX][0-9a-fA-F]+('_'[0-9a-fA-F]+)* | '0'[bB][01]+('_'[01]+)* )
return_statement ::=
'return' _expression? _semicolon
throw_expression ::=
'throw' _expression
while_statement ::=
'while' parenthesized_expression ( _statement | colon_block 'endwhile' _semicolon )
do_statement ::=
'do' _statement 'while' parenthesized_expression _semicolon
for_statement ::=
'for' '(' _expressions? ';' _expressions? ';' _expressions? ')' ( _semicolon | _statement | ':' _statement* 'endfor' _semicolon )
_expressions ::=
_expression
| sequence_expression
sequence_expression ::=
_expression ',' ( sequence_expression | _expression )
foreach_statement ::=
'foreach' '(' _expression 'as' ( foreach_pair | _foreach_value ) ')' ( _semicolon | _statement | colon_block 'endforeach' _semicolon )
foreach_pair ::=
_expression '=>' _foreach_value
_foreach_value ::=
by_ref
| _expression
| list_literal
if_statement ::=
'if' parenthesized_expression ( _statement else_if_clause* else_clause? | colon_block else_if_clause_2* else_clause_2? 'endif' _semicolon )
colon_block ::=
':' _statement*
else_if_clause ::=
'elseif' parenthesized_expression _statement
else_clause ::=
'else' _statement
else_if_clause_2 ::=
'elseif' parenthesized_expression colon_block
else_clause_2 ::=
'else' colon_block
match_expression ::=
'match' parenthesized_expression match_block
match_block ::=
'{' ( ( match_conditional_expression | match_default_expression ) ( ',' ( match_conditional_expression | match_default_expression ) )* )? ','? '}'
match_condition_list ::=
_expression ( ',' _expression )* ','?
match_conditional_expression ::=
match_condition_list '=>' _expression
match_default_expression ::=
'default' '=>' _expression
switch_statement ::=
'switch' parenthesized_expression switch_block
switch_block ::=
'{' ( case_statement | default_statement )* '}'
| ':' ( case_statement | default_statement )* 'endswitch' _semicolon
case_statement ::=
'case' _expression ( ':' | ';' ) _statement*
default_statement ::=
'default' ( ':' | ';' ) _statement*
compound_statement ::=
'{' _statement* '}'
named_label_statement ::=
name ':'
expression_statement ::=
_expression _semicolon
_expression ::=
conditional_expression
| match_expression
| augmented_assignment_expression
| assignment_expression
| reference_assignment_expression
| yield_expression
| _unary_expression
| error_suppression_expression
| binary_expression
| include_expression
| include_once_expression
| require_expression
| require_once_expression
_unary_expression ::=
clone_expression
| _primary_expression
| unary_op_expression
| cast_expression
unary_op_expression ::=
( '+' | '-' | '~' | '!' ) _expression
error_suppression_expression ::=
'@' _expression
clone_expression ::=
'clone' _primary_expression
_primary_expression ::=
_variable
| _literal
| class_constant_access_expression
| qualified_name
| name
| array_creation_expression
| print_intrinsic
| anonymous_function_creation_expression
| arrow_function
| object_creation_expression
| update_expression
| shell_command_expression
| parenthesized_expression
| throw_expression
| arrow_function
parenthesized_expression ::=
'(' _expression ')'
class_constant_access_expression ::=
_scope_resolution_qualifier '::' ( name | _reserved_identifier | '{' _expression '}' )
print_intrinsic ::=
'print' _expression
anonymous_function_creation_expression ::=
attribute_list? 'static'? 'function' reference_modifier? formal_parameters anonymous_function_use_clause? _return_type? compound_statement
anonymous_function_use_clause ::=
'use' '(' ( variable_reference | variable_name ) ( ',' ( variable_reference | variable_name ) )* ','? ')'
object_creation_expression ::=
'new' _class_type_designator arguments? | 'new' attribute_list? 'class' arguments? base_clause? class_interface_clause? declaration_list
_class_type_designator ::=
qualified_name
| name
| _reserved_identifier
| subscript_expression
| member_access_expression
| nullsafe_member_access_expression
| scoped_property_access_expression
| _variable_name
update_expression ::=
( '--' | '++' ) _variable | _variable ( '--' | '++' )
cast_expression ::=
'(' cast_type ')' ( _unary_expression | include_expression | include_once_expression )
cast_variable ::=
'(' cast_type ')' _variable
assignment_expression ::=
( _variable | list_literal ) '=' _expression
reference_assignment_expression ::=
( _variable | list_literal ) '=' '&' _expression
conditional_expression ::=
_expression '?' _expression? ':' _expression
augmented_assignment_expression ::=
_variable ( '**=' | '*=' | '/=' | '%=' | '+=' | '-=' | '.=' | '<<=' | '>>=' | '&=' | '^=' | '|=' | '??=' ) _expression
_variable ::=
cast_variable
| _callable_variable
| scoped_property_access_expression
| member_access_expression
| nullsafe_member_access_expression
member_access_expression ::=
_dereferencable_expression '->' _member_name
nullsafe_member_access_expression ::=
_dereferencable_expression '?->' _member_name
scoped_property_access_expression ::=
_scope_resolution_qualifier '::' _variable_name
list_literal ::=
_list_destructing
| _array_destructing
_list_destructing ::=
'list' '(' ( _list_destructing | _variable | by_ref | _expression '=>' ( _list_destructing | _variable | by_ref ) )? ( ',' ( _list_destructing | _variable | by_ref | _expression '=>' ( _list_destructing | _variable | by_ref ) )? )* ')'
_array_destructing ::=
'[' _array_destructing_element? ( ',' _array_destructing_element? )* ']'
_array_destructing_element ::=
_array_destructing
| _variable
| by_ref
| _expression '=>' ( _array_destructing | _variable | by_ref )
_callable_variable ::=
_variable_name
| subscript_expression
| member_call_expression
| nullsafe_member_call_expression
| scoped_call_expression
| function_call_expression
function_call_expression ::=
( name | _reserved_identifier | qualified_name | _callable_expression ) arguments
_callable_expression ::=
_callable_variable
| parenthesized_expression
| array_creation_expression
| _string
scoped_call_expression ::=
_scope_resolution_qualifier '::' _member_name arguments
_scope_resolution_qualifier ::=
relative_scope
| name
| _reserved_identifier
| qualified_name
| _dereferencable_expression
relative_scope ::=
'self' | 'parent' | 'static'
variadic_placeholder ::=
'...'
arguments ::=
'(' ( ( argument ( ',' argument )* )? ','? | variadic_placeholder ) ')'
argument ::=
_argument_name? reference_modifier? ( _reserved_identifier | variadic_unpacking | _expression )
_argument_name ::=
( name | 'array' | 'fn' | 'function' | 'match' | 'namespace' | 'null' | 'static' | 'parent' | 'self' | 'true'|'false' ) ':'
member_call_expression ::=
_dereferencable_expression '->' _member_name arguments
nullsafe_member_call_expression ::=
_dereferencable_expression '?->' _member_name arguments
variadic_unpacking ::=
'...' _expression
_member_name ::=
( _reserved_identifier | name | _variable_name )
| '{' _expression '}'
subscript_expression ::=
_dereferencable_expression ( '[' _expression? ']' | '{' _expression '}' )
_dereferencable_expression ::=
_variable | class_constant_access_expression | parenthesized_expression | array_creation_expression | name | _reserved_identifier | qualified_name | _string
array_creation_expression ::=
'array' '(' ( array_element_initializer ( ',' array_element_initializer )* )? ','? ')'
| '[' ( array_element_initializer ( ',' array_element_initializer )* )? ','? ']'
attribute_group ::=
'#[' attribute ( ',' attribute )* ','? ']'
attribute_list ::=
attribute_group+
attribute ::=
( name | _reserved_identifier | qualified_name ) arguments?
_complex_string_part ::=
'{' _expression '}'
_simple_string_member_access_expression ::=
variable_name '->' name
_simple_string_subscript_unary_expression ::=
'-' integer
_simple_string_array_access_argument ::=
integer
| _simple_string_subscript_unary_expression
| name
| variable_name
_simple_string_subscript_expression ::=
variable_name '[' _simple_string_array_access_argument ']'
_simple_string_part ::=
_simple_string_member_access_expression
| _variable_name
| _simple_string_subscript_expression
escape_sequence ::=
( '\\' ( 'n' | 'r' | 't' | 'v' | 'e' | 'f' | '\\' | '\$' | '"' | '`' | [0-7]'{1,3}' | 'x'[0-9A-Fa-f]'{1,2}' | 'u{'[0-9A-Fa-f]+'}' ) )
_interpolated_string_body ::=
( escape_sequence | variable_name encapsed_string_chars_after_variable | encapsed_string_chars | _simple_string_part | _complex_string_part | '\u' | "'" )+
_interpolated_string_body_heredoc ::=
( escape_sequence | variable_name encapsed_string_chars_after_variable_heredoc | encapsed_string_chars_heredoc | _simple_string_part | _complex_string_part | '\u' | "'" | '<?' | '?>' )+
encapsed_string ::=
( [bB]'"' | '"' ) _interpolated_string_body? '"'
string ::=
( [bB]"'" | "'" ) ( ( '\\' | "\'" ) | string_value )* "'"
string_value ::=
'\'?[^'\]+
heredoc_body ::=
_new_line _new_line? _interpolated_string_body_heredoc+
heredoc ::=
'<<<' ( heredoc_start | '"' heredoc_start '"' ) ( heredoc_body _new_line heredoc_end | heredoc_body? heredoc_end )
_new_line ::=
'#x0D'?'#x0A'|'#x0D'
nowdoc_body ::=
_new_line ( nowdoc_string+ | '' )
nowdoc ::=
'<<<' "'" heredoc_start "'" ( nowdoc_body _new_line heredoc_end | nowdoc_body? heredoc_end )
_interpolated_execution_operator_body ::=
( escape_sequence | variable_name execution_string_chars_after_variable | execution_string_chars | _simple_string_part | _complex_string_part | '\u' )+
shell_command_expression ::=
'`' _interpolated_execution_operator_body? '`'
boolean ::=
'true'|'false'
null ::=
'null'
_string ::=
encapsed_string
| string
| heredoc
| nowdoc
dynamic_variable_name ::=
'$' _variable_name
| '$' '{' _expression '}'
_variable_name ::=
dynamic_variable_name
| variable_name
variable_name ::=
'$' name
variable_reference ::=
'&' variable_name
by_ref ::=
'&' ( _callable_variable | member_access_expression | nullsafe_member_access_expression )
yield_expression ::=
'yield' ( array_element_initializer | 'from' _expression )?
array_element_initializer ::=
by_ref | _expression | _expression '=>' ( by_ref | _expression ) | variadic_unpacking
binary_expression ::=
_unary_expression 'instanceof' _class_type_designator
| _expression '??' _expression
| _expression '**' _expression
| _expression 'and' _expression
| _expression 'or' _expression
| _expression 'xor' _expression
| _expression '||' _expression
| _expression '&&' _expression
| _expression '|' _expression
| _expression '^' _expression
| _expression '&' _expression
| _expression '==' _expression
| _expression '!=' _expression
| _expression '<>' _expression
| _expression '===' _expression
| _expression '!==' _expression
| _expression '<' _expression
| _expression '>' _expression
| _expression '<=' _expression
| _expression '>=' _expression
| _expression '<=>' _expression
| _expression '<<' _expression
| _expression '>>' _expression
| _expression '+' _expression
| _expression '-' _expression
| _expression '.' _expression
| _expression '*' _expression
| _expression '/' _expression
| _expression '%' _expression
include_expression ::=
'include' _expression
include_once_expression ::=
'include_once' _expression
require_expression ::=
'require' _expression
require_once_expression ::=
'require_once' _expression
name ::=
[_a-zA-Z\u0080-\u009f\u00a1-\u200a\u200c-\u205f\u2061-\ufefe\uff00-\uffff][_a-zA-Z\u0080-\u009f\u00a1-\u200a\u200c-\u205f\u2061-\ufefe\uff00-\uffff0-9]*
_reserved_identifier ::=
'self'
| 'parent'
| 'static'
comment ::=
( '//' | '#'[^?#x5B?#x0D?#x0A] ) [^?#x0D?#x0A]|'\'?[^>#x0D#x0A]* '\'?'#x0D'?'#x0A'? | '#' | '/*' [^*]*'\'*+([^/*][^*]*'\'*+)* '/'
_semicolon ::=
_automatic_semicolon
| ';'