php7.y 52 KB

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