php7.y 46 KB

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