You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1168 lines
54 KiB

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