PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ext/melbourne/grammar18.y

https://github.com/p8/rubinius
Happy | 6095 lines | 5550 code | 545 blank | 0 comment | 0 complexity | 0499a8e92768e9872d9d40662461ef83 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. /**********************************************************************
  2. parse.y -
  3. $Author: matz $
  4. $Date: 2004/11/29 06:13:51 $
  5. created at: Fri May 28 18:02:42 JST 1993
  6. Copyright (C) 1993-2003 Yukihiro Matsumoto
  7. **********************************************************************/
  8. %{
  9. #define YYDEBUG 1
  10. #define YYERROR_VERBOSE 1
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <assert.h>
  18. #include "ruby.h"
  19. #include "grammar18.hpp"
  20. #include "parser_state18.hpp"
  21. #include "visitor18.hpp"
  22. #include "symbols.hpp"
  23. namespace melbourne {
  24. namespace grammar18 {
  25. #ifndef isnumber
  26. #define isnumber isdigit
  27. #endif
  28. /* Defined at least in mach/boolean.h on OS X. */
  29. #ifdef TRUE
  30. #undef TRUE
  31. #endif
  32. #ifdef FALSE
  33. #undef FALSE
  34. #endif
  35. #define TRUE true
  36. #define FALSE false
  37. /*
  38. #define ISALPHA isalpha
  39. #define ISSPACE isspace
  40. #define ISALNUM(x) (isalpha(x) || isnumber(x))
  41. #define ISDIGIT isdigit
  42. #define ISXDIGIT isxdigit
  43. #define ISUPPER isupper
  44. */
  45. #define ismbchar(c) (0)
  46. #define mbclen(c) (1)
  47. #define string_new(ptr, len) blk2bstr(ptr, len)
  48. #define string_new2(ptr) cstr2bstr(ptr)
  49. static int
  50. mel_yyerror(const char *, rb_parser_state*);
  51. #define yyparse mel_yyparse
  52. #define yylex mel_yylex
  53. #define yyerror(str) mel_yyerror(str, (rb_parser_state*)parser_state)
  54. #define yylval mel_yylval
  55. #define yychar mel_yychar
  56. #define yydebug mel_yydebug
  57. #define YYPARSE_PARAM parser_state
  58. #define YYLEX_PARAM parser_state
  59. #define ID_SCOPE_SHIFT 3
  60. #define ID_SCOPE_MASK 0x07
  61. #define ID_LOCAL 0x01
  62. #define ID_INSTANCE 0x02
  63. #define ID_GLOBAL 0x03
  64. #define ID_ATTRSET 0x04
  65. #define ID_CONST 0x05
  66. #define ID_CLASS 0x06
  67. #define ID_JUNK 0x07
  68. #define ID_INTERNAL ID_JUNK
  69. #define is_notop_id(id) ((id)>tLAST_TOKEN)
  70. #define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
  71. #define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
  72. #define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
  73. #define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
  74. #define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
  75. #define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
  76. #define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
  77. #define is_asgn_or_id(id) ((is_notop_id(id)) && \
  78. (((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
  79. ((id)&ID_SCOPE_MASK) == ID_INSTANCE || \
  80. ((id)&ID_SCOPE_MASK) == ID_CLASS))
  81. /* FIXME these went into the ruby_state instead of parser_state
  82. because a ton of other crap depends on it
  83. char *ruby_sourcefile; current source file
  84. int ruby_sourceline; current line no.
  85. */
  86. static int yylex(void*, void *);
  87. #define BITSTACK_PUSH(stack, n) (stack = (stack<<1)|((n)&1))
  88. #define BITSTACK_POP(stack) (stack >>= 1)
  89. #define BITSTACK_LEXPOP(stack) (stack = (stack >> 1) | (stack & 1))
  90. #define BITSTACK_SET_P(stack) (stack&1)
  91. #define COND_PUSH(n) BITSTACK_PUSH(cond_stack, n)
  92. #define COND_POP() BITSTACK_POP(cond_stack)
  93. #define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
  94. #define COND_P() BITSTACK_SET_P(cond_stack)
  95. #define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, n)
  96. #define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
  97. #define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
  98. #define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
  99. static NODE *cond(NODE*,rb_parser_state*);
  100. static NODE *logop(enum node_type,NODE*,NODE*,rb_parser_state*);
  101. static int cond_negative(NODE**);
  102. static NODE *newline_node(rb_parser_state*,NODE*);
  103. static void fixpos(NODE*,NODE*);
  104. static int value_expr0(NODE*,rb_parser_state*);
  105. static void void_expr0(NODE*,rb_parser_state*);
  106. static void void_stmts(NODE*,rb_parser_state*);
  107. static NODE *remove_begin(NODE*,rb_parser_state*);
  108. #define value_expr(node) value_expr0((node) = \
  109. remove_begin(node, (rb_parser_state*)parser_state), \
  110. (rb_parser_state*)parser_state)
  111. #define void_expr(node) void_expr0((node) = \
  112. remove_begin(node, (rb_parser_state*)parser_state), \
  113. (rb_parser_state*)parser_state)
  114. static NODE *block_append(rb_parser_state*,NODE*,NODE*);
  115. static NODE *list_append(rb_parser_state*,NODE*,NODE*);
  116. static NODE *list_concat(NODE*,NODE*);
  117. static NODE *arg_concat(rb_parser_state*,NODE*,NODE*);
  118. static NODE *arg_prepend(rb_parser_state*,NODE*,NODE*);
  119. static NODE *literal_concat(rb_parser_state*,NODE*,NODE*);
  120. static NODE *new_evstr(rb_parser_state*,NODE*);
  121. static NODE *evstr2dstr(rb_parser_state*,NODE*);
  122. static NODE *call_op(NODE*,QUID,int,NODE*,rb_parser_state*);
  123. /* static NODE *negate_lit(NODE*); */
  124. static NODE *ret_args(rb_parser_state*,NODE*);
  125. static NODE *arg_blk_pass(NODE*,NODE*);
  126. static NODE *new_call(rb_parser_state*,NODE*,QUID,NODE*);
  127. static NODE *new_fcall(rb_parser_state*,QUID,NODE*);
  128. static NODE *new_super(rb_parser_state*,NODE*);
  129. static NODE *new_yield(rb_parser_state*,NODE*);
  130. static NODE *mel_gettable(rb_parser_state*,QUID);
  131. #define gettable(i) mel_gettable((rb_parser_state*)parser_state, i)
  132. static NODE *assignable(QUID,NODE*,rb_parser_state*);
  133. static NODE *aryset(NODE*,NODE*,rb_parser_state*);
  134. static NODE *attrset(NODE*,QUID,rb_parser_state*);
  135. static void rb_backref_error(NODE*,rb_parser_state*);
  136. static NODE *node_assign(NODE*,NODE*,rb_parser_state*);
  137. static NODE *match_gen(NODE*,NODE*,rb_parser_state*);
  138. static void mel_local_push(rb_parser_state*, int cnt);
  139. #define local_push(cnt) mel_local_push(vps, cnt)
  140. static void mel_local_pop(rb_parser_state*);
  141. #define local_pop() mel_local_pop(vps)
  142. static intptr_t mel_local_cnt(rb_parser_state*,QUID);
  143. #define local_cnt(i) mel_local_cnt(vps, i)
  144. static int mel_local_id(rb_parser_state*,QUID);
  145. #define local_id(i) mel_local_id(vps, i)
  146. static QUID *mel_local_tbl(rb_parser_state *st);
  147. static QUID convert_op(QUID id);
  148. #define QUID2SYM(x) (x)
  149. static void tokadd(char c, rb_parser_state *parser_state);
  150. static int tokadd_string(int, int, int, QUID*, rb_parser_state*);
  151. rb_parser_state *parser_alloc_state() {
  152. rb_parser_state *parser_state;
  153. parser_state = (rb_parser_state*)calloc(1, sizeof(rb_parser_state));
  154. lex_pbeg = 0;
  155. lex_p = 0;
  156. lex_pend = 0;
  157. parse_error = false;
  158. eofp = false;
  159. command_start = true;
  160. class_nest = 0;
  161. in_single = 0;
  162. in_def = 0;
  163. compile_for_eval = 0;
  164. cur_mid = 0;
  165. tokenbuf = NULL;
  166. tokidx = 0;
  167. toksiz = 0;
  168. memory_cur = NULL;
  169. memory_last_addr = NULL;
  170. current_pool = 0;
  171. pool_size = 0;
  172. memory_size = 204800;
  173. memory_pools = NULL;
  174. emit_warnings = 0;
  175. verbose = RTEST(ruby_verbose);
  176. magic_comments = new std::vector<bstring>;
  177. start_lines = new std::list<StartPosition>;
  178. return parser_state;
  179. }
  180. void *pt_allocate(rb_parser_state *parser_state, int size) {
  181. void *cur;
  182. if(!memory_cur || ((memory_cur + size) >= memory_last_addr)) {
  183. if(memory_cur) current_pool++;
  184. if(current_pool == pool_size) {
  185. pool_size += 10;
  186. if(memory_pools) {
  187. memory_pools = (void**)realloc(memory_pools, sizeof(void*) * pool_size);
  188. } else {
  189. memory_pools = (void**)malloc(sizeof(void*) * pool_size);
  190. }
  191. }
  192. memory_pools[current_pool] = malloc(memory_size);
  193. memory_cur = (char*)memory_pools[current_pool];
  194. memory_last_addr = memory_cur + memory_size - 1;
  195. }
  196. cur = (void*)memory_cur;
  197. memory_cur = memory_cur + size;
  198. return cur;
  199. }
  200. void pt_free(rb_parser_state *parser_state) {
  201. int i;
  202. if(line_buffer) {
  203. bdestroy(line_buffer);
  204. }
  205. if(lex_lastline) {
  206. bdestroy(lex_lastline);
  207. }
  208. free(tokenbuf);
  209. delete variables;
  210. for(std::vector<bstring>::iterator i = magic_comments->begin();
  211. i != magic_comments->end();
  212. i++) {
  213. bdestroy(*i);
  214. }
  215. delete magic_comments;
  216. delete start_lines;
  217. if(!memory_pools) return;
  218. for(i = 0; i <= current_pool; i++) {
  219. free(memory_pools[i]);
  220. }
  221. free(memory_pools);
  222. }
  223. #define SHOW_PARSER_WARNS 0
  224. static int rb_compile_error(rb_parser_state *st, const char *fmt, ...) {
  225. va_list ar;
  226. char msg[256];
  227. int count;
  228. va_start(ar, fmt);
  229. count = vsnprintf(msg, 256, fmt, ar);
  230. va_end(ar);
  231. mel_yyerror(msg, st);
  232. return count;
  233. }
  234. static int _debug_print(const char *fmt, ...) {
  235. #if SHOW_PARSER_WARNS
  236. va_list ar;
  237. int i;
  238. va_start(ar, fmt);
  239. i = vprintf(fmt, ar);
  240. va_end(ar);
  241. return i;
  242. #else
  243. return 0;
  244. #endif
  245. }
  246. #define rb_warn _debug_print
  247. #define rb_warning _debug_print
  248. void push_start_line(rb_parser_state* parser_state, int line, const char* which) {
  249. start_lines->push_back(StartPosition(line, which));
  250. }
  251. #define PUSH_LINE(which) push_start_line((rb_parser_state*)parser_state, ruby_sourceline, which)
  252. void pop_start_line(rb_parser_state* parser_state) {
  253. start_lines->pop_back();
  254. }
  255. #define POP_LINE() pop_start_line((rb_parser_state*)parser_state)
  256. static QUID rb_parser_sym(const char *name);
  257. static QUID rb_id_attrset(QUID);
  258. static int scan_oct(const char *start, int len, int *retlen);
  259. static int scan_hex(const char *start, int len, int *retlen);
  260. static void reset_block(rb_parser_state *parser_state);
  261. static NODE *extract_block_vars(rb_parser_state *parser_state, NODE* node, var_table vars);
  262. #ifndef RE_OPTION_IGNORECASE
  263. #define RE_OPTION_IGNORECASE (1)
  264. #endif
  265. #ifndef RE_OPTION_EXTENDED
  266. #define RE_OPTION_EXTENDED (2)
  267. #endif
  268. #ifndef RE_OPTION_MULTILINE
  269. #define RE_OPTION_MULTILINE (4)
  270. #endif
  271. #define RE_OPTION_DONT_CAPTURE_GROUP (128)
  272. #define RE_OPTION_CAPTURE_GROUP (256)
  273. #define RE_OPTION_ONCE (8192)
  274. #define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
  275. #define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
  276. #define SIGN_EXTEND(x,n) (((1<<((n)-1))^((x)&~(~0<<(n))))-(1<<((n)-1)))
  277. #define nd_func u1.id
  278. #if SIZEOF_SHORT != 2
  279. #define nd_term(node) SIGN_EXTEND((node)->u2.id, (CHAR_BIT*2))
  280. #else
  281. #define nd_term(node) ((signed short)(node)->u2.id)
  282. #endif
  283. #define nd_paren(node) (char)((node)->u2.id >> (CHAR_BIT*2))
  284. #define nd_nest u3.id
  285. #define NEW_BLOCK_VAR(b, v) NEW_NODE(NODE_BLOCK_PASS, 0, b, v)
  286. /* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
  287. for instance). This is too low for Ruby to parse some files, such as
  288. date/format.rb, therefore bump the value up to at least Bison's default. */
  289. #ifdef OLD_YACC
  290. #ifndef YYMAXDEPTH
  291. #define YYMAXDEPTH 10000
  292. #endif
  293. #endif
  294. #define vps ((rb_parser_state*)parser_state)
  295. %}
  296. %pure-parser
  297. %union {
  298. VALUE val;
  299. NODE *node;
  300. QUID id;
  301. int num;
  302. var_table vars;
  303. }
  304. %token kCLASS
  305. kMODULE
  306. kDEF
  307. kUNDEF
  308. kBEGIN
  309. kRESCUE
  310. kENSURE
  311. kEND
  312. kIF
  313. kUNLESS
  314. kTHEN
  315. kELSIF
  316. kELSE
  317. kCASE
  318. kWHEN
  319. kWHILE
  320. kUNTIL
  321. kFOR
  322. kBREAK
  323. kNEXT
  324. kREDO
  325. kRETRY
  326. kIN
  327. kDO
  328. kDO_COND
  329. kDO_BLOCK
  330. kRETURN
  331. kYIELD
  332. kSUPER
  333. kSELF
  334. kNIL
  335. kTRUE
  336. kFALSE
  337. kAND
  338. kOR
  339. kNOT
  340. kIF_MOD
  341. kUNLESS_MOD
  342. kWHILE_MOD
  343. kUNTIL_MOD
  344. kRESCUE_MOD
  345. kALIAS
  346. kDEFINED
  347. klBEGIN
  348. klEND
  349. k__LINE__
  350. k__FILE__
  351. %token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tXSTRING_BEG
  352. %token <node> tINTEGER tFLOAT tSTRING_CONTENT
  353. %token <node> tNTH_REF tBACK_REF
  354. %token <num> tREGEXP_END
  355. %type <node> singleton strings string string1 xstring regexp
  356. %type <node> string_contents xstring_contents string_content
  357. %type <node> words qwords word_list qword_list word
  358. %type <node> literal numeric dsym cpath
  359. %type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
  360. %type <node> expr_value arg_value primary_value
  361. %type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
  362. %type <node> args when_args call_args call_args2 open_args paren_args opt_paren_args
  363. %type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
  364. %type <node> mrhs superclass block_call block_command
  365. %type <node> f_arglist f_args f_optarg f_opt f_block_arg opt_f_block_arg
  366. %type <node> assoc_list assocs assoc undef_list backref string_dvar
  367. %type <node> for_var block_var opt_block_var block_par
  368. %type <node> brace_block cmd_brace_block do_block lhs none fitem
  369. %type <node> mlhs mlhs_head mlhs_basic mlhs_entry mlhs_item mlhs_node
  370. %type <id> fsym variable sym symbol operation operation2 operation3
  371. %type <id> cname fname op f_rest_arg
  372. %type <num> f_norm_arg f_arg
  373. %token tUPLUS /* unary+ */
  374. %token tUMINUS /* unary- */
  375. %token tUBS /* unary\ */
  376. %token tPOW /* ** */
  377. %token tCMP /* <=> */
  378. %token tEQ /* == */
  379. %token tEQQ /* === */
  380. %token tNEQ /* != */
  381. %token tGEQ /* >= */
  382. %token tLEQ /* <= */
  383. %token tANDOP tOROP /* && and || */
  384. %token tMATCH tNMATCH /* =~ and !~ */
  385. %token tDOT2 tDOT3 /* .. and ... */
  386. %token tAREF tASET /* [] and []= */
  387. %token tLSHFT tRSHFT /* << and >> */
  388. %token tCOLON2 /* :: */
  389. %token tCOLON3 /* :: at EXPR_BEG */
  390. %token <id> tOP_ASGN /* +=, -= etc. */
  391. %token tASSOC /* => */
  392. %token tLPAREN /* ( */
  393. %token tLPAREN_ARG /* ( */
  394. %token tRPAREN /* ) */
  395. %token tLBRACK /* [ */
  396. %token tLBRACE /* { */
  397. %token tLBRACE_ARG /* { */
  398. %token tSTAR /* * */
  399. %token tAMPER /* & */
  400. %token tSYMBEG tSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
  401. %token tSTRING_DBEG tSTRING_DVAR tSTRING_END
  402. /*
  403. * precedence table
  404. */
  405. %nonassoc tLOWEST
  406. %nonassoc tLBRACE_ARG
  407. %nonassoc kIF_MOD kUNLESS_MOD kWHILE_MOD kUNTIL_MOD
  408. %left kOR kAND
  409. %right kNOT
  410. %nonassoc kDEFINED
  411. %right '=' tOP_ASGN
  412. %left kRESCUE_MOD
  413. %right '?' ':'
  414. %nonassoc tDOT2 tDOT3
  415. %left tOROP
  416. %left tANDOP
  417. %nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
  418. %left '>' tGEQ '<' tLEQ
  419. %left '|' '^'
  420. %left '&'
  421. %left tLSHFT tRSHFT
  422. %left '+' '-'
  423. %left '*' '/' '%'
  424. %right tUMINUS_NUM tUMINUS
  425. %right tPOW
  426. %right '!' '~' tUPLUS
  427. %token tLAST_TOKEN
  428. %%
  429. program : {
  430. lex_state = EXPR_BEG;
  431. variables = new LocalState(0);
  432. class_nest = 0;
  433. }
  434. compstmt
  435. {
  436. if ($2 && !compile_for_eval) {
  437. /* last expression should not be void */
  438. if (nd_type($2) != NODE_BLOCK) void_expr($2);
  439. else {
  440. NODE *node = $2;
  441. while (node->nd_next) {
  442. node = node->nd_next;
  443. }
  444. void_expr(node->nd_head);
  445. }
  446. }
  447. top_node = block_append(vps, top_node, $2);
  448. class_nest = 0;
  449. }
  450. ;
  451. bodystmt : compstmt
  452. opt_rescue
  453. opt_else
  454. opt_ensure
  455. {
  456. $$ = $1;
  457. if ($2) {
  458. $$ = NEW_RESCUE($1, $2, $3);
  459. }
  460. else if ($3) {
  461. rb_warn("else without rescue is useless");
  462. $$ = block_append(vps, $$, $3);
  463. }
  464. if ($4) {
  465. $$ = NEW_ENSURE($$, $4);
  466. }
  467. fixpos($$, $1);
  468. }
  469. ;
  470. compstmt : stmts opt_terms
  471. {
  472. void_stmts($1, vps);
  473. $$ = $1;
  474. }
  475. ;
  476. stmts : none
  477. | stmt
  478. {
  479. $$ = newline_node(vps, $1);
  480. }
  481. | stmts terms stmt
  482. {
  483. $$ = block_append(vps, $1, newline_node(vps, $3));
  484. }
  485. | error stmt
  486. {
  487. $$ = remove_begin($2, vps);
  488. }
  489. ;
  490. stmt : kALIAS fitem {lex_state = EXPR_FNAME;} fitem
  491. {
  492. $$ = NEW_ALIAS($2, $4);
  493. }
  494. | kALIAS tGVAR tGVAR
  495. {
  496. $$ = NEW_VALIAS($2, $3);
  497. }
  498. | kALIAS tGVAR tBACK_REF
  499. {
  500. char buf[3];
  501. snprintf(buf, sizeof(buf), "$%c", (char)$3->nd_nth);
  502. $$ = NEW_VALIAS($2, rb_parser_sym(buf));
  503. }
  504. | kALIAS tGVAR tNTH_REF
  505. {
  506. yyerror("can't make alias for the number variables");
  507. $$ = 0;
  508. }
  509. | kUNDEF undef_list
  510. {
  511. $$ = $2;
  512. }
  513. | stmt kIF_MOD expr_value
  514. {
  515. $$ = NEW_IF(cond($3, vps), remove_begin($1, vps), 0);
  516. fixpos($$, $3);
  517. if (cond_negative(&$$->nd_cond)) {
  518. $$->nd_else = $$->nd_body;
  519. $$->nd_body = 0;
  520. }
  521. }
  522. | stmt kUNLESS_MOD expr_value
  523. {
  524. $$ = NEW_UNLESS(cond($3, vps), remove_begin($1, vps), 0);
  525. fixpos($$, $3);
  526. if (cond_negative(&$$->nd_cond)) {
  527. $$->nd_body = $$->nd_else;
  528. $$->nd_else = 0;
  529. }
  530. }
  531. | stmt kWHILE_MOD expr_value
  532. {
  533. if ($1 && nd_type($1) == NODE_BEGIN) {
  534. $$ = NEW_WHILE(cond($3, vps), $1->nd_body, 0);
  535. }
  536. else {
  537. $$ = NEW_WHILE(cond($3, vps), $1, 1);
  538. }
  539. if (cond_negative(&$$->nd_cond)) {
  540. nd_set_type($$, NODE_UNTIL);
  541. }
  542. }
  543. | stmt kUNTIL_MOD expr_value
  544. {
  545. if ($1 && nd_type($1) == NODE_BEGIN) {
  546. $$ = NEW_UNTIL(cond($3, vps), $1->nd_body, 0);
  547. }
  548. else {
  549. $$ = NEW_UNTIL(cond($3, vps), $1, 1);
  550. }
  551. if (cond_negative(&$$->nd_cond)) {
  552. nd_set_type($$, NODE_WHILE);
  553. }
  554. }
  555. | stmt kRESCUE_MOD stmt
  556. {
  557. NODE *resq = NEW_RESBODY(0, remove_begin($3, vps), 0);
  558. $$ = NEW_RESCUE(remove_begin($1, vps), resq, 0);
  559. }
  560. | klBEGIN
  561. {
  562. if (in_def || in_single) {
  563. yyerror("BEGIN in method");
  564. }
  565. local_push(0);
  566. }
  567. '{' compstmt '}'
  568. {
  569. /*
  570. ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
  571. NEW_PREEXE($4));
  572. */
  573. local_pop();
  574. $$ = 0;
  575. }
  576. | klEND '{' compstmt '}'
  577. {
  578. if (in_def || in_single) {
  579. rb_warn("END in method; use at_exit");
  580. }
  581. $$ = NEW_ITER(0, NEW_POSTEXE(), $3);
  582. }
  583. | lhs '=' command_call
  584. {
  585. $$ = node_assign($1, $3, vps);
  586. }
  587. | mlhs '=' command_call
  588. {
  589. value_expr($3);
  590. $1->nd_value = ($1->nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  591. $$ = $1;
  592. }
  593. | var_lhs tOP_ASGN command_call
  594. {
  595. value_expr($3);
  596. if ($1) {
  597. QUID vid = $1->nd_vid;
  598. if ($2 == tOROP) {
  599. $1->nd_value = $3;
  600. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  601. if (is_asgn_or_id(vid)) {
  602. $$->nd_aid = vid;
  603. }
  604. }
  605. else if ($2 == tANDOP) {
  606. $1->nd_value = $3;
  607. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  608. }
  609. else {
  610. $$ = $1;
  611. $$->nd_value = call_op(gettable(vid),$2,1,$3, vps);
  612. }
  613. }
  614. else {
  615. $$ = 0;
  616. }
  617. }
  618. | primary_value '[' aref_args ']' tOP_ASGN command_call
  619. {
  620. NODE *args;
  621. value_expr($6);
  622. if (!$3) $3 = NEW_ZARRAY();
  623. args = arg_concat(vps, $6, $3);
  624. if ($5 == tOROP) {
  625. $5 = 0;
  626. }
  627. else if ($5 == tANDOP) {
  628. $5 = 1;
  629. }
  630. $$ = NEW_OP_ASGN1($1, $5, args);
  631. fixpos($$, $1);
  632. }
  633. | primary_value '.' tIDENTIFIER tOP_ASGN command_call
  634. {
  635. value_expr($5);
  636. if ($4 == tOROP) {
  637. $4 = 0;
  638. }
  639. else if ($4 == tANDOP) {
  640. $4 = 1;
  641. }
  642. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  643. fixpos($$, $1);
  644. }
  645. | primary_value '.' tCONSTANT tOP_ASGN command_call
  646. {
  647. value_expr($5);
  648. if ($4 == tOROP) {
  649. $4 = 0;
  650. }
  651. else if ($4 == tANDOP) {
  652. $4 = 1;
  653. }
  654. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  655. fixpos($$, $1);
  656. }
  657. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
  658. {
  659. value_expr($5);
  660. if ($4 == tOROP) {
  661. $4 = 0;
  662. }
  663. else if ($4 == tANDOP) {
  664. $4 = 1;
  665. }
  666. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  667. fixpos($$, $1);
  668. }
  669. | backref tOP_ASGN command_call
  670. {
  671. rb_backref_error($1, vps);
  672. $$ = 0;
  673. }
  674. | lhs '=' mrhs
  675. {
  676. $$ = node_assign($1, NEW_SVALUE($3), vps);
  677. }
  678. | mlhs '=' arg_value
  679. {
  680. $1->nd_value = ($1->nd_head) ? NEW_TO_ARY($3) : NEW_ARRAY($3);
  681. $$ = $1;
  682. }
  683. | mlhs '=' mrhs
  684. {
  685. $1->nd_value = $3;
  686. $$ = $1;
  687. }
  688. | expr
  689. ;
  690. expr : command_call
  691. | expr kAND expr
  692. {
  693. $$ = logop(NODE_AND, $1, $3, vps);
  694. }
  695. | expr kOR expr
  696. {
  697. $$ = logop(NODE_OR, $1, $3, vps);
  698. }
  699. | kNOT expr
  700. {
  701. $$ = NEW_NOT(cond($2, vps));
  702. }
  703. | '!' command_call
  704. {
  705. $$ = NEW_NOT(cond($2, vps));
  706. }
  707. | arg
  708. ;
  709. expr_value : expr
  710. {
  711. value_expr($$);
  712. $$ = $1;
  713. }
  714. ;
  715. command_call : command
  716. | block_command
  717. | kRETURN call_args
  718. {
  719. $$ = NEW_RETURN(ret_args(vps, $2));
  720. }
  721. | kBREAK call_args
  722. {
  723. $$ = NEW_BREAK(ret_args(vps, $2));
  724. }
  725. | kNEXT call_args
  726. {
  727. $$ = NEW_NEXT(ret_args(vps, $2));
  728. }
  729. ;
  730. block_command : block_call
  731. | block_call '.' operation2 command_args
  732. {
  733. $$ = new_call(vps, $1, $3, $4);
  734. }
  735. | block_call tCOLON2 operation2 command_args
  736. {
  737. $$ = new_call(vps, $1, $3, $4);
  738. }
  739. ;
  740. cmd_brace_block : tLBRACE_ARG
  741. {
  742. $<num>1 = ruby_sourceline;
  743. reset_block(vps);
  744. }
  745. opt_block_var { $<vars>$ = variables->block_vars; }
  746. compstmt
  747. '}'
  748. {
  749. $$ = NEW_ITER($3, 0, extract_block_vars(vps, $5, $<vars>4));
  750. nd_set_line($$, $<num>1);
  751. }
  752. ;
  753. command : operation command_args %prec tLOWEST
  754. {
  755. $$ = new_fcall(vps, $1, $2);
  756. fixpos($$, $2);
  757. }
  758. | operation command_args cmd_brace_block
  759. {
  760. $$ = new_fcall(vps, $1, $2);
  761. if ($3) {
  762. if (nd_type($$) == NODE_BLOCK_PASS) {
  763. rb_compile_error(vps, "both block arg and actual block given");
  764. }
  765. $3->nd_iter = $$;
  766. $$ = $3;
  767. }
  768. fixpos($$, $2);
  769. }
  770. | primary_value '.' operation2 command_args %prec tLOWEST
  771. {
  772. $$ = new_call(vps, $1, $3, $4);
  773. fixpos($$, $1);
  774. }
  775. | primary_value '.' operation2 command_args cmd_brace_block
  776. {
  777. $$ = new_call(vps, $1, $3, $4);
  778. if ($5) {
  779. if (nd_type($$) == NODE_BLOCK_PASS) {
  780. rb_compile_error(vps, "both block arg and actual block given");
  781. }
  782. $5->nd_iter = $$;
  783. $$ = $5;
  784. }
  785. fixpos($$, $1);
  786. }
  787. | primary_value tCOLON2 operation2 command_args %prec tLOWEST
  788. {
  789. $$ = new_call(vps, $1, $3, $4);
  790. fixpos($$, $1);
  791. }
  792. | primary_value tCOLON2 operation2 command_args cmd_brace_block
  793. {
  794. $$ = new_call(vps, $1, $3, $4);
  795. if ($5) {
  796. if (nd_type($$) == NODE_BLOCK_PASS) {
  797. rb_compile_error(vps, "both block arg and actual block given");
  798. }
  799. $5->nd_iter = $$;
  800. $$ = $5;
  801. }
  802. fixpos($$, $1);
  803. }
  804. | kSUPER command_args
  805. {
  806. $$ = new_super(vps, $2);
  807. fixpos($$, $2);
  808. }
  809. | kYIELD command_args
  810. {
  811. $$ = new_yield(vps, $2);
  812. fixpos($$, $2);
  813. }
  814. ;
  815. mlhs : mlhs_basic
  816. | tLPAREN mlhs_entry ')'
  817. {
  818. $$ = $2;
  819. }
  820. ;
  821. mlhs_entry : mlhs_basic
  822. | tLPAREN mlhs_entry ')'
  823. {
  824. $$ = NEW_MASGN(NEW_LIST($2), 0);
  825. }
  826. ;
  827. mlhs_basic : mlhs_head
  828. {
  829. $$ = NEW_MASGN($1, 0);
  830. }
  831. | mlhs_head mlhs_item
  832. {
  833. $$ = NEW_MASGN(list_append(vps, $1,$2), 0);
  834. }
  835. | mlhs_head tSTAR mlhs_node
  836. {
  837. $$ = NEW_MASGN($1, $3);
  838. }
  839. | mlhs_head tSTAR
  840. {
  841. $$ = NEW_MASGN($1, -1);
  842. }
  843. | tSTAR mlhs_node
  844. {
  845. $$ = NEW_MASGN(0, $2);
  846. }
  847. | tSTAR
  848. {
  849. $$ = NEW_MASGN(0, -1);
  850. }
  851. ;
  852. mlhs_item : mlhs_node
  853. | tLPAREN mlhs_entry ')'
  854. {
  855. $$ = $2;
  856. }
  857. ;
  858. mlhs_head : mlhs_item ','
  859. {
  860. $$ = NEW_LIST($1);
  861. }
  862. | mlhs_head mlhs_item ','
  863. {
  864. $$ = list_append(vps, $1, $2);
  865. }
  866. ;
  867. mlhs_node : variable
  868. {
  869. $$ = assignable($1, 0, vps);
  870. }
  871. | primary_value '[' aref_args ']'
  872. {
  873. $$ = aryset($1, $3, vps);
  874. }
  875. | primary_value '.' tIDENTIFIER
  876. {
  877. $$ = attrset($1, $3, vps);
  878. }
  879. | primary_value tCOLON2 tIDENTIFIER
  880. {
  881. $$ = attrset($1, $3, vps);
  882. }
  883. | primary_value '.' tCONSTANT
  884. {
  885. $$ = attrset($1, $3, vps);
  886. }
  887. | primary_value tCOLON2 tCONSTANT
  888. {
  889. if (in_def || in_single)
  890. yyerror("dynamic constant assignment");
  891. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  892. }
  893. | tCOLON3 tCONSTANT
  894. {
  895. if (in_def || in_single)
  896. yyerror("dynamic constant assignment");
  897. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  898. }
  899. | backref
  900. {
  901. rb_backref_error($1, vps);
  902. $$ = 0;
  903. }
  904. ;
  905. lhs : variable
  906. {
  907. $$ = assignable($1, 0, vps);
  908. }
  909. | primary_value '[' aref_args ']'
  910. {
  911. $$ = aryset($1, $3, vps);
  912. }
  913. | primary_value '.' tIDENTIFIER
  914. {
  915. $$ = attrset($1, $3, vps);
  916. }
  917. | primary_value tCOLON2 tIDENTIFIER
  918. {
  919. $$ = attrset($1, $3, vps);
  920. }
  921. | primary_value '.' tCONSTANT
  922. {
  923. $$ = attrset($1, $3, vps);
  924. }
  925. | primary_value tCOLON2 tCONSTANT
  926. {
  927. if (in_def || in_single)
  928. yyerror("dynamic constant assignment");
  929. $$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
  930. }
  931. | tCOLON3 tCONSTANT
  932. {
  933. if (in_def || in_single)
  934. yyerror("dynamic constant assignment");
  935. $$ = NEW_CDECL(0, 0, NEW_COLON3($2));
  936. }
  937. | backref
  938. {
  939. rb_backref_error($1, vps);
  940. $$ = 0;
  941. }
  942. ;
  943. cname : tIDENTIFIER
  944. {
  945. yyerror("class/module name must be CONSTANT");
  946. }
  947. | tCONSTANT
  948. ;
  949. cpath : tCOLON3 cname
  950. {
  951. $$ = NEW_COLON3($2);
  952. }
  953. | cname
  954. {
  955. $$ = NEW_COLON2(0, $$);
  956. }
  957. | primary_value tCOLON2 cname
  958. {
  959. $$ = NEW_COLON2($1, $3);
  960. }
  961. ;
  962. fname : tIDENTIFIER
  963. | tCONSTANT
  964. | tFID
  965. | op
  966. {
  967. lex_state = EXPR_END;
  968. $$ = convert_op($1);
  969. }
  970. | reswords
  971. {
  972. lex_state = EXPR_END;
  973. $$ = $<id>1;
  974. }
  975. ;
  976. fsym : fname
  977. | symbol
  978. ;
  979. fitem : fsym
  980. {
  981. $$ = NEW_LIT(QUID2SYM($1));
  982. }
  983. | dsym
  984. ;
  985. undef_list : fitem
  986. {
  987. $$ = NEW_UNDEF($1);
  988. }
  989. | undef_list ',' {lex_state = EXPR_FNAME;} fitem
  990. {
  991. $$ = block_append(vps, $1, NEW_UNDEF($4));
  992. }
  993. ;
  994. op : '|' { $$ = '|'; }
  995. | '^' { $$ = '^'; }
  996. | '&' { $$ = '&'; }
  997. | tCMP { $$ = tCMP; }
  998. | tEQ { $$ = tEQ; }
  999. | tEQQ { $$ = tEQQ; }
  1000. | tMATCH { $$ = tMATCH; }
  1001. | '>' { $$ = '>'; }
  1002. | tGEQ { $$ = tGEQ; }
  1003. | '<' { $$ = '<'; }
  1004. | tLEQ { $$ = tLEQ; }
  1005. | tLSHFT { $$ = tLSHFT; }
  1006. | tRSHFT { $$ = tRSHFT; }
  1007. | '+' { $$ = '+'; }
  1008. | '-' { $$ = '-'; }
  1009. | '*' { $$ = '*'; }
  1010. | tSTAR { $$ = '*'; }
  1011. | '/' { $$ = '/'; }
  1012. | '%' { $$ = '%'; }
  1013. | tPOW { $$ = tPOW; }
  1014. | '~' { $$ = '~'; }
  1015. | tUPLUS { $$ = tUPLUS; }
  1016. | tUMINUS { $$ = tUMINUS; }
  1017. | tAREF { $$ = tAREF; }
  1018. | tASET { $$ = tASET; }
  1019. | '`' { $$ = '`'; }
  1020. ;
  1021. reswords : k__LINE__ | k__FILE__ | klBEGIN | klEND
  1022. | kALIAS | kAND | kBEGIN | kBREAK | kCASE | kCLASS | kDEF
  1023. | kDEFINED | kDO | kELSE | kELSIF | kEND | kENSURE | kFALSE
  1024. | kFOR | kIN | kMODULE | kNEXT | kNIL | kNOT
  1025. | kOR | kREDO | kRESCUE | kRETRY | kRETURN | kSELF | kSUPER
  1026. | kTHEN | kTRUE | kUNDEF | kWHEN | kYIELD
  1027. | kIF_MOD | kUNLESS_MOD | kWHILE_MOD | kUNTIL_MOD | kRESCUE_MOD
  1028. ;
  1029. arg : lhs '=' arg
  1030. {
  1031. $$ = node_assign($1, $3, vps);
  1032. }
  1033. | lhs '=' arg kRESCUE_MOD arg
  1034. {
  1035. $$ = node_assign($1, NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0), vps);
  1036. }
  1037. | var_lhs tOP_ASGN arg
  1038. {
  1039. value_expr($3);
  1040. if ($1) {
  1041. QUID vid = $1->nd_vid;
  1042. if ($2 == tOROP) {
  1043. $1->nd_value = $3;
  1044. $$ = NEW_OP_ASGN_OR(gettable(vid), $1);
  1045. if (is_asgn_or_id(vid)) {
  1046. $$->nd_aid = vid;
  1047. }
  1048. }
  1049. else if ($2 == tANDOP) {
  1050. $1->nd_value = $3;
  1051. $$ = NEW_OP_ASGN_AND(gettable(vid), $1);
  1052. }
  1053. else {
  1054. $$ = $1;
  1055. $$->nd_value = call_op(gettable(vid),$2,1,$3, vps);
  1056. }
  1057. }
  1058. else {
  1059. $$ = 0;
  1060. }
  1061. }
  1062. | primary_value '[' aref_args ']' tOP_ASGN arg
  1063. {
  1064. NODE *args;
  1065. value_expr($6);
  1066. if (!$3) $3 = NEW_ZARRAY();
  1067. args = arg_concat(vps, $6, $3);
  1068. if ($5 == tOROP) {
  1069. $5 = 0;
  1070. }
  1071. else if ($5 == tANDOP) {
  1072. $5 = 1;
  1073. }
  1074. $$ = NEW_OP_ASGN1($1, $5, args);
  1075. fixpos($$, $1);
  1076. }
  1077. | primary_value '.' tIDENTIFIER tOP_ASGN arg
  1078. {
  1079. value_expr($5);
  1080. if ($4 == tOROP) {
  1081. $4 = 0;
  1082. }
  1083. else if ($4 == tANDOP) {
  1084. $4 = 1;
  1085. }
  1086. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  1087. fixpos($$, $1);
  1088. }
  1089. | primary_value '.' tCONSTANT tOP_ASGN arg
  1090. {
  1091. value_expr($5);
  1092. if ($4 == tOROP) {
  1093. $4 = 0;
  1094. }
  1095. else if ($4 == tANDOP) {
  1096. $4 = 1;
  1097. }
  1098. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  1099. fixpos($$, $1);
  1100. }
  1101. | primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
  1102. {
  1103. value_expr($5);
  1104. if ($4 == tOROP) {
  1105. $4 = 0;
  1106. }
  1107. else if ($4 == tANDOP) {
  1108. $4 = 1;
  1109. }
  1110. $$ = NEW_OP_ASGN2($1, $3, $4, $5);
  1111. fixpos($$, $1);
  1112. }
  1113. | primary_value tCOLON2 tCONSTANT tOP_ASGN arg
  1114. {
  1115. yyerror("constant re-assignment");
  1116. $$ = 0;
  1117. }
  1118. | tCOLON3 tCONSTANT tOP_ASGN arg
  1119. {
  1120. yyerror("constant re-assignment");
  1121. $$ = 0;
  1122. }
  1123. | backref tOP_ASGN arg
  1124. {
  1125. rb_backref_error($1, vps);
  1126. $$ = 0;
  1127. }
  1128. | arg tDOT2 arg
  1129. {
  1130. value_expr($1);
  1131. value_expr($3);
  1132. $$ = NEW_DOT2($1, $3);
  1133. }
  1134. | arg tDOT3 arg
  1135. {
  1136. value_expr($1);
  1137. value_expr($3);
  1138. $$ = NEW_DOT3($1, $3);
  1139. }
  1140. | arg '+' arg
  1141. {
  1142. $$ = call_op($1, '+', 1, $3, vps);
  1143. }
  1144. | arg '-' arg
  1145. {
  1146. $$ = call_op($1, '-', 1, $3, vps);
  1147. }
  1148. | arg '*' arg
  1149. {
  1150. $$ = call_op($1, '*', 1, $3, vps);
  1151. }
  1152. | arg '/' arg
  1153. {
  1154. $$ = call_op($1, '/', 1, $3, vps);
  1155. }
  1156. | arg '%' arg
  1157. {
  1158. $$ = call_op($1, '%', 1, $3, vps);
  1159. }
  1160. | arg tPOW arg
  1161. {
  1162. $$ = call_op($1, tPOW, 1, $3, vps);
  1163. }
  1164. | tUMINUS_NUM tINTEGER tPOW arg
  1165. {
  1166. $$ = call_op(call_op($2, tPOW, 1, $4, vps), tUMINUS, 0, 0, vps);
  1167. }
  1168. | tUMINUS_NUM tFLOAT tPOW arg
  1169. {
  1170. $$ = call_op(call_op($2, tPOW, 1, $4, vps), tUMINUS, 0, 0, vps);
  1171. }
  1172. | tUPLUS arg
  1173. {
  1174. if ($2 && nd_type($2) == NODE_LIT) {
  1175. $$ = $2;
  1176. }
  1177. else {
  1178. $$ = call_op($2, tUPLUS, 0, 0, vps);
  1179. }
  1180. }
  1181. | tUMINUS arg
  1182. {
  1183. $$ = call_op($2, tUMINUS, 0, 0, vps);
  1184. }
  1185. | arg '|' arg
  1186. {
  1187. $$ = call_op($1, '|', 1, $3, vps);
  1188. }
  1189. | arg '^' arg
  1190. {
  1191. $$ = call_op($1, '^', 1, $3, vps);
  1192. }
  1193. | arg '&' arg
  1194. {
  1195. $$ = call_op($1, '&', 1, $3, vps);
  1196. }
  1197. | arg tCMP arg
  1198. {
  1199. $$ = call_op($1, tCMP, 1, $3, vps);
  1200. }
  1201. | arg '>' arg
  1202. {
  1203. $$ = call_op($1, '>', 1, $3, vps);
  1204. }
  1205. | arg tGEQ arg
  1206. {
  1207. $$ = call_op($1, tGEQ, 1, $3, vps);
  1208. }
  1209. | arg '<' arg
  1210. {
  1211. $$ = call_op($1, '<', 1, $3, vps);
  1212. }
  1213. | arg tLEQ arg
  1214. {
  1215. $$ = call_op($1, tLEQ, 1, $3, vps);
  1216. }
  1217. | arg tEQ arg
  1218. {
  1219. $$ = call_op($1, tEQ, 1, $3, vps);
  1220. }
  1221. | arg tEQQ arg
  1222. {
  1223. $$ = call_op($1, tEQQ, 1, $3, vps);
  1224. }
  1225. | arg tNEQ arg
  1226. {
  1227. $$ = NEW_NOT(call_op($1, tEQ, 1, $3, vps));
  1228. }
  1229. | arg tMATCH arg
  1230. {
  1231. $$ = match_gen($1, $3, vps);
  1232. }
  1233. | arg tNMATCH arg
  1234. {
  1235. $$ = NEW_NOT(match_gen($1, $3, vps));
  1236. }
  1237. | '!' arg
  1238. {
  1239. $$ = NEW_NOT(cond($2, vps));
  1240. }
  1241. | '~' arg
  1242. {
  1243. $$ = call_op($2, '~', 0, 0, vps);
  1244. }
  1245. | arg tLSHFT arg
  1246. {
  1247. $$ = call_op($1, tLSHFT, 1, $3, vps);
  1248. }
  1249. | arg tRSHFT arg
  1250. {
  1251. $$ = call_op($1, tRSHFT, 1, $3, vps);
  1252. }
  1253. | arg tANDOP arg
  1254. {
  1255. $$ = logop(NODE_AND, $1, $3, vps);
  1256. }
  1257. | arg tOROP arg
  1258. {
  1259. $$ = logop(NODE_OR, $1, $3, vps);
  1260. }
  1261. | kDEFINED opt_nl {in_defined = 1;} arg
  1262. {
  1263. in_defined = 0;
  1264. $$ = NEW_DEFINED($4);
  1265. }
  1266. | arg '?' {ternary_colon++;} arg ':' arg
  1267. {
  1268. $$ = NEW_IF(cond($1, vps), $4, $6);
  1269. fixpos($$, $1);
  1270. ternary_colon--;
  1271. }
  1272. | primary
  1273. {
  1274. $$ = $1;
  1275. }
  1276. ;
  1277. arg_value : arg
  1278. {
  1279. value_expr($1);
  1280. $$ = $1;
  1281. }
  1282. ;
  1283. aref_args : none
  1284. | command opt_nl
  1285. {
  1286. rb_warn("parenthesize argument(s) for future version");
  1287. $$ = NEW_LIST($1);
  1288. }
  1289. | args trailer
  1290. {
  1291. $$ = $1;
  1292. }
  1293. | args ',' tSTAR arg opt_nl
  1294. {
  1295. value_expr($4);
  1296. $$ = arg_concat(vps, $1, $4);
  1297. }
  1298. | assocs trailer
  1299. {
  1300. $$ = NEW_LIST(NEW_HASH($1));
  1301. }
  1302. | tSTAR arg opt_nl
  1303. {
  1304. value_expr($2);
  1305. $$ = NEW_NEWLINE(NEW_SPLAT($2));
  1306. }
  1307. ;
  1308. paren_args : '(' none ')'
  1309. {
  1310. $$ = $2;
  1311. }
  1312. | '(' call_args opt_nl ')'
  1313. {
  1314. $$ = $2;
  1315. }
  1316. | '(' block_call opt_nl ')'
  1317. {
  1318. rb_warn("parenthesize argument for future version");
  1319. $$ = NEW_LIST($2);
  1320. }
  1321. | '(' args ',' block_call opt_nl ')'
  1322. {
  1323. rb_warn("parenthesize argument for future version");
  1324. $$ = list_append(vps, $2, $4);
  1325. }
  1326. ;
  1327. opt_paren_args : none
  1328. | paren_args
  1329. ;
  1330. call_args : command
  1331. {
  1332. rb_warn("parenthesize argument(s) for future version");
  1333. $$ = NEW_LIST($1);
  1334. }
  1335. | args opt_block_arg
  1336. {
  1337. $$ = arg_blk_pass($1, $2);
  1338. }
  1339. | args ',' tSTAR arg_value opt_block_arg
  1340. {
  1341. $$ = arg_concat(vps, $1, $4);
  1342. $$ = arg_blk_pass($$, $5);
  1343. }
  1344. | assocs opt_block_arg
  1345. {
  1346. $$ = NEW_LIST(NEW_HASH($1));
  1347. $$ = arg_blk_pass($$, $2);
  1348. }
  1349. | assocs ',' tSTAR arg_value opt_block_arg
  1350. {
  1351. $$ = arg_concat(vps, NEW_LIST(NEW_HASH($1)), $4);
  1352. $$ = arg_blk_pass($$, $5);
  1353. }
  1354. | args ',' assocs opt_block_arg
  1355. {
  1356. $$ = list_append(vps, $1, NEW_HASH($3));
  1357. $$ = arg_blk_pass($$, $4);
  1358. }
  1359. | args ',' assocs ',' tSTAR arg opt_block_arg
  1360. {
  1361. value_expr($6);
  1362. $$ = arg_concat(vps, list_append(vps, $1, NEW_HASH($3)), $6);
  1363. $$ = arg_blk_pass($$, $7);
  1364. }
  1365. | tSTAR arg_value opt_block_arg
  1366. {
  1367. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1368. }
  1369. | block_arg
  1370. ;
  1371. call_args2 : arg_value ',' args opt_block_arg
  1372. {
  1373. $$ = arg_blk_pass(list_concat(NEW_LIST($1),$3), $4);
  1374. }
  1375. | arg_value ',' block_arg
  1376. {
  1377. $$ = arg_blk_pass($1, $3);
  1378. }
  1379. | arg_value ',' tSTAR arg_value opt_block_arg
  1380. {
  1381. $$ = arg_concat(vps, NEW_LIST($1), $4);
  1382. $$ = arg_blk_pass($$, $5);
  1383. }
  1384. | arg_value ',' args ',' tSTAR arg_value opt_block_arg
  1385. {
  1386. $$ = arg_concat(vps, list_concat(NEW_LIST($1),$3), $6);
  1387. $$ = arg_blk_pass($$, $7);
  1388. }
  1389. | assocs opt_block_arg
  1390. {
  1391. $$ = NEW_LIST(NEW_HASH($1));
  1392. $$ = arg_blk_pass($$, $2);
  1393. }
  1394. | assocs ',' tSTAR arg_value opt_block_arg
  1395. {
  1396. $$ = arg_concat(vps, NEW_LIST(NEW_HASH($1)), $4);
  1397. $$ = arg_blk_pass($$, $5);
  1398. }
  1399. | arg_value ',' assocs opt_block_arg
  1400. {
  1401. $$ = list_append(vps, NEW_LIST($1), NEW_HASH($3));
  1402. $$ = arg_blk_pass($$, $4);
  1403. }
  1404. | arg_value ',' args ',' assocs opt_block_arg
  1405. {
  1406. $$ = list_append(vps, list_concat(NEW_LIST($1),$3), NEW_HASH($5));
  1407. $$ = arg_blk_pass($$, $6);
  1408. }
  1409. | arg_value ',' assocs ',' tSTAR arg_value opt_block_arg
  1410. {
  1411. $$ = arg_concat(vps, list_append(vps, NEW_LIST($1), NEW_HASH($3)), $6);
  1412. $$ = arg_blk_pass($$, $7);
  1413. }
  1414. | arg_value ',' args ',' assocs ',' tSTAR arg_value opt_block_arg
  1415. {
  1416. $$ = arg_concat(vps, list_append(vps,
  1417. list_concat(NEW_LIST($1), $3), NEW_HASH($5)), $8);
  1418. $$ = arg_blk_pass($$, $9);
  1419. }
  1420. | tSTAR arg_value opt_block_arg
  1421. {
  1422. $$ = arg_blk_pass(NEW_SPLAT($2), $3);
  1423. }
  1424. | block_arg
  1425. ;
  1426. command_args : {
  1427. $<val>$ = cmdarg_stack;
  1428. CMDARG_PUSH(1);
  1429. }
  1430. open_args
  1431. {
  1432. /* CMDARG_POP() */
  1433. cmdarg_stack = $<val>1;
  1434. $$ = $2;
  1435. }
  1436. ;
  1437. open_args : call_args
  1438. | tLPAREN_ARG {lex_state = EXPR_ENDARG;} ')'
  1439. {
  1440. rb_warn("don't put space before argument parentheses");
  1441. $$ = 0;
  1442. }
  1443. | tLPAREN_ARG call_args2 {lex_state = EXPR_ENDARG;} ')'
  1444. {
  1445. rb_warn("don't put space before argument parentheses");
  1446. $$ = $2;
  1447. }
  1448. ;
  1449. block_arg : tAMPER arg_value
  1450. {
  1451. $$ = NEW_BLOCK_PASS($2);
  1452. }
  1453. ;
  1454. opt_block_arg : ',' block_arg
  1455. {
  1456. $$ = $2;
  1457. }
  1458. | none
  1459. ;
  1460. args : arg_value
  1461. {
  1462. $$ = NEW_LIST($1);
  1463. }
  1464. | args ',' arg_value
  1465. {
  1466. $$ = list_append(vps, $1, $3);
  1467. }
  1468. ;
  1469. mrhs : args ',' arg_value
  1470. {
  1471. $$ = list_append(vps, $1, $3);
  1472. }
  1473. | args ',' tSTAR arg_value
  1474. {
  1475. $$ = arg_concat(vps, $1, $4);
  1476. }
  1477. | tSTAR arg_value
  1478. {
  1479. $$ = NEW_SPLAT($2);
  1480. }
  1481. ;
  1482. primary : literal
  1483. | strings
  1484. | xstring
  1485. | regexp
  1486. | words
  1487. | qwords
  1488. | var_ref
  1489. | backref
  1490. | tFID
  1491. {
  1492. $$ = NEW_FCALL($1, 0);
  1493. }
  1494. | kBEGIN
  1495. {
  1496. $<num>1 = ruby_sourceline;
  1497. PUSH_LINE("begin");
  1498. }
  1499. bodystmt
  1500. kEND
  1501. {
  1502. POP_LINE();
  1503. if ($3 == NULL)
  1504. $$ = NEW_NIL();
  1505. else
  1506. $$ = NEW_BEGIN($3);
  1507. nd_set_line($$, $<num>1);
  1508. }
  1509. | tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} opt_nl ')'
  1510. {
  1511. rb_warning("(...) interpreted as grouped expression");
  1512. $$ = $2;
  1513. }
  1514. | tLPAREN compstmt ')'
  1515. {
  1516. if (!$2) $$ = NEW_NIL();
  1517. else $$ = $2;
  1518. }
  1519. | primary_value tCOLON2 tCONSTANT
  1520. {
  1521. $$ = NEW_COLON2($1, $3);
  1522. }
  1523. | tCOLON3 tCONSTANT
  1524. {
  1525. $$ = NEW_COLON3($2);
  1526. }
  1527. | primary_value '[' aref_args ']'
  1528. {
  1529. if ($1 && nd_type($1) == NODE_SELF) {
  1530. $$ = NEW_FCALL(convert_op(tAREF), $3);
  1531. } else {
  1532. $$ = NEW_CALL($1, convert_op(tAREF), $3);
  1533. }
  1534. fixpos($$, $1);
  1535. }
  1536. | tLBRACK aref_args ']'
  1537. {
  1538. if ($2 == 0) {
  1539. $$ = NEW_ZARRAY(); /* zero length array*/
  1540. }
  1541. else {
  1542. $$ = $2;
  1543. }
  1544. }
  1545. | tLBRACE assoc_list '}'
  1546. {
  1547. $$ = NEW_HASH($2);
  1548. }
  1549. | kRETURN
  1550. {
  1551. $$ = NEW_RETURN(0);
  1552. }
  1553. | kYIELD '(' call_args ')'
  1554. {
  1555. $$ = new_yield(vps, $3);
  1556. }
  1557. | kYIELD '(' ')'
  1558. {
  1559. $$ = NEW_YIELD(0, Qfalse);
  1560. }
  1561. | kYIELD
  1562. {
  1563. $$ = NEW_YIELD(0, Qfalse);
  1564. }
  1565. | kDEFINED opt_nl '(' {in_defined = 1;} expr ')'
  1566. {
  1567. in_defined = 0;
  1568. $$ = NEW_DEFINED($5);
  1569. }
  1570. | operation brace_block
  1571. {
  1572. $2->nd_iter = NEW_FCALL($1, 0);
  1573. $$ = $2;
  1574. fixpos($2->nd_iter, $2);
  1575. }
  1576. | method_call
  1577. | method_call brace_block
  1578. {
  1579. if ($1 && nd_type($1) == NODE_BLOCK_PASS) {
  1580. rb_compile_error(vps, "both block arg and actual block given");
  1581. }
  1582. $2->nd_iter = $1;
  1583. $$ = $2;
  1584. fixpos($$, $1);
  1585. }
  1586. | kIF {
  1587. PUSH_LINE("if");
  1588. } expr_value then
  1589. compstmt
  1590. if_tail
  1591. kEND
  1592. {
  1593. POP_LINE();
  1594. $$ = NEW_IF(cond($3, vps), $5, $6);
  1595. fixpos($$, $3);
  1596. if (cond_negative(&$$->nd_cond)) {
  1597. NODE *tmp = $$->nd_body;
  1598. $$->nd_body = $$->nd_else;
  1599. $$->nd_else = tmp;
  1600. }
  1601. }
  1602. | kUNLESS {
  1603. PUSH_LINE("unless");
  1604. } expr_value then
  1605. compstmt
  1606. opt_else
  1607. kEND
  1608. {
  1609. POP_LINE();
  1610. $$ = NEW_UNLESS(cond($3, vps), $5, $6);
  1611. fixpos($$, $3);
  1612. if (cond_negative(&$$->nd_cond)) {
  1613. NODE *tmp = $$->nd_body;
  1614. $$->nd_body = $$->nd_else;
  1615. $$->nd_else = tmp;
  1616. }
  1617. }
  1618. | kWHILE {
  1619. PUSH_LINE("while");
  1620. COND_PUSH(1);
  1621. } expr_value do {COND_POP();}
  1622. compstmt
  1623. kEND
  1624. {
  1625. POP_LINE();
  1626. $$ = NEW_WHILE(cond($3, vps), $6, 1);
  1627. fixpos($$, $3);
  1628. if (cond_negative(&$$->nd_cond)) {
  1629. nd_set_type($$, NODE_UNTIL);
  1630. }
  1631. }
  1632. | kUNTIL {
  1633. PUSH_LINE("until");
  1634. COND_PUSH(1);
  1635. } expr_value do {COND_POP();}
  1636. compstmt
  1637. kEND
  1638. {
  1639. POP_LINE();
  1640. $$ = NEW_UNTIL(cond($3, vps), $6, 1);
  1641. fixpos($$, $3);
  1642. if (cond_negative(&$$->nd_cond)) {
  1643. nd_set_type($$, NODE_WHILE);
  1644. }
  1645. }
  1646. | kCASE {
  1647. PUSH_LINE("case");
  1648. } expr_value opt_terms
  1649. case_body
  1650. kEND
  1651. {
  1652. POP_LINE();
  1653. $$ = NEW_CASE($3, $5);
  1654. fixpos($$, $3);
  1655. }
  1656. | kCASE opt_terms {
  1657. push_start_line((rb_parser_state*)parser_state, ruby_sourceline - 1, "case");
  1658. } case_body kEND
  1659. {
  1660. POP_LINE();
  1661. $$ = $4;
  1662. }
  1663. | kCASE opt_terms {
  1664. push_start_line((rb_parser_state*)parser_state, ruby_sourceline - 1, "case");
  1665. } kELSE compstmt kEND
  1666. {
  1667. POP_LINE();
  1668. $$ = $5;
  1669. }
  1670. | kFOR {
  1671. PUSH_LINE("for");
  1672. } for_var kIN {COND_PUSH(1);} expr_value do {COND_POP();}
  1673. compstmt
  1674. kEND
  1675. {
  1676. POP_LINE();
  1677. $$ = NEW_FOR($3, $6, $9);
  1678. fixpos($$, $3);
  1679. }
  1680. | kCLASS cpath superclass
  1681. {
  1682. PUSH_LINE("class");
  1683. if (in_def || in_single)
  1684. yyerror("class definition in method body");
  1685. class_nest++;
  1686. local_push(0);
  1687. $<num>$ = ruby_sourceline;
  1688. }
  1689. bodystmt
  1690. kEND
  1691. {
  1692. POP_LINE();
  1693. $$ = NEW_CLASS($2, $5, $3);
  1694. nd_set_line($$, $<num>4);
  1695. local_pop();
  1696. class_nest--;
  1697. }
  1698. | kCLASS tLSHFT expr
  1699. {
  1700. PUSH_LINE("class");
  1701. $<num>$ = in_def;
  1702. in_def = 0;
  1703. }
  1704. term
  1705. {
  1706. $<num>$ = in_single;
  1707. in_single = 0;
  1708. class_nest++;
  1709. local_push(0);
  1710. }
  1711. bodystmt
  1712. kEND
  1713. {
  1714. POP_LINE();
  1715. $$ = NEW_SCLASS($3, $7);
  1716. fixpos($$, $3);
  1717. local_pop();
  1718. class_nest--;
  1719. in_def = $<num>4;
  1720. in_single = $<num>6;
  1721. }
  1722. | kMODULE cpath
  1723. {
  1724. PUSH_LINE("module");
  1725. if (in_def || in_single)
  1726. yyerror("module definition in method body");
  1727. class_nest++;
  1728. local_push(0);
  1729. $<num>$ = ruby_sourceline;
  1730. }
  1731. bodystmt
  1732. kEND
  1733. {
  1734. POP_LINE();
  1735. $$ = NEW_MODULE($2, $4);
  1736. nd_set_line($$, $<num>3);
  1737. local_pop();
  1738. class_nest--;
  1739. }
  1740. | kDEF fname
  1741. {
  1742. PUSH_LINE("def");
  1743. $<id>$ = cur_mid;
  1744. cur_mid = $2;
  1745. in_def++;
  1746. local_push(0);
  1747. }
  1748. f_arglist
  1749. bodystmt
  1750. kEND
  1751. {
  1752. POP_LINE();
  1753. if (!$5) $5 = NEW_NIL();
  1754. $$ = NEW_DEFN($2, $4, $5, NOEX_PRIVATE);
  1755. fixpos($$, $4);
  1756. local_pop();
  1757. in_def--;
  1758. cur_mid = $<id>3;
  1759. }
  1760. | kDEF singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
  1761. {
  1762. PUSH_LINE("def");
  1763. in_single++;
  1764. local_push(0);
  1765. lex_state = EXPR_END; /* force for args */
  1766. }
  1767. f_arglist
  1768. bodystmt
  1769. kEND
  1770. {
  1771. POP_LINE();
  1772. $$ = NEW_DEFS($2, $5, $7, $8);
  1773. fixpos($$, $2);
  1774. local_pop();
  1775. in_single--;
  1776. }
  1777. | kBREAK
  1778. {
  1779. $$ = NEW_BREAK(0);
  1780. }
  1781. | kNEXT
  1782. {
  1783. $$ = NEW_NEXT(0);
  1784. }
  1785. | kREDO
  1786. {
  1787. $$ = NEW_REDO();
  1788. }
  1789. | kRETRY
  1790. {
  1791. $$ = NEW_RETRY();
  1792. }
  1793. ;
  1794. primary_value : primary
  1795. {
  1796. value_expr($1);
  1797. $$ = $1;
  1798. }
  1799. ;
  1800. then : term
  1801. | ':'
  1802. | kTHEN
  1803. | term kTHEN
  1804. ;
  1805. do : term
  1806. | ':'
  1807. | kDO_COND
  1808. ;
  1809. if_tail : opt_else
  1810. | kELSIF expr_value then
  1811. compstmt
  1812. if_tail
  1813. {
  1814. $$ = NEW_IF(cond($2, vps), $4, $5);
  1815. fixpos($$, $2);
  1816. }
  1817. ;
  1818. opt_else : none
  1819. | kELSE compstmt
  1820. {
  1821. $$ = $2;
  1822. }
  1823. ;
  1824. for_var : lhs
  1825. | mlhs
  1826. ;
  1827. block_par : mlhs_item
  1828. {
  1829. $$ = NEW_LIST($1);
  1830. }
  1831. | block_par ',' mlhs_item
  1832. {
  1833. $$ = list_append(vps, $1, $3);
  1834. }
  1835. ;
  1836. block_var : block_par
  1837. {
  1838. if ($1->nd_alen == 1) {
  1839. $$ = $1->nd_head;
  1840. }
  1841. else {
  1842. $$ = NEW_MASGN($1, 0);
  1843. }
  1844. }
  1845. | block_par ','
  1846. {
  1847. $$ = NEW_MASGN($1, 0);
  1848. }
  1849. | block_par ',' tAMPER lhs
  1850. {
  1851. $$ = NEW_BLOCK_VAR($4, NEW_MASGN($1, 0));
  1852. }
  1853. | block_par ',' tSTAR lhs ',' tAMPER lhs
  1854. {
  1855. $$ = NEW_BLOCK_VAR($7, NEW_MASGN($1, $4));
  1856. }
  1857. | block_par ',' tSTAR ',' tAMPER lhs
  1858. {
  1859. $$ = NEW_BLOCK_VAR($6, NEW_MASGN($1, -1));
  1860. }
  1861. | block_par ',' tSTAR lhs
  1862. {
  1863. $$ = NEW_MASGN($1, $4);
  1864. }
  1865. | block_par ',' tSTAR
  1866. {
  1867. $$ = NEW_MASGN($1, -1);
  1868. }
  1869. | tSTAR lhs ',' tAMPER lhs
  1870. {
  1871. $$ = NEW_BLOCK_VAR($5, NEW_MASGN(0, $2));
  1872. }
  1873. | tSTAR ',' tAMPER lhs
  1874. {
  1875. $$ = NEW_BLOCK_VAR($4, NEW_MASGN(0, -1));
  1876. }
  1877. | tSTAR lhs
  1878. {
  1879. $$ = NEW_MASGN(0, $2);
  1880. }
  1881. | tSTAR
  1882. {
  1883. $$ = NEW_MASGN(0, -1);
  1884. }
  1885. | tAMPER lhs
  1886. {
  1887. $$ = NEW_BLOCK_VAR($2, (NODE*)1);
  1888. }
  1889. ;
  1890. opt_block_var : none
  1891. | '|' /* none */ '|'
  1892. {
  1893. $$ = (NODE*)1;
  1894. }
  1895. | tOROP
  1896. {
  1897. $$ = (NODE*)1;
  1898. }
  1899. | '|' block_var '|'
  1900. {
  1901. $$ = $2;
  1902. }
  1903. ;
  1904. do_block : kDO_BLOCK
  1905. {
  1906. PUSH_LINE("do");
  1907. $<num>1 = ruby_sourceline;
  1908. reset_block(vps);
  1909. }
  1910. opt_block_var
  1911. {
  1912. $<vars>$ = variables->block_vars;
  1913. }
  1914. compstmt
  1915. kEND
  1916. {
  1917. POP_LINE();
  1918. $$ = NEW_ITER($3, 0, extract_block_vars(vps, $5, $<vars>4));
  1919. nd_set_line($$, $<num>1);
  1920. }
  1921. ;
  1922. block_call : command do_block
  1923. {
  1924. if ($1 && nd_type($1) == NODE_BLOCK_PASS) {
  1925. rb_compile_error(vps, "both block arg and actual block given");
  1926. }
  1927. $2->nd_iter = $1;
  1928. $$ = $2;
  1929. fixpos($$, $1);
  1930. }
  1931. | block_call '.' operation2 opt_paren_args
  1932. {
  1933. $$ = new_call(vps, $1, $3, $4);
  1934. }
  1935. | block_call tCOLON2 operation2 opt_paren_args
  1936. {
  1937. $$ = new_call(vps, $1, $3, $4);
  1938. }
  1939. ;
  1940. method_call : operation paren_args
  1941. {
  1942. $$ = new_fcall(vps, $1, $2);
  1943. fixpos($$, $2);
  1944. }
  1945. | primary_value '.' operation2 opt_paren_args
  1946. {
  1947. $$ = new_call(vps, $1, $3, $4);
  1948. fixpos($$, $1);
  1949. }
  1950. | primary_value tCOLON2 operation2 paren_args
  1951. {
  1952. $$ = new_call(vps, $1, $3, $4);
  1953. fixpos($$, $1);
  1954. }
  1955. | primary_value tCOLON2 operation3
  1956. {
  1957. $$ = new_call(vps, $1, $3, 0);
  1958. }
  1959. | primary_value '\\' operation2
  1960. {
  1961. $$ = NEW_CALL($1, rb_parser_sym("get_reference"),
  1962. NEW_LIST(NEW_LIT(QUID2SYM($3))));
  1963. }
  1964. | tUBS operation2
  1965. {
  1966. $$ = NEW_FCALL(rb_parser_sym("get_reference"),
  1967. NEW_LIST(NEW_LIT(QUID2SYM($2))));
  1968. }
  1969. | kSUPER paren_args
  1970. {
  1971. $$ = new_super(vps, $2);
  1972. }
  1973. | kSUPER
  1974. {
  1975. $$ = NEW_ZSUPER();
  1976. }
  1977. ;
  1978. brace_block : '{'
  1979. {
  1980. $<num>1 = ruby_sourceline;
  1981. reset_block(vps);
  1982. }
  1983. opt_block_var { $<vars>$ = variables->block_vars; }
  1984. compstmt '}'
  1985. {
  1986. $$ = NEW_ITER($3, 0, extract_block_vars(vps, $5, $<vars>4));
  1987. nd_set_line($$, $<num>1);
  1988. }
  1989. | kDO
  1990. {
  1991. PUSH_LINE("do");
  1992. $<num>1 = ruby_sourceline;
  1993. reset_block(vps);
  1994. }
  1995. opt_block_var { $<vars>$ = variables->block_vars; }
  1996. compstmt kEND
  1997. {
  1998. POP_LINE();
  1999. $$ = NEW_ITER($3, 0, extract_block_vars(vps, $5, $<vars>4));
  2000. nd_set_line($$, $<num>1);
  2001. }
  2002. ;
  2003. case_body : kWHEN when_args then
  2004. compstmt
  2005. cases
  2006. {
  2007. $$ = NEW_WHEN($2, $4, $5);
  2008. }
  2009. ;
  2010. when_args : args
  2011. | args ',' tSTAR arg_value
  2012. {
  2013. $$ = list_append(vps, $1, NEW_WHEN($4, 0, 0));
  2014. }
  2015. | tSTAR arg_value
  2016. {
  2017. $$ = NEW_LIST(NEW_WHEN($2, 0, 0));
  2018. }
  2019. ;
  2020. cases : opt_else
  2021. | case_body
  2022. ;
  2023. opt_rescue : kRESCUE exc_list exc_var then
  2024. compstmt
  2025. opt_rescue
  2026. {
  2027. if ($3) {
  2028. $3 = node_assign($3, NEW_GVAR(rb_parser_sym("$!")), vps);
  2029. $5 = block_append(vps, $3, $5);
  2030. }
  2031. $$ = NEW_RESBODY($2, $5, $6);
  2032. fixpos($$, $2?$2:$5);
  2033. }
  2034. | none
  2035. ;
  2036. exc_list : arg_value
  2037. {
  2038. $$ = NEW_LIST($1);
  2039. }
  2040. | mrhs
  2041. | none
  2042. ;
  2043. exc_var : tASSOC lhs
  2044. {
  2045. $$ = $2;
  2046. }
  2047. | none
  2048. ;
  2049. opt_ensure : kENSURE compstmt
  2050. {
  2051. if ($2)
  2052. $$ = $2;
  2053. else
  2054. /* place holder */
  2055. $$ = NEW_NIL();
  2056. }
  2057. | none
  2058. ;
  2059. literal : numeric
  2060. | symbol
  2061. {
  2062. $$ = NEW_LIT(QUID2SYM($1));
  2063. }
  2064. | dsym
  2065. ;
  2066. strings : string
  2067. {
  2068. NODE *node = $1;
  2069. if (!node) {
  2070. node = NEW_STR(string_new(0, 0));
  2071. }
  2072. else {
  2073. node = evstr2dstr(vps, node);
  2074. }
  2075. $$ = node;
  2076. }
  2077. ;
  2078. string : string1
  2079. | string string1
  2080. {
  2081. $$ = literal_concat(vps, $1, $2);
  2082. }
  2083. ;
  2084. string1 : tSTRING_BEG string_contents tSTRING_END
  2085. {
  2086. $$ = $2;
  2087. }
  2088. ;
  2089. xstring : tXSTRING_BEG xstring_contents tSTRING_END
  2090. {
  2091. NODE *node = $2;
  2092. if (!node) {
  2093. node = NEW_XSTR(string_new(0, 0));
  2094. }
  2095. else {
  2096. switch (nd_type(node)) {
  2097. case NODE_STR:
  2098. nd_set_type(node, NODE_XSTR);
  2099. break;
  2100. case NODE_DSTR:
  2101. nd_set_type(node, NODE_DXSTR);
  2102. break;
  2103. default:
  2104. node = NEW_NODE(NODE_DXSTR, string_new(0, 0), 1, NEW_LIST(node));
  2105. break;
  2106. }
  2107. }
  2108. $$ = node;
  2109. }
  2110. ;
  2111. regexp : tREGEXP_BEG xstring_contents tREGEXP_END
  2112. {
  2113. intptr_t options = $3;
  2114. NODE *node = $2;
  2115. if (!node) {
  2116. node = NEW_REGEX(string_new2(""), options & ~RE_OPTION_ONCE);
  2117. }
  2118. else switch (nd_type(node)) {
  2119. case NODE_STR:
  2120. {
  2121. nd_set_type(node, NODE_REGEX);
  2122. node->nd_cnt = options & ~RE_OPTION_ONCE;
  2123. }
  2124. break;
  2125. default:
  2126. node = NEW_NODE(NODE_DSTR, string_new(0, 0), 1, NEW_LIST(node));
  2127. case NODE_DSTR:
  2128. if (options & RE_OPTION_ONCE) {
  2129. nd_set_type(node, NODE_DREGX_ONCE);
  2130. }
  2131. else {
  2132. nd_set_type(node, NODE_DREGX);
  2133. }
  2134. node->nd_cflag = options & ~RE_OPTION_ONCE;
  2135. break;
  2136. }
  2137. $$ = node;
  2138. }
  2139. ;
  2140. words : tWORDS_BEG ' ' tSTRING_END
  2141. {
  2142. $$ = NEW_ZARRAY();
  2143. }
  2144. | tWORDS_BEG word_list tSTRING_END
  2145. {
  2146. $$ = $2;
  2147. }
  2148. ;
  2149. word_list : /* none */
  2150. {
  2151. $$ = 0;
  2152. }
  2153. | word_list word ' '
  2154. {
  2155. $$ = list_append(vps, $1, evstr2dstr(vps, $2));
  2156. }
  2157. ;
  2158. word : string_content
  2159. | word string_content
  2160. {
  2161. $$ = literal_concat(vps, $1, $2);
  2162. }
  2163. ;
  2164. qwords : tQWORDS_BEG ' ' tSTRING_END
  2165. {
  2166. $$ = NEW_ZARRAY();
  2167. }
  2168. | tQWORDS_BEG qword_list tSTRING_END
  2169. {
  2170. $$ = $2;
  2171. }
  2172. ;
  2173. qword_list : /* none */
  2174. {
  2175. $$ = 0;
  2176. }
  2177. | qword_list tSTRING_CONTENT ' '
  2178. {
  2179. $$ = list_append(vps, $1, $2);
  2180. }
  2181. ;
  2182. string_contents : /* none */
  2183. {
  2184. $$ = 0;
  2185. }
  2186. | string_contents string_content
  2187. {
  2188. $$ = literal_concat(vps, $1, $2);
  2189. }
  2190. ;
  2191. xstring_contents: /* none */
  2192. {
  2193. $$ = 0;
  2194. }
  2195. | xstring_contents string_content
  2196. {
  2197. $$ = literal_concat(vps, $1, $2);
  2198. }
  2199. ;
  2200. string_content : tSTRING_CONTENT
  2201. | tSTRING_DVAR
  2202. {
  2203. $<node>$ = lex_strterm;
  2204. lex_strterm = 0;
  2205. lex_state = EXPR_BEG;
  2206. }
  2207. string_dvar
  2208. {
  2209. lex_strterm = $<node>2;
  2210. $$ = NEW_EVSTR($3);
  2211. }
  2212. | tSTRING_DBEG
  2213. {
  2214. $<node>$ = lex_strterm;
  2215. lex_strterm = 0;
  2216. lex_state = EXPR_BEG;
  2217. COND_PUSH(0);
  2218. CMDARG_PUSH(0);
  2219. }
  2220. compstmt '}'
  2221. {
  2222. lex_strterm = $<node>2;
  2223. COND_LEXPOP();
  2224. CMDARG_LEXPOP();
  2225. if (($$ = $3) && nd_type($$) == NODE_NEWLINE) {
  2226. $$ = $$->nd_next;
  2227. }
  2228. $$ = new_evstr(vps, $$);
  2229. }
  2230. ;
  2231. string_dvar : tGVAR {$$ = NEW_GVAR($1);}
  2232. | tIVAR {$$ = NEW_IVAR($1);}
  2233. | tCVAR {$$ = NEW_CVAR($1);}
  2234. | backref
  2235. ;
  2236. symbol : tSYMBEG sym
  2237. {
  2238. lex_state = EXPR_END;
  2239. $$ = $2;
  2240. }
  2241. ;
  2242. sym : fname
  2243. | tIVAR
  2244. | tGVAR
  2245. | tCVAR
  2246. ;
  2247. dsym : tSYMBEG xstring_contents tSTRING_END
  2248. {
  2249. lex_state = EXPR_END;
  2250. if (!($$ = $2)) {
  2251. yyerror("empty symbol literal");
  2252. }
  2253. else {
  2254. switch (nd_type($$)) {
  2255. case NODE_DSTR:
  2256. nd_set_type($$, NODE_DSYM);
  2257. break;
  2258. case NODE_STR:
  2259. /* TODO: this line should never fail unless nd_str is binary */
  2260. if (strlen(bdatae($$->nd_str,"")) == (size_t)blength($$->nd_str)) {
  2261. QUID tmp = rb_parser_sym(bdata($$->nd_str));
  2262. bdestroy($$->nd_str);
  2263. $$->nd_lit = QUID2SYM(tmp);
  2264. nd_set_type($$, NODE_LIT);
  2265. break;
  2266. } else {
  2267. bdestroy($$->nd_str);
  2268. }
  2269. /* fall through */
  2270. default:
  2271. $$ = NEW_NODE(NODE_DSYM, string_new(0, 0), 1, NEW_LIST($$));
  2272. break;
  2273. }
  2274. }
  2275. }
  2276. ;
  2277. numeric : tINTEGER
  2278. | tFLOAT
  2279. | tUMINUS_NUM tINTEGER %prec tLOWEST
  2280. {
  2281. $$ = NEW_NEGATE($2);
  2282. }
  2283. | tUMINUS_NUM tFLOAT %prec tLOWEST
  2284. {
  2285. $$ = NEW_NEGATE($2);
  2286. }
  2287. ;
  2288. variable : tIDENTIFIER
  2289. | tIVAR
  2290. | tGVAR
  2291. | tCONSTANT
  2292. | tCVAR
  2293. | kNIL {$$ = kNIL;}
  2294. | kSELF {$$ = kSELF;}
  2295. | kTRUE {$$ = kTRUE;}
  2296. | kFALSE {$$ = kFALSE;}
  2297. | k__FILE__ {$$ = k__FILE__;}
  2298. | k__LINE__ {$$ = k__LINE__;}
  2299. ;
  2300. var_ref : variable
  2301. {
  2302. $$ = gettable($1);
  2303. }
  2304. ;
  2305. var_lhs : variable
  2306. {
  2307. $$ = assignable($1, 0, vps);
  2308. }
  2309. ;
  2310. backref : tNTH_REF
  2311. | tBACK_REF
  2312. ;
  2313. superclass : term
  2314. {
  2315. $$ = 0;
  2316. }
  2317. | '<'
  2318. {
  2319. lex_state = EXPR_BEG;
  2320. }
  2321. expr_value term
  2322. {
  2323. $$ = $3;
  2324. }
  2325. | error term {yyerrok; $$ = 0;}
  2326. ;
  2327. f_arglist : '(' f_args opt_nl ')'
  2328. {
  2329. $$ = $2;
  2330. lex_state = EXPR_BEG;
  2331. command_start = TRUE;
  2332. }
  2333. | f_args term
  2334. {
  2335. $$ = $1;
  2336. }
  2337. ;
  2338. f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
  2339. {
  2340. $$ = block_append(vps, NEW_ARGS((intptr_t)$1, $3, $5), $6);
  2341. }
  2342. | f_arg ',' f_optarg opt_f_block_arg
  2343. {
  2344. $$ = block_append(vps, NEW_ARGS((intptr_t)$1, $3, 0), $4);
  2345. }
  2346. | f_arg ',' f_rest_arg opt_f_block_arg
  2347. {
  2348. $$ = block_append(vps, NEW_ARGS((intptr_t)$1, 0, $3), $4);
  2349. }
  2350. | f_arg opt_f_block_arg
  2351. {
  2352. $$ = block_append(vps, NEW_ARGS((intptr_t)$1, 0, 0), $2);
  2353. }
  2354. | f_optarg ',' f_rest_arg opt_f_block_arg
  2355. {
  2356. $$ = block_append(vps, NEW_ARGS(0, $1, $3), $4);
  2357. }
  2358. | f_optarg opt_f_block_arg
  2359. {
  2360. $$ = block_append(vps, NEW_ARGS(0, $1, 0), $2);
  2361. }
  2362. | f_rest_arg opt_f_block_arg
  2363. {
  2364. $$ = block_append(vps, NEW_ARGS(0, 0, $1), $2);
  2365. }
  2366. | f_block_arg
  2367. {
  2368. $$ = block_append(vps, NEW_ARGS(0, 0, 0), $1);
  2369. }
  2370. | /* none */
  2371. {
  2372. $$ = NEW_ARGS(0, 0, 0);
  2373. }
  2374. ;
  2375. f_norm_arg : tCONSTANT
  2376. {
  2377. yyerror("formal argument cannot be a constant");
  2378. }
  2379. | tIVAR
  2380. {
  2381. yyerror("formal argument cannot be an instance variable");
  2382. }
  2383. | tGVAR
  2384. {
  2385. yyerror("formal argument cannot be a global variable");
  2386. }
  2387. | tCVAR
  2388. {
  2389. yyerror("formal argument cannot be a class variable");
  2390. }
  2391. | tIDENTIFIER
  2392. {
  2393. if (!is_local_id($1))
  2394. yyerror("formal argument must be local variable");
  2395. else if (local_id($1))
  2396. yyerror("duplicate argument name");
  2397. local_cnt($1);
  2398. $$ = 1;
  2399. }
  2400. ;
  2401. f_arg : f_norm_arg
  2402. | f_arg ',' f_norm_arg
  2403. {
  2404. $$ += 1;
  2405. }
  2406. ;
  2407. f_opt : tIDENTIFIER '=' arg_value
  2408. {
  2409. if (!is_local_id($1))
  2410. yyerror("formal argument must be local variable");
  2411. else if (local_id($1))
  2412. yyerror("duplicate optional argument name");
  2413. $$ = assignable($1, $3, vps);
  2414. }
  2415. ;
  2416. f_optarg : f_opt
  2417. {
  2418. $$ = NEW_BLOCK($1);
  2419. $$->nd_end = $$;
  2420. }
  2421. | f_optarg ',' f_opt
  2422. {
  2423. $$ = block_append(vps, $1, $3);
  2424. }
  2425. ;
  2426. restarg_mark : '*'
  2427. | tSTAR
  2428. ;
  2429. f_rest_arg : restarg_mark tIDENTIFIER
  2430. {
  2431. if (!is_local_id($2))
  2432. yyerror("rest argument must be local variable");
  2433. else if (local_id($2))
  2434. yyerror("duplicate rest argument name");
  2435. $$ = local_cnt($2) + 1;
  2436. }
  2437. | restarg_mark
  2438. {
  2439. $$ = -2;
  2440. }
  2441. ;
  2442. blkarg_mark : '&'
  2443. | tAMPER
  2444. ;
  2445. f_block_arg : blkarg_mark tIDENTIFIER
  2446. {
  2447. if (!is_local_id($2))
  2448. yyerror("block argument must be local variable");
  2449. else if (local_id($2))
  2450. yyerror("duplicate block argument name");
  2451. $$ = NEW_BLOCK_ARG($2);
  2452. }
  2453. ;
  2454. opt_f_block_arg : ',' f_block_arg
  2455. {
  2456. $$ = $2;
  2457. }
  2458. | none
  2459. ;
  2460. singleton : var_ref
  2461. {
  2462. $$ = $1;
  2463. value_expr($$);
  2464. }
  2465. | '(' {lex_state = EXPR_BEG;} expr opt_nl ')'
  2466. {
  2467. if ($3 == 0) {
  2468. yyerror("can't define singleton method for ().");
  2469. }
  2470. else {
  2471. switch (nd_type($3)) {
  2472. case NODE_STR:
  2473. case NODE_DSTR:
  2474. case NODE_XSTR:
  2475. case NODE_DXSTR:
  2476. case NODE_DREGX:
  2477. case NODE_LIT:
  2478. case NODE_ARRAY:
  2479. case NODE_ZARRAY:
  2480. yyerror("can't define singleton method for literals");
  2481. default:
  2482. value_expr($3);
  2483. break;
  2484. }
  2485. }
  2486. $$ = $3;
  2487. }
  2488. ;
  2489. assoc_list : none
  2490. | assocs trailer
  2491. {
  2492. $$ = $1;
  2493. }
  2494. | args trailer
  2495. {
  2496. if ($1->nd_alen%2 != 0) {
  2497. yyerror("odd number list for Hash");
  2498. }
  2499. $$ = $1;
  2500. }
  2501. ;
  2502. assocs : assoc
  2503. | assocs ',' assoc
  2504. {
  2505. $$ = list_concat($1, $3);
  2506. }
  2507. ;
  2508. assoc : arg_value tASSOC arg_value
  2509. {
  2510. $$ = list_append(vps, NEW_LIST($1), $3);
  2511. }
  2512. ;
  2513. operation : tIDENTIFIER
  2514. | tCONSTANT
  2515. | tFID
  2516. ;
  2517. operation2 : tIDENTIFIER
  2518. | tCONSTANT
  2519. | tFID
  2520. | op
  2521. ;
  2522. operation3 : tIDENTIFIER
  2523. | tFID
  2524. | op
  2525. ;
  2526. dot_or_colon : '.'
  2527. | tCOLON2
  2528. ;
  2529. opt_terms : /* none */
  2530. | terms
  2531. ;
  2532. opt_nl : /* none */
  2533. | '\n'
  2534. ;
  2535. trailer : /* none */
  2536. | '\n'
  2537. | ','
  2538. ;
  2539. term : ';' {yyerrok;}
  2540. | '\n'
  2541. ;
  2542. terms : term
  2543. | terms ';' {yyerrok;}
  2544. ;
  2545. none : /* none */ {$$ = 0;}
  2546. ;
  2547. %%
  2548. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  2549. since ours (we hope) works properly with all combinations of
  2550. machines, compilers, `char' and `unsigned char' argument types.
  2551. (Per Bothner suggested the basic approach.) */
  2552. #undef SIGN_EXTEND_CHAR
  2553. #if __STDC__
  2554. # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
  2555. #else /* not __STDC__ */
  2556. /* As in Harbison and Steele. */
  2557. # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
  2558. #endif
  2559. #define is_identchar(c) (SIGN_EXTEND_CHAR(c)!=-1&&(ISALNUM(c) || (c) == '_' || ismbchar(c)))
  2560. #define LEAVE_BS 1
  2561. static int
  2562. mel_yyerror(const char *msg, rb_parser_state *parser_state)
  2563. {
  2564. create_error(parser_state, (char *)msg);
  2565. return 1;
  2566. }
  2567. static int
  2568. yycompile(rb_parser_state *parser_state, char *f, int line)
  2569. {
  2570. int n;
  2571. /* Setup an initial empty scope. */
  2572. heredoc_end = 0;
  2573. lex_strterm = 0;
  2574. end_seen = 0;
  2575. ruby_sourcefile = f;
  2576. command_start = TRUE;
  2577. n = yyparse(parser_state);
  2578. ruby_debug_lines = 0;
  2579. compile_for_eval = 0;
  2580. cond_stack = 0;
  2581. cmdarg_stack = 0;
  2582. command_start = TRUE;
  2583. class_nest = 0;
  2584. in_single = 0;
  2585. in_def = 0;
  2586. cur_mid = 0;
  2587. lex_strterm = 0;
  2588. return n;
  2589. }
  2590. static bool
  2591. lex_get_str(rb_parser_state *parser_state)
  2592. {
  2593. const char *str;
  2594. const char *beg, *end, *pend;
  2595. int sz;
  2596. str = bdata(lex_string);
  2597. beg = str;
  2598. if (lex_str_used) {
  2599. if (blength(lex_string) == lex_str_used) {
  2600. return false;
  2601. }
  2602. beg += lex_str_used;
  2603. }
  2604. pend = str + blength(lex_string);
  2605. end = beg;
  2606. while(end < pend) {
  2607. if(*end++ == '\n') break;
  2608. }
  2609. sz = (int)(end - beg);
  2610. bcatblk(line_buffer, beg, sz);
  2611. lex_str_used += sz;
  2612. return TRUE;
  2613. }
  2614. static bool
  2615. lex_getline(rb_parser_state *parser_state)
  2616. {
  2617. if(!line_buffer) {
  2618. line_buffer = cstr2bstr("");
  2619. } else {
  2620. btrunc(line_buffer, 0);
  2621. }
  2622. return lex_gets(parser_state);
  2623. }
  2624. VALUE
  2625. string_to_ast(VALUE ptp, const char *f, bstring s, int line)
  2626. {
  2627. int n;
  2628. VALUE ret;
  2629. rb_parser_state *parser_state = parser_alloc_state();
  2630. lex_string = s;
  2631. lex_gets = lex_get_str;
  2632. processor = ptp;
  2633. ruby_sourceline = line - 1;
  2634. compile_for_eval = 1;
  2635. n = yycompile(parser_state, (char*)f, line);
  2636. if(!parse_error) {
  2637. for(std::vector<bstring>::iterator i = magic_comments->begin();
  2638. i != magic_comments->end();
  2639. i++) {
  2640. rb_funcall(ptp, rb_intern("add_magic_comment"), 1,
  2641. rb_str_new((const char*)(*i)->data, (*i)->slen));
  2642. }
  2643. ret = process_parse_tree(parser_state, ptp, top_node, NULL);
  2644. } else {
  2645. ret = Qnil;
  2646. }
  2647. pt_free(parser_state);
  2648. free(parser_state);
  2649. quark_cleanup();
  2650. return ret;
  2651. }
  2652. static bool parse_io_gets(rb_parser_state *parser_state) {
  2653. if(feof(lex_io)) {
  2654. return false;
  2655. }
  2656. while(TRUE) {
  2657. char *ptr, buf[1024];
  2658. int read;
  2659. ptr = fgets(buf, sizeof(buf), lex_io);
  2660. if(!ptr) {
  2661. return false;
  2662. }
  2663. read = (int)strlen(ptr);
  2664. bcatblk(line_buffer, ptr, read);
  2665. /* check whether we read a full line */
  2666. if(!(read == (sizeof(buf) - 1) && ptr[read] != '\n')) {
  2667. break;
  2668. }
  2669. }
  2670. return TRUE;
  2671. }
  2672. VALUE
  2673. file_to_ast(VALUE ptp, const char *f, FILE *file, int start)
  2674. {
  2675. int n;
  2676. VALUE ret;
  2677. rb_parser_state *parser_state = parser_alloc_state();
  2678. lex_io = file;
  2679. lex_gets = parse_io_gets;
  2680. processor = ptp;
  2681. ruby_sourceline = start - 1;
  2682. n = yycompile(parser_state, (char*)f, start);
  2683. if(!parse_error) {
  2684. for(std::vector<bstring>::iterator i = magic_comments->begin();
  2685. i != magic_comments->end();
  2686. i++) {
  2687. rb_funcall(ptp, rb_intern("add_magic_comment"), 1,
  2688. rb_str_new((const char*)(*i)->data, (*i)->slen));
  2689. }
  2690. ret = process_parse_tree(parser_state, ptp, top_node, NULL);
  2691. if (end_seen && lex_io) {
  2692. rb_funcall(ptp, rb_sData, 1, ULONG2NUM(ftell(lex_io)));
  2693. }
  2694. } else {
  2695. ret = Qnil;
  2696. }
  2697. pt_free(parser_state);
  2698. free(parser_state);
  2699. quark_cleanup();
  2700. return ret;
  2701. }
  2702. #define nextc() ps_nextc(parser_state)
  2703. static inline int
  2704. ps_nextc(rb_parser_state *parser_state)
  2705. {
  2706. int c;
  2707. if (lex_p == lex_pend) {
  2708. bstring v;
  2709. if (!lex_getline(parser_state)) return -1;
  2710. v = line_buffer;
  2711. if (heredoc_end > 0) {
  2712. ruby_sourceline = heredoc_end;
  2713. heredoc_end = 0;
  2714. }
  2715. ruby_sourceline++;
  2716. /* This code is setup so that lex_pend can be compared to
  2717. the data in lex_lastline. Thats important, otherwise
  2718. the heredoc code breaks. */
  2719. if(lex_lastline) {
  2720. bassign(lex_lastline, v);
  2721. } else {
  2722. lex_lastline = bstrcpy(v);
  2723. }
  2724. v = lex_lastline;
  2725. lex_pbeg = lex_p = bdata(v);
  2726. lex_pend = lex_p + blength(v);
  2727. }
  2728. c = (unsigned char)*(lex_p++);
  2729. if (c == '\r' && lex_p < lex_pend && *(lex_p) == '\n') {
  2730. lex_p++;
  2731. c = '\n';
  2732. column = 0;
  2733. } else if(c == '\n') {
  2734. column = 0;
  2735. } else {
  2736. column++;
  2737. }
  2738. return c;
  2739. }
  2740. static void
  2741. pushback(int c, rb_parser_state *parser_state)
  2742. {
  2743. if (c == -1) return;
  2744. lex_p--;
  2745. }
  2746. /* Indicates if we're currently at the beginning of a line. */
  2747. #define was_bol() (lex_p == lex_pbeg + 1)
  2748. #define peek(c) (lex_p != lex_pend && (c) == *(lex_p))
  2749. /* The token buffer. It's just a global string that has
  2750. functions to build up the string easily. */
  2751. #define tokfix() (tokenbuf[tokidx]='\0')
  2752. #define tok() tokenbuf
  2753. #define toklen() tokidx
  2754. #define toklast() (tokidx>0?tokenbuf[tokidx-1]:0)
  2755. static char*
  2756. newtok(rb_parser_state *parser_state)
  2757. {
  2758. tokidx = 0;
  2759. if (!tokenbuf) {
  2760. toksiz = 60;
  2761. tokenbuf = ALLOC_N(char, 60);
  2762. }
  2763. if (toksiz > 4096) {
  2764. toksiz = 60;
  2765. REALLOC_N(tokenbuf, char, 60);
  2766. }
  2767. return tokenbuf;
  2768. }
  2769. static void tokadd(char c, rb_parser_state *parser_state)
  2770. {
  2771. assert(tokidx < toksiz && tokidx >= 0);
  2772. tokenbuf[tokidx++] = c;
  2773. if (tokidx >= toksiz) {
  2774. toksiz *= 2;
  2775. REALLOC_N(tokenbuf, char, toksiz);
  2776. }
  2777. }
  2778. static int
  2779. read_escape(rb_parser_state *parser_state)
  2780. {
  2781. int c;
  2782. switch (c = nextc()) {
  2783. case '\\': /* Backslash */
  2784. return c;
  2785. case 'n': /* newline */
  2786. return '\n';
  2787. case 't': /* horizontal tab */
  2788. return '\t';
  2789. case 'r': /* carriage-return */
  2790. return '\r';
  2791. case 'f': /* form-feed */
  2792. return '\f';
  2793. case 'v': /* vertical tab */
  2794. return '\13';
  2795. case 'a': /* alarm(bell) */
  2796. return '\007';
  2797. case 'e': /* escape */
  2798. return 033;
  2799. case '0': case '1': case '2': case '3': /* octal constant */
  2800. case '4': case '5': case '6': case '7':
  2801. {
  2802. int numlen;
  2803. pushback(c, parser_state);
  2804. c = scan_oct(lex_p, 3, &numlen);
  2805. lex_p += numlen;
  2806. }
  2807. return c;
  2808. case 'x': /* hex constant */
  2809. {
  2810. int numlen;
  2811. c = scan_hex(lex_p, 2, &numlen);
  2812. if (numlen == 0) {
  2813. yyerror("Invalid escape character syntax");
  2814. return 0;
  2815. }
  2816. lex_p += numlen;
  2817. }
  2818. return c;
  2819. case 'b': /* backspace */
  2820. return '\010';
  2821. case 's': /* space */
  2822. return ' ';
  2823. case 'M':
  2824. if ((c = nextc()) != '-') {
  2825. yyerror("Invalid escape character syntax");
  2826. pushback(c, parser_state);
  2827. return '\0';
  2828. }
  2829. if ((c = nextc()) == '\\') {
  2830. return read_escape(parser_state) | 0x80;
  2831. }
  2832. else if (c == -1) goto eof;
  2833. else {
  2834. return ((c & 0xff) | 0x80);
  2835. }
  2836. case 'C':
  2837. if ((c = nextc()) != '-') {
  2838. yyerror("Invalid escape character syntax");
  2839. pushback(c, parser_state);
  2840. return '\0';
  2841. }
  2842. case 'c':
  2843. if ((c = nextc())== '\\') {
  2844. c = read_escape(parser_state);
  2845. }
  2846. else if (c == '?')
  2847. return 0177;
  2848. else if (c == -1) goto eof;
  2849. return c & 0x9f;
  2850. eof:
  2851. case -1:
  2852. yyerror("Invalid escape character syntax");
  2853. return '\0';
  2854. default:
  2855. return c;
  2856. }
  2857. }
  2858. static int
  2859. tokadd_escape(int term, rb_parser_state *parser_state)
  2860. {
  2861. int c;
  2862. switch (c = nextc()) {
  2863. case '\n':
  2864. return 0; /* just ignore */
  2865. case '0': case '1': case '2': case '3': /* octal constant */
  2866. case '4': case '5': case '6': case '7':
  2867. {
  2868. int i;
  2869. tokadd((char)'\\', parser_state);
  2870. tokadd((char)c, parser_state);
  2871. for (i=0; i<2; i++) {
  2872. c = nextc();
  2873. if (c == -1) goto eof;
  2874. if (c < '0' || '7' < c) {
  2875. pushback(c, parser_state);
  2876. break;
  2877. }
  2878. tokadd((char)c, parser_state);
  2879. }
  2880. }
  2881. return 0;
  2882. case 'x': /* hex constant */
  2883. {
  2884. int numlen;
  2885. tokadd('\\', parser_state);
  2886. tokadd((char)c, parser_state);
  2887. scan_hex(lex_p, 2, &numlen);
  2888. if (numlen == 0) {
  2889. yyerror("Invalid escape character syntax");
  2890. return -1;
  2891. }
  2892. while (numlen--)
  2893. tokadd((char)nextc(), parser_state);
  2894. }
  2895. return 0;
  2896. case 'M':
  2897. if ((c = nextc()) != '-') {
  2898. yyerror("Invalid escape character syntax");
  2899. pushback(c, parser_state);
  2900. return 0;
  2901. }
  2902. tokadd('\\',parser_state);
  2903. tokadd('M', parser_state);
  2904. tokadd('-', parser_state);
  2905. goto escaped;
  2906. case 'C':
  2907. if ((c = nextc()) != '-') {
  2908. yyerror("Invalid escape character syntax");
  2909. pushback(c, parser_state);
  2910. return 0;
  2911. }
  2912. tokadd('\\', parser_state);
  2913. tokadd('C', parser_state);
  2914. tokadd('-', parser_state);
  2915. goto escaped;
  2916. case 'c':
  2917. tokadd('\\', parser_state);
  2918. tokadd('c', parser_state);
  2919. escaped:
  2920. if ((c = nextc()) == '\\') {
  2921. return tokadd_escape(term, parser_state);
  2922. }
  2923. else if (c == -1) goto eof;
  2924. tokadd((char)c, parser_state);
  2925. return 0;
  2926. eof:
  2927. case -1:
  2928. yyerror("Invalid escape character syntax");
  2929. return -1;
  2930. default:
  2931. if (c != '\\' || c != term)
  2932. tokadd('\\', parser_state);
  2933. tokadd((char)c, parser_state);
  2934. }
  2935. return 0;
  2936. }
  2937. static int
  2938. regx_options(rb_parser_state *parser_state)
  2939. {
  2940. char kcode = 0;
  2941. int options = 0;
  2942. int c;
  2943. newtok(parser_state);
  2944. while (c = nextc(), ISALPHA(c)) {
  2945. switch (c) {
  2946. case 'i':
  2947. options |= RE_OPTION_IGNORECASE;
  2948. break;
  2949. case 'x':
  2950. options |= RE_OPTION_EXTENDED;
  2951. break;
  2952. case 'm':
  2953. options |= RE_OPTION_MULTILINE;
  2954. break;
  2955. case 'o':
  2956. options |= RE_OPTION_ONCE;
  2957. break;
  2958. case 'G':
  2959. options |= RE_OPTION_CAPTURE_GROUP;
  2960. break;
  2961. case 'g':
  2962. options |= RE_OPTION_DONT_CAPTURE_GROUP;
  2963. break;
  2964. case 'n':
  2965. kcode = 16;
  2966. break;
  2967. case 'e':
  2968. kcode = 32;
  2969. break;
  2970. case 's':
  2971. kcode = 48;
  2972. break;
  2973. case 'u':
  2974. kcode = 64;
  2975. break;
  2976. default:
  2977. tokadd((char)c, parser_state);
  2978. break;
  2979. }
  2980. }
  2981. pushback(c, parser_state);
  2982. if (toklen()) {
  2983. tokfix();
  2984. rb_compile_error(parser_state, "unknown regexp option%s - %s",
  2985. toklen() > 1 ? "s" : "", tok());
  2986. }
  2987. return options | kcode;
  2988. }
  2989. #define STR_FUNC_ESCAPE 0x01
  2990. #define STR_FUNC_EXPAND 0x02
  2991. #define STR_FUNC_REGEXP 0x04
  2992. #define STR_FUNC_QWORDS 0x08
  2993. #define STR_FUNC_SYMBOL 0x10
  2994. #define STR_FUNC_INDENT 0x20
  2995. enum string_type {
  2996. str_squote = (0),
  2997. str_dquote = (STR_FUNC_EXPAND),
  2998. str_xquote = (STR_FUNC_EXPAND),
  2999. str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
  3000. str_sword = (STR_FUNC_QWORDS),
  3001. str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND),
  3002. str_ssym = (STR_FUNC_SYMBOL),
  3003. str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND),
  3004. };
  3005. static int tokadd_string(int func, int term, int paren, quark *nest, rb_parser_state *parser_state)
  3006. {
  3007. int c;
  3008. while ((c = nextc()) != -1) {
  3009. if (paren && c == paren) {
  3010. ++*nest;
  3011. }
  3012. else if (c == term) {
  3013. if (!nest || !*nest) {
  3014. pushback(c, parser_state);
  3015. break;
  3016. }
  3017. --*nest;
  3018. }
  3019. else if ((func & STR_FUNC_EXPAND) && c == '#' && lex_p < lex_pend) {
  3020. int c2 = *(lex_p);
  3021. if (c2 == '$' || c2 == '@' || c2 == '{') {
  3022. pushback(c, parser_state);
  3023. break;
  3024. }
  3025. }
  3026. else if (c == '\\') {
  3027. c = nextc();
  3028. switch (c) {
  3029. case '\n':
  3030. if (func & STR_FUNC_QWORDS) break;
  3031. if (func & STR_FUNC_EXPAND) continue;
  3032. tokadd('\\', parser_state);
  3033. break;
  3034. case '\\':
  3035. if (func & STR_FUNC_ESCAPE) tokadd((char)c, parser_state);
  3036. break;
  3037. default:
  3038. if (func & STR_FUNC_REGEXP) {
  3039. pushback(c, parser_state);
  3040. if (tokadd_escape(term, parser_state) < 0)
  3041. return -1;
  3042. continue;
  3043. }
  3044. else if (func & STR_FUNC_EXPAND) {
  3045. pushback(c, parser_state);
  3046. if (func & STR_FUNC_ESCAPE) tokadd('\\', parser_state);
  3047. c = read_escape(parser_state);
  3048. }
  3049. else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
  3050. /* ignore backslashed spaces in %w */
  3051. }
  3052. else if (c != term && !(paren && c == paren)) {
  3053. tokadd('\\', parser_state);
  3054. }
  3055. }
  3056. }
  3057. else if (ismbchar(c)) {
  3058. int i, len = mbclen(c)-1;
  3059. for (i = 0; i < len; i++) {
  3060. tokadd((char)c, parser_state);
  3061. c = nextc();
  3062. }
  3063. }
  3064. else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
  3065. pushback(c, parser_state);
  3066. break;
  3067. }
  3068. if (!c && (func & STR_FUNC_SYMBOL)) {
  3069. func &= ~STR_FUNC_SYMBOL;
  3070. rb_compile_error(parser_state, "symbol cannot contain '\\0'");
  3071. continue;
  3072. }
  3073. tokadd((char)c, parser_state);
  3074. }
  3075. return c;
  3076. }
  3077. #define NEW_STRTERM(func, term, paren) \
  3078. node_newnode(NODE_STRTERM, (VALUE)(func), \
  3079. (VALUE)((term) | ((paren) << (CHAR_BIT * 2))), 0)
  3080. #define pslval ((YYSTYPE *)lval)
  3081. static int
  3082. parse_string(NODE *quote, rb_parser_state *parser_state)
  3083. {
  3084. int func = (int)quote->nd_func;
  3085. int term = nd_term(quote);
  3086. int paren = nd_paren(quote);
  3087. int c, space = 0;
  3088. long start_line = ruby_sourceline;
  3089. if (func == -1) return tSTRING_END;
  3090. c = nextc();
  3091. if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
  3092. do {c = nextc();} while (ISSPACE(c));
  3093. space = 1;
  3094. }
  3095. if (c == term && !quote->nd_nest) {
  3096. if (func & STR_FUNC_QWORDS) {
  3097. quote->nd_func = -1;
  3098. return ' ';
  3099. }
  3100. if (!(func & STR_FUNC_REGEXP)) return tSTRING_END;
  3101. pslval->num = regx_options(parser_state);
  3102. return tREGEXP_END;
  3103. }
  3104. if (space) {
  3105. pushback(c, parser_state);
  3106. return ' ';
  3107. }
  3108. newtok(parser_state);
  3109. if ((func & STR_FUNC_EXPAND) && c == '#') {
  3110. switch (c = nextc()) {
  3111. case '$':
  3112. case '@':
  3113. pushback(c, parser_state);
  3114. return tSTRING_DVAR;
  3115. case '{':
  3116. return tSTRING_DBEG;
  3117. }
  3118. tokadd('#', parser_state);
  3119. }
  3120. pushback(c, parser_state);
  3121. if (tokadd_string(func, term, paren, &quote->nd_nest, parser_state) == -1) {
  3122. ruby_sourceline = nd_line(quote);
  3123. rb_compile_error(parser_state, "unterminated string meets end of file");
  3124. return tSTRING_END;
  3125. }
  3126. tokfix();
  3127. pslval->node = NEW_STR(string_new(tok(), toklen()));
  3128. nd_set_line(pslval->node, start_line);
  3129. return tSTRING_CONTENT;
  3130. }
  3131. /* Called when the lexer detects a heredoc is beginning. This pulls
  3132. in more characters and detects what kind of heredoc it is. */
  3133. static int
  3134. heredoc_identifier(rb_parser_state *parser_state)
  3135. {
  3136. int c = nextc(), term, func = 0;
  3137. size_t len;
  3138. if (c == '-') {
  3139. c = nextc();
  3140. func = STR_FUNC_INDENT;
  3141. }
  3142. switch (c) {
  3143. case '\'':
  3144. func |= str_squote; goto quoted;
  3145. case '"':
  3146. func |= str_dquote; goto quoted;
  3147. case '`':
  3148. func |= str_xquote;
  3149. quoted:
  3150. /* The heredoc indent is quoted, so its easy to find, we just
  3151. continue to consume characters into the token buffer until
  3152. we hit the terminating character. */
  3153. newtok(parser_state);
  3154. tokadd((char)func, parser_state);
  3155. term = c;
  3156. /* Where of where has the term gone.. */
  3157. while ((c = nextc()) != -1 && c != term) {
  3158. len = mbclen(c);
  3159. do {
  3160. tokadd((char)c, parser_state);
  3161. } while (--len > 0 && (c = nextc()) != -1);
  3162. }
  3163. /* Ack! end of file or end of string. */
  3164. if (c == -1) {
  3165. rb_compile_error(parser_state, "unterminated here document identifier");
  3166. return 0;
  3167. }
  3168. break;
  3169. default:
  3170. /* Ok, this is an unquoted heredoc ident. We just consume
  3171. until we hit a non-ident character. */
  3172. /* Do a quick check that first character is actually valid.
  3173. if it's not, then this isn't actually a heredoc at all!
  3174. It sucks that it's way down here in this function that in
  3175. finally bails with this not being a heredoc.*/
  3176. if (!is_identchar(c)) {
  3177. pushback(c, parser_state);
  3178. if (func & STR_FUNC_INDENT) {
  3179. pushback('-', parser_state);
  3180. }
  3181. return 0;
  3182. }
  3183. /* Finally, setup the token buffer and begin to fill it. */
  3184. newtok(parser_state);
  3185. term = '"';
  3186. tokadd((char)(func |= str_dquote), parser_state);
  3187. do {
  3188. len = mbclen(c);
  3189. do { tokadd((char)c, parser_state); } while (--len > 0 && (c = nextc()) != -1);
  3190. } while ((c = nextc()) != -1 && is_identchar(c));
  3191. pushback(c, parser_state);
  3192. break;
  3193. }
  3194. /* Fixup the token buffer, ie set the last character to null. */
  3195. tokfix();
  3196. len = lex_p - lex_pbeg;
  3197. lex_p = lex_pend;
  3198. pslval->id = 0;
  3199. /* Tell the lexer that we're inside a string now. nd_lit is
  3200. the heredoc identifier that we watch the stream for to
  3201. detect the end of the heredoc. */
  3202. bstring str = bstrcpy(lex_lastline);
  3203. lex_strterm = node_newnode(NODE_HEREDOC,
  3204. (VALUE)string_new(tok(), toklen()), /* nd_lit */
  3205. (VALUE)len, /* nd_nth */
  3206. (VALUE)str); /* nd_orig */
  3207. return term == '`' ? tXSTRING_BEG : tSTRING_BEG;
  3208. }
  3209. static void
  3210. heredoc_restore(NODE *here, rb_parser_state *parser_state)
  3211. {
  3212. bstring line = here->nd_orig;
  3213. bdestroy(lex_lastline);
  3214. lex_lastline = line;
  3215. lex_pbeg = bdata(line);
  3216. lex_pend = lex_pbeg + blength(line);
  3217. lex_p = lex_pbeg + here->nd_nth;
  3218. heredoc_end = ruby_sourceline;
  3219. ruby_sourceline = nd_line(here);
  3220. bdestroy((bstring)here->nd_lit);
  3221. }
  3222. static int
  3223. whole_match_p(const char *eos, int len, int indent, rb_parser_state *parser_state)
  3224. {
  3225. char *p = lex_pbeg;
  3226. int n;
  3227. if (indent) {
  3228. while (*p && ISSPACE(*p)) p++;
  3229. }
  3230. n = (int)(lex_pend - (p + len));
  3231. if (n < 0 || (n > 0 && p[len] != '\n' && p[len] != '\r')) return FALSE;
  3232. if (strncmp(eos, p, len) == 0) return TRUE;
  3233. return FALSE;
  3234. }
  3235. /* Called when the lexer knows it's inside a heredoc. This function
  3236. is responsible for detecting an expandions (ie #{}) in the heredoc
  3237. and emitting a lex token and also detecting the end of the heredoc. */
  3238. static int
  3239. here_document(NODE *here, rb_parser_state *parser_state)
  3240. {
  3241. int c, func, indent = 0;
  3242. char *eos, *p, *pend;
  3243. long len;
  3244. bstring str = NULL;
  3245. /* eos == the heredoc ident that we found when the heredoc started */
  3246. eos = bdata(here->nd_str);
  3247. len = blength(here->nd_str) - 1;
  3248. /* indicates if we should search for expansions. */
  3249. indent = (func = *eos++) & STR_FUNC_INDENT;
  3250. /* Ack! EOF or end of input string! */
  3251. if ((c = nextc()) == -1) {
  3252. error:
  3253. rb_compile_error(parser_state, "can't find string \"%s\" anywhere before EOF", eos);
  3254. heredoc_restore(lex_strterm, parser_state);
  3255. lex_strterm = 0;
  3256. return 0;
  3257. }
  3258. /* Gr. not yet sure what was_bol() means other than it seems like
  3259. it means only 1 character has been consumed. */
  3260. if (was_bol() && whole_match_p(eos, (int)len, indent, parser_state)) {
  3261. heredoc_restore(lex_strterm, parser_state);
  3262. return tSTRING_END;
  3263. }
  3264. /* If aren't doing expansions, we can just scan until
  3265. we find the identifier. */
  3266. if ((func & STR_FUNC_EXPAND) == 0) {
  3267. do {
  3268. p = bdata(lex_lastline);
  3269. pend = lex_pend;
  3270. if (pend > p) {
  3271. switch (pend[-1]) {
  3272. case '\n':
  3273. if (--pend == p || pend[-1] != '\r') {
  3274. pend++;
  3275. break;
  3276. }
  3277. case '\r':
  3278. --pend;
  3279. }
  3280. }
  3281. if (str) {
  3282. bcatblk(str, p, (int)(pend - p));
  3283. } else {
  3284. str = blk2bstr(p, (int)(pend - p));
  3285. }
  3286. if (pend < lex_pend) bcatblk(str, "\n", 1);
  3287. lex_p = lex_pend;
  3288. if (nextc() == -1) {
  3289. if (str) bdestroy(str);
  3290. goto error;
  3291. }
  3292. } while (!whole_match_p(eos, (int)len, indent, parser_state));
  3293. }
  3294. else {
  3295. newtok(parser_state);
  3296. if (c == '#') {
  3297. switch (c = nextc()) {
  3298. case '$':
  3299. case '@':
  3300. pushback(c, parser_state);
  3301. return tSTRING_DVAR;
  3302. case '{':
  3303. return tSTRING_DBEG;
  3304. }
  3305. tokadd('#', parser_state);
  3306. }
  3307. /* Loop while we haven't found a the heredoc ident. */
  3308. do {
  3309. pushback(c, parser_state);
  3310. /* Scan up until a \n and fill in the token buffer. */
  3311. if ((c = tokadd_string(func, '\n', 0, NULL, parser_state)) == -1) goto error;
  3312. /* We finished scanning, but didn't find a \n, so we setup the node
  3313. and have the lexer file in more. */
  3314. if (c != '\n') {
  3315. pslval->node = NEW_STR(string_new(tok(), toklen()));
  3316. return tSTRING_CONTENT;
  3317. }
  3318. /* I think this consumes the \n */
  3319. tokadd((char)nextc(), parser_state);
  3320. if ((c = nextc()) == -1) goto error;
  3321. } while (!whole_match_p(eos, (int)len, indent, parser_state));
  3322. str = string_new(tok(), toklen());
  3323. }
  3324. heredoc_restore(lex_strterm, parser_state);
  3325. lex_strterm = NEW_STRTERM(-1, 0, 0);
  3326. pslval->node = NEW_STR(str);
  3327. return tSTRING_CONTENT;
  3328. }
  3329. #include "lex.c.tab"
  3330. static void
  3331. arg_ambiguous()
  3332. {
  3333. rb_warning("ambiguous first argument; put parentheses or even spaces");
  3334. }
  3335. #define IS_ARG() (lex_state == EXPR_ARG || lex_state == EXPR_CMDARG)
  3336. static char* parse_comment(struct rb_parser_state* parser_state) {
  3337. int len = (int)(lex_pend - lex_p);
  3338. char* str = lex_p;
  3339. while(len-- > 0 && ISSPACE(str[0])) str++;
  3340. if(len <= 2) return NULL;
  3341. if(str[0] == '-' && str[1] == '*' && str[2] == '-') return str;
  3342. return NULL;
  3343. }
  3344. static int
  3345. yylex(void *yylval_v, void *vstate)
  3346. {
  3347. register int c;
  3348. int space_seen = 0;
  3349. int cmd_state;
  3350. struct rb_parser_state *parser_state;
  3351. bstring cur_line;
  3352. enum lex_state_e last_state;
  3353. YYSTYPE *yylval = (YYSTYPE*)yylval_v;
  3354. parser_state = (struct rb_parser_state*)vstate;
  3355. lval = (void *)yylval;
  3356. /*
  3357. c = nextc();
  3358. printf("lex char: %c\n", c);
  3359. pushback(c, parser_state);
  3360. */
  3361. if (lex_strterm) {
  3362. int token;
  3363. if (nd_type(lex_strterm) == NODE_HEREDOC) {
  3364. token = here_document(lex_strterm, parser_state);
  3365. if (token == tSTRING_END) {
  3366. lex_strterm = 0;
  3367. lex_state = EXPR_END;
  3368. }
  3369. }
  3370. else {
  3371. token = parse_string(lex_strterm, parser_state);
  3372. if (token == tSTRING_END || token == tREGEXP_END) {
  3373. lex_strterm = 0;
  3374. lex_state = EXPR_END;
  3375. }
  3376. }
  3377. return token;
  3378. }
  3379. cmd_state = command_start;
  3380. command_start = FALSE;
  3381. retry:
  3382. switch (c = nextc()) {
  3383. case '\0': /* NUL */
  3384. case '\004': /* ^D */
  3385. case '\032': /* ^Z */
  3386. case -1: /* end of script. */
  3387. return 0;
  3388. /* white spaces */
  3389. case ' ': case '\t': case '\f': case '\r':
  3390. case '\13': /* '\v' */
  3391. space_seen++;
  3392. goto retry;
  3393. case '#': /* it's a comment */
  3394. if(char* str = parse_comment(parser_state)) {
  3395. int len = (int)(lex_pend - str - 1); // - 1 for the \n
  3396. cur_line = blk2bstr(str, len);
  3397. magic_comments->push_back(cur_line);
  3398. }
  3399. lex_p = lex_pend;
  3400. /* fall through */
  3401. case '\n':
  3402. switch (lex_state) {
  3403. case EXPR_BEG:
  3404. case EXPR_FNAME:
  3405. case EXPR_DOT:
  3406. case EXPR_CLASS:
  3407. goto retry;
  3408. default:
  3409. break;
  3410. }
  3411. command_start = TRUE;
  3412. lex_state = EXPR_BEG;
  3413. return '\n';
  3414. case '*':
  3415. if ((c = nextc()) == '*') {
  3416. if ((c = nextc()) == '=') {
  3417. pslval->id = tPOW;
  3418. lex_state = EXPR_BEG;
  3419. return tOP_ASGN;
  3420. }
  3421. pushback(c, parser_state);
  3422. c = tPOW;
  3423. }
  3424. else {
  3425. if (c == '=') {
  3426. pslval->id = '*';
  3427. lex_state = EXPR_BEG;
  3428. return tOP_ASGN;
  3429. }
  3430. pushback(c, parser_state);
  3431. if (IS_ARG() && space_seen && !ISSPACE(c)){
  3432. rb_warning("`*' interpreted as argument prefix");
  3433. c = tSTAR;
  3434. }
  3435. else if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  3436. c = tSTAR;
  3437. }
  3438. else {
  3439. c = '*';
  3440. }
  3441. }
  3442. switch (lex_state) {
  3443. case EXPR_FNAME: case EXPR_DOT:
  3444. lex_state = EXPR_ARG; break;
  3445. default:
  3446. lex_state = EXPR_BEG; break;
  3447. }
  3448. return c;
  3449. case '!':
  3450. lex_state = EXPR_BEG;
  3451. if ((c = nextc()) == '=') {
  3452. return tNEQ;
  3453. }
  3454. if (c == '~') {
  3455. return tNMATCH;
  3456. }
  3457. pushback(c, parser_state);
  3458. return '!';
  3459. case '=':
  3460. if (was_bol()) {
  3461. /* skip embedded rd document */
  3462. if (strncmp(lex_p, "begin", 5) == 0 && ISSPACE(lex_p[5])) {
  3463. for (;;) {
  3464. lex_p = lex_pend;
  3465. c = nextc();
  3466. if (c == -1) {
  3467. rb_compile_error(parser_state, "embedded document meets end of file");
  3468. return 0;
  3469. }
  3470. if (c != '=') continue;
  3471. if (strncmp(lex_p, "end", 3) == 0 &&
  3472. (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
  3473. break;
  3474. }
  3475. }
  3476. lex_p = lex_pend;
  3477. goto retry;
  3478. }
  3479. }
  3480. switch (lex_state) {
  3481. case EXPR_FNAME: case EXPR_DOT:
  3482. lex_state = EXPR_ARG; break;
  3483. default:
  3484. lex_state = EXPR_BEG; break;
  3485. }
  3486. if ((c = nextc()) == '=') {
  3487. if ((c = nextc()) == '=') {
  3488. return tEQQ;
  3489. }
  3490. pushback(c, parser_state);
  3491. return tEQ;
  3492. }
  3493. if (c == '~') {
  3494. return tMATCH;
  3495. }
  3496. else if (c == '>') {
  3497. return tASSOC;
  3498. }
  3499. pushback(c, parser_state);
  3500. return '=';
  3501. case '<':
  3502. c = nextc();
  3503. if (c == '<' &&
  3504. lex_state != EXPR_END &&
  3505. lex_state != EXPR_DOT &&
  3506. lex_state != EXPR_ENDARG &&
  3507. lex_state != EXPR_CLASS &&
  3508. (!IS_ARG() || space_seen)) {
  3509. int token = heredoc_identifier(parser_state);
  3510. if (token) return token;
  3511. }
  3512. switch (lex_state) {
  3513. case EXPR_FNAME: case EXPR_DOT:
  3514. lex_state = EXPR_ARG; break;
  3515. default:
  3516. lex_state = EXPR_BEG; break;
  3517. }
  3518. if (c == '=') {
  3519. if ((c = nextc()) == '>') {
  3520. return tCMP;
  3521. }
  3522. pushback(c, parser_state);
  3523. return tLEQ;
  3524. }
  3525. if (c == '<') {
  3526. if ((c = nextc()) == '=') {
  3527. pslval->id = tLSHFT;
  3528. lex_state = EXPR_BEG;
  3529. return tOP_ASGN;
  3530. }
  3531. pushback(c, parser_state);
  3532. return tLSHFT;
  3533. }
  3534. pushback(c, parser_state);
  3535. return '<';
  3536. case '>':
  3537. switch (lex_state) {
  3538. case EXPR_FNAME: case EXPR_DOT:
  3539. lex_state = EXPR_ARG; break;
  3540. default:
  3541. lex_state = EXPR_BEG; break;
  3542. }
  3543. if ((c = nextc()) == '=') {
  3544. return tGEQ;
  3545. }
  3546. if (c == '>') {
  3547. if ((c = nextc()) == '=') {
  3548. pslval->id = tRSHFT;
  3549. lex_state = EXPR_BEG;
  3550. return tOP_ASGN;
  3551. }
  3552. pushback(c, parser_state);
  3553. return tRSHFT;
  3554. }
  3555. pushback(c, parser_state);
  3556. return '>';
  3557. case '"':
  3558. lex_strterm = NEW_STRTERM(str_dquote, '"', 0);
  3559. return tSTRING_BEG;
  3560. case '`':
  3561. if (lex_state == EXPR_FNAME) {
  3562. lex_state = EXPR_END;
  3563. return c;
  3564. }
  3565. if (lex_state == EXPR_DOT) {
  3566. if (cmd_state)
  3567. lex_state = EXPR_CMDARG;
  3568. else
  3569. lex_state = EXPR_ARG;
  3570. return c;
  3571. }
  3572. lex_strterm = NEW_STRTERM(str_xquote, '`', 0);
  3573. pslval->id = 0; /* so that xstring gets used normally */
  3574. return tXSTRING_BEG;
  3575. case '\'':
  3576. lex_strterm = NEW_STRTERM(str_squote, '\'', 0);
  3577. pslval->id = 0; /* so that xstring gets used normally */
  3578. return tSTRING_BEG;
  3579. case '?':
  3580. if (lex_state == EXPR_END || lex_state == EXPR_ENDARG) {
  3581. lex_state = EXPR_BEG;
  3582. return '?';
  3583. }
  3584. c = nextc();
  3585. if (c == -1) {
  3586. rb_compile_error(parser_state, "incomplete character syntax");
  3587. return 0;
  3588. }
  3589. if (ISSPACE(c)){
  3590. if (!IS_ARG()){
  3591. int c2 = 0;
  3592. switch (c) {
  3593. case ' ':
  3594. c2 = 's';
  3595. break;
  3596. case '\n':
  3597. c2 = 'n';
  3598. break;
  3599. case '\t':
  3600. c2 = 't';
  3601. break;
  3602. case '\v':
  3603. c2 = 'v';
  3604. break;
  3605. case '\r':
  3606. c2 = 'r';
  3607. break;
  3608. case '\f':
  3609. c2 = 'f';
  3610. break;
  3611. }
  3612. if (c2) {
  3613. rb_warn("invalid character syntax; use ?\\%c", c2);
  3614. }
  3615. }
  3616. ternary:
  3617. pushback(c, parser_state);
  3618. lex_state = EXPR_BEG;
  3619. ternary_colon = 1;
  3620. return '?';
  3621. }
  3622. else if (ismbchar(c)) {
  3623. rb_warn("multibyte character literal not supported yet; use ?\\%.3o", c);
  3624. goto ternary;
  3625. }
  3626. else if ((ISALNUM(c) || c == '_') && lex_p < lex_pend && is_identchar(*(lex_p))) {
  3627. goto ternary;
  3628. }
  3629. else if (c == '\\') {
  3630. c = read_escape(parser_state);
  3631. }
  3632. c &= 0xff;
  3633. lex_state = EXPR_END;
  3634. pslval->node = NEW_FIXNUM((intptr_t)c);
  3635. return tINTEGER;
  3636. case '&':
  3637. if ((c = nextc()) == '&') {
  3638. lex_state = EXPR_BEG;
  3639. if ((c = nextc()) == '=') {
  3640. pslval->id = tANDOP;
  3641. lex_state = EXPR_BEG;
  3642. return tOP_ASGN;
  3643. }
  3644. pushback(c, parser_state);
  3645. return tANDOP;
  3646. }
  3647. else if (c == '=') {
  3648. pslval->id = '&';
  3649. lex_state = EXPR_BEG;
  3650. return tOP_ASGN;
  3651. }
  3652. pushback(c, parser_state);
  3653. if (IS_ARG() && space_seen && !ISSPACE(c)){
  3654. rb_warning("`&' interpreted as argument prefix");
  3655. c = tAMPER;
  3656. }
  3657. else if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  3658. c = tAMPER;
  3659. }
  3660. else {
  3661. c = '&';
  3662. }
  3663. switch (lex_state) {
  3664. case EXPR_FNAME: case EXPR_DOT:
  3665. lex_state = EXPR_ARG; break;
  3666. default:
  3667. lex_state = EXPR_BEG;
  3668. }
  3669. return c;
  3670. case '|':
  3671. if ((c = nextc()) == '|') {
  3672. lex_state = EXPR_BEG;
  3673. if ((c = nextc()) == '=') {
  3674. pslval->id = tOROP;
  3675. lex_state = EXPR_BEG;
  3676. return tOP_ASGN;
  3677. }
  3678. pushback(c, parser_state);
  3679. return tOROP;
  3680. }
  3681. if (c == '=') {
  3682. pslval->id = '|';
  3683. lex_state = EXPR_BEG;
  3684. return tOP_ASGN;
  3685. }
  3686. if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
  3687. lex_state = EXPR_ARG;
  3688. }
  3689. else {
  3690. lex_state = EXPR_BEG;
  3691. }
  3692. pushback(c, parser_state);
  3693. return '|';
  3694. case '+':
  3695. c = nextc();
  3696. if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
  3697. lex_state = EXPR_ARG;
  3698. if (c == '@') {
  3699. return tUPLUS;
  3700. }
  3701. pushback(c, parser_state);
  3702. return '+';
  3703. }
  3704. if (c == '=') {
  3705. pslval->id = '+';
  3706. lex_state = EXPR_BEG;
  3707. return tOP_ASGN;
  3708. }
  3709. if (lex_state == EXPR_BEG || lex_state == EXPR_MID ||
  3710. (IS_ARG() && space_seen && !ISSPACE(c))) {
  3711. if (IS_ARG()) arg_ambiguous();
  3712. lex_state = EXPR_BEG;
  3713. pushback(c, parser_state);
  3714. if (ISDIGIT(c)) {
  3715. c = '+';
  3716. goto start_num;
  3717. }
  3718. return tUPLUS;
  3719. }
  3720. lex_state = EXPR_BEG;
  3721. pushback(c, parser_state);
  3722. return '+';
  3723. case '-':
  3724. c = nextc();
  3725. if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
  3726. lex_state = EXPR_ARG;
  3727. if (c == '@') {
  3728. return tUMINUS;
  3729. }
  3730. pushback(c, parser_state);
  3731. return '-';
  3732. }
  3733. if (c == '=') {
  3734. pslval->id = '-';
  3735. lex_state = EXPR_BEG;
  3736. return tOP_ASGN;
  3737. }
  3738. if (lex_state == EXPR_BEG || lex_state == EXPR_MID ||
  3739. (IS_ARG() && space_seen && !ISSPACE(c))) {
  3740. if (IS_ARG()) arg_ambiguous();
  3741. lex_state = EXPR_BEG;
  3742. pushback(c, parser_state);
  3743. if (ISDIGIT(c)) {
  3744. return tUMINUS_NUM;
  3745. }
  3746. return tUMINUS;
  3747. }
  3748. lex_state = EXPR_BEG;
  3749. pushback(c, parser_state);
  3750. return '-';
  3751. case '.':
  3752. lex_state = EXPR_BEG;
  3753. if ((c = nextc()) == '.') {
  3754. if ((c = nextc()) == '.') {
  3755. return tDOT3;
  3756. }
  3757. pushback(c, parser_state);
  3758. return tDOT2;
  3759. }
  3760. pushback(c, parser_state);
  3761. if (ISDIGIT(c)) {
  3762. yyerror("no .<digit> floating literal anymore; put 0 before dot");
  3763. }
  3764. lex_state = EXPR_DOT;
  3765. return '.';
  3766. start_num:
  3767. case '0': case '1': case '2': case '3': case '4':
  3768. case '5': case '6': case '7': case '8': case '9':
  3769. {
  3770. int is_float, seen_point, seen_e, nondigit;
  3771. is_float = seen_point = seen_e = nondigit = 0;
  3772. lex_state = EXPR_END;
  3773. newtok(parser_state);
  3774. if (c == '-' || c == '+') {
  3775. tokadd((char)c,parser_state);
  3776. c = nextc();
  3777. }
  3778. if (c == '0') {
  3779. int start = toklen();
  3780. c = nextc();
  3781. if (c == 'x' || c == 'X') {
  3782. /* hexadecimal */
  3783. c = nextc();
  3784. if (ISXDIGIT(c)) {
  3785. do {
  3786. if (c == '_') {
  3787. if (nondigit) break;
  3788. nondigit = c;
  3789. continue;
  3790. }
  3791. if (!ISXDIGIT(c)) break;
  3792. nondigit = 0;
  3793. tokadd((char)c,parser_state);
  3794. } while ((c = nextc()) != -1);
  3795. }
  3796. pushback(c, parser_state);
  3797. tokfix();
  3798. if (toklen() == start) {
  3799. yyerror("numeric literal without digits");
  3800. }
  3801. else if (nondigit) goto trailing_uc;
  3802. pslval->node = NEW_HEXNUM(string_new2(tok()));
  3803. return tINTEGER;
  3804. }
  3805. if (c == 'b' || c == 'B') {
  3806. /* binary */
  3807. c = nextc();
  3808. if (c == '0' || c == '1') {
  3809. do {
  3810. if (c == '_') {
  3811. if (nondigit) break;
  3812. nondigit = c;
  3813. continue;
  3814. }
  3815. if (c != '0' && c != '1') break;
  3816. nondigit = 0;
  3817. tokadd((char)c, parser_state);
  3818. } while ((c = nextc()) != -1);
  3819. }
  3820. pushback(c, parser_state);
  3821. tokfix();
  3822. if (toklen() == start) {
  3823. yyerror("numeric literal without digits");
  3824. }
  3825. else if (nondigit) goto trailing_uc;
  3826. pslval->node = NEW_BINNUM(string_new2(tok()));
  3827. return tINTEGER;
  3828. }
  3829. if (c == 'd' || c == 'D') {
  3830. /* decimal */
  3831. c = nextc();
  3832. if (ISDIGIT(c)) {
  3833. do {
  3834. if (c == '_') {
  3835. if (nondigit) break;
  3836. nondigit = c;
  3837. continue;
  3838. }
  3839. if (!ISDIGIT(c)) break;
  3840. nondigit = 0;
  3841. tokadd((char)c, parser_state);
  3842. } while ((c = nextc()) != -1);
  3843. }
  3844. pushback(c, parser_state);
  3845. tokfix();
  3846. if (toklen() == start) {
  3847. yyerror("numeric literal without digits");
  3848. }
  3849. else if (nondigit) goto trailing_uc;
  3850. pslval->node = NEW_NUMBER(string_new2(tok()));
  3851. return tINTEGER;
  3852. }
  3853. if (c == '_') {
  3854. /* 0_0 */
  3855. goto octal_number;
  3856. }
  3857. if (c == 'o' || c == 'O') {
  3858. /* prefixed octal */
  3859. c = nextc();
  3860. if (c == '_') {
  3861. yyerror("numeric literal without digits");
  3862. }
  3863. }
  3864. if (c >= '0' && c <= '7') {
  3865. /* octal */
  3866. octal_number:
  3867. do {
  3868. if (c == '_') {
  3869. if (nondigit) break;
  3870. nondigit = c;
  3871. continue;
  3872. }
  3873. if (c < '0' || c > '7') break;
  3874. nondigit = 0;
  3875. tokadd((char)c, parser_state);
  3876. } while ((c = nextc()) != -1);
  3877. if (toklen() > start) {
  3878. pushback(c, parser_state);
  3879. tokfix();
  3880. if (nondigit) goto trailing_uc;
  3881. pslval->node = NEW_OCTNUM(string_new2(tok()));
  3882. return tINTEGER;
  3883. }
  3884. if (nondigit) {
  3885. pushback(c, parser_state);
  3886. goto trailing_uc;
  3887. }
  3888. }
  3889. if (c > '7' && c <= '9') {
  3890. yyerror("Illegal octal digit");
  3891. }
  3892. else if (c == '.' || c == 'e' || c == 'E') {
  3893. tokadd('0', parser_state);
  3894. }
  3895. else {
  3896. pushback(c, parser_state);
  3897. pslval->node = NEW_FIXNUM(0);
  3898. return tINTEGER;
  3899. }
  3900. }
  3901. for (;;) {
  3902. switch (c) {
  3903. case '0': case '1': case '2': case '3': case '4':
  3904. case '5': case '6': case '7': case '8': case '9':
  3905. nondigit = 0;
  3906. tokadd((char)c, parser_state);
  3907. break;
  3908. case '.':
  3909. if (nondigit) goto trailing_uc;
  3910. if (seen_point || seen_e) {
  3911. goto decode_num;
  3912. }
  3913. else {
  3914. int c0 = nextc();
  3915. if (!ISDIGIT(c0)) {
  3916. pushback(c0, parser_state);
  3917. goto decode_num;
  3918. }
  3919. c = c0;
  3920. }
  3921. tokadd('.', parser_state);
  3922. tokadd((char)c, parser_state);
  3923. is_float++;
  3924. seen_point++;
  3925. nondigit = 0;
  3926. break;
  3927. case 'e':
  3928. case 'E':
  3929. if (nondigit) {
  3930. pushback(c, parser_state);
  3931. c = nondigit;
  3932. goto decode_num;
  3933. }
  3934. if (seen_e) {
  3935. goto decode_num;
  3936. }
  3937. tokadd((char)c, parser_state);
  3938. seen_e++;
  3939. is_float++;
  3940. nondigit = c;
  3941. c = nextc();
  3942. if (c != '-' && c != '+') continue;
  3943. tokadd((char)c, parser_state);
  3944. nondigit = c;
  3945. break;
  3946. case '_': /* `_' in number just ignored */
  3947. if (nondigit) goto decode_num;
  3948. nondigit = c;
  3949. break;
  3950. default:
  3951. goto decode_num;
  3952. }
  3953. c = nextc();
  3954. }
  3955. decode_num:
  3956. pushback(c, parser_state);
  3957. tokfix();
  3958. if (nondigit) {
  3959. char tmp[30];
  3960. trailing_uc:
  3961. snprintf(tmp, sizeof(tmp), "trailing `%c' in number", nondigit);
  3962. yyerror(tmp);
  3963. }
  3964. if (is_float) {
  3965. pslval->node = NEW_FLOAT(string_new2(tok()));
  3966. return tFLOAT;
  3967. }
  3968. pslval->node = NEW_NUMBER(string_new2(tok()));
  3969. return tINTEGER;
  3970. }
  3971. case ']':
  3972. case '}':
  3973. case ')':
  3974. COND_LEXPOP();
  3975. CMDARG_LEXPOP();
  3976. lex_state = EXPR_END;
  3977. return c;
  3978. case ':':
  3979. c = nextc();
  3980. if (c == ':') {
  3981. if (lex_state == EXPR_BEG || lex_state == EXPR_MID ||
  3982. lex_state == EXPR_CLASS || (IS_ARG() && space_seen)) {
  3983. lex_state = EXPR_BEG;
  3984. return tCOLON3;
  3985. }
  3986. lex_state = EXPR_DOT;
  3987. return tCOLON2;
  3988. }
  3989. if (lex_state == EXPR_END || lex_state == EXPR_ENDARG || ISSPACE(c)) {
  3990. pushback(c, parser_state);
  3991. lex_state = EXPR_BEG;
  3992. return ':';
  3993. }
  3994. switch (c) {
  3995. case '\'':
  3996. lex_strterm = NEW_STRTERM(str_ssym, (intptr_t)c, 0);
  3997. break;
  3998. case '"':
  3999. lex_strterm = NEW_STRTERM(str_dsym, (intptr_t)c, 0);
  4000. break;
  4001. default:
  4002. pushback(c, parser_state);
  4003. break;
  4004. }
  4005. lex_state = EXPR_FNAME;
  4006. return tSYMBEG;
  4007. case '/':
  4008. if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  4009. lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
  4010. return tREGEXP_BEG;
  4011. }
  4012. if ((c = nextc()) == '=') {
  4013. pslval->id = '/';
  4014. lex_state = EXPR_BEG;
  4015. return tOP_ASGN;
  4016. }
  4017. pushback(c, parser_state);
  4018. if (IS_ARG() && space_seen) {
  4019. if (!ISSPACE(c)) {
  4020. arg_ambiguous();
  4021. lex_strterm = NEW_STRTERM(str_regexp, '/', 0);
  4022. return tREGEXP_BEG;
  4023. }
  4024. }
  4025. switch (lex_state) {
  4026. case EXPR_FNAME: case EXPR_DOT:
  4027. lex_state = EXPR_ARG; break;
  4028. default:
  4029. lex_state = EXPR_BEG; break;
  4030. }
  4031. return '/';
  4032. case '^':
  4033. if ((c = nextc()) == '=') {
  4034. pslval->id = '^';
  4035. lex_state = EXPR_BEG;
  4036. return tOP_ASGN;
  4037. }
  4038. switch (lex_state) {
  4039. case EXPR_FNAME: case EXPR_DOT:
  4040. lex_state = EXPR_ARG; break;
  4041. default:
  4042. lex_state = EXPR_BEG; break;
  4043. }
  4044. pushback(c, parser_state);
  4045. return '^';
  4046. case ';':
  4047. command_start = TRUE;
  4048. case ',':
  4049. lex_state = EXPR_BEG;
  4050. return c;
  4051. case '~':
  4052. if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
  4053. if ((c = nextc()) != '@') {
  4054. pushback(c, parser_state);
  4055. }
  4056. }
  4057. switch (lex_state) {
  4058. case EXPR_FNAME: case EXPR_DOT:
  4059. lex_state = EXPR_ARG; break;
  4060. default:
  4061. lex_state = EXPR_BEG; break;
  4062. }
  4063. return '~';
  4064. case '(':
  4065. command_start = TRUE;
  4066. if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  4067. c = tLPAREN;
  4068. }
  4069. else if (space_seen) {
  4070. if (lex_state == EXPR_CMDARG) {
  4071. c = tLPAREN_ARG;
  4072. }
  4073. else if (lex_state == EXPR_ARG) {
  4074. rb_warn("don't put space before argument parentheses");
  4075. c = '(';
  4076. }
  4077. }
  4078. COND_PUSH(0);
  4079. CMDARG_PUSH(0);
  4080. lex_state = EXPR_BEG;
  4081. return c;
  4082. case '[':
  4083. if (lex_state == EXPR_FNAME || lex_state == EXPR_DOT) {
  4084. lex_state = EXPR_ARG;
  4085. if ((c = nextc()) == ']') {
  4086. if ((c = nextc()) == '=') {
  4087. return tASET;
  4088. }
  4089. pushback(c, parser_state);
  4090. return tAREF;
  4091. }
  4092. pushback(c, parser_state);
  4093. return '[';
  4094. }
  4095. else if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  4096. c = tLBRACK;
  4097. }
  4098. else if (IS_ARG() && space_seen) {
  4099. c = tLBRACK;
  4100. }
  4101. lex_state = EXPR_BEG;
  4102. COND_PUSH(0);
  4103. CMDARG_PUSH(0);
  4104. return c;
  4105. case '{':
  4106. if (IS_ARG() || lex_state == EXPR_END)
  4107. c = '{'; /* block (primary) */
  4108. else if (lex_state == EXPR_ENDARG)
  4109. c = tLBRACE_ARG; /* block (expr) */
  4110. else
  4111. c = tLBRACE; /* hash */
  4112. COND_PUSH(0);
  4113. CMDARG_PUSH(0);
  4114. lex_state = EXPR_BEG;
  4115. return c;
  4116. case '\\':
  4117. c = nextc();
  4118. if (c == '\n') {
  4119. space_seen = 1;
  4120. goto retry; /* skip \\n */
  4121. }
  4122. pushback(c, parser_state);
  4123. if(lex_state == EXPR_BEG
  4124. || lex_state == EXPR_MID || space_seen) {
  4125. lex_state = EXPR_DOT;
  4126. return tUBS;
  4127. }
  4128. lex_state = EXPR_DOT;
  4129. return '\\';
  4130. case '%':
  4131. if (lex_state == EXPR_BEG || lex_state == EXPR_MID) {
  4132. intptr_t term;
  4133. intptr_t paren;
  4134. char tmpstr[256];
  4135. char *cur;
  4136. c = nextc();
  4137. quotation:
  4138. if (!ISALNUM(c)) {
  4139. term = c;
  4140. c = 'Q';
  4141. }
  4142. else {
  4143. term = nextc();
  4144. if (ISALNUM(term) || ismbchar(term)) {
  4145. cur = tmpstr;
  4146. *cur++ = c;
  4147. while(ISALNUM(term) || ismbchar(term)) {
  4148. *cur++ = term;
  4149. term = nextc();
  4150. }
  4151. *cur = 0;
  4152. c = 1;
  4153. }
  4154. }
  4155. if (c == -1 || term == -1) {
  4156. rb_compile_error(parser_state, "unterminated quoted string meets end of file");
  4157. return 0;
  4158. }
  4159. paren = term;
  4160. if (term == '(') term = ')';
  4161. else if (term == '[') term = ']';
  4162. else if (term == '{') term = '}';
  4163. else if (term == '<') term = '>';
  4164. else paren = 0;
  4165. switch (c) {
  4166. case 'Q':
  4167. lex_strterm = NEW_STRTERM(str_dquote, term, paren);
  4168. return tSTRING_BEG;
  4169. case 'q':
  4170. lex_strterm = NEW_STRTERM(str_squote, term, paren);
  4171. return tSTRING_BEG;
  4172. case 'W':
  4173. lex_strterm = NEW_STRTERM(str_dquote | STR_FUNC_QWORDS, term, paren);
  4174. do {c = nextc();} while (ISSPACE(c));
  4175. pushback(c, parser_state);
  4176. return tWORDS_BEG;
  4177. case 'w':
  4178. lex_strterm = NEW_STRTERM(str_squote | STR_FUNC_QWORDS, term, paren);
  4179. do {c = nextc();} while (ISSPACE(c));
  4180. pushback(c, parser_state);
  4181. return tQWORDS_BEG;
  4182. case 'x':
  4183. lex_strterm = NEW_STRTERM(str_xquote, term, paren);
  4184. pslval->id = 0;
  4185. return tXSTRING_BEG;
  4186. case 'r':
  4187. lex_strterm = NEW_STRTERM(str_regexp, term, paren);
  4188. return tREGEXP_BEG;
  4189. case 's':
  4190. lex_strterm = NEW_STRTERM(str_ssym, term, paren);
  4191. lex_state = EXPR_FNAME;
  4192. return tSYMBEG;
  4193. case 1:
  4194. lex_strterm = NEW_STRTERM(str_xquote, term, paren);
  4195. pslval->id = rb_parser_sym(tmpstr);
  4196. return tXSTRING_BEG;
  4197. default:
  4198. lex_strterm = NEW_STRTERM(str_xquote, term, paren);
  4199. tmpstr[0] = c;
  4200. tmpstr[1] = 0;
  4201. pslval->id = rb_parser_sym(tmpstr);
  4202. return tXSTRING_BEG;
  4203. }
  4204. }
  4205. if ((c = nextc()) == '=') {
  4206. pslval->id = '%';
  4207. lex_state = EXPR_BEG;
  4208. return tOP_ASGN;
  4209. }
  4210. if (IS_ARG() && space_seen && !ISSPACE(c)) {
  4211. goto quotation;
  4212. }
  4213. switch (lex_state) {
  4214. case EXPR_FNAME: case EXPR_DOT:
  4215. lex_state = EXPR_ARG; break;
  4216. default:
  4217. lex_state = EXPR_BEG; break;
  4218. }
  4219. pushback(c, parser_state);
  4220. return '%';
  4221. case '$':
  4222. last_state = lex_state;
  4223. lex_state = EXPR_END;
  4224. newtok(parser_state);
  4225. c = nextc();
  4226. switch (c) {
  4227. case '_': /* $_: last read line string */
  4228. c = nextc();
  4229. if (is_identchar(c)) {
  4230. tokadd('$', parser_state);
  4231. tokadd('_', parser_state);
  4232. break;
  4233. }
  4234. pushback(c, parser_state);
  4235. c = '_';
  4236. /* fall through */
  4237. case '~': /* $~: match-data */
  4238. local_cnt(c);
  4239. /* fall through */
  4240. case '*': /* $*: argv */
  4241. case '$': /* $$: pid */
  4242. case '?': /* $?: last status */
  4243. case '!': /* $!: error string */
  4244. case '@': /* $@: error position */
  4245. case '/': /* $/: input record separator */
  4246. case '\\': /* $\: output record separator */
  4247. case ';': /* $;: field separator */
  4248. case ',': /* $,: output field separator */
  4249. case '.': /* $.: last read line number */
  4250. case '=': /* $=: ignorecase */
  4251. case ':': /* $:: load path */
  4252. case '<': /* $<: reading filename */
  4253. case '>': /* $>: default output handle */
  4254. case '\"': /* $": already loaded files */
  4255. tokadd('$', parser_state);
  4256. tokadd((char)c, parser_state);
  4257. tokfix();
  4258. pslval->id = rb_parser_sym(tok());
  4259. return tGVAR;
  4260. case '-':
  4261. tokadd('$', parser_state);
  4262. tokadd((char)c, parser_state);
  4263. c = nextc();
  4264. tokadd((char)c, parser_state);
  4265. gvar:
  4266. tokfix();
  4267. pslval->id = rb_parser_sym(tok());
  4268. /* xxx shouldn't check if valid option variable */
  4269. return tGVAR;
  4270. case '&': /* $&: last match */
  4271. case '`': /* $`: string before last match */
  4272. case '\'': /* $': string after last match */
  4273. case '+': /* $+: string matches last paren. */
  4274. if (last_state == EXPR_FNAME) {
  4275. tokadd((char)'$', parser_state);
  4276. tokadd(c, parser_state);
  4277. goto gvar;
  4278. }
  4279. pslval->node = NEW_BACK_REF((intptr_t)c);
  4280. return tBACK_REF;
  4281. case '1': case '2': case '3':
  4282. case '4': case '5': case '6':
  4283. case '7': case '8': case '9':
  4284. tokadd('$', parser_state);
  4285. do {
  4286. tokadd((char)c, parser_state);
  4287. c = nextc();
  4288. } while (ISDIGIT(c));
  4289. pushback(c, parser_state);
  4290. if (last_state == EXPR_FNAME) goto gvar;
  4291. tokfix();
  4292. pslval->node = NEW_NTH_REF((intptr_t)atoi(tok()+1));
  4293. return tNTH_REF;
  4294. default:
  4295. if (!is_identchar(c)) {
  4296. pushback(c, parser_state);
  4297. return '$';
  4298. }
  4299. case '0':
  4300. tokadd('$', parser_state);
  4301. }
  4302. break;
  4303. case '@':
  4304. c = nextc();
  4305. newtok(parser_state);
  4306. tokadd('@', parser_state);
  4307. if (c == '@') {
  4308. tokadd('@', parser_state);
  4309. c = nextc();
  4310. }
  4311. if (ISDIGIT(c)) {
  4312. if (tokidx == 1) {
  4313. rb_compile_error(parser_state,
  4314. "`@%c' is not allowed as an instance variable name", c);
  4315. }
  4316. else {
  4317. rb_compile_error(parser_state,
  4318. "`@@%c' is not allowed as a class variable name", c);
  4319. }
  4320. }
  4321. if (!is_identchar(c)) {
  4322. pushback(c, parser_state);
  4323. return '@';
  4324. }
  4325. break;
  4326. case '_':
  4327. if (was_bol() && whole_match_p("__END__", 7, 0, parser_state)) {
  4328. end_seen = 1;
  4329. return -1;
  4330. }
  4331. newtok(parser_state);
  4332. break;
  4333. default:
  4334. if (!is_identchar(c)) {
  4335. rb_compile_error(parser_state, "Invalid char `\\%03o' in expression", c);
  4336. goto retry;
  4337. }
  4338. newtok(parser_state);
  4339. break;
  4340. }
  4341. do {
  4342. tokadd((char)c, parser_state);
  4343. if (ismbchar(c)) {
  4344. int i, len = mbclen(c)-1;
  4345. for (i = 0; i < len; i++) {
  4346. c = nextc();
  4347. tokadd((char)c, parser_state);
  4348. }
  4349. }
  4350. c = nextc();
  4351. } while (is_identchar(c));
  4352. if ((c == '!' || c == '?') && is_identchar(tok()[0]) && !peek('=')) {
  4353. tokadd((char)c, parser_state);
  4354. }
  4355. else {
  4356. pushback(c, parser_state);
  4357. }
  4358. tokfix();
  4359. {
  4360. int result = 0;
  4361. last_state = lex_state;
  4362. switch (tok()[0]) {
  4363. case '$':
  4364. lex_state = EXPR_END;
  4365. result = tGVAR;
  4366. break;
  4367. case '@':
  4368. lex_state = EXPR_END;
  4369. if (tok()[1] == '@')
  4370. result = tCVAR;
  4371. else
  4372. result = tIVAR;
  4373. break;
  4374. default:
  4375. if (toklast() == '!' || toklast() == '?') {
  4376. result = tFID;
  4377. }
  4378. else {
  4379. if (lex_state == EXPR_FNAME) {
  4380. if ((c = nextc()) == '=' && !peek('~') && !peek('>') &&
  4381. (!peek('=') || (lex_p + 1 < lex_pend && (lex_p)[1] == '>'))) {
  4382. result = tIDENTIFIER;
  4383. tokadd((char)c, parser_state);
  4384. tokfix();
  4385. }
  4386. else {
  4387. pushback(c, parser_state);
  4388. }
  4389. }
  4390. if (result == 0 && ISUPPER(tok()[0])) {
  4391. result = tCONSTANT;
  4392. }
  4393. else {
  4394. result = tIDENTIFIER;
  4395. }
  4396. }
  4397. if (lex_state != EXPR_DOT) {
  4398. const struct kwtable *kw;
  4399. /* See if it is a reserved word. */
  4400. kw = mel_reserved_word(tok(), toklen());
  4401. if (kw) {
  4402. enum lex_state_e state = lex_state;
  4403. lex_state = kw->state;
  4404. if (state == EXPR_FNAME) {
  4405. pslval->id = rb_parser_sym(kw->name);
  4406. // Hack. Ignore the different variants of do
  4407. // if we're just trying to match a FNAME
  4408. if(kw->id[0] == kDO) return kDO;
  4409. }
  4410. if (kw->id[0] == kDO) {
  4411. command_start = TRUE;
  4412. if (COND_P()) return kDO_COND;
  4413. if (CMDARG_P() && state != EXPR_CMDARG)
  4414. return kDO_BLOCK;
  4415. if (state == EXPR_ENDARG)
  4416. return kDO_BLOCK;
  4417. return kDO;
  4418. }
  4419. if (state == EXPR_BEG)
  4420. return kw->id[0];
  4421. else {
  4422. if (kw->id[0] != kw->id[1])
  4423. lex_state = EXPR_BEG;
  4424. return kw->id[1];
  4425. }
  4426. }
  4427. }
  4428. if (lex_state == EXPR_BEG ||
  4429. lex_state == EXPR_MID ||
  4430. lex_state == EXPR_DOT ||
  4431. lex_state == EXPR_ARG ||
  4432. lex_state == EXPR_CMDARG) {
  4433. if (cmd_state) {
  4434. lex_state = EXPR_CMDARG;
  4435. }
  4436. else {
  4437. lex_state = EXPR_ARG;
  4438. }
  4439. }
  4440. else {
  4441. lex_state = EXPR_END;
  4442. }
  4443. }
  4444. pslval->id = rb_parser_sym(tok());
  4445. if(is_local_id(pslval->id) &&
  4446. last_state != EXPR_DOT &&
  4447. local_id(pslval->id)) {
  4448. lex_state = EXPR_END;
  4449. }
  4450. /* if (is_local_id(pslval->id) && local_id(pslval->id)) { */
  4451. /* lex_state = EXPR_END; */
  4452. /* } */
  4453. return result;
  4454. }
  4455. }
  4456. NODE*
  4457. parser_node_newnode(rb_parser_state *parser_state, enum node_type type,
  4458. VALUE a0, VALUE a1, VALUE a2)
  4459. {
  4460. NODE *n = (NODE*)pt_allocate(parser_state, sizeof(NODE));
  4461. n->flags = 0;
  4462. nd_set_type(n, type);
  4463. nd_set_line(n, ruby_sourceline);
  4464. n->nd_file = ruby_sourcefile;
  4465. n->u1.value = a0;
  4466. n->u2.value = a1;
  4467. n->u3.value = a2;
  4468. return n;
  4469. }
  4470. static NODE*
  4471. newline_node(rb_parser_state *parser_state, NODE *node)
  4472. {
  4473. NODE *nl = 0;
  4474. if (node) {
  4475. if (nd_type(node) == NODE_NEWLINE) return node;
  4476. nl = NEW_NEWLINE(node);
  4477. fixpos(nl, node);
  4478. nl->nd_nth = nd_line(node);
  4479. }
  4480. return nl;
  4481. }
  4482. static void
  4483. fixpos(NODE *node, NODE *orig)
  4484. {
  4485. if (!node) return;
  4486. if (!orig) return;
  4487. if (orig == (NODE*)1) return;
  4488. node->nd_file = orig->nd_file;
  4489. nd_set_line(node, nd_line(orig));
  4490. }
  4491. static void
  4492. parser_warning(rb_parser_state *parser_state, NODE *node, const char *mesg)
  4493. {
  4494. int line = ruby_sourceline;
  4495. if(emit_warnings) {
  4496. ruby_sourceline = nd_line(node);
  4497. printf("%s:%i: warning: %s\n", ruby_sourcefile, ruby_sourceline, mesg);
  4498. ruby_sourceline = line;
  4499. }
  4500. }
  4501. static NODE*
  4502. block_append(rb_parser_state *parser_state, NODE *head, NODE *tail)
  4503. {
  4504. NODE *end, *h = head;
  4505. if (tail == 0) return head;
  4506. again:
  4507. if (h == 0) return tail;
  4508. switch (nd_type(h)) {
  4509. case NODE_NEWLINE:
  4510. h = h->nd_next;
  4511. goto again;
  4512. case NODE_STR:
  4513. case NODE_LIT:
  4514. parser_warning(parser_state, h, "unused literal ignored");
  4515. return tail;
  4516. default:
  4517. h = end = NEW_BLOCK(head);
  4518. end->nd_end = end;
  4519. fixpos(end, head);
  4520. head = end;
  4521. break;
  4522. case NODE_BLOCK:
  4523. end = h->nd_end;
  4524. break;
  4525. }
  4526. if (verbose) {
  4527. NODE *nd = end->nd_head;
  4528. newline:
  4529. switch (nd_type(nd)) {
  4530. case NODE_RETURN:
  4531. case NODE_BREAK:
  4532. case NODE_NEXT:
  4533. case NODE_REDO:
  4534. case NODE_RETRY:
  4535. parser_warning(parser_state, nd, "statement not reached");
  4536. break;
  4537. case NODE_NEWLINE:
  4538. nd = nd->nd_next;
  4539. goto newline;
  4540. default:
  4541. break;
  4542. }
  4543. }
  4544. if (nd_type(tail) != NODE_BLOCK) {
  4545. tail = NEW_BLOCK(tail);
  4546. tail->nd_end = tail;
  4547. }
  4548. end->nd_next = tail;
  4549. h->nd_end = tail->nd_end;
  4550. return head;
  4551. }
  4552. /* append item to the list */
  4553. static NODE*
  4554. list_append(rb_parser_state *parser_state, NODE *list, NODE *item)
  4555. {
  4556. NODE *last;
  4557. if (list == 0) return NEW_LIST(item);
  4558. if (list->nd_next) {
  4559. last = list->nd_next->nd_end;
  4560. }
  4561. else {
  4562. last = list;
  4563. }
  4564. list->nd_alen += 1;
  4565. last->nd_next = NEW_LIST(item);
  4566. list->nd_next->nd_end = last->nd_next;
  4567. return list;
  4568. }
  4569. /* concat two lists */
  4570. static NODE*
  4571. list_concat(NODE *head, NODE *tail)
  4572. {
  4573. NODE *last;
  4574. if (head->nd_next) {
  4575. last = head->nd_next->nd_end;
  4576. }
  4577. else {
  4578. last = head;
  4579. }
  4580. head->nd_alen += tail->nd_alen;
  4581. last->nd_next = tail;
  4582. if (tail->nd_next) {
  4583. head->nd_next->nd_end = tail->nd_next->nd_end;
  4584. }
  4585. else {
  4586. head->nd_next->nd_end = tail;
  4587. }
  4588. return head;
  4589. }
  4590. /* concat two string literals */
  4591. static NODE *
  4592. literal_concat(rb_parser_state *parser_state, NODE *head, NODE *tail)
  4593. {
  4594. enum node_type htype;
  4595. if (!head) return tail;
  4596. if (!tail) return head;
  4597. htype = (enum node_type)nd_type(head);
  4598. if (htype == NODE_EVSTR) {
  4599. NODE *node = NEW_DSTR(string_new(0, 0));
  4600. head = list_append(parser_state, node, head);
  4601. }
  4602. switch (nd_type(tail)) {
  4603. case NODE_STR:
  4604. if (htype == NODE_STR) {
  4605. if(head->nd_str) {
  4606. bconcat(head->nd_str, tail->nd_str);
  4607. bdestroy(tail->nd_str);
  4608. } else {
  4609. head = tail;
  4610. }
  4611. }
  4612. else {
  4613. list_append(parser_state, head, tail);
  4614. }
  4615. break;
  4616. case NODE_DSTR:
  4617. if (htype == NODE_STR) {
  4618. bconcat(head->nd_str, tail->nd_str);
  4619. bdestroy(tail->nd_str);
  4620. tail->nd_lit = head->nd_lit;
  4621. head = tail;
  4622. }
  4623. else {
  4624. nd_set_type(tail, NODE_ARRAY);
  4625. tail->nd_head = NEW_STR(tail->nd_lit);
  4626. list_concat(head, tail);
  4627. }
  4628. break;
  4629. case NODE_EVSTR:
  4630. if (htype == NODE_STR) {
  4631. nd_set_type(head, NODE_DSTR);
  4632. head->nd_alen = 1;
  4633. }
  4634. list_append(parser_state, head, tail);
  4635. break;
  4636. }
  4637. return head;
  4638. }
  4639. static NODE *
  4640. evstr2dstr(rb_parser_state *parser_state, NODE *node)
  4641. {
  4642. if (nd_type(node) == NODE_EVSTR) {
  4643. node = list_append(parser_state, NEW_DSTR(string_new(0, 0)), node);
  4644. }
  4645. return node;
  4646. }
  4647. static NODE *
  4648. new_evstr(rb_parser_state *parser_state, NODE *node)
  4649. {
  4650. NODE *head = node;
  4651. again:
  4652. if (node) {
  4653. switch (nd_type(node)) {
  4654. case NODE_STR: case NODE_DSTR: case NODE_EVSTR:
  4655. return node;
  4656. case NODE_NEWLINE:
  4657. node = node->nd_next;
  4658. goto again;
  4659. }
  4660. }
  4661. return NEW_EVSTR(head);
  4662. }
  4663. static const struct {
  4664. QUID token;
  4665. const char name[12];
  4666. } op_tbl[] = {
  4667. {tDOT2, ".."},
  4668. {tDOT3, "..."},
  4669. {'+', "+"},
  4670. {'-', "-"},
  4671. {'+', "+(binary)"},
  4672. {'-', "-(binary)"},
  4673. {'*', "*"},
  4674. {'/', "/"},
  4675. {'%', "%"},
  4676. {tPOW, "**"},
  4677. {tUPLUS, "+@"},
  4678. {tUMINUS, "-@"},
  4679. {tUPLUS, "+(unary)"},
  4680. {tUMINUS, "-(unary)"},
  4681. {'|', "|"},
  4682. {'^', "^"},
  4683. {'&', "&"},
  4684. {tCMP, "<=>"},
  4685. {'>', ">"},
  4686. {tGEQ, ">="},
  4687. {'<', "<"},
  4688. {tLEQ, "<="},
  4689. {tEQ, "=="},
  4690. {tEQQ, "==="},
  4691. {tNEQ, "!="},
  4692. {tMATCH, "=~"},
  4693. {tNMATCH, "!~"},
  4694. {'!', "!"},
  4695. {'~', "~"},
  4696. {'!', "!(unary)"},
  4697. {'~', "~(unary)"},
  4698. {'!', "!@"},
  4699. {'~', "~@"},
  4700. {tAREF, "[]"},
  4701. {tASET, "[]="},
  4702. {tLSHFT, "<<"},
  4703. {tRSHFT, ">>"},
  4704. {tCOLON2, "::"},
  4705. {'`', "`"},
  4706. {0, ""}
  4707. };
  4708. static QUID convert_op(QUID id) {
  4709. int i;
  4710. for(i = 0; op_tbl[i].token; i++) {
  4711. if(op_tbl[i].token == id) {
  4712. return rb_parser_sym(op_tbl[i].name);
  4713. }
  4714. }
  4715. return id;
  4716. }
  4717. static NODE *
  4718. call_op(NODE *recv, QUID id, int narg, NODE *arg1, rb_parser_state *parser_state)
  4719. {
  4720. value_expr(recv);
  4721. if (narg == 1) {
  4722. value_expr(arg1);
  4723. arg1 = NEW_LIST(arg1);
  4724. }
  4725. else {
  4726. arg1 = 0;
  4727. }
  4728. id = convert_op(id);
  4729. NODE* n = NEW_CALL(recv, id, arg1);
  4730. fixpos(n, recv);
  4731. return n;
  4732. }
  4733. static NODE*
  4734. match_gen(NODE *node1, NODE *node2, rb_parser_state *parser_state)
  4735. {
  4736. local_cnt('~');
  4737. value_expr(node1);
  4738. value_expr(node2);
  4739. if (node1) {
  4740. switch (nd_type(node1)) {
  4741. case NODE_DREGX:
  4742. case NODE_DREGX_ONCE:
  4743. return NEW_MATCH2(node1, node2);
  4744. case NODE_REGEX:
  4745. return NEW_MATCH2(node1, node2);
  4746. }
  4747. }
  4748. if (node2) {
  4749. switch (nd_type(node2)) {
  4750. case NODE_DREGX:
  4751. case NODE_DREGX_ONCE:
  4752. return NEW_MATCH3(node2, node1);
  4753. case NODE_REGEX:
  4754. return NEW_MATCH3(node2, node1);
  4755. }
  4756. }
  4757. return NEW_CALL(node1, convert_op(tMATCH), NEW_LIST(node2));
  4758. }
  4759. static NODE*
  4760. mel_gettable(rb_parser_state *parser_state, QUID id)
  4761. {
  4762. if (id == kSELF) {
  4763. return NEW_SELF();
  4764. }
  4765. else if (id == kNIL) {
  4766. return NEW_NIL();
  4767. }
  4768. else if (id == kTRUE) {
  4769. return NEW_TRUE();
  4770. }
  4771. else if (id == kFALSE) {
  4772. return NEW_FALSE();
  4773. }
  4774. else if (id == k__FILE__) {
  4775. return NEW_FILE();
  4776. }
  4777. else if (id == k__LINE__) {
  4778. return NEW_FIXNUM(ruby_sourceline);
  4779. }
  4780. else if (is_local_id(id)) {
  4781. if (local_id(id)) return NEW_LVAR(id);
  4782. /* method call without arguments */
  4783. return NEW_VCALL(id);
  4784. }
  4785. else if (is_global_id(id)) {
  4786. return NEW_GVAR(id);
  4787. }
  4788. else if (is_instance_id(id)) {
  4789. return NEW_IVAR(id);
  4790. }
  4791. else if (is_const_id(id)) {
  4792. return NEW_CONST(id);
  4793. }
  4794. else if (is_class_id(id)) {
  4795. return NEW_CVAR(id);
  4796. }
  4797. /* FIXME: indicate which identifier. */
  4798. rb_compile_error(parser_state, "identifier is not valid 1\n");
  4799. return 0;
  4800. }
  4801. static void
  4802. reset_block(rb_parser_state *parser_state) {
  4803. if(!variables->block_vars) {
  4804. variables->block_vars = var_table_create();
  4805. } else {
  4806. variables->block_vars = var_table_push(variables->block_vars);
  4807. }
  4808. }
  4809. static NODE *
  4810. extract_block_vars(rb_parser_state *parser_state, NODE* node, var_table vars)
  4811. {
  4812. int i;
  4813. NODE *var, *out = node;
  4814. // we don't create any DASGN_CURR nodes
  4815. goto out;
  4816. if (!node) goto out;
  4817. if(var_table_size(vars) == 0) goto out;
  4818. var = NULL;
  4819. for(i = 0; i < var_table_size(vars); i++) {
  4820. var = NEW_DASGN_CURR(var_table_get(vars, i), var);
  4821. }
  4822. out = block_append(parser_state, var, node);
  4823. out:
  4824. variables->block_vars = var_table_pop(variables->block_vars);
  4825. return out;
  4826. }
  4827. static NODE*
  4828. assignable(QUID id, NODE *val, rb_parser_state *parser_state)
  4829. {
  4830. value_expr(val);
  4831. if (id == kSELF) {
  4832. yyerror("Can't change the value of self");
  4833. }
  4834. else if (id == kNIL) {
  4835. yyerror("Can't assign to nil");
  4836. }
  4837. else if (id == kTRUE) {
  4838. yyerror("Can't assign to true");
  4839. }
  4840. else if (id == kFALSE) {
  4841. yyerror("Can't assign to false");
  4842. }
  4843. else if (id == k__FILE__) {
  4844. yyerror("Can't assign to __FILE__");
  4845. }
  4846. else if (id == k__LINE__) {
  4847. yyerror("Can't assign to __LINE__");
  4848. }
  4849. else if (is_local_id(id)) {
  4850. if(variables->block_vars) {
  4851. var_table_add(variables->block_vars, id);
  4852. }
  4853. return NEW_LASGN(id, val);
  4854. }
  4855. else if (is_global_id(id)) {
  4856. return NEW_GASGN(id, val);
  4857. }
  4858. else if (is_instance_id(id)) {
  4859. return NEW_IASGN(id, val);
  4860. }
  4861. else if (is_const_id(id)) {
  4862. if (in_def || in_single)
  4863. yyerror("dynamic constant assignment");
  4864. return NEW_CDECL(id, val, 0);
  4865. }
  4866. else if (is_class_id(id)) {
  4867. if (in_def || in_single) return NEW_CVASGN(id, val);
  4868. return NEW_CVDECL(id, val);
  4869. }
  4870. else {
  4871. /* FIXME: indicate which identifier. */
  4872. rb_compile_error(parser_state, "identifier is not valid 2 (%d)\n", id);
  4873. }
  4874. return 0;
  4875. }
  4876. static NODE *
  4877. aryset(NODE *recv, NODE *idx, rb_parser_state *parser_state)
  4878. {
  4879. if (recv && nd_type(recv) == NODE_SELF)
  4880. recv = (NODE *)1;
  4881. else
  4882. value_expr(recv);
  4883. return NEW_ATTRASGN(recv, convert_op(tASET), idx);
  4884. }
  4885. static QUID
  4886. rb_id_attrset(QUID id)
  4887. {
  4888. id &= ~ID_SCOPE_MASK;
  4889. id |= ID_ATTRSET;
  4890. return id;
  4891. }
  4892. static NODE *
  4893. attrset(NODE *recv, QUID id, rb_parser_state *parser_state)
  4894. {
  4895. if (recv && nd_type(recv) == NODE_SELF)
  4896. recv = (NODE *)1;
  4897. else
  4898. value_expr(recv);
  4899. return NEW_ATTRASGN(recv, rb_id_attrset(id), 0);
  4900. }
  4901. static void
  4902. rb_backref_error(NODE *node, rb_parser_state *parser_state)
  4903. {
  4904. switch (nd_type(node)) {
  4905. case NODE_NTH_REF:
  4906. rb_compile_error(parser_state, "Can't set variable $%u", node->nd_nth);
  4907. break;
  4908. case NODE_BACK_REF:
  4909. rb_compile_error(parser_state, "Can't set variable $%c", (int)node->nd_nth);
  4910. break;
  4911. }
  4912. }
  4913. static NODE *
  4914. arg_concat(rb_parser_state *parser_state, NODE *node1, NODE *node2)
  4915. {
  4916. if (!node2) return node1;
  4917. return NEW_ARGSCAT(node1, node2);
  4918. }
  4919. static NODE *
  4920. arg_add(rb_parser_state *parser_state, NODE *node1, NODE *node2)
  4921. {
  4922. if (!node1) return NEW_LIST(node2);
  4923. if (nd_type(node1) == NODE_ARRAY) {
  4924. return list_append(parser_state, node1, node2);
  4925. }
  4926. else {
  4927. return NEW_ARGSPUSH(node1, node2);
  4928. }
  4929. }
  4930. static NODE*
  4931. node_assign(NODE *lhs, NODE *rhs, rb_parser_state *parser_state)
  4932. {
  4933. if (!lhs) return 0;
  4934. value_expr(rhs);
  4935. switch (nd_type(lhs)) {
  4936. case NODE_GASGN:
  4937. case NODE_IASGN:
  4938. case NODE_LASGN:
  4939. case NODE_DASGN:
  4940. case NODE_DASGN_CURR:
  4941. case NODE_MASGN:
  4942. case NODE_CDECL:
  4943. case NODE_CVDECL:
  4944. case NODE_CVASGN:
  4945. lhs->nd_value = rhs;
  4946. break;
  4947. case NODE_ATTRASGN:
  4948. case NODE_CALL:
  4949. lhs->nd_args = arg_add(parser_state, lhs->nd_args, rhs);
  4950. break;
  4951. default:
  4952. /* should not happen */
  4953. break;
  4954. }
  4955. return lhs;
  4956. }
  4957. static int
  4958. value_expr0(NODE *node, rb_parser_state *parser_state)
  4959. {
  4960. int cond = 0;
  4961. while (node) {
  4962. switch (nd_type(node)) {
  4963. case NODE_DEFN:
  4964. case NODE_DEFS:
  4965. parser_warning(parser_state, node, "void value expression");
  4966. return FALSE;
  4967. case NODE_RETURN:
  4968. case NODE_BREAK:
  4969. case NODE_NEXT:
  4970. case NODE_REDO:
  4971. case NODE_RETRY:
  4972. if (!cond) yyerror("void value expression");
  4973. /* or "control never reach"? */
  4974. return FALSE;
  4975. case NODE_BLOCK:
  4976. while (node->nd_next) {
  4977. node = node->nd_next;
  4978. }
  4979. node = node->nd_head;
  4980. break;
  4981. case NODE_BEGIN:
  4982. node = node->nd_body;
  4983. break;
  4984. case NODE_IF:
  4985. if (!value_expr(node->nd_body)) return FALSE;
  4986. node = node->nd_else;
  4987. break;
  4988. case NODE_AND:
  4989. case NODE_OR:
  4990. cond = 1;
  4991. node = node->nd_2nd;
  4992. break;
  4993. case NODE_NEWLINE:
  4994. node = node->nd_next;
  4995. break;
  4996. default:
  4997. return TRUE;
  4998. }
  4999. }
  5000. return TRUE;
  5001. }
  5002. static void
  5003. void_expr0(NODE *node, rb_parser_state *parser_state)
  5004. {
  5005. const char *useless = NULL;
  5006. if (!verbose) return;
  5007. again:
  5008. if (!node) return;
  5009. switch (nd_type(node)) {
  5010. case NODE_NEWLINE:
  5011. node = node->nd_next;
  5012. goto again;
  5013. case NODE_CALL:
  5014. switch (node->nd_mid) {
  5015. case '+':
  5016. case '-':
  5017. case '*':
  5018. case '/':
  5019. case '%':
  5020. case tPOW:
  5021. case tUPLUS:
  5022. case tUMINUS:
  5023. case '|':
  5024. case '^':
  5025. case '&':
  5026. case tCMP:
  5027. case '>':
  5028. case tGEQ:
  5029. case '<':
  5030. case tLEQ:
  5031. case tEQ:
  5032. case tNEQ:
  5033. useless = "";
  5034. break;
  5035. }
  5036. break;
  5037. case NODE_LVAR:
  5038. case NODE_DVAR:
  5039. case NODE_GVAR:
  5040. case NODE_IVAR:
  5041. case NODE_CVAR:
  5042. case NODE_NTH_REF:
  5043. case NODE_BACK_REF:
  5044. useless = "a variable";
  5045. break;
  5046. case NODE_CONST:
  5047. case NODE_CREF:
  5048. useless = "a constant";
  5049. break;
  5050. case NODE_LIT:
  5051. case NODE_STR:
  5052. case NODE_DSTR:
  5053. case NODE_DREGX:
  5054. case NODE_DREGX_ONCE:
  5055. useless = "a literal";
  5056. break;
  5057. case NODE_COLON2:
  5058. case NODE_COLON3:
  5059. useless = "::";
  5060. break;
  5061. case NODE_DOT2:
  5062. useless = "..";
  5063. break;
  5064. case NODE_DOT3:
  5065. useless = "...";
  5066. break;
  5067. case NODE_SELF:
  5068. useless = "self";
  5069. break;
  5070. case NODE_NIL:
  5071. useless = "nil";
  5072. break;
  5073. case NODE_TRUE:
  5074. useless = "true";
  5075. break;
  5076. case NODE_FALSE:
  5077. useless = "false";
  5078. break;
  5079. case NODE_DEFINED:
  5080. useless = "defined?";
  5081. break;
  5082. }
  5083. if (useless) {
  5084. int line = ruby_sourceline;
  5085. ruby_sourceline = nd_line(node);
  5086. rb_warn("useless use of %s in void context", useless);
  5087. ruby_sourceline = line;
  5088. }
  5089. }
  5090. static void
  5091. void_stmts(NODE *node, rb_parser_state *parser_state)
  5092. {
  5093. if (!verbose) return;
  5094. if (!node) return;
  5095. if (nd_type(node) != NODE_BLOCK) return;
  5096. for (;;) {
  5097. if (!node->nd_next) return;
  5098. void_expr(node->nd_head);
  5099. node = node->nd_next;
  5100. }
  5101. }
  5102. static NODE *
  5103. remove_begin(NODE *node, rb_parser_state *parser_state)
  5104. {
  5105. NODE **n = &node;
  5106. while (*n) {
  5107. switch (nd_type(*n)) {
  5108. case NODE_NEWLINE:
  5109. n = &(*n)->nd_next;
  5110. continue;
  5111. case NODE_BEGIN:
  5112. *n = (*n)->nd_body;
  5113. default:
  5114. return node;
  5115. }
  5116. }
  5117. return node;
  5118. }
  5119. static int
  5120. assign_in_cond(NODE *node, rb_parser_state *parser_state)
  5121. {
  5122. switch (nd_type(node)) {
  5123. case NODE_MASGN:
  5124. yyerror("multiple assignment in conditional");
  5125. return 1;
  5126. case NODE_LASGN:
  5127. case NODE_DASGN:
  5128. case NODE_GASGN:
  5129. case NODE_IASGN:
  5130. break;
  5131. case NODE_NEWLINE:
  5132. default:
  5133. return 0;
  5134. }
  5135. switch (nd_type(node->nd_value)) {
  5136. case NODE_LIT:
  5137. case NODE_STR:
  5138. case NODE_NIL:
  5139. case NODE_TRUE:
  5140. case NODE_FALSE:
  5141. return 1;
  5142. case NODE_DSTR:
  5143. case NODE_XSTR:
  5144. case NODE_DXSTR:
  5145. case NODE_EVSTR:
  5146. case NODE_DREGX:
  5147. default:
  5148. break;
  5149. }
  5150. return 1;
  5151. }
  5152. static int
  5153. parser_e_option_supplied(rb_parser_state* parser_state)
  5154. {
  5155. if (strcmp(ruby_sourcefile, "-e") == 0)
  5156. return TRUE;
  5157. return FALSE;
  5158. }
  5159. #define e_option_supplied() parser_e_option_supplied(parser_state)
  5160. static void
  5161. warn_unless_e_option(rb_parser_state *parser_state, NODE *node, const char *str)
  5162. {
  5163. if (!e_option_supplied()) parser_warning(parser_state, node, str);
  5164. }
  5165. static NODE *cond0(NODE *node, rb_parser_state *parser_state);
  5166. static NODE*
  5167. range_op(NODE *node, rb_parser_state *parser_state)
  5168. {
  5169. enum node_type type;
  5170. if (!e_option_supplied()) return node;
  5171. if (node == 0) return 0;
  5172. value_expr(node);
  5173. node = cond0(node, parser_state);
  5174. type = (enum node_type)nd_type(node);
  5175. if (type == NODE_NEWLINE) {
  5176. node = node->nd_next;
  5177. type = (enum node_type)nd_type(node);
  5178. }
  5179. if (type == NODE_LIT && FIXNUM_P(node->nd_lit)) {
  5180. warn_unless_e_option(parser_state, node, "integer literal in conditional range");
  5181. return call_op(node,tEQ,1,NEW_GVAR(rb_parser_sym("$.")), parser_state);
  5182. }
  5183. return node;
  5184. }
  5185. static int
  5186. literal_node(NODE *node)
  5187. {
  5188. if (!node) return 1; /* same as NODE_NIL */
  5189. switch (nd_type(node)) {
  5190. case NODE_LIT:
  5191. case NODE_STR:
  5192. case NODE_DSTR:
  5193. case NODE_EVSTR:
  5194. case NODE_DREGX:
  5195. case NODE_DREGX_ONCE:
  5196. case NODE_DSYM:
  5197. return 2;
  5198. case NODE_TRUE:
  5199. case NODE_FALSE:
  5200. case NODE_NIL:
  5201. return 1;
  5202. }
  5203. return 0;
  5204. }
  5205. static NODE*
  5206. cond0(NODE *node, rb_parser_state *parser_state)
  5207. {
  5208. if (node == 0) return 0;
  5209. assign_in_cond(node, parser_state);
  5210. switch (nd_type(node)) {
  5211. case NODE_DSTR:
  5212. case NODE_EVSTR:
  5213. case NODE_STR:
  5214. break;
  5215. case NODE_DREGX:
  5216. case NODE_DREGX_ONCE:
  5217. local_cnt('_');
  5218. local_cnt('~');
  5219. return NEW_MATCH2(node, NEW_GVAR(rb_parser_sym("$_")));
  5220. case NODE_AND:
  5221. case NODE_OR:
  5222. node->nd_1st = cond0(node->nd_1st, parser_state);
  5223. node->nd_2nd = cond0(node->nd_2nd, parser_state);
  5224. break;
  5225. case NODE_DOT2:
  5226. case NODE_DOT3:
  5227. node->nd_beg = range_op(node->nd_beg, parser_state);
  5228. node->nd_end = range_op(node->nd_end, parser_state);
  5229. if (nd_type(node) == NODE_DOT2) nd_set_type(node,NODE_FLIP2);
  5230. else if (nd_type(node) == NODE_DOT3) nd_set_type(node, NODE_FLIP3);
  5231. if (!e_option_supplied()) {
  5232. int b = literal_node(node->nd_beg);
  5233. int e = literal_node(node->nd_end);
  5234. if ((b == 1 && e == 1) || (b + e >= 2 && verbose)) {
  5235. }
  5236. }
  5237. break;
  5238. case NODE_DSYM:
  5239. break;
  5240. case NODE_REGEX:
  5241. nd_set_type(node, NODE_MATCH);
  5242. local_cnt('_');
  5243. local_cnt('~');
  5244. default:
  5245. break;
  5246. }
  5247. return node;
  5248. }
  5249. static NODE*
  5250. cond(NODE *node, rb_parser_state *parser_state)
  5251. {
  5252. if (node == 0) return 0;
  5253. value_expr(node);
  5254. if (nd_type(node) == NODE_NEWLINE){
  5255. node->nd_next = cond0(node->nd_next, parser_state);
  5256. return node;
  5257. }
  5258. return cond0(node, parser_state);
  5259. }
  5260. static NODE*
  5261. logop(enum node_type type, NODE *left, NODE *right, rb_parser_state *parser_state)
  5262. {
  5263. value_expr(left);
  5264. if (left && nd_type(left) == type) {
  5265. NODE *node = left, *second;
  5266. while ((second = node->nd_2nd) != 0 && nd_type(second) == type) {
  5267. node = second;
  5268. }
  5269. node->nd_2nd = NEW_NODE(type, second, right, 0);
  5270. return left;
  5271. }
  5272. return NEW_NODE(type, left, right, 0);
  5273. }
  5274. static int
  5275. cond_negative(NODE **nodep)
  5276. {
  5277. NODE *c = *nodep;
  5278. if (!c) return 0;
  5279. switch (nd_type(c)) {
  5280. case NODE_NOT:
  5281. *nodep = c->nd_body;
  5282. return 1;
  5283. case NODE_NEWLINE:
  5284. if (c->nd_next && nd_type(c->nd_next) == NODE_NOT) {
  5285. c->nd_next = c->nd_next->nd_body;
  5286. return 1;
  5287. }
  5288. }
  5289. return 0;
  5290. }
  5291. static void
  5292. no_blockarg(rb_parser_state *parser_state, NODE *node)
  5293. {
  5294. if (node && nd_type(node) == NODE_BLOCK_PASS) {
  5295. rb_compile_error(parser_state, "block argument should not be given");
  5296. }
  5297. }
  5298. static NODE *
  5299. ret_args(rb_parser_state *parser_state, NODE *node)
  5300. {
  5301. if (node) {
  5302. no_blockarg(parser_state, node);
  5303. if (nd_type(node) == NODE_ARRAY && node->nd_next == 0) {
  5304. node = node->nd_head;
  5305. }
  5306. if (node && nd_type(node) == NODE_SPLAT) {
  5307. node = NEW_SVALUE(node);
  5308. }
  5309. }
  5310. return node;
  5311. }
  5312. static NODE *
  5313. new_yield(rb_parser_state *parser_state, NODE *node)
  5314. {
  5315. VALUE state = Qtrue;
  5316. if (node) {
  5317. no_blockarg(parser_state, node);
  5318. if (nd_type(node) == NODE_ARRAY && node->nd_next == 0) {
  5319. node = node->nd_head;
  5320. state = Qfalse;
  5321. }
  5322. if (node && nd_type(node) == NODE_SPLAT) {
  5323. state = Qtrue;
  5324. }
  5325. }
  5326. else {
  5327. state = Qfalse;
  5328. }
  5329. return NEW_YIELD(node, state);
  5330. }
  5331. static NODE *
  5332. arg_blk_pass(NODE *node1, NODE *node2)
  5333. {
  5334. if (node2) {
  5335. node2->nd_head = node1;
  5336. return node2;
  5337. }
  5338. return node1;
  5339. }
  5340. static NODE*
  5341. arg_prepend(rb_parser_state *parser_state, NODE *node1, NODE *node2)
  5342. {
  5343. switch (nd_type(node2)) {
  5344. case NODE_ARRAY:
  5345. return list_concat(NEW_LIST(node1), node2);
  5346. case NODE_SPLAT:
  5347. return arg_concat(parser_state, node1, node2->nd_head);
  5348. case NODE_BLOCK_PASS:
  5349. node2->nd_body = arg_prepend(parser_state, node1, node2->nd_body);
  5350. return node2;
  5351. default:
  5352. printf("unknown nodetype(%d) for arg_prepend", nd_type(node2));
  5353. abort();
  5354. }
  5355. return 0; /* not reached */
  5356. }
  5357. static NODE*
  5358. new_call(rb_parser_state *parser_state,NODE *r,QUID m,NODE *a)
  5359. {
  5360. if (a && nd_type(a) == NODE_BLOCK_PASS) {
  5361. a->nd_iter = NEW_CALL(r,convert_op(m),a->nd_head);
  5362. return a;
  5363. }
  5364. return NEW_CALL(r,convert_op(m),a);
  5365. }
  5366. static NODE*
  5367. new_fcall(rb_parser_state *parser_state,QUID m,NODE *a)
  5368. {
  5369. if (a && nd_type(a) == NODE_BLOCK_PASS) {
  5370. a->nd_iter = NEW_FCALL(m,a->nd_head);
  5371. return a;
  5372. }
  5373. return NEW_FCALL(m,a);
  5374. }
  5375. static NODE*
  5376. new_super(rb_parser_state *parser_state,NODE *a)
  5377. {
  5378. if (a && nd_type(a) == NODE_BLOCK_PASS) {
  5379. a->nd_iter = NEW_SUPER(a->nd_head);
  5380. return a;
  5381. }
  5382. return NEW_SUPER(a);
  5383. }
  5384. static void
  5385. mel_local_push(rb_parser_state *parser_state, int cnt)
  5386. {
  5387. variables = LocalState::push(variables);
  5388. }
  5389. static void
  5390. mel_local_pop(rb_parser_state *parser_state)
  5391. {
  5392. variables = LocalState::pop(variables);
  5393. }
  5394. static QUID*
  5395. mel_local_tbl(rb_parser_state *parser_state)
  5396. {
  5397. QUID *lcl_tbl;
  5398. var_table tbl;
  5399. int i, len;
  5400. tbl = variables->local_vars;
  5401. len = var_table_size(tbl);
  5402. lcl_tbl = (QUID*)pt_allocate(parser_state, (int)(sizeof(QUID) * (len + 3)));
  5403. lcl_tbl[0] = (QUID)len;
  5404. lcl_tbl[1] = '_';
  5405. lcl_tbl[2] = '~';
  5406. for(i = 0; i < len; i++) {
  5407. lcl_tbl[i + 3] = var_table_get(tbl, i);
  5408. }
  5409. return lcl_tbl;
  5410. }
  5411. static intptr_t
  5412. mel_local_cnt(rb_parser_state *parser_state, QUID id)
  5413. {
  5414. int idx;
  5415. /* Leave these hardcoded here because they arne't REALLY ids at all. */
  5416. if(id == '_') {
  5417. return 0;
  5418. } else if(id == '~') {
  5419. return 1;
  5420. }
  5421. // if there are block variables, check to see if there is already
  5422. // a local by this name. If not, create one in the top block_vars
  5423. // table.
  5424. if(variables->block_vars) {
  5425. idx = var_table_find_chained(variables->block_vars, id);
  5426. if(idx >= 0) {
  5427. return idx;
  5428. } else {
  5429. return var_table_add(variables->block_vars, id);
  5430. }
  5431. }
  5432. idx = var_table_find(variables->local_vars, id);
  5433. if(idx >= 0) {
  5434. return idx + 2;
  5435. }
  5436. return var_table_add(variables->local_vars, id);
  5437. }
  5438. static int
  5439. mel_local_id(rb_parser_state *parser_state, QUID id)
  5440. {
  5441. if(variables->block_vars) {
  5442. if(var_table_find_chained(variables->block_vars, id) >= 0) return 1;
  5443. }
  5444. if(var_table_find(variables->local_vars, id) >= 0) return 1;
  5445. return 0;
  5446. }
  5447. static QUID
  5448. rb_parser_sym(const char *name)
  5449. {
  5450. const char *m = name;
  5451. QUID id, pre, qrk, bef;
  5452. int last;
  5453. id = 0;
  5454. last = (int)strlen(name)-1;
  5455. switch (*name) {
  5456. case '$':
  5457. id |= ID_GLOBAL;
  5458. m++;
  5459. if(!m[0]) { // Detect a :"$"
  5460. id = ID_LOCAL;
  5461. } else if (!is_identchar(*m)) {
  5462. m++;
  5463. }
  5464. break;
  5465. case '@':
  5466. if (name[1] == '@') {
  5467. m++;
  5468. id |= ID_CLASS;
  5469. }
  5470. else {
  5471. id |= ID_INSTANCE;
  5472. }
  5473. m++;
  5474. break;
  5475. default:
  5476. if (name[0] != '_' && !ISALPHA(name[0]) && !ismbchar(name[0])) {
  5477. int i;
  5478. for (i=0; op_tbl[i].token; i++) {
  5479. if (*op_tbl[i].name == *name &&
  5480. strcmp(op_tbl[i].name, name) == 0) {
  5481. id = op_tbl[i].token;
  5482. return id;
  5483. }
  5484. }
  5485. }
  5486. if (name[last] == '=') {
  5487. id = ID_ATTRSET;
  5488. }
  5489. else if (ISUPPER(name[0])) {
  5490. id = ID_CONST;
  5491. }
  5492. else {
  5493. id = ID_LOCAL;
  5494. }
  5495. break;
  5496. }
  5497. while (m <= name + last && is_identchar(*m)) {
  5498. m += mbclen(*m);
  5499. }
  5500. if (*m) id = ID_JUNK;
  5501. qrk = (QUID)quark_from_string(name);
  5502. pre = qrk + tLAST_TOKEN;
  5503. bef = id;
  5504. id |= ( pre << ID_SCOPE_SHIFT );
  5505. return id;
  5506. }
  5507. static int
  5508. scan_oct(const char *start, int len, int *retlen)
  5509. {
  5510. register const char *s = start;
  5511. register int retval = 0;
  5512. while (len-- && *s >= '0' && *s <= '7') {
  5513. retval <<= 3;
  5514. retval |= *s++ - '0';
  5515. }
  5516. *retlen = (int)(s - start);
  5517. return retval;
  5518. }
  5519. static int
  5520. scan_hex(const char *start, int len, int *retlen)
  5521. {
  5522. static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
  5523. register const char *s = start;
  5524. register int retval = 0;
  5525. const char *tmp;
  5526. while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
  5527. retval <<= 4;
  5528. retval |= (int)((tmp - hexdigit) & 15);
  5529. s++;
  5530. }
  5531. *retlen = (int)(s - start);
  5532. return retval;
  5533. }
  5534. const char *op_to_name(QUID id) {
  5535. if(id < tLAST_TOKEN) {
  5536. int i = 0;
  5537. for (i=0; op_tbl[i].token; i++) {
  5538. if (op_tbl[i].token == id)
  5539. return op_tbl[i].name;
  5540. }
  5541. }
  5542. return NULL;
  5543. }
  5544. quark id_to_quark(QUID id) {
  5545. quark qrk;
  5546. qrk = (quark)((id >> ID_SCOPE_SHIFT) - tLAST_TOKEN);
  5547. return qrk;
  5548. }
  5549. }; // namespace grammar18
  5550. }; // namespace melbourne