php7.y 55 KB

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