php7.y 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. %pure_parser
  2. %expect 2
  3. %tokens
  4. %%
  5. start:
  6. top_statement_list { $$ = $this->handleNamespaces($1); }
  7. ;
  8. top_statement_list_ex:
  9. top_statement_list_ex top_statement { pushNormalizing($1, $2); }
  10. | /* empty */ { init(); }
  11. ;
  12. top_statement_list:
  13. top_statement_list_ex
  14. { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
  15. if ($nop !== null) { $1[] = $nop; } $$ = $1; }
  16. ;
  17. ampersand:
  18. T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
  19. | T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG
  20. ;
  21. reserved_non_modifiers:
  22. T_INCLUDE | T_INCLUDE_ONCE | T_EVAL | T_REQUIRE | T_REQUIRE_ONCE | T_LOGICAL_OR | T_LOGICAL_XOR | T_LOGICAL_AND
  23. | T_INSTANCEOF | T_NEW | T_CLONE | T_EXIT | T_IF | T_ELSEIF | T_ELSE | T_ENDIF | T_ECHO | T_DO | T_WHILE
  24. | T_ENDWHILE | T_FOR | T_ENDFOR | T_FOREACH | T_ENDFOREACH | T_DECLARE | T_ENDDECLARE | T_AS | T_TRY | T_CATCH
  25. | T_FINALLY | T_THROW | T_USE | T_INSTEADOF | T_GLOBAL | T_VAR | T_UNSET | T_ISSET | T_EMPTY | T_CONTINUE | T_GOTO
  26. | T_FUNCTION | T_CONST | T_RETURN | T_PRINT | T_YIELD | T_LIST | T_SWITCH | T_ENDSWITCH | T_CASE | T_DEFAULT
  27. | T_BREAK | T_ARRAY | T_CALLABLE | T_EXTENDS | T_IMPLEMENTS | T_NAMESPACE | T_TRAIT | T_INTERFACE | T_CLASS
  28. | T_CLASS_C | T_TRAIT_C | T_FUNC_C | T_METHOD_C | T_LINE | T_FILE | T_DIR | T_NS_C | T_HALT_COMPILER | T_FN
  29. | T_MATCH | T_ENUM
  30. ;
  31. semi_reserved:
  32. reserved_non_modifiers
  33. | T_STATIC | T_ABSTRACT | T_FINAL | T_PRIVATE | T_PROTECTED | T_PUBLIC | T_READONLY
  34. ;
  35. identifier_maybe_reserved:
  36. T_STRING { $$ = Node\Identifier[$1]; }
  37. | semi_reserved { $$ = Node\Identifier[$1]; }
  38. ;
  39. identifier_not_reserved:
  40. T_STRING { $$ = Node\Identifier[$1]; }
  41. ;
  42. reserved_non_modifiers_identifier:
  43. reserved_non_modifiers { $$ = Node\Identifier[$1]; }
  44. ;
  45. namespace_declaration_name:
  46. T_STRING { $$ = Name[$1]; }
  47. | semi_reserved { $$ = Name[$1]; }
  48. | T_NAME_QUALIFIED { $$ = Name[$1]; }
  49. ;
  50. namespace_name:
  51. T_STRING { $$ = Name[$1]; }
  52. | T_NAME_QUALIFIED { $$ = Name[$1]; }
  53. ;
  54. legacy_namespace_name:
  55. namespace_name { $$ = $1; }
  56. | T_NAME_FULLY_QUALIFIED { $$ = Name[substr($1, 1)]; }
  57. ;
  58. plain_variable:
  59. T_VARIABLE { $$ = Expr\Variable[parseVar($1)]; }
  60. ;
  61. semi:
  62. ';' { /* nothing */ }
  63. | error { /* nothing */ }
  64. ;
  65. no_comma:
  66. /* empty */ { /* nothing */ }
  67. | ',' { $this->emitError(new Error('A trailing comma is not allowed here', attributes())); }
  68. ;
  69. optional_comma:
  70. /* empty */
  71. | ','
  72. ;
  73. attribute_decl:
  74. class_name { $$ = Node\Attribute[$1, []]; }
  75. | class_name argument_list { $$ = Node\Attribute[$1, $2]; }
  76. ;
  77. attribute_group:
  78. attribute_decl { init($1); }
  79. | attribute_group ',' attribute_decl { push($1, $3); }
  80. ;
  81. attribute:
  82. T_ATTRIBUTE attribute_group optional_comma ']' { $$ = Node\AttributeGroup[$2]; }
  83. ;
  84. attributes:
  85. attribute { init($1); }
  86. | attributes attribute { push($1, $2); }
  87. ;
  88. optional_attributes:
  89. /* empty */ { $$ = []; }
  90. | attributes { $$ = $1; }
  91. ;
  92. top_statement:
  93. statement { $$ = $1; }
  94. | function_declaration_statement { $$ = $1; }
  95. | class_declaration_statement { $$ = $1; }
  96. | T_HALT_COMPILER
  97. { $$ = Stmt\HaltCompiler[$this->lexer->handleHaltCompiler()]; }
  98. | T_NAMESPACE namespace_declaration_name semi
  99. { $$ = Stmt\Namespace_[$2, null];
  100. $$->setAttribute('kind', Stmt\Namespace_::KIND_SEMICOLON);
  101. $this->checkNamespace($$); }
  102. | T_NAMESPACE namespace_declaration_name '{' top_statement_list '}'
  103. { $$ = Stmt\Namespace_[$2, $4];
  104. $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED);
  105. $this->checkNamespace($$); }
  106. | T_NAMESPACE '{' top_statement_list '}'
  107. { $$ = Stmt\Namespace_[null, $3];
  108. $$->setAttribute('kind', Stmt\Namespace_::KIND_BRACED);
  109. $this->checkNamespace($$); }
  110. | T_USE use_declarations semi { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; }
  111. | T_USE use_type use_declarations semi { $$ = Stmt\Use_[$3, $2]; }
  112. | group_use_declaration semi { $$ = $1; }
  113. | T_CONST constant_declaration_list semi { $$ = Stmt\Const_[$2]; }
  114. ;
  115. use_type:
  116. T_FUNCTION { $$ = Stmt\Use_::TYPE_FUNCTION; }
  117. | T_CONST { $$ = Stmt\Use_::TYPE_CONSTANT; }
  118. ;
  119. group_use_declaration:
  120. T_USE use_type legacy_namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
  121. { $$ = Stmt\GroupUse[$3, $6, $2]; }
  122. | T_USE legacy_namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
  123. { $$ = Stmt\GroupUse[$2, $5, Stmt\Use_::TYPE_UNKNOWN]; }
  124. ;
  125. unprefixed_use_declarations:
  126. non_empty_unprefixed_use_declarations optional_comma { $$ = $1; }
  127. ;
  128. non_empty_unprefixed_use_declarations:
  129. non_empty_unprefixed_use_declarations ',' unprefixed_use_declaration
  130. { push($1, $3); }
  131. | unprefixed_use_declaration { init($1); }
  132. ;
  133. use_declarations:
  134. non_empty_use_declarations no_comma { $$ = $1; }
  135. ;
  136. non_empty_use_declarations:
  137. non_empty_use_declarations ',' use_declaration { push($1, $3); }
  138. | use_declaration { init($1); }
  139. ;
  140. inline_use_declarations:
  141. non_empty_inline_use_declarations optional_comma { $$ = $1; }
  142. ;
  143. non_empty_inline_use_declarations:
  144. non_empty_inline_use_declarations ',' inline_use_declaration
  145. { push($1, $3); }
  146. | inline_use_declaration { init($1); }
  147. ;
  148. unprefixed_use_declaration:
  149. namespace_name
  150. { $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
  151. | namespace_name T_AS identifier_not_reserved
  152. { $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
  153. ;
  154. use_declaration:
  155. legacy_namespace_name
  156. { $$ = Stmt\UseUse[$1, null, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #1); }
  157. | legacy_namespace_name T_AS identifier_not_reserved
  158. { $$ = Stmt\UseUse[$1, $3, Stmt\Use_::TYPE_UNKNOWN]; $this->checkUseUse($$, #3); }
  159. ;
  160. inline_use_declaration:
  161. unprefixed_use_declaration { $$ = $1; $$->type = Stmt\Use_::TYPE_NORMAL; }
  162. | use_type unprefixed_use_declaration { $$ = $2; $$->type = $1; }
  163. ;
  164. constant_declaration_list:
  165. non_empty_constant_declaration_list no_comma { $$ = $1; }
  166. ;
  167. non_empty_constant_declaration_list:
  168. non_empty_constant_declaration_list ',' constant_declaration
  169. { push($1, $3); }
  170. | constant_declaration { init($1); }
  171. ;
  172. constant_declaration:
  173. identifier_not_reserved '=' expr { $$ = Node\Const_[$1, $3]; }
  174. ;
  175. class_const_list:
  176. non_empty_class_const_list no_comma { $$ = $1; }
  177. ;
  178. non_empty_class_const_list:
  179. non_empty_class_const_list ',' class_const { push($1, $3); }
  180. | class_const { init($1); }
  181. ;
  182. class_const:
  183. T_STRING '=' expr
  184. { $$ = Node\Const_[new Node\Identifier($1, stackAttributes(#1)), $3]; }
  185. | semi_reserved '=' expr
  186. { $$ = Node\Const_[new Node\Identifier($1, stackAttributes(#1)), $3]; }
  187. ;
  188. inner_statement_list_ex:
  189. inner_statement_list_ex inner_statement { pushNormalizing($1, $2); }
  190. | /* empty */ { init(); }
  191. ;
  192. inner_statement_list:
  193. inner_statement_list_ex
  194. { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
  195. if ($nop !== null) { $1[] = $nop; } $$ = $1; }
  196. ;
  197. inner_statement:
  198. statement { $$ = $1; }
  199. | function_declaration_statement { $$ = $1; }
  200. | class_declaration_statement { $$ = $1; }
  201. | T_HALT_COMPILER
  202. { throw new Error('__HALT_COMPILER() can only be used from the outermost scope', attributes()); }
  203. ;
  204. non_empty_statement:
  205. '{' inner_statement_list '}'
  206. {
  207. if ($2) {
  208. $$ = $2; prependLeadingComments($$);
  209. } else {
  210. makeNop($$, $this->startAttributeStack[#1], $this->endAttributes);
  211. if (null === $$) { $$ = array(); }
  212. }
  213. }
  214. | T_IF '(' expr ')' statement elseif_list else_single
  215. { $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; }
  216. | T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
  217. { $$ = Stmt\If_[$3, ['stmts' => $6, 'elseifs' => $7, 'else' => $8]]; }
  218. | T_WHILE '(' expr ')' while_statement { $$ = Stmt\While_[$3, $5]; }
  219. | T_DO statement T_WHILE '(' expr ')' ';' { $$ = Stmt\Do_ [$5, toArray($2)]; }
  220. | T_FOR '(' for_expr ';' for_expr ';' for_expr ')' for_statement
  221. { $$ = Stmt\For_[['init' => $3, 'cond' => $5, 'loop' => $7, 'stmts' => $9]]; }
  222. | T_SWITCH '(' expr ')' switch_case_list { $$ = Stmt\Switch_[$3, $5]; }
  223. | T_BREAK optional_expr semi { $$ = Stmt\Break_[$2]; }
  224. | T_CONTINUE optional_expr semi { $$ = Stmt\Continue_[$2]; }
  225. | T_RETURN optional_expr semi { $$ = Stmt\Return_[$2]; }
  226. | T_GLOBAL global_var_list semi { $$ = Stmt\Global_[$2]; }
  227. | T_STATIC static_var_list semi { $$ = Stmt\Static_[$2]; }
  228. | T_ECHO expr_list_forbid_comma semi { $$ = Stmt\Echo_[$2]; }
  229. | T_INLINE_HTML { $$ = Stmt\InlineHTML[$1]; }
  230. | expr semi {
  231. $e = $1;
  232. if ($e instanceof Expr\Throw_) {
  233. // For backwards-compatibility reasons, convert throw in statement position into
  234. // Stmt\Throw_ rather than Stmt\Expression(Expr\Throw_).
  235. $$ = Stmt\Throw_[$e->expr];
  236. } else {
  237. $$ = Stmt\Expression[$e];
  238. }
  239. }
  240. | T_UNSET '(' variables_list ')' semi { $$ = Stmt\Unset_[$3]; }
  241. | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
  242. { $$ = Stmt\Foreach_[$3, $5[0], ['keyVar' => null, 'byRef' => $5[1], 'stmts' => $7]]; }
  243. | T_FOREACH '(' expr T_AS variable T_DOUBLE_ARROW foreach_variable ')' foreach_statement
  244. { $$ = Stmt\Foreach_[$3, $7[0], ['keyVar' => $5, 'byRef' => $7[1], 'stmts' => $9]]; }
  245. | T_FOREACH '(' expr error ')' foreach_statement
  246. { $$ = Stmt\Foreach_[$3, new Expr\Error(stackAttributes(#4)), ['stmts' => $6]]; }
  247. | T_DECLARE '(' declare_list ')' declare_statement { $$ = Stmt\Declare_[$3, $5]; }
  248. | T_TRY '{' inner_statement_list '}' catches optional_finally
  249. { $$ = Stmt\TryCatch[$3, $5, $6]; $this->checkTryCatch($$); }
  250. | T_GOTO identifier_not_reserved semi { $$ = Stmt\Goto_[$2]; }
  251. | identifier_not_reserved ':' { $$ = Stmt\Label[$1]; }
  252. | error { $$ = array(); /* means: no statement */ }
  253. ;
  254. statement:
  255. non_empty_statement { $$ = $1; }
  256. | ';'
  257. { makeNop($$, $this->startAttributeStack[#1], $this->endAttributes);
  258. if ($$ === null) $$ = array(); /* means: no statement */ }
  259. ;
  260. catches:
  261. /* empty */ { init(); }
  262. | catches catch { push($1, $2); }
  263. ;
  264. name_union:
  265. name { init($1); }
  266. | name_union '|' name { push($1, $3); }
  267. ;
  268. catch:
  269. T_CATCH '(' name_union optional_plain_variable ')' '{' inner_statement_list '}'
  270. { $$ = Stmt\Catch_[$3, $4, $7]; }
  271. ;
  272. optional_finally:
  273. /* empty */ { $$ = null; }
  274. | T_FINALLY '{' inner_statement_list '}' { $$ = Stmt\Finally_[$3]; }
  275. ;
  276. variables_list:
  277. non_empty_variables_list optional_comma { $$ = $1; }
  278. ;
  279. non_empty_variables_list:
  280. variable { init($1); }
  281. | non_empty_variables_list ',' variable { push($1, $3); }
  282. ;
  283. optional_ref:
  284. /* empty */ { $$ = false; }
  285. | ampersand { $$ = true; }
  286. ;
  287. optional_arg_ref:
  288. /* empty */ { $$ = false; }
  289. | T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG { $$ = true; }
  290. ;
  291. optional_ellipsis:
  292. /* empty */ { $$ = false; }
  293. | T_ELLIPSIS { $$ = true; }
  294. ;
  295. block_or_error:
  296. '{' inner_statement_list '}' { $$ = $2; }
  297. | error { $$ = []; }
  298. ;
  299. identifier_maybe_readonly:
  300. identifier_not_reserved { $$ = $1; }
  301. | T_READONLY { $$ = Node\Identifier[$1]; }
  302. ;
  303. function_declaration_statement:
  304. T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
  305. { $$ = Stmt\Function_[$3, ['byRef' => $2, 'params' => $5, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
  306. | attributes T_FUNCTION optional_ref identifier_maybe_readonly '(' parameter_list ')' optional_return_type block_or_error
  307. { $$ = Stmt\Function_[$4, ['byRef' => $3, 'params' => $6, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
  308. ;
  309. class_declaration_statement:
  310. class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
  311. { $$ = Stmt\Class_[$2, ['type' => $1, 'extends' => $3, 'implements' => $4, 'stmts' => $6, 'attrGroups' => []]];
  312. $this->checkClass($$, #2); }
  313. | attributes class_entry_type identifier_not_reserved extends_from implements_list '{' class_statement_list '}'
  314. { $$ = Stmt\Class_[$3, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
  315. $this->checkClass($$, #3); }
  316. | optional_attributes T_INTERFACE identifier_not_reserved interface_extends_list '{' class_statement_list '}'
  317. { $$ = Stmt\Interface_[$3, ['extends' => $4, 'stmts' => $6, 'attrGroups' => $1]];
  318. $this->checkInterface($$, #3); }
  319. | optional_attributes T_TRAIT identifier_not_reserved '{' class_statement_list '}'
  320. { $$ = Stmt\Trait_[$3, ['stmts' => $5, 'attrGroups' => $1]]; }
  321. | optional_attributes T_ENUM identifier_not_reserved enum_scalar_type implements_list '{' class_statement_list '}'
  322. { $$ = Stmt\Enum_[$3, ['scalarType' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]];
  323. $this->checkEnum($$, #3); }
  324. ;
  325. enum_scalar_type:
  326. /* empty */ { $$ = null; }
  327. | ':' type { $$ = $2; }
  328. enum_case_expr:
  329. /* empty */ { $$ = null; }
  330. | '=' expr { $$ = $2; }
  331. ;
  332. class_entry_type:
  333. T_CLASS { $$ = 0; }
  334. | class_modifiers T_CLASS { $$ = $1; }
  335. ;
  336. class_modifiers:
  337. class_modifier { $$ = $1; }
  338. | class_modifiers class_modifier { $this->checkClassModifier($1, $2, #2); $$ = $1 | $2; }
  339. ;
  340. class_modifier:
  341. T_ABSTRACT { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
  342. | T_FINAL { $$ = Stmt\Class_::MODIFIER_FINAL; }
  343. | T_READONLY { $$ = Stmt\Class_::MODIFIER_READONLY; }
  344. ;
  345. extends_from:
  346. /* empty */ { $$ = null; }
  347. | T_EXTENDS class_name { $$ = $2; }
  348. ;
  349. interface_extends_list:
  350. /* empty */ { $$ = array(); }
  351. | T_EXTENDS class_name_list { $$ = $2; }
  352. ;
  353. implements_list:
  354. /* empty */ { $$ = array(); }
  355. | T_IMPLEMENTS class_name_list { $$ = $2; }
  356. ;
  357. class_name_list:
  358. non_empty_class_name_list no_comma { $$ = $1; }
  359. ;
  360. non_empty_class_name_list:
  361. class_name { init($1); }
  362. | non_empty_class_name_list ',' class_name { push($1, $3); }
  363. ;
  364. for_statement:
  365. statement { $$ = toArray($1); }
  366. | ':' inner_statement_list T_ENDFOR ';' { $$ = $2; }
  367. ;
  368. foreach_statement:
  369. statement { $$ = toArray($1); }
  370. | ':' inner_statement_list T_ENDFOREACH ';' { $$ = $2; }
  371. ;
  372. declare_statement:
  373. non_empty_statement { $$ = toArray($1); }
  374. | ';' { $$ = null; }
  375. | ':' inner_statement_list T_ENDDECLARE ';' { $$ = $2; }
  376. ;
  377. declare_list:
  378. non_empty_declare_list no_comma { $$ = $1; }
  379. ;
  380. non_empty_declare_list:
  381. declare_list_element { init($1); }
  382. | non_empty_declare_list ',' declare_list_element { push($1, $3); }
  383. ;
  384. declare_list_element:
  385. identifier_not_reserved '=' expr { $$ = Stmt\DeclareDeclare[$1, $3]; }
  386. ;
  387. switch_case_list:
  388. '{' case_list '}' { $$ = $2; }
  389. | '{' ';' case_list '}' { $$ = $3; }
  390. | ':' case_list T_ENDSWITCH ';' { $$ = $2; }
  391. | ':' ';' case_list T_ENDSWITCH ';' { $$ = $3; }
  392. ;
  393. case_list:
  394. /* empty */ { init(); }
  395. | case_list case { push($1, $2); }
  396. ;
  397. case:
  398. T_CASE expr case_separator inner_statement_list_ex { $$ = Stmt\Case_[$2, $4]; }
  399. | T_DEFAULT case_separator inner_statement_list_ex { $$ = Stmt\Case_[null, $3]; }
  400. ;
  401. case_separator:
  402. ':'
  403. | ';'
  404. ;
  405. match:
  406. T_MATCH '(' expr ')' '{' match_arm_list '}' { $$ = Expr\Match_[$3, $6]; }
  407. ;
  408. match_arm_list:
  409. /* empty */ { $$ = []; }
  410. | non_empty_match_arm_list optional_comma { $$ = $1; }
  411. ;
  412. non_empty_match_arm_list:
  413. match_arm { init($1); }
  414. | non_empty_match_arm_list ',' match_arm { push($1, $3); }
  415. ;
  416. match_arm:
  417. expr_list_allow_comma T_DOUBLE_ARROW expr { $$ = Node\MatchArm[$1, $3]; }
  418. | T_DEFAULT optional_comma T_DOUBLE_ARROW expr { $$ = Node\MatchArm[null, $4]; }
  419. ;
  420. while_statement:
  421. statement { $$ = toArray($1); }
  422. | ':' inner_statement_list T_ENDWHILE ';' { $$ = $2; }
  423. ;
  424. elseif_list:
  425. /* empty */ { init(); }
  426. | elseif_list elseif { push($1, $2); }
  427. ;
  428. elseif:
  429. T_ELSEIF '(' expr ')' statement { $$ = Stmt\ElseIf_[$3, toArray($5)]; }
  430. ;
  431. new_elseif_list:
  432. /* empty */ { init(); }
  433. | new_elseif_list new_elseif { push($1, $2); }
  434. ;
  435. new_elseif:
  436. T_ELSEIF '(' expr ')' ':' inner_statement_list
  437. { $$ = Stmt\ElseIf_[$3, $6]; $this->fixupAlternativeElse($$); }
  438. ;
  439. else_single:
  440. /* empty */ { $$ = null; }
  441. | T_ELSE statement { $$ = Stmt\Else_[toArray($2)]; }
  442. ;
  443. new_else_single:
  444. /* empty */ { $$ = null; }
  445. | T_ELSE ':' inner_statement_list
  446. { $$ = Stmt\Else_[$3]; $this->fixupAlternativeElse($$); }
  447. ;
  448. foreach_variable:
  449. variable { $$ = array($1, false); }
  450. | ampersand variable { $$ = array($2, true); }
  451. | list_expr { $$ = array($1, false); }
  452. | array_short_syntax { $$ = array($1, false); }
  453. ;
  454. parameter_list:
  455. non_empty_parameter_list optional_comma { $$ = $1; }
  456. | /* empty */ { $$ = array(); }
  457. ;
  458. non_empty_parameter_list:
  459. parameter { init($1); }
  460. | non_empty_parameter_list ',' parameter { push($1, $3); }
  461. ;
  462. optional_property_modifiers:
  463. /* empty */ { $$ = 0; }
  464. | optional_property_modifiers property_modifier
  465. { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
  466. ;
  467. property_modifier:
  468. T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
  469. | T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
  470. | T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
  471. | T_READONLY { $$ = Stmt\Class_::MODIFIER_READONLY; }
  472. ;
  473. parameter:
  474. optional_attributes optional_property_modifiers optional_type_without_static
  475. optional_arg_ref optional_ellipsis plain_variable
  476. { $$ = new Node\Param($6, null, $3, $4, $5, attributes(), $2, $1);
  477. $this->checkParam($$); }
  478. | optional_attributes optional_property_modifiers optional_type_without_static
  479. optional_arg_ref optional_ellipsis plain_variable '=' expr
  480. { $$ = new Node\Param($6, $8, $3, $4, $5, attributes(), $2, $1);
  481. $this->checkParam($$); }
  482. | optional_attributes optional_property_modifiers optional_type_without_static
  483. optional_arg_ref optional_ellipsis error
  484. { $$ = new Node\Param(Expr\Error[], null, $3, $4, $5, attributes(), $2, $1); }
  485. ;
  486. type_expr:
  487. type { $$ = $1; }
  488. | '?' type { $$ = Node\NullableType[$2]; }
  489. | union_type { $$ = Node\UnionType[$1]; }
  490. | intersection_type { $$ = $1; }
  491. ;
  492. type:
  493. type_without_static { $$ = $1; }
  494. | T_STATIC { $$ = Node\Name['static']; }
  495. ;
  496. type_without_static:
  497. name { $$ = $this->handleBuiltinTypes($1); }
  498. | T_ARRAY { $$ = Node\Identifier['array']; }
  499. | T_CALLABLE { $$ = Node\Identifier['callable']; }
  500. ;
  501. union_type_element:
  502. type { $$ = $1; }
  503. | '(' intersection_type ')' { $$ = $2; }
  504. ;
  505. union_type:
  506. union_type_element '|' union_type_element { init($1, $3); }
  507. | union_type '|' union_type_element { push($1, $3); }
  508. ;
  509. union_type_without_static_element:
  510. type_without_static { $$ = $1; }
  511. | '(' intersection_type_without_static ')' { $$ = $2; }
  512. ;
  513. union_type_without_static:
  514. union_type_without_static_element '|' union_type_without_static_element { init($1, $3); }
  515. | union_type_without_static '|' union_type_without_static_element { push($1, $3); }
  516. ;
  517. intersection_type_list:
  518. type T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type { init($1, $3); }
  519. | intersection_type_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type
  520. { push($1, $3); }
  521. ;
  522. intersection_type:
  523. intersection_type_list { $$ = Node\IntersectionType[$1]; }
  524. ;
  525. intersection_type_without_static_list:
  526. type_without_static T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
  527. { init($1, $3); }
  528. | intersection_type_without_static_list T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG type_without_static
  529. { push($1, $3); }
  530. ;
  531. intersection_type_without_static:
  532. intersection_type_without_static_list { $$ = Node\IntersectionType[$1]; }
  533. ;
  534. type_expr_without_static:
  535. type_without_static { $$ = $1; }
  536. | '?' type_without_static { $$ = Node\NullableType[$2]; }
  537. | union_type_without_static { $$ = Node\UnionType[$1]; }
  538. | intersection_type_without_static { $$ = $1; }
  539. ;
  540. optional_type_without_static:
  541. /* empty */ { $$ = null; }
  542. | type_expr_without_static { $$ = $1; }
  543. ;
  544. optional_return_type:
  545. /* empty */ { $$ = null; }
  546. | ':' type_expr { $$ = $2; }
  547. | ':' error { $$ = null; }
  548. ;
  549. argument_list:
  550. '(' ')' { $$ = array(); }
  551. | '(' non_empty_argument_list optional_comma ')' { $$ = $2; }
  552. | '(' variadic_placeholder ')' { init($2); }
  553. ;
  554. variadic_placeholder:
  555. T_ELLIPSIS { $$ = Node\VariadicPlaceholder[]; }
  556. ;
  557. non_empty_argument_list:
  558. argument { init($1); }
  559. | non_empty_argument_list ',' argument { push($1, $3); }
  560. ;
  561. argument:
  562. expr { $$ = Node\Arg[$1, false, false]; }
  563. | ampersand variable { $$ = Node\Arg[$2, true, false]; }
  564. | T_ELLIPSIS expr { $$ = Node\Arg[$2, false, true]; }
  565. | identifier_maybe_reserved ':' expr
  566. { $$ = new Node\Arg($3, false, false, attributes(), $1); }
  567. ;
  568. global_var_list:
  569. non_empty_global_var_list no_comma { $$ = $1; }
  570. ;
  571. non_empty_global_var_list:
  572. non_empty_global_var_list ',' global_var { push($1, $3); }
  573. | global_var { init($1); }
  574. ;
  575. global_var:
  576. simple_variable { $$ = $1; }
  577. ;
  578. static_var_list:
  579. non_empty_static_var_list no_comma { $$ = $1; }
  580. ;
  581. non_empty_static_var_list:
  582. non_empty_static_var_list ',' static_var { push($1, $3); }
  583. | static_var { init($1); }
  584. ;
  585. static_var:
  586. plain_variable { $$ = Stmt\StaticVar[$1, null]; }
  587. | plain_variable '=' expr { $$ = Stmt\StaticVar[$1, $3]; }
  588. ;
  589. class_statement_list_ex:
  590. class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } else { $$ = $1; } }
  591. | /* empty */ { init(); }
  592. ;
  593. class_statement_list:
  594. class_statement_list_ex
  595. { makeZeroLengthNop($nop, $this->lookaheadStartAttributes);
  596. if ($nop !== null) { $1[] = $nop; } $$ = $1; }
  597. ;
  598. class_statement:
  599. optional_attributes variable_modifiers optional_type_without_static property_declaration_list semi
  600. { $$ = new Stmt\Property($2, $4, attributes(), $3, $1);
  601. $this->checkProperty($$, #2); }
  602. | optional_attributes method_modifiers T_CONST class_const_list semi
  603. { $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
  604. $this->checkClassConst($$, #2); }
  605. | optional_attributes method_modifiers T_CONST type_expr class_const_list semi
  606. { $$ = new Stmt\ClassConst($5, $2, attributes(), $1, $4);
  607. $this->checkClassConst($$, #2); }
  608. | optional_attributes method_modifiers T_FUNCTION optional_ref identifier_maybe_reserved '(' parameter_list ')'
  609. optional_return_type method_body
  610. { $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
  611. $this->checkClassMethod($$, #2); }
  612. | T_USE class_name_list trait_adaptations { $$ = Stmt\TraitUse[$2, $3]; }
  613. | optional_attributes T_CASE identifier_maybe_reserved enum_case_expr semi
  614. { $$ = Stmt\EnumCase[$3, $4, $1]; }
  615. | error { $$ = null; /* will be skipped */ }
  616. ;
  617. trait_adaptations:
  618. ';' { $$ = array(); }
  619. | '{' trait_adaptation_list '}' { $$ = $2; }
  620. ;
  621. trait_adaptation_list:
  622. /* empty */ { init(); }
  623. | trait_adaptation_list trait_adaptation { push($1, $2); }
  624. ;
  625. trait_adaptation:
  626. trait_method_reference_fully_qualified T_INSTEADOF class_name_list ';'
  627. { $$ = Stmt\TraitUseAdaptation\Precedence[$1[0], $1[1], $3]; }
  628. | trait_method_reference T_AS member_modifier identifier_maybe_reserved ';'
  629. { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, $4]; }
  630. | trait_method_reference T_AS member_modifier ';'
  631. { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], $3, null]; }
  632. | trait_method_reference T_AS identifier_not_reserved ';'
  633. { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
  634. | trait_method_reference T_AS reserved_non_modifiers_identifier ';'
  635. { $$ = Stmt\TraitUseAdaptation\Alias[$1[0], $1[1], null, $3]; }
  636. ;
  637. trait_method_reference_fully_qualified:
  638. name T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved { $$ = array($1, $3); }
  639. ;
  640. trait_method_reference:
  641. trait_method_reference_fully_qualified { $$ = $1; }
  642. | identifier_maybe_reserved { $$ = array(null, $1); }
  643. ;
  644. method_body:
  645. ';' /* abstract method */ { $$ = null; }
  646. | block_or_error { $$ = $1; }
  647. ;
  648. variable_modifiers:
  649. non_empty_member_modifiers { $$ = $1; }
  650. | T_VAR { $$ = 0; }
  651. ;
  652. method_modifiers:
  653. /* empty */ { $$ = 0; }
  654. | non_empty_member_modifiers { $$ = $1; }
  655. ;
  656. non_empty_member_modifiers:
  657. member_modifier { $$ = $1; }
  658. | non_empty_member_modifiers member_modifier { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
  659. ;
  660. member_modifier:
  661. T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
  662. | T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
  663. | T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
  664. | T_STATIC { $$ = Stmt\Class_::MODIFIER_STATIC; }
  665. | T_ABSTRACT { $$ = Stmt\Class_::MODIFIER_ABSTRACT; }
  666. | T_FINAL { $$ = Stmt\Class_::MODIFIER_FINAL; }
  667. | T_READONLY { $$ = Stmt\Class_::MODIFIER_READONLY; }
  668. ;
  669. property_declaration_list:
  670. non_empty_property_declaration_list no_comma { $$ = $1; }
  671. ;
  672. non_empty_property_declaration_list:
  673. property_declaration { init($1); }
  674. | non_empty_property_declaration_list ',' property_declaration
  675. { push($1, $3); }
  676. ;
  677. property_decl_name:
  678. T_VARIABLE { $$ = Node\VarLikeIdentifier[parseVar($1)]; }
  679. ;
  680. property_declaration:
  681. property_decl_name { $$ = Stmt\PropertyProperty[$1, null]; }
  682. | property_decl_name '=' expr { $$ = Stmt\PropertyProperty[$1, $3]; }
  683. ;
  684. expr_list_forbid_comma:
  685. non_empty_expr_list no_comma { $$ = $1; }
  686. ;
  687. expr_list_allow_comma:
  688. non_empty_expr_list optional_comma { $$ = $1; }
  689. ;
  690. non_empty_expr_list:
  691. non_empty_expr_list ',' expr { push($1, $3); }
  692. | expr { init($1); }
  693. ;
  694. for_expr:
  695. /* empty */ { $$ = array(); }
  696. | expr_list_forbid_comma { $$ = $1; }
  697. ;
  698. expr:
  699. variable { $$ = $1; }
  700. | list_expr '=' expr { $$ = Expr\Assign[$1, $3]; }
  701. | array_short_syntax '=' expr { $$ = Expr\Assign[$1, $3]; }
  702. | variable '=' expr { $$ = Expr\Assign[$1, $3]; }
  703. | variable '=' ampersand variable { $$ = Expr\AssignRef[$1, $4]; }
  704. | new_expr { $$ = $1; }
  705. | match { $$ = $1; }
  706. | T_CLONE expr { $$ = Expr\Clone_[$2]; }
  707. | variable T_PLUS_EQUAL expr { $$ = Expr\AssignOp\Plus [$1, $3]; }
  708. | variable T_MINUS_EQUAL expr { $$ = Expr\AssignOp\Minus [$1, $3]; }
  709. | variable T_MUL_EQUAL expr { $$ = Expr\AssignOp\Mul [$1, $3]; }
  710. | variable T_DIV_EQUAL expr { $$ = Expr\AssignOp\Div [$1, $3]; }
  711. | variable T_CONCAT_EQUAL expr { $$ = Expr\AssignOp\Concat [$1, $3]; }
  712. | variable T_MOD_EQUAL expr { $$ = Expr\AssignOp\Mod [$1, $3]; }
  713. | variable T_AND_EQUAL expr { $$ = Expr\AssignOp\BitwiseAnd[$1, $3]; }
  714. | variable T_OR_EQUAL expr { $$ = Expr\AssignOp\BitwiseOr [$1, $3]; }
  715. | variable T_XOR_EQUAL expr { $$ = Expr\AssignOp\BitwiseXor[$1, $3]; }
  716. | variable T_SL_EQUAL expr { $$ = Expr\AssignOp\ShiftLeft [$1, $3]; }
  717. | variable T_SR_EQUAL expr { $$ = Expr\AssignOp\ShiftRight[$1, $3]; }
  718. | variable T_POW_EQUAL expr { $$ = Expr\AssignOp\Pow [$1, $3]; }
  719. | variable T_COALESCE_EQUAL expr { $$ = Expr\AssignOp\Coalesce [$1, $3]; }
  720. | variable T_INC { $$ = Expr\PostInc[$1]; }
  721. | T_INC variable { $$ = Expr\PreInc [$2]; }
  722. | variable T_DEC { $$ = Expr\PostDec[$1]; }
  723. | T_DEC variable { $$ = Expr\PreDec [$2]; }
  724. | expr T_BOOLEAN_OR expr { $$ = Expr\BinaryOp\BooleanOr [$1, $3]; }
  725. | expr T_BOOLEAN_AND expr { $$ = Expr\BinaryOp\BooleanAnd[$1, $3]; }
  726. | expr T_LOGICAL_OR expr { $$ = Expr\BinaryOp\LogicalOr [$1, $3]; }
  727. | expr T_LOGICAL_AND expr { $$ = Expr\BinaryOp\LogicalAnd[$1, $3]; }
  728. | expr T_LOGICAL_XOR expr { $$ = Expr\BinaryOp\LogicalXor[$1, $3]; }
  729. | expr '|' expr { $$ = Expr\BinaryOp\BitwiseOr [$1, $3]; }
  730. | expr T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG expr { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
  731. | expr T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG expr { $$ = Expr\BinaryOp\BitwiseAnd[$1, $3]; }
  732. | expr '^' expr { $$ = Expr\BinaryOp\BitwiseXor[$1, $3]; }
  733. | expr '.' expr { $$ = Expr\BinaryOp\Concat [$1, $3]; }
  734. | expr '+' expr { $$ = Expr\BinaryOp\Plus [$1, $3]; }
  735. | expr '-' expr { $$ = Expr\BinaryOp\Minus [$1, $3]; }
  736. | expr '*' expr { $$ = Expr\BinaryOp\Mul [$1, $3]; }
  737. | expr '/' expr { $$ = Expr\BinaryOp\Div [$1, $3]; }
  738. | expr '%' expr { $$ = Expr\BinaryOp\Mod [$1, $3]; }
  739. | expr T_SL expr { $$ = Expr\BinaryOp\ShiftLeft [$1, $3]; }
  740. | expr T_SR expr { $$ = Expr\BinaryOp\ShiftRight[$1, $3]; }
  741. | expr T_POW expr { $$ = Expr\BinaryOp\Pow [$1, $3]; }
  742. | '+' expr %prec T_INC { $$ = Expr\UnaryPlus [$2]; }
  743. | '-' expr %prec T_INC { $$ = Expr\UnaryMinus[$2]; }
  744. | '!' expr { $$ = Expr\BooleanNot[$2]; }
  745. | '~' expr { $$ = Expr\BitwiseNot[$2]; }
  746. | expr T_IS_IDENTICAL expr { $$ = Expr\BinaryOp\Identical [$1, $3]; }
  747. | expr T_IS_NOT_IDENTICAL expr { $$ = Expr\BinaryOp\NotIdentical [$1, $3]; }
  748. | expr T_IS_EQUAL expr { $$ = Expr\BinaryOp\Equal [$1, $3]; }
  749. | expr T_IS_NOT_EQUAL expr { $$ = Expr\BinaryOp\NotEqual [$1, $3]; }
  750. | expr T_SPACESHIP expr { $$ = Expr\BinaryOp\Spaceship [$1, $3]; }
  751. | expr '<' expr { $$ = Expr\BinaryOp\Smaller [$1, $3]; }
  752. | expr T_IS_SMALLER_OR_EQUAL expr { $$ = Expr\BinaryOp\SmallerOrEqual[$1, $3]; }
  753. | expr '>' expr { $$ = Expr\BinaryOp\Greater [$1, $3]; }
  754. | expr T_IS_GREATER_OR_EQUAL expr { $$ = Expr\BinaryOp\GreaterOrEqual[$1, $3]; }
  755. | expr T_INSTANCEOF class_name_reference { $$ = Expr\Instanceof_[$1, $3]; }
  756. | '(' expr ')' { $$ = $2; }
  757. | expr '?' expr ':' expr { $$ = Expr\Ternary[$1, $3, $5]; }
  758. | expr '?' ':' expr { $$ = Expr\Ternary[$1, null, $4]; }
  759. | expr T_COALESCE expr { $$ = Expr\BinaryOp\Coalesce[$1, $3]; }
  760. | T_ISSET '(' expr_list_allow_comma ')' { $$ = Expr\Isset_[$3]; }
  761. | T_EMPTY '(' expr ')' { $$ = Expr\Empty_[$3]; }
  762. | T_INCLUDE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE]; }
  763. | T_INCLUDE_ONCE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_INCLUDE_ONCE]; }
  764. | T_EVAL '(' expr ')' { $$ = Expr\Eval_[$3]; }
  765. | T_REQUIRE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE]; }
  766. | T_REQUIRE_ONCE expr { $$ = Expr\Include_[$2, Expr\Include_::TYPE_REQUIRE_ONCE]; }
  767. | T_INT_CAST expr { $$ = Expr\Cast\Int_ [$2]; }
  768. | T_DOUBLE_CAST expr
  769. { $attrs = attributes();
  770. $attrs['kind'] = $this->getFloatCastKind($1);
  771. $$ = new Expr\Cast\Double($2, $attrs); }
  772. | T_STRING_CAST expr { $$ = Expr\Cast\String_ [$2]; }
  773. | T_ARRAY_CAST expr { $$ = Expr\Cast\Array_ [$2]; }
  774. | T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
  775. | T_BOOL_CAST expr { $$ = Expr\Cast\Bool_ [$2]; }
  776. | T_UNSET_CAST expr { $$ = Expr\Cast\Unset_ [$2]; }
  777. | T_EXIT exit_expr
  778. { $attrs = attributes();
  779. $attrs['kind'] = strtolower($1) === 'exit' ? Expr\Exit_::KIND_EXIT : Expr\Exit_::KIND_DIE;
  780. $$ = new Expr\Exit_($2, $attrs); }
  781. | '@' expr { $$ = Expr\ErrorSuppress[$2]; }
  782. | scalar { $$ = $1; }
  783. | '`' backticks_expr '`' { $$ = Expr\ShellExec[$2]; }
  784. | T_PRINT expr { $$ = Expr\Print_[$2]; }
  785. | T_YIELD { $$ = Expr\Yield_[null, null]; }
  786. | T_YIELD expr { $$ = Expr\Yield_[$2, null]; }
  787. | T_YIELD expr T_DOUBLE_ARROW expr { $$ = Expr\Yield_[$4, $2]; }
  788. | T_YIELD_FROM expr { $$ = Expr\YieldFrom[$2]; }
  789. | T_THROW expr { $$ = Expr\Throw_[$2]; }
  790. | T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
  791. { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $2, 'params' => $4, 'returnType' => $6, 'expr' => $8, 'attrGroups' => []]]; }
  792. | T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
  793. { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => []]]; }
  794. | T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
  795. { $$ = Expr\Closure[['static' => false, 'byRef' => $2, 'params' => $4, 'uses' => $6, 'returnType' => $7, 'stmts' => $8, 'attrGroups' => []]]; }
  796. | T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
  797. { $$ = Expr\Closure[['static' => true, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => []]]; }
  798. | attributes T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
  799. { $$ = Expr\ArrowFunction[['static' => false, 'byRef' => $3, 'params' => $5, 'returnType' => $7, 'expr' => $9, 'attrGroups' => $1]]; }
  800. | attributes T_STATIC T_FN optional_ref '(' parameter_list ')' optional_return_type T_DOUBLE_ARROW expr %prec T_THROW
  801. { $$ = Expr\ArrowFunction[['static' => true, 'byRef' => $4, 'params' => $6, 'returnType' => $8, 'expr' => $10, 'attrGroups' => $1]]; }
  802. | attributes T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
  803. { $$ = Expr\Closure[['static' => false, 'byRef' => $3, 'params' => $5, 'uses' => $7, 'returnType' => $8, 'stmts' => $9, 'attrGroups' => $1]]; }
  804. | attributes T_STATIC T_FUNCTION optional_ref '(' parameter_list ')' lexical_vars optional_return_type block_or_error
  805. { $$ = Expr\Closure[['static' => true, 'byRef' => $4, 'params' => $6, 'uses' => $8, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]]; }
  806. ;
  807. anonymous_class:
  808. optional_attributes class_entry_type ctor_arguments extends_from implements_list '{' class_statement_list '}'
  809. { $$ = array(Stmt\Class_[null, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]], $3);
  810. $this->checkClass($$[0], -1); }
  811. ;
  812. new_expr:
  813. T_NEW class_name_reference ctor_arguments { $$ = Expr\New_[$2, $3]; }
  814. | T_NEW anonymous_class
  815. { list($class, $ctorArgs) = $2; $$ = Expr\New_[$class, $ctorArgs]; }
  816. ;
  817. lexical_vars:
  818. /* empty */ { $$ = array(); }
  819. | T_USE '(' lexical_var_list ')' { $$ = $3; }
  820. ;
  821. lexical_var_list:
  822. non_empty_lexical_var_list optional_comma { $$ = $1; }
  823. ;
  824. non_empty_lexical_var_list:
  825. lexical_var { init($1); }
  826. | non_empty_lexical_var_list ',' lexical_var { push($1, $3); }
  827. ;
  828. lexical_var:
  829. optional_ref plain_variable { $$ = Expr\ClosureUse[$2, $1]; }
  830. ;
  831. name_readonly:
  832. T_READONLY { $$ = Name[$1]; }
  833. ;
  834. function_call:
  835. name argument_list { $$ = Expr\FuncCall[$1, $2]; }
  836. | name_readonly argument_list { $$ = Expr\FuncCall[$1, $2]; }
  837. | callable_expr argument_list { $$ = Expr\FuncCall[$1, $2]; }
  838. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
  839. { $$ = Expr\StaticCall[$1, $3, $4]; }
  840. ;
  841. class_name:
  842. T_STATIC { $$ = Name[$1]; }
  843. | name { $$ = $1; }
  844. ;
  845. name:
  846. T_STRING { $$ = Name[$1]; }
  847. | T_NAME_QUALIFIED { $$ = Name[$1]; }
  848. | T_NAME_FULLY_QUALIFIED { $$ = Name\FullyQualified[substr($1, 1)]; }
  849. | T_NAME_RELATIVE { $$ = Name\Relative[substr($1, 10)]; }
  850. ;
  851. class_name_reference:
  852. class_name { $$ = $1; }
  853. | new_variable { $$ = $1; }
  854. | '(' expr ')' { $$ = $2; }
  855. | error { $$ = Expr\Error[]; $this->errorState = 2; }
  856. ;
  857. class_name_or_var:
  858. class_name { $$ = $1; }
  859. | fully_dereferencable { $$ = $1; }
  860. ;
  861. exit_expr:
  862. /* empty */ { $$ = null; }
  863. | '(' optional_expr ')' { $$ = $2; }
  864. ;
  865. backticks_expr:
  866. /* empty */ { $$ = array(); }
  867. | T_ENCAPSED_AND_WHITESPACE
  868. { $$ = array(Scalar\EncapsedStringPart[Scalar\String_::parseEscapeSequences($1, '`')]); }
  869. | encaps_list { parseEncapsed($1, '`', true); $$ = $1; }
  870. ;
  871. ctor_arguments:
  872. /* empty */ { $$ = array(); }
  873. | argument_list { $$ = $1; }
  874. ;
  875. constant:
  876. name { $$ = Expr\ConstFetch[$1]; }
  877. | T_LINE { $$ = Scalar\MagicConst\Line[]; }
  878. | T_FILE { $$ = Scalar\MagicConst\File[]; }
  879. | T_DIR { $$ = Scalar\MagicConst\Dir[]; }
  880. | T_CLASS_C { $$ = Scalar\MagicConst\Class_[]; }
  881. | T_TRAIT_C { $$ = Scalar\MagicConst\Trait_[]; }
  882. | T_METHOD_C { $$ = Scalar\MagicConst\Method[]; }
  883. | T_FUNC_C { $$ = Scalar\MagicConst\Function_[]; }
  884. | T_NS_C { $$ = Scalar\MagicConst\Namespace_[]; }
  885. ;
  886. class_constant:
  887. class_name_or_var T_PAAMAYIM_NEKUDOTAYIM identifier_maybe_reserved
  888. { $$ = Expr\ClassConstFetch[$1, $3]; }
  889. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM '{' expr '}'
  890. { $$ = Expr\ClassConstFetch[$1, $4]; }
  891. /* We interpret an isolated FOO:: as an unfinished class constant fetch. It could also be
  892. an unfinished static property fetch or unfinished scoped call. */
  893. | class_name_or_var T_PAAMAYIM_NEKUDOTAYIM error
  894. { $$ = Expr\ClassConstFetch[$1, new Expr\Error(stackAttributes(#3))]; $this->errorState = 2; }
  895. ;
  896. array_short_syntax:
  897. '[' array_pair_list ']'
  898. { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
  899. $$ = new Expr\Array_($2, $attrs); }
  900. ;
  901. dereferencable_scalar:
  902. T_ARRAY '(' array_pair_list ')'
  903. { $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
  904. $$ = new Expr\Array_($3, $attrs); }
  905. | array_short_syntax { $$ = $1; }
  906. | T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_::fromString($1, attributes()); }
  907. | '"' encaps_list '"'
  908. { $attrs = attributes(); $attrs['kind'] = Scalar\String_::KIND_DOUBLE_QUOTED;
  909. parseEncapsed($2, '"', true); $$ = new Scalar\Encapsed($2, $attrs); }
  910. ;
  911. scalar:
  912. T_LNUMBER { $$ = $this->parseLNumber($1, attributes()); }
  913. | T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
  914. | dereferencable_scalar { $$ = $1; }
  915. | constant { $$ = $1; }
  916. | class_constant { $$ = $1; }
  917. | T_START_HEREDOC T_ENCAPSED_AND_WHITESPACE T_END_HEREDOC
  918. { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
  919. | T_START_HEREDOC T_END_HEREDOC
  920. { $$ = $this->parseDocString($1, '', $2, attributes(), stackAttributes(#2), true); }
  921. | T_START_HEREDOC encaps_list T_END_HEREDOC
  922. { $$ = $this->parseDocString($1, $2, $3, attributes(), stackAttributes(#3), true); }
  923. ;
  924. optional_expr:
  925. /* empty */ { $$ = null; }
  926. | expr { $$ = $1; }
  927. ;
  928. fully_dereferencable:
  929. variable { $$ = $1; }
  930. | '(' expr ')' { $$ = $2; }
  931. | dereferencable_scalar { $$ = $1; }
  932. | class_constant { $$ = $1; }
  933. ;
  934. array_object_dereferencable:
  935. fully_dereferencable { $$ = $1; }
  936. | constant { $$ = $1; }
  937. ;
  938. callable_expr:
  939. callable_variable { $$ = $1; }
  940. | '(' expr ')' { $$ = $2; }
  941. | dereferencable_scalar { $$ = $1; }
  942. ;
  943. callable_variable:
  944. simple_variable { $$ = $1; }
  945. | array_object_dereferencable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
  946. | array_object_dereferencable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
  947. | function_call { $$ = $1; }
  948. | array_object_dereferencable T_OBJECT_OPERATOR property_name argument_list
  949. { $$ = Expr\MethodCall[$1, $3, $4]; }
  950. | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name argument_list
  951. { $$ = Expr\NullsafeMethodCall[$1, $3, $4]; }
  952. ;
  953. optional_plain_variable:
  954. /* empty */ { $$ = null; }
  955. | plain_variable { $$ = $1; }
  956. ;
  957. variable:
  958. callable_variable { $$ = $1; }
  959. | static_member { $$ = $1; }
  960. | array_object_dereferencable T_OBJECT_OPERATOR property_name
  961. { $$ = Expr\PropertyFetch[$1, $3]; }
  962. | array_object_dereferencable T_NULLSAFE_OBJECT_OPERATOR property_name
  963. { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
  964. ;
  965. simple_variable:
  966. plain_variable { $$ = $1; }
  967. | '$' '{' expr '}' { $$ = Expr\Variable[$3]; }
  968. | '$' simple_variable { $$ = Expr\Variable[$2]; }
  969. | '$' error { $$ = Expr\Variable[Expr\Error[]]; $this->errorState = 2; }
  970. ;
  971. static_member_prop_name:
  972. simple_variable
  973. { $var = $1->name; $$ = \is_string($var) ? Node\VarLikeIdentifier[$var] : $var; }
  974. ;
  975. static_member:
  976. class_name_or_var T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
  977. { $$ = Expr\StaticPropertyFetch[$1, $3]; }
  978. ;
  979. new_variable:
  980. simple_variable { $$ = $1; }
  981. | new_variable '[' optional_expr ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
  982. | new_variable '{' expr '}' { $$ = Expr\ArrayDimFetch[$1, $3]; }
  983. | new_variable T_OBJECT_OPERATOR property_name { $$ = Expr\PropertyFetch[$1, $3]; }
  984. | new_variable T_NULLSAFE_OBJECT_OPERATOR property_name { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
  985. | class_name T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
  986. { $$ = Expr\StaticPropertyFetch[$1, $3]; }
  987. | new_variable T_PAAMAYIM_NEKUDOTAYIM static_member_prop_name
  988. { $$ = Expr\StaticPropertyFetch[$1, $3]; }
  989. ;
  990. member_name:
  991. identifier_maybe_reserved { $$ = $1; }
  992. | '{' expr '}' { $$ = $2; }
  993. | simple_variable { $$ = $1; }
  994. ;
  995. property_name:
  996. identifier_not_reserved { $$ = $1; }
  997. | '{' expr '}' { $$ = $2; }
  998. | simple_variable { $$ = $1; }
  999. | error { $$ = Expr\Error[]; $this->errorState = 2; }
  1000. ;
  1001. list_expr:
  1002. T_LIST '(' inner_array_pair_list ')' { $$ = Expr\List_[$3]; }
  1003. ;
  1004. array_pair_list:
  1005. inner_array_pair_list
  1006. { $$ = $1; $end = count($$)-1; if ($$[$end] === null) array_pop($$); }
  1007. ;
  1008. comma_or_error:
  1009. ','
  1010. | error
  1011. { /* do nothing -- prevent default action of $$=$1. See #551. */ }
  1012. ;
  1013. inner_array_pair_list:
  1014. inner_array_pair_list comma_or_error array_pair { push($1, $3); }
  1015. | array_pair { init($1); }
  1016. ;
  1017. array_pair:
  1018. expr { $$ = Expr\ArrayItem[$1, null, false]; }
  1019. | ampersand variable { $$ = Expr\ArrayItem[$2, null, true]; }
  1020. | list_expr { $$ = Expr\ArrayItem[$1, null, false]; }
  1021. | expr T_DOUBLE_ARROW expr { $$ = Expr\ArrayItem[$3, $1, false]; }
  1022. | expr T_DOUBLE_ARROW ampersand variable { $$ = Expr\ArrayItem[$4, $1, true]; }
  1023. | expr T_DOUBLE_ARROW list_expr { $$ = Expr\ArrayItem[$3, $1, false]; }
  1024. | T_ELLIPSIS expr { $$ = new Expr\ArrayItem($2, null, false, attributes(), true); }
  1025. | /* empty */ { $$ = null; }
  1026. ;
  1027. encaps_list:
  1028. encaps_list encaps_var { push($1, $2); }
  1029. | encaps_list encaps_string_part { push($1, $2); }
  1030. | encaps_var { init($1); }
  1031. | encaps_string_part encaps_var { init($1, $2); }
  1032. ;
  1033. encaps_string_part:
  1034. T_ENCAPSED_AND_WHITESPACE { $$ = Scalar\EncapsedStringPart[$1]; }
  1035. ;
  1036. encaps_str_varname:
  1037. T_STRING_VARNAME { $$ = Expr\Variable[$1]; }
  1038. ;
  1039. encaps_var:
  1040. plain_variable { $$ = $1; }
  1041. | plain_variable '[' encaps_var_offset ']' { $$ = Expr\ArrayDimFetch[$1, $3]; }
  1042. | plain_variable T_OBJECT_OPERATOR identifier_not_reserved
  1043. { $$ = Expr\PropertyFetch[$1, $3]; }
  1044. | plain_variable T_NULLSAFE_OBJECT_OPERATOR identifier_not_reserved
  1045. { $$ = Expr\NullsafePropertyFetch[$1, $3]; }
  1046. | T_DOLLAR_OPEN_CURLY_BRACES expr '}' { $$ = Expr\Variable[$2]; }
  1047. | T_DOLLAR_OPEN_CURLY_BRACES T_STRING_VARNAME '}' { $$ = Expr\Variable[$2]; }
  1048. | T_DOLLAR_OPEN_CURLY_BRACES encaps_str_varname '[' expr ']' '}'
  1049. { $$ = Expr\ArrayDimFetch[$2, $4]; }
  1050. | T_CURLY_OPEN variable '}' { $$ = $2; }
  1051. ;
  1052. encaps_var_offset:
  1053. T_STRING { $$ = Scalar\String_[$1]; }
  1054. | T_NUM_STRING { $$ = $this->parseNumString($1, attributes()); }
  1055. | '-' T_NUM_STRING { $$ = $this->parseNumString('-' . $2, attributes()); }
  1056. | plain_variable { $$ = $1; }
  1057. ;
  1058. %%