php7.y 55 KB

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