php.y 56 KB

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