1/**********************************************************************
6 created at: Fri May 28 18:02:42 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10**********************************************************************/
15# error needs pure parser
18#define YYERROR_VERBOSE 1
19#define YYSTACK_USE_ALLOCA 0
23# include RUBY_EXTCONF_H
26#include "ruby/internal/config.h"
30#ifdef UNIVERSAL_PARSER
32#include "internal/ruby_parser.h"
33#include "parser_node.h"
34#include "universal_parser.c"
37#define STATIC_ID2SYM p->config->static_id2sym
38#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
44#include "internal/compile.h"
45#include "internal/compilers.h"
46#include "internal/complex.h"
47#include "internal/encoding.h"
48#include "internal/error.h"
49#include "internal/hash.h"
50#include "internal/io.h"
51#include "internal/numeric.h"
52#include "internal/parse.h"
53#include "internal/rational.h"
54#include "internal/re.h"
55#include "internal/ruby_parser.h"
56#include "internal/symbol.h"
57#include "internal/thread.h"
58#include "internal/variable.h"
60#include "parser_node.h"
63#include "ruby/encoding.h"
64#include "ruby/regex.h"
68#include "ruby/ractor.h"
75 return rb_class_new_instance(0, 0, rb_eSyntaxError);
79static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable);
81#define compile_callback rb_suppress_tracing
82#endif /* !UNIVERSAL_PARSER */
84#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
85#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
87static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
90static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
94node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
96 return (n1->minus != n2->minus ||
97 n1->base != n2->base ||
98 strcmp(n1->val, n2->val));
102node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
104 return (n1->minus != n2->minus ||
105 strcmp(n1->val, n2->val));
109node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
111 return (n1->minus != n2->minus ||
112 n1->base != n2->base ||
113 n1->seen_point != n2->seen_point ||
114 strcmp(n1->val, n2->val));
118node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
120 return (n1->minus != n2->minus ||
121 n1->base != n2->base ||
122 n1->seen_point != n2->seen_point ||
123 n1->type != n2->type ||
124 strcmp(n1->val, n2->val));
128rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
130 return (n1->options != n2->options ||
131 rb_parser_string_hash_cmp(n1->string, n2->string));
134static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
135static st_index_t rb_char_p_hash(const char *c);
138literal_cmp(st_data_t val, st_data_t lit)
140 if (val == lit) return 0;
142 NODE *node_val = RNODE(val);
143 NODE *node_lit = RNODE(lit);
144 enum node_type type_val = nd_type(node_val);
145 enum node_type type_lit = nd_type(node_lit);
147 if (type_val != type_lit) {
153 return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
155 return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
157 return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
159 return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
161 return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
163 return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
165 return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
167 return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
169 return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
171 return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
173#ifdef UNIVERSAL_PARSER
176 rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
182literal_hash(st_data_t a)
184 NODE *node = (NODE *)a;
185 enum node_type type = nd_type(node);
189 return rb_char_p_hash(RNODE_INTEGER(node)->val);
191 return rb_char_p_hash(RNODE_FLOAT(node)->val);
193 return rb_char_p_hash(RNODE_RATIONAL(node)->val);
195 return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
197 return rb_parser_str_hash(RNODE_STR(node)->string);
199 return rb_parser_str_hash(RNODE_SYM(node)->string);
201 return rb_parser_str_hash(RNODE_REGX(node)->string);
203 return (st_index_t)node->nd_loc.beg_pos.lineno;
205 return rb_parser_str_hash(RNODE_FILE(node)->path);
207 return (st_index_t)RNODE_ENCODING(node)->enc;
209#ifdef UNIVERSAL_PARSER
212 rb_bug("unexpected node: %s", ruby_node_name(type));
220 return '\0' <= c && c <= '\x7f';
224#define ISASCII parse_isascii
229 return c == ' ' || ('\t' <= c && c <= '\r');
233#define ISSPACE parse_isspace
238 return ('\0' <= c && c < ' ') || c == '\x7f';
242#define ISCNTRL(c) parse_iscntrl(c)
247 return 'A' <= c && c <= 'Z';
253 return 'a' <= c && c <= 'z';
259 return parse_isupper(c) || parse_islower(c);
263#define ISALPHA(c) parse_isalpha(c)
268 return '0' <= c && c <= '9';
272#define ISDIGIT(c) parse_isdigit(c)
277 return parse_isalpha(c) || parse_isdigit(c);
281#define ISALNUM(c) parse_isalnum(c)
286 return parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
290#define ISXDIGIT(c) parse_isxdigit(c)
292#include "parser_st.h"
295#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
298#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
301#include "ripper_init.h"
312 unsigned int in_defined: 1;
313 unsigned int in_kwarg: 1;
314 unsigned int in_argdef: 1;
315 unsigned int in_def: 1;
316 unsigned int in_class: 1;
317 BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
318 BITFIELD(enum rescue_context, in_rescue, 2);
319 unsigned int cant_return: 1;
322typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
324#if defined(__GNUC__) && !defined(__clang__)
325// Suppress "parameter passing for argument of type 'struct
326// lex_context' changed" notes. `struct lex_context` is file scope,
327// and has no ABI compatibility issue.
329RBIMPL_WARNING_IGNORED(-Wpsabi)
331// Not sure why effective even after popped.
336#define NO_LEX_CTXT (struct lex_context){0}
338#ifndef WARN_PAST_SCOPE
339# define WARN_PAST_SCOPE 0
344#define yydebug (p->debug) /* disable the global variable definition */
346#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
347#define YY_LOCATION_PRINT(File, loc, p) \
348 rb_parser_printf(p, "%d.%d-%d.%d", \
349 (loc).beg_pos.lineno, (loc).beg_pos.column,\
350 (loc).end_pos.lineno, (loc).end_pos.column)
351#define YYLLOC_DEFAULT(Current, Rhs, N) \
355 (Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
356 (Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
360 (Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
361 (Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
365 (((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
366 "nesting too deep" : (Msgid))
368#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
369 rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
370#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
371 rb_parser_set_location_of_delayed_token(p, &(Current))
372#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
373 rb_parser_set_location_of_heredoc_end(p, &(Current))
374#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
375 rb_parser_set_location_of_dummy_end(p, &(Current))
376#define RUBY_SET_YYLLOC_OF_NONE(Current) \
377 rb_parser_set_location_of_none(p, &(Current))
378#define RUBY_SET_YYLLOC(Current) \
379 rb_parser_set_location(p, &(Current))
380#define RUBY_INIT_YYLLOC() \
382 {p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
383 {p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
386#define IS_lex_state_for(x, ls) ((x) & (ls))
387#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
388#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
389#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
391# define SET_LEX_STATE(ls) \
392 parser_set_lex_state(p, ls, __LINE__)
393static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
395typedef VALUE stack_type;
397static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
399# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
400# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
401# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
402# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
403# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
405/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
406 Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
407#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
408#define COND_POP() BITSTACK_POP(cond_stack)
409#define COND_P() BITSTACK_SET_P(cond_stack)
410#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
412/* A flag to identify keyword_do_block; "do" keyword after command_call.
413 Example: `foo 1, 2 do`. */
414#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
415#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
416#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
417#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
433 struct local_vars *prev;
435 NODE *outer, *inner, *current;
446#define DVARS_INHERIT ((void*)1)
447#define DVARS_TOPSCOPE NULL
448#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
450typedef struct token_info {
452 rb_code_position_t beg;
455 struct token_info *next;
458typedef struct end_expect_token_locations {
459 const rb_code_position_t *pos;
460 struct end_expect_token_locations *prev;
461} end_expect_token_locations_t;
463typedef struct parser_string_buffer_elem {
464 struct parser_string_buffer_elem *next;
465 long len; /* Total length of allocated buf */
466 long used; /* Current usage of buf */
467 rb_parser_string_t *buf[FLEX_ARY_LEN];
468} parser_string_buffer_elem_t;
470typedef struct parser_string_buffer {
471 parser_string_buffer_elem_t *head;
472 parser_string_buffer_elem_t *last;
473} parser_string_buffer_t;
475#define AFTER_HEREDOC_WITHOUT_TERMINTOR ((rb_parser_string_t *)1)
478 Structure of Lexer Buffer:
480 lex.pbeg lex.ptok lex.pcur lex.pend
482 |------------+------------+------------|
486struct parser_params {
491 rb_strterm_t *strterm;
492 rb_parser_lex_gets_func *gets;
493 rb_parser_input_data input;
494 parser_string_buffer_t string_buffer;
495 rb_parser_string_t *lastline;
496 rb_parser_string_t *nextline;
501 enum lex_state_e state;
502 /* track the nest level of any parens "()[]{}" */
504 /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
506 /* track the nest level of only braces "{}" */
509 stack_type cond_stack;
510 stack_type cmdarg_stack;
515 int heredoc_line_indent;
517 struct local_vars *lvtbl;
521 int ruby_sourceline; /* current line no. */
522 const char *ruby_sourcefile; /* current source file */
523 VALUE ruby_sourcefile_string;
525 token_info *token_info;
526 st_table *case_labels;
527 rb_node_exits_t *exits;
533 rb_parser_string_t *token;
543 st_table *warn_duplicate_keys_table;
548 struct lex_context ctxt;
550 NODE *eval_tree_begin;
552 const struct rb_iseq_struct *parent_iseq;
554#ifdef UNIVERSAL_PARSER
555 const rb_parser_config_t *config;
558 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
560 unsigned int command_start:1;
561 unsigned int eofp: 1;
562 unsigned int ruby__end__seen: 1;
563 unsigned int debug: 1;
564 unsigned int has_shebang: 1;
565 unsigned int token_seen: 1;
566 unsigned int token_info_enabled: 1;
568 unsigned int past_scope_enabled: 1;
570 unsigned int error_p: 1;
571 unsigned int cr_seen: 1;
576 unsigned int do_print: 1;
577 unsigned int do_loop: 1;
578 unsigned int do_chomp: 1;
579 unsigned int do_split: 1;
580 unsigned int error_tolerant: 1;
581 unsigned int keep_tokens: 1;
584 rb_parser_ary_t *debug_lines;
586 * Store specific keyword locations to generate dummy end token.
587 * Refer to the tail of list element.
589 end_expect_token_locations_t *end_expect_token_locations;
592 /* Array for term tokens */
593 rb_parser_ary_t *tokens;
599 VALUE parsing_thread;
600 VALUE s_value; /* Token VALUE */
601 VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
606#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
607#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
608#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
610numparam_id_p(struct parser_params *p, ID id)
612 if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
613 unsigned int idx = NUMPARAM_ID_TO_IDX(id);
614 return idx > 0 && idx <= NUMPARAM_MAX;
616static void numparam_name(struct parser_params *p, ID id);
620after_shift(struct parser_params *p)
623 rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
625 rb_ary_push(p->s_value_stack, p->s_value);
630before_reduce(int len, struct parser_params *p)
632 // Initialize $$ with $1.
633 if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
637after_reduce(int len, struct parser_params *p)
639 for (int i = 0; i < len; i++) {
640 VALUE tos = rb_ary_pop(p->s_value_stack);
642 rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
646 rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
648 rb_ary_push(p->s_value_stack, p->s_lvalue);
653after_shift_error_token(struct parser_params *p)
656 rb_parser_printf(p, "after-shift-error-token:\n");
658 rb_ary_push(p->s_value_stack, Qnil);
662after_pop_stack(int len, struct parser_params *p)
664 for (int i = 0; i < len; i++) {
665 VALUE tos = rb_ary_pop(p->s_value_stack);
667 rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
673after_shift(struct parser_params *p)
678before_reduce(int len, struct parser_params *p)
683after_reduce(int len, struct parser_params *p)
688after_shift_error_token(struct parser_params *p)
693after_pop_stack(int len, struct parser_params *p)
698#define intern_cstr(n,l,en) rb_intern3(n,l,en)
700#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
702#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
703#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
704#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
705#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
706#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
707#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
711char_at_end(struct parser_params *p, VALUE str, int when_empty)
713 long len = RSTRING_LEN(str);
714 return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
719pop_pvtbl(struct parser_params *p, st_table *tbl)
721 st_free_table(p->pvtbl);
726pop_pktbl(struct parser_params *p, st_table *tbl)
728 if (p->pktbl) st_free_table(p->pktbl);
732#define STRING_BUF_DEFAULT_LEN 16
735string_buffer_init(struct parser_params *p)
737 parser_string_buffer_t *buf = &p->lex.string_buffer;
738 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
740 buf->head = buf->last = xmalloc(size);
741 buf->head->len = STRING_BUF_DEFAULT_LEN;
743 buf->head->next = NULL;
747string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
749 parser_string_buffer_t *buf = &p->lex.string_buffer;
751 if (buf->head->used >= buf->head->len) {
752 parser_string_buffer_elem_t *elem;
753 long n = buf->head->len * 2;
754 const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
756 elem = xmalloc(size);
760 buf->last->next = elem;
763 buf->last->buf[buf->last->used++] = str;
767string_buffer_free(struct parser_params *p)
769 parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
772 parser_string_buffer_elem_t *next_elem = elem->next;
774 for (long i = 0; i < elem->used; i++) {
775 rb_parser_string_free(p, elem->buf[i]);
784static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
787debug_end_expect_token_locations(struct parser_params *p, const char *name)
790 VALUE mesg = rb_sprintf("%s: [", name);
792 for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
794 rb_str_cat_cstr(mesg, ", ");
795 rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
798 rb_str_cat_cstr(mesg, "]\n");
799 flush_debug_buffer(p, p->debug_output, mesg);
804push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
806 if(!p->error_tolerant) return;
808 end_expect_token_locations_t *locations;
809 locations = ALLOC(end_expect_token_locations_t);
810 locations->pos = pos;
811 locations->prev = p->end_expect_token_locations;
812 p->end_expect_token_locations = locations;
814 debug_end_expect_token_locations(p, "push_end_expect_token_locations");
818pop_end_expect_token_locations(struct parser_params *p)
820 if(!p->end_expect_token_locations) return;
822 end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
823 ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
824 p->end_expect_token_locations = locations;
826 debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
829static end_expect_token_locations_t *
830peek_end_expect_token_locations(struct parser_params *p)
832 return p->end_expect_token_locations;
836parser_token2char(struct parser_params *p, enum yytokentype tok)
839#define TOKEN2CHAR(tok) case tok: return (#tok);
840#define TOKEN2CHAR2(tok, name) case tok: return (name);
841 TOKEN2CHAR2(' ', "word_sep");
842 TOKEN2CHAR2('!', "!")
843 TOKEN2CHAR2('%', "%");
844 TOKEN2CHAR2('&', "&");
845 TOKEN2CHAR2('*', "*");
846 TOKEN2CHAR2('+', "+");
847 TOKEN2CHAR2('-', "-");
848 TOKEN2CHAR2('/', "/");
849 TOKEN2CHAR2('<', "<");
850 TOKEN2CHAR2('=', "=");
851 TOKEN2CHAR2('>', ">");
852 TOKEN2CHAR2('?', "?");
853 TOKEN2CHAR2('^', "^");
854 TOKEN2CHAR2('|', "|");
855 TOKEN2CHAR2('~', "~");
856 TOKEN2CHAR2(':', ":");
857 TOKEN2CHAR2(',', ",");
858 TOKEN2CHAR2('.', ".");
859 TOKEN2CHAR2(';', ";");
860 TOKEN2CHAR2('`', "`");
861 TOKEN2CHAR2('\n', "nl");
862 TOKEN2CHAR2('{', "\"{\"");
863 TOKEN2CHAR2('}', "\"}\"");
864 TOKEN2CHAR2('[', "\"[\"");
865 TOKEN2CHAR2(']', "\"]\"");
866 TOKEN2CHAR2('(', "\"(\"");
867 TOKEN2CHAR2(')', "\")\"");
868 TOKEN2CHAR2('\\', "backslash");
869 TOKEN2CHAR(keyword_class);
870 TOKEN2CHAR(keyword_module);
871 TOKEN2CHAR(keyword_def);
872 TOKEN2CHAR(keyword_undef);
873 TOKEN2CHAR(keyword_begin);
874 TOKEN2CHAR(keyword_rescue);
875 TOKEN2CHAR(keyword_ensure);
876 TOKEN2CHAR(keyword_end);
877 TOKEN2CHAR(keyword_if);
878 TOKEN2CHAR(keyword_unless);
879 TOKEN2CHAR(keyword_then);
880 TOKEN2CHAR(keyword_elsif);
881 TOKEN2CHAR(keyword_else);
882 TOKEN2CHAR(keyword_case);
883 TOKEN2CHAR(keyword_when);
884 TOKEN2CHAR(keyword_while);
885 TOKEN2CHAR(keyword_until);
886 TOKEN2CHAR(keyword_for);
887 TOKEN2CHAR(keyword_break);
888 TOKEN2CHAR(keyword_next);
889 TOKEN2CHAR(keyword_redo);
890 TOKEN2CHAR(keyword_retry);
891 TOKEN2CHAR(keyword_in);
892 TOKEN2CHAR(keyword_do);
893 TOKEN2CHAR(keyword_do_cond);
894 TOKEN2CHAR(keyword_do_block);
895 TOKEN2CHAR(keyword_do_LAMBDA);
896 TOKEN2CHAR(keyword_return);
897 TOKEN2CHAR(keyword_yield);
898 TOKEN2CHAR(keyword_super);
899 TOKEN2CHAR(keyword_self);
900 TOKEN2CHAR(keyword_nil);
901 TOKEN2CHAR(keyword_true);
902 TOKEN2CHAR(keyword_false);
903 TOKEN2CHAR(keyword_and);
904 TOKEN2CHAR(keyword_or);
905 TOKEN2CHAR(keyword_not);
906 TOKEN2CHAR(modifier_if);
907 TOKEN2CHAR(modifier_unless);
908 TOKEN2CHAR(modifier_while);
909 TOKEN2CHAR(modifier_until);
910 TOKEN2CHAR(modifier_rescue);
911 TOKEN2CHAR(keyword_alias);
912 TOKEN2CHAR(keyword_defined);
913 TOKEN2CHAR(keyword_BEGIN);
914 TOKEN2CHAR(keyword_END);
915 TOKEN2CHAR(keyword__LINE__);
916 TOKEN2CHAR(keyword__FILE__);
917 TOKEN2CHAR(keyword__ENCODING__);
918 TOKEN2CHAR(tIDENTIFIER);
922 TOKEN2CHAR(tCONSTANT);
925 TOKEN2CHAR(tINTEGER);
927 TOKEN2CHAR(tRATIONAL);
928 TOKEN2CHAR(tIMAGINARY);
930 TOKEN2CHAR(tNTH_REF);
931 TOKEN2CHAR(tBACK_REF);
932 TOKEN2CHAR(tSTRING_CONTENT);
933 TOKEN2CHAR(tREGEXP_END);
934 TOKEN2CHAR(tDUMNY_END);
960 TOKEN2CHAR(tOP_ASGN);
963 TOKEN2CHAR(tLPAREN_ARG);
967 TOKEN2CHAR(tLBRACE_ARG);
973 TOKEN2CHAR(tSTRING_BEG);
974 TOKEN2CHAR(tXSTRING_BEG);
975 TOKEN2CHAR(tREGEXP_BEG);
976 TOKEN2CHAR(tWORDS_BEG);
977 TOKEN2CHAR(tQWORDS_BEG);
978 TOKEN2CHAR(tSYMBOLS_BEG);
979 TOKEN2CHAR(tQSYMBOLS_BEG);
980 TOKEN2CHAR(tSTRING_END);
981 TOKEN2CHAR(tSTRING_DEND);
982 TOKEN2CHAR(tSTRING_DBEG);
983 TOKEN2CHAR(tSTRING_DVAR);
985 TOKEN2CHAR(tLABEL_END);
986 TOKEN2CHAR(tIGNORED_NL);
987 TOKEN2CHAR(tCOMMENT);
988 TOKEN2CHAR(tEMBDOC_BEG);
990 TOKEN2CHAR(tEMBDOC_END);
991 TOKEN2CHAR(tHEREDOC_BEG);
992 TOKEN2CHAR(tHEREDOC_END);
993 TOKEN2CHAR(k__END__);
995 TOKEN2CHAR(tUMINUS_NUM);
996 TOKEN2CHAR(tLAST_TOKEN);
1001 rb_bug("parser_token2id: unknown token %d", tok);
1003 UNREACHABLE_RETURN(0);
1007push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
1012pop_end_expect_token_locations(struct parser_params *p)
1017RBIMPL_ATTR_NONNULL((1, 2, 3))
1018static int parser_yyerror(struct parser_params*, const YYLTYPE *yylloc, const char*);
1019RBIMPL_ATTR_NONNULL((1, 2))
1020static int parser_yyerror0(struct parser_params*, const char*);
1021#define yyerror0(msg) parser_yyerror0(p, (msg))
1022#define yyerror1(loc, msg) parser_yyerror(p, (loc), (msg))
1023#define yyerror(yylloc, p, msg) parser_yyerror(p, yylloc, msg)
1024#define token_flush(ptr) ((ptr)->lex.ptok = (ptr)->lex.pcur)
1025#define lex_goto_eol(p) ((p)->lex.pcur = (p)->lex.pend)
1026#define lex_eol_p(p) lex_eol_n_p(p, 0)
1027#define lex_eol_n_p(p,n) lex_eol_ptr_n_p(p, (p)->lex.pcur, n)
1028#define lex_eol_ptr_p(p,ptr) lex_eol_ptr_n_p(p,ptr,0)
1029#define lex_eol_ptr_n_p(p,ptr,n) ((ptr)+(n) >= (p)->lex.pend)
1031static void token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc);
1032static void token_info_push(struct parser_params*, const char *token, const rb_code_location_t *loc);
1033static void token_info_pop(struct parser_params*, const char *token, const rb_code_location_t *loc);
1034static void token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc);
1035static void token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos);
1038#define compile_for_eval (0)
1040#define compile_for_eval (p->parent_iseq != 0)
1043#define token_column ((int)(p->lex.ptok - p->lex.pbeg))
1045#define CALL_Q_P(q) ((q) == tANDDOT)
1046#define NEW_QCALL(q,r,m,a,loc) (CALL_Q_P(q) ? NEW_QCALL0(r,m,a,loc) : NEW_CALL(r,m,a,loc))
1048#define lambda_beginning_p() (p->lex.lpar_beg == p->lex.paren_nest)
1050static enum yytokentype yylex(YYSTYPE*, YYLTYPE*, struct parser_params*);
1053rb_discard_node(struct parser_params *p, NODE *n)
1055 rb_ast_delete_node(p->ast, n);
1058static rb_node_scope_t *rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1059static rb_node_scope_t *rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1060static rb_node_block_t *rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1061static rb_node_if_t *rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc);
1062static rb_node_unless_t *rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc);
1063static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1064static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1065static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
1066static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc);
1067static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1068static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1069static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
1070static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1071static rb_node_for_t *rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc);
1072static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc);
1073static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
1074static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1075static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1076static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1077static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
1078static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1079static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1080static rb_node_masgn_t *rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc);
1081static rb_node_lasgn_t *rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1082static rb_node_dasgn_t *rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1083static rb_node_gasgn_t *rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1084static rb_node_iasgn_t *rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1085static rb_node_cdecl_t *rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1086static rb_node_cvasgn_t *rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc);
1087static rb_node_op_asgn1_t *rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1088static rb_node_op_asgn2_t *rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1089static rb_node_op_asgn_or_t *rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1090static rb_node_op_asgn_and_t *rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc);
1091static rb_node_op_cdecl_t *rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc);
1092static rb_node_call_t *rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1093static rb_node_opcall_t *rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1094static rb_node_fcall_t *rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1095static rb_node_vcall_t *rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1096static rb_node_qcall_t *rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1097static rb_node_super_t *rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc);
1098static rb_node_zsuper_t * rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc);
1099static rb_node_list_t *rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1100static rb_node_list_t *rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1101static rb_node_zlist_t *rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc);
1102static rb_node_hash_t *rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1103static rb_node_return_t *rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1104static rb_node_yield_t *rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1105static rb_node_lvar_t *rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1106static rb_node_dvar_t *rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1107static rb_node_gvar_t *rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1108static rb_node_ivar_t *rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1109static rb_node_const_t *rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1110static rb_node_cvar_t *rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc);
1111static rb_node_nth_ref_t *rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1112static rb_node_back_ref_t *rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc);
1113static rb_node_match2_t *rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1114static rb_node_match3_t *rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc);
1115static rb_node_integer_t * rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc);
1116static rb_node_float_t * rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc);
1117static rb_node_rational_t * rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc);
1118static rb_node_imaginary_t * rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type, const YYLTYPE *loc);
1119static rb_node_str_t *rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1120static rb_node_dstr_t *rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1121static rb_node_dstr_t *rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1122static rb_node_xstr_t *rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc);
1123static rb_node_dxstr_t *rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1124static rb_node_evstr_t *rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1125static rb_node_regx_t *rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc);
1126static rb_node_once_t *rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1127static rb_node_args_t *rb_node_args_new(struct parser_params *p, const YYLTYPE *loc);
1128static rb_node_args_aux_t *rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc);
1129static rb_node_opt_arg_t *rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1130static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1131static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
1132static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1133static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
1134static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1135static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc);
1136static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1137static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
1138static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1139static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1140static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
1141static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc);
1142static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
1143static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
1144static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
1145static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1146static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
1147static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
1148static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
1149static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
1150static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *loc);
1151static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
1152static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
1153static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1154static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1155static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1156static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
1157static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
1158static rb_node_lambda_t *rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
1159static rb_node_aryptn_t *rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1160static rb_node_hshptn_t *rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc);
1161static rb_node_fndptn_t *rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1162static rb_node_line_t *rb_node_line_new(struct parser_params *p, const YYLTYPE *loc);
1163static rb_node_file_t *rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
1164static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE *loc);
1166#define NEW_SCOPE(a,b,loc) (NODE *)rb_node_scope_new(p,a,b,loc)
1167#define NEW_SCOPE2(t,a,b,loc) (NODE *)rb_node_scope_new2(p,t,a,b,loc)
1168#define NEW_BLOCK(a,loc) (NODE *)rb_node_block_new(p,a,loc)
1169#define NEW_IF(c,t,e,loc) (NODE *)rb_node_if_new(p,c,t,e,loc)
1170#define NEW_UNLESS(c,t,e,loc,k_loc,t_loc,e_loc) (NODE *)rb_node_unless_new(p,c,t,e,loc,k_loc,t_loc,e_loc)
1171#define NEW_CASE(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case_new(p,h,b,loc,ck_loc,ek_loc)
1172#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
1173#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
1174#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc)
1175#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
1176#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc)
1177#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc)
1178#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
1179#define NEW_FOR(i,b,loc) (NODE *)rb_node_for_new(p,i,b,loc)
1180#define NEW_FOR_MASGN(v,loc) (NODE *)rb_node_for_masgn_new(p,v,loc)
1181#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
1182#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
1183#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1184#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
1185#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
1186#define NEW_AND(f,s,loc,op_loc) (NODE *)rb_node_and_new(p,f,s,loc,op_loc)
1187#define NEW_OR(f,s,loc,op_loc) (NODE *)rb_node_or_new(p,f,s,loc,op_loc)
1188#define NEW_MASGN(l,r,loc) rb_node_masgn_new(p,l,r,loc)
1189#define NEW_LASGN(v,val,loc) (NODE *)rb_node_lasgn_new(p,v,val,loc)
1190#define NEW_DASGN(v,val,loc) (NODE *)rb_node_dasgn_new(p,v,val,loc)
1191#define NEW_GASGN(v,val,loc) (NODE *)rb_node_gasgn_new(p,v,val,loc)
1192#define NEW_IASGN(v,val,loc) (NODE *)rb_node_iasgn_new(p,v,val,loc)
1193#define NEW_CDECL(v,val,path,share,loc) (NODE *)rb_node_cdecl_new(p,v,val,path,share,loc)
1194#define NEW_CVASGN(v,val,loc) (NODE *)rb_node_cvasgn_new(p,v,val,loc)
1195#define NEW_OP_ASGN1(r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc) (NODE *)rb_node_op_asgn1_new(p,r,id,idx,rval,loc,c_op_loc,o_loc,c_loc,b_op_loc)
1196#define NEW_OP_ASGN2(r,t,i,o,val,loc,c_op_loc,m_loc,b_op_loc) (NODE *)rb_node_op_asgn2_new(p,r,val,i,o,t,loc,c_op_loc,m_loc,b_op_loc)
1197#define NEW_OP_ASGN_OR(i,val,loc) (NODE *)rb_node_op_asgn_or_new(p,i,val,loc)
1198#define NEW_OP_ASGN_AND(i,val,loc) (NODE *)rb_node_op_asgn_and_new(p,i,val,loc)
1199#define NEW_OP_CDECL(v,op,val,share,loc) (NODE *)rb_node_op_cdecl_new(p,v,val,op,share,loc)
1200#define NEW_CALL(r,m,a,loc) (NODE *)rb_node_call_new(p,r,m,a,loc)
1201#define NEW_OPCALL(r,m,a,loc) (NODE *)rb_node_opcall_new(p,r,m,a,loc)
1202#define NEW_FCALL(m,a,loc) rb_node_fcall_new(p,m,a,loc)
1203#define NEW_VCALL(m,loc) (NODE *)rb_node_vcall_new(p,m,loc)
1204#define NEW_QCALL0(r,m,a,loc) (NODE *)rb_node_qcall_new(p,r,m,a,loc)
1205#define NEW_SUPER(a,loc) (NODE *)rb_node_super_new(p,a,loc)
1206#define NEW_ZSUPER(loc) (NODE *)rb_node_zsuper_new(p,loc)
1207#define NEW_LIST(a,loc) (NODE *)rb_node_list_new(p,a,loc)
1208#define NEW_LIST2(h,l,n,loc) (NODE *)rb_node_list_new2(p,h,l,n,loc)
1209#define NEW_ZLIST(loc) (NODE *)rb_node_zlist_new(p,loc)
1210#define NEW_HASH(a,loc) (NODE *)rb_node_hash_new(p,a,loc)
1211#define NEW_RETURN(s,loc,k_loc) (NODE *)rb_node_return_new(p,s,loc,k_loc)
1212#define NEW_YIELD(a,loc) (NODE *)rb_node_yield_new(p,a,loc)
1213#define NEW_LVAR(v,loc) (NODE *)rb_node_lvar_new(p,v,loc)
1214#define NEW_DVAR(v,loc) (NODE *)rb_node_dvar_new(p,v,loc)
1215#define NEW_GVAR(v,loc) (NODE *)rb_node_gvar_new(p,v,loc)
1216#define NEW_IVAR(v,loc) (NODE *)rb_node_ivar_new(p,v,loc)
1217#define NEW_CONST(v,loc) (NODE *)rb_node_const_new(p,v,loc)
1218#define NEW_CVAR(v,loc) (NODE *)rb_node_cvar_new(p,v,loc)
1219#define NEW_NTH_REF(n,loc) (NODE *)rb_node_nth_ref_new(p,n,loc)
1220#define NEW_BACK_REF(n,loc) (NODE *)rb_node_back_ref_new(p,n,loc)
1221#define NEW_MATCH2(n1,n2,loc) (NODE *)rb_node_match2_new(p,n1,n2,loc)
1222#define NEW_MATCH3(r,n2,loc) (NODE *)rb_node_match3_new(p,r,n2,loc)
1223#define NEW_INTEGER(val, base,loc) (NODE *)rb_node_integer_new(p,val,base,loc)
1224#define NEW_FLOAT(val,loc) (NODE *)rb_node_float_new(p,val,loc)
1225#define NEW_RATIONAL(val,base,seen_point,loc) (NODE *)rb_node_rational_new(p,val,base,seen_point,loc)
1226#define NEW_IMAGINARY(val,base,seen_point,numeric_type,loc) (NODE *)rb_node_imaginary_new(p,val,base,seen_point,numeric_type,loc)
1227#define NEW_STR(s,loc) (NODE *)rb_node_str_new(p,s,loc)
1228#define NEW_DSTR0(s,l,n,loc) (NODE *)rb_node_dstr_new0(p,s,l,n,loc)
1229#define NEW_DSTR(s,loc) (NODE *)rb_node_dstr_new(p,s,loc)
1230#define NEW_XSTR(s,loc) (NODE *)rb_node_xstr_new(p,s,loc)
1231#define NEW_DXSTR(s,l,n,loc) (NODE *)rb_node_dxstr_new(p,s,l,n,loc)
1232#define NEW_EVSTR(n,loc) (NODE *)rb_node_evstr_new(p,n,loc)
1233#define NEW_REGX(str,opts,loc) (NODE *)rb_node_regx_new(p,str,opts,loc)
1234#define NEW_ONCE(b,loc) (NODE *)rb_node_once_new(p,b,loc)
1235#define NEW_ARGS(loc) rb_node_args_new(p,loc)
1236#define NEW_ARGS_AUX(r,b,loc) rb_node_args_aux_new(p,r,b,loc)
1237#define NEW_OPT_ARG(v,loc) rb_node_opt_arg_new(p,v,loc)
1238#define NEW_KW_ARG(v,loc) rb_node_kw_arg_new(p,v,loc)
1239#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
1240#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
1241#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
1242#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
1243#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_loc)
1244#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
1245#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
1246#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
1247#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
1248#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
1249#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
1250#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1251#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1252#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
1253#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
1254#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc)
1255#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc)
1256#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
1257#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
1258#define NEW_TRUE(loc) (NODE *)rb_node_true_new(p,loc)
1259#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
1260#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
1261#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1262#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc)
1263#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
1264#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
1265#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
1266#define NEW_LAMBDA(a,b,loc) (NODE *)rb_node_lambda_new(p,a,b,loc)
1267#define NEW_ARYPTN(pre,r,post,loc) (NODE *)rb_node_aryptn_new(p,pre,r,post,loc)
1268#define NEW_HSHPTN(c,kw,kwrest,loc) (NODE *)rb_node_hshptn_new(p,c,kw,kwrest,loc)
1269#define NEW_FNDPTN(pre,a,post,loc) (NODE *)rb_node_fndptn_new(p,pre,a,post,loc)
1270#define NEW_LINE(loc) (NODE *)rb_node_line_new(p,loc)
1271#define NEW_FILE(str,loc) (NODE *)rb_node_file_new(p,str,loc)
1272#define NEW_ENCODING(loc) (NODE *)rb_node_encoding_new(p,loc)
1273#define NEW_ERROR(loc) (NODE *)rb_node_error_new(p,loc)
1275enum internal_node_type {
1276 NODE_INTERNAL_ONLY = NODE_LAST,
1283parser_node_name(int node)
1287 return "NODE_DEF_TEMP";
1289 return "NODE_EXITS";
1291 return ruby_node_name(node);
1295/* This node is parse.y internal */
1296struct RNode_DEF_TEMP {
1299 /* for NODE_DEFN/NODE_DEFS */
1301 struct RNode *nd_def;
1306 NODE *numparam_save;
1307 struct lex_context ctxt;
1311#define RNODE_DEF_TEMP(node) ((struct RNode_DEF_TEMP *)(node))
1313static rb_node_break_t *rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1314static rb_node_next_t *rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1315static rb_node_redo_t *rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
1316static rb_node_def_temp_t *rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc);
1317static rb_node_def_temp_t *def_head_save(struct parser_params *p, rb_node_def_temp_t *n);
1319#define NEW_BREAK(s,loc,k_loc) (NODE *)rb_node_break_new(p,s,loc,k_loc)
1320#define NEW_NEXT(s,loc,k_loc) (NODE *)rb_node_next_new(p,s,loc,k_loc)
1321#define NEW_REDO(loc,k_loc) (NODE *)rb_node_redo_new(p,loc,k_loc)
1322#define NEW_DEF_TEMP(loc) rb_node_def_temp_new(p,loc)
1324/* Make a new internal node, which should not be appeared in the
1325 * result AST and does not have node_id and location. */
1326static NODE* node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment);
1327#define NODE_NEW_INTERNAL(ndtype, type) (type *)node_new_internal(p, (enum node_type)(ndtype), sizeof(type), RUBY_ALIGNOF(type))
1329static NODE *nd_set_loc(NODE *nd, const YYLTYPE *loc);
1332parser_get_node_id(struct parser_params *p)
1334 int node_id = p->node_id;
1340anddot_multiple_assignment_check(struct parser_params* p, const YYLTYPE *loc, ID id)
1342 if (id == tANDDOT) {
1343 yyerror1(loc, "&. inside multiple assignment destination");
1348set_line_body(NODE *body, int line)
1351 switch (nd_type(body)) {
1354 nd_set_line(body, line);
1359set_embraced_location(NODE *node, const rb_code_location_t *beg, const rb_code_location_t *end)
1361 RNODE_ITER(node)->nd_body->nd_loc = code_loc_gen(beg, end);
1362 nd_set_line(node, beg->end_pos.lineno);
1366last_expr_node(NODE *expr)
1369 if (nd_type_p(expr, NODE_BLOCK)) {
1370 expr = RNODE_BLOCK(RNODE_BLOCK(expr)->nd_end)->nd_head;
1372 else if (nd_type_p(expr, NODE_BEGIN) && RNODE_BEGIN(expr)->nd_body) {
1373 expr = RNODE_BEGIN(expr)->nd_body;
1383#define yyparse ruby_yyparse
1386static NODE* cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1387static NODE* method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc);
1388#define new_nil(loc) NEW_NIL(loc)
1389static NODE *new_nil_at(struct parser_params *p, const rb_code_position_t *pos);
1390static NODE *new_if(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*);
1391static NODE *new_unless(struct parser_params*,NODE*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*,const YYLTYPE*);
1392static NODE *logop(struct parser_params*,ID,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1394static NODE *newline_node(NODE*);
1395static void fixpos(NODE*,NODE*);
1397static int value_expr_gen(struct parser_params*,NODE*);
1398static void void_expr(struct parser_params*,NODE*);
1399static NODE *remove_begin(NODE*);
1400#define value_expr(node) value_expr_gen(p, (node))
1401static NODE *void_stmts(struct parser_params*,NODE*);
1402static void reduce_nodes(struct parser_params*,NODE**);
1403static void block_dup_check(struct parser_params*,NODE*,NODE*);
1405static NODE *block_append(struct parser_params*,NODE*,NODE*);
1406static NODE *list_append(struct parser_params*,NODE*,NODE*);
1407static NODE *list_concat(NODE*,NODE*);
1408static NODE *arg_append(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1409static NODE *last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc);
1410static NODE *rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc);
1411static NODE *literal_concat(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1412static NODE *new_evstr(struct parser_params*,NODE*,const YYLTYPE*);
1413static NODE *new_dstr(struct parser_params*,NODE*,const YYLTYPE*);
1414static NODE *str2dstr(struct parser_params*,NODE*);
1415static NODE *evstr2dstr(struct parser_params*,NODE*);
1416static NODE *splat_array(NODE*);
1417static void mark_lvar_used(struct parser_params *p, NODE *rhs);
1419static NODE *call_bin_op(struct parser_params*,NODE*,ID,NODE*,const YYLTYPE*,const YYLTYPE*);
1420static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYLTYPE*);
1421static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
1422static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
1423static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;}
1425static bool args_info_empty_p(struct rb_args_info *args);
1426static rb_node_args_t *new_args(struct parser_params*,rb_node_args_aux_t*,rb_node_opt_arg_t*,ID,rb_node_args_aux_t*,rb_node_args_t*,const YYLTYPE*);
1427static rb_node_args_t *new_args_tail(struct parser_params*,rb_node_kw_arg_t*,ID,ID,const YYLTYPE*);
1428static NODE *new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc);
1429static NODE *new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc);
1430static NODE *new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc);
1431static NODE *new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc);
1432static NODE *new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc);
1433static NODE *new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc);
1435static rb_node_kw_arg_t *new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc);
1436static rb_node_args_t *args_with_numbered(struct parser_params*,rb_node_args_t*,int,ID);
1438static NODE* negate_lit(struct parser_params*, NODE*);
1439static NODE *ret_args(struct parser_params*,NODE*);
1440static NODE *arg_blk_pass(NODE*,rb_node_block_pass_t*);
1441static NODE *new_yield(struct parser_params*,NODE*,const YYLTYPE*);
1442static NODE *dsym_node(struct parser_params*,NODE*,const YYLTYPE*);
1444static NODE *gettable(struct parser_params*,ID,const YYLTYPE*);
1445static NODE *assignable(struct parser_params*,ID,NODE*,const YYLTYPE*);
1447static NODE *aryset(struct parser_params*,NODE*,NODE*,const YYLTYPE*);
1448static NODE *attrset(struct parser_params*,NODE*,ID,ID,const YYLTYPE*);
1450static VALUE rb_backref_error(struct parser_params*,NODE*);
1451static NODE *node_assign(struct parser_params*,NODE*,NODE*,struct lex_context,const YYLTYPE*);
1453static NODE *new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1454static NODE *new_ary_op_assign(struct parser_params *p, NODE *ary, NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc);
1455static NODE *new_attr_op_assign(struct parser_params *p, NODE *lhs, ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc);
1456static NODE *new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context, const YYLTYPE *loc);
1457static NODE *new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc);
1459static NODE *const_decl(struct parser_params *p, NODE* path, const YYLTYPE *loc);
1461static rb_node_opt_arg_t *opt_arg_append(rb_node_opt_arg_t*, rb_node_opt_arg_t*);
1462static rb_node_kw_arg_t *kwd_append(rb_node_kw_arg_t*, rb_node_kw_arg_t*);
1464static NODE *new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1465static NODE *new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc);
1467static NODE *new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc);
1469static NODE *new_regexp(struct parser_params *, NODE *, int, const YYLTYPE *);
1471#define make_list(list, loc) ((list) ? (nd_set_loc(list, loc), list) : NEW_ZLIST(loc))
1473static NODE *new_xstring(struct parser_params *, NODE *, const YYLTYPE *loc);
1475static NODE *symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol);
1477static NODE *match_op(struct parser_params*,NODE*,NODE*,const YYLTYPE*,const YYLTYPE*);
1479static rb_ast_id_table_t *local_tbl(struct parser_params*);
1481static VALUE reg_compile(struct parser_params*, rb_parser_string_t*, int);
1482static void reg_fragment_setenc(struct parser_params*, rb_parser_string_t*, int);
1483int rb_parser_reg_fragment_check(struct parser_params*, rb_parser_string_t*, int, rb_parser_reg_fragment_error_func);
1484static void reg_fragment_error(struct parser_params *, VALUE);
1485#define reg_fragment_check(p, str, option) rb_parser_reg_fragment_check(p, str, option, reg_fragment_error)
1487static int literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail);
1488static NODE *heredoc_dedent(struct parser_params*,NODE*);
1490static void check_literal_when(struct parser_params *p, NODE *args, const YYLTYPE *loc);
1493#define get_value(idx) (rb_ary_entry(p->s_value_stack, idx))
1494#define set_value(val) (p->s_lvalue = val)
1495static VALUE assign_error(struct parser_params *p, const char *mesg, VALUE a);
1496static int id_is_var(struct parser_params *p, ID id);
1499RUBY_SYMBOL_EXPORT_BEGIN
1500VALUE rb_parser_reg_compile(struct parser_params* p, VALUE str, int options);
1501int rb_reg_fragment_setenc(struct parser_params*, rb_parser_string_t *, int);
1502enum lex_state_e rb_parser_trace_lex_state(struct parser_params *, enum lex_state_e, enum lex_state_e, int);
1503VALUE rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state);
1504void rb_parser_show_bitstack(struct parser_params *, stack_type, const char *, int);
1505PRINTF_ARGS(void rb_parser_fatal(struct parser_params *p, const char *fmt, ...), 2, 3);
1506YYLTYPE *rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc);
1507YYLTYPE *rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc);
1508YYLTYPE *rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc);
1509YYLTYPE *rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc);
1510YYLTYPE *rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc);
1511YYLTYPE *rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc);
1512void ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str);
1513RUBY_SYMBOL_EXPORT_END
1515static void flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back);
1516static void error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc);
1517static void error_duplicate_pattern_key(struct parser_params *p, ID id, const YYLTYPE *loc);
1518static VALUE formal_argument_error(struct parser_params*, ID);
1519static ID shadowing_lvar(struct parser_params*,ID);
1520static void new_bv(struct parser_params*,ID);
1522static void local_push(struct parser_params*,int);
1523static void local_pop(struct parser_params*);
1524static void local_var(struct parser_params*, ID);
1525static void arg_var(struct parser_params*, ID);
1526static int local_id(struct parser_params *p, ID id);
1527static int local_id_ref(struct parser_params*, ID, ID **);
1528#define internal_id rb_parser_internal_id
1529ID internal_id(struct parser_params*);
1530static NODE *new_args_forward_call(struct parser_params*, NODE*, const YYLTYPE*, const YYLTYPE*);
1531static int check_forwarding_args(struct parser_params*);
1532static void add_forwarding_args(struct parser_params *p);
1533static void forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var);
1535static const struct vtable *dyna_push(struct parser_params *);
1536static void dyna_pop(struct parser_params*, const struct vtable *);
1537static int dyna_in_block(struct parser_params*);
1538#define dyna_var(p, id) local_var(p, id)
1539static int dvar_defined(struct parser_params*, ID);
1540#define dvar_defined_ref rb_parser_dvar_defined_ref
1541int dvar_defined_ref(struct parser_params*, ID, ID**);
1542static int dvar_curr(struct parser_params*,ID);
1544static int lvar_defined(struct parser_params*, ID);
1546static NODE *numparam_push(struct parser_params *p);
1547static void numparam_pop(struct parser_params *p, NODE *prev_inner);
1549#define METHOD_NOT '!'
1551#define idFWD_REST '*'
1552#define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */
1553#define idFWD_BLOCK '&'
1554#define idFWD_ALL idDot3
1555#define arg_FWD_BLOCK idFWD_BLOCK
1557#define RE_ONIG_OPTION_IGNORECASE 1
1558#define RE_ONIG_OPTION_EXTEND (RE_ONIG_OPTION_IGNORECASE<<1)
1559#define RE_ONIG_OPTION_MULTILINE (RE_ONIG_OPTION_EXTEND<<1)
1560#define RE_OPTION_ONCE (1<<16)
1561#define RE_OPTION_ENCODING_SHIFT 8
1562#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
1563#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
1564#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
1565#define RE_OPTION_MASK 0xff
1566#define RE_OPTION_ARG_ENCODING_NONE 32
1568#define CHECK_LITERAL_WHEN (st_table *)1
1569#define CASE_LABELS_ENABLED_P(case_labels) (case_labels && case_labels != CHECK_LITERAL_WHEN)
1571#define yytnamerr(yyres, yystr) (YYSIZE_T)rb_yytnamerr(p, yyres, yystr)
1572RUBY_FUNC_EXPORTED size_t rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr);
1574#define TOKEN2ID(tok) ( \
1575 tTOKEN_LOCAL_BEGIN<(tok)&&(tok)<tTOKEN_LOCAL_END ? TOKEN2LOCALID(tok) : \
1576 tTOKEN_INSTANCE_BEGIN<(tok)&&(tok)<tTOKEN_INSTANCE_END ? TOKEN2INSTANCEID(tok) : \
1577 tTOKEN_GLOBAL_BEGIN<(tok)&&(tok)<tTOKEN_GLOBAL_END ? TOKEN2GLOBALID(tok) : \
1578 tTOKEN_CONST_BEGIN<(tok)&&(tok)<tTOKEN_CONST_END ? TOKEN2CONSTID(tok) : \
1579 tTOKEN_CLASS_BEGIN<(tok)&&(tok)<tTOKEN_CLASS_END ? TOKEN2CLASSID(tok) : \
1580 tTOKEN_ATTRSET_BEGIN<(tok)&&(tok)<tTOKEN_ATTRSET_END ? TOKEN2ATTRSETID(tok) : \
1581 ((tok) / ((tok)<tPRESERVED_ID_END && ((tok)>=128 || rb_ispunct(tok)))))
1583/****** Ripper *******/
1587#include "eventids1.h"
1588#include "eventids2.h"
1590extern const struct ripper_parser_ids ripper_parser_ids;
1592static VALUE ripper_dispatch0(struct parser_params*,ID);
1593static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
1594static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
1595static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
1596static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
1597static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
1598static VALUE ripper_dispatch7(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE,VALUE);
1599void ripper_error(struct parser_params *p);
1601#define dispatch0(n) ripper_dispatch0(p, RIPPER_ID(n))
1602#define dispatch1(n,a) ripper_dispatch1(p, RIPPER_ID(n), (a))
1603#define dispatch2(n,a,b) ripper_dispatch2(p, RIPPER_ID(n), (a), (b))
1604#define dispatch3(n,a,b,c) ripper_dispatch3(p, RIPPER_ID(n), (a), (b), (c))
1605#define dispatch4(n,a,b,c,d) ripper_dispatch4(p, RIPPER_ID(n), (a), (b), (c), (d))
1606#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(p, RIPPER_ID(n), (a), (b), (c), (d), (e))
1607#define dispatch7(n,a,b,c,d,e,f,g) ripper_dispatch7(p, RIPPER_ID(n), (a), (b), (c), (d), (e), (f), (g))
1609#define yyparse ripper_yyparse
1612aryptn_pre_args(struct parser_params *p, VALUE pre_arg, VALUE pre_args)
1614 if (!NIL_P(pre_arg)) {
1615 if (!NIL_P(pre_args)) {
1616 rb_ary_unshift(pre_args, pre_arg);
1619 pre_args = rb_ary_new_from_args(1, pre_arg);
1625#define ID2VAL(id) STATIC_ID2SYM(id)
1626#define TOKEN2VAL(t) ID2VAL(TOKEN2ID(t))
1629#define KWD2EID(t, v) keyword_##t
1632new_scope_body(struct parser_params *p, rb_node_args_t *args, NODE *body, const YYLTYPE *loc)
1634 body = remove_begin(body);
1635 reduce_nodes(p, &body);
1636 NODE *n = NEW_SCOPE(args, body, loc);
1637 nd_set_line(n, loc->end_pos.lineno);
1638 set_line_body(body, loc->beg_pos.lineno);
1643rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
1644 const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
1646 YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1647 rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
1648 loc.beg_pos = arg_loc->beg_pos;
1649 return NEW_RESCUE(arg, rescue, 0, &loc);
1652static NODE *add_block_exit(struct parser_params *p, NODE *node);
1653static rb_node_exits_t *init_block_exit(struct parser_params *p);
1654static rb_node_exits_t *allow_block_exit(struct parser_params *p);
1655static void restore_block_exit(struct parser_params *p, rb_node_exits_t *exits);
1656static void clear_block_exit(struct parser_params *p, bool error);
1659next_rescue_context(struct lex_context *next, const struct lex_context *outer, enum rescue_context def)
1661 next->in_rescue = outer->in_rescue == after_rescue ? after_rescue : def;
1665restore_defun(struct parser_params *p, rb_node_def_temp_t *temp)
1667 /* See: def_name action */
1668 struct lex_context ctxt = temp->save.ctxt;
1669 p->ctxt.in_def = ctxt.in_def;
1670 p->ctxt.shareable_constant_value = ctxt.shareable_constant_value;
1671 p->ctxt.in_rescue = ctxt.in_rescue;
1672 p->max_numparam = temp->save.max_numparam;
1673 numparam_pop(p, temp->save.numparam_save);
1674 clear_block_exit(p, true);
1678endless_method_name(struct parser_params *p, ID mid, const YYLTYPE *loc)
1680 if (is_attrset_id(mid)) {
1681 yyerror1(loc, "setter method cannot be defined in an endless method definition");
1683 token_info_drop(p, "def", loc->beg_pos);
1686#define debug_token_line(p, name, line) do { \
1688 const char *const pcur = p->lex.pcur; \
1689 const char *const ptok = p->lex.ptok; \
1690 rb_parser_printf(p, name ":%d (%d: %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF")\n", \
1691 line, p->ruby_sourceline, \
1692 ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur); \
1696#define begin_definition(k, loc_beg, loc_end) \
1698 if (!(p->ctxt.in_class = (k)[0] != 0)) { \
1699 /* singleton class */ \
1700 p->ctxt.cant_return = !p->ctxt.in_def; \
1701 p->ctxt.in_def = 0; \
1703 else if (p->ctxt.in_def) { \
1704 YYLTYPE loc = code_loc_gen(loc_beg, loc_end); \
1705 yyerror1(&loc, k " definition in method body"); \
1708 p->ctxt.cant_return = 1; \
1714# define ifndef_ripper(x) (x)
1715# define ifdef_ripper(r,x) (x)
1717# define ifndef_ripper(x)
1718# define ifdef_ripper(r,x) (r)
1721# define rb_warn0(fmt) WARN_CALL(WARN_ARGS(fmt, 1))
1722# define rb_warn1(fmt,a) WARN_CALL(WARN_ARGS(fmt, 2), (a))
1723# define rb_warn2(fmt,a,b) WARN_CALL(WARN_ARGS(fmt, 3), (a), (b))
1724# define rb_warn3(fmt,a,b,c) WARN_CALL(WARN_ARGS(fmt, 4), (a), (b), (c))
1725# define rb_warn4(fmt,a,b,c,d) WARN_CALL(WARN_ARGS(fmt, 5), (a), (b), (c), (d))
1726# define rb_warning0(fmt) WARNING_CALL(WARNING_ARGS(fmt, 1))
1727# define rb_warning1(fmt,a) WARNING_CALL(WARNING_ARGS(fmt, 2), (a))
1728# define rb_warning2(fmt,a,b) WARNING_CALL(WARNING_ARGS(fmt, 3), (a), (b))
1729# define rb_warning3(fmt,a,b,c) WARNING_CALL(WARNING_ARGS(fmt, 4), (a), (b), (c))
1730# define rb_warning4(fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS(fmt, 5), (a), (b), (c), (d))
1731# define rb_warn0L(l,fmt) WARN_CALL(WARN_ARGS_L(l, fmt, 1))
1732# define rb_warn1L(l,fmt,a) WARN_CALL(WARN_ARGS_L(l, fmt, 2), (a))
1733# define rb_warn2L(l,fmt,a,b) WARN_CALL(WARN_ARGS_L(l, fmt, 3), (a), (b))
1734# define rb_warn3L(l,fmt,a,b,c) WARN_CALL(WARN_ARGS_L(l, fmt, 4), (a), (b), (c))
1735# define rb_warn4L(l,fmt,a,b,c,d) WARN_CALL(WARN_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1736# define rb_warning0L(l,fmt) WARNING_CALL(WARNING_ARGS_L(l, fmt, 1))
1737# define rb_warning1L(l,fmt,a) WARNING_CALL(WARNING_ARGS_L(l, fmt, 2), (a))
1738# define rb_warning2L(l,fmt,a,b) WARNING_CALL(WARNING_ARGS_L(l, fmt, 3), (a), (b))
1739# define rb_warning3L(l,fmt,a,b,c) WARNING_CALL(WARNING_ARGS_L(l, fmt, 4), (a), (b), (c))
1740# define rb_warning4L(l,fmt,a,b,c,d) WARNING_CALL(WARNING_ARGS_L(l, fmt, 5), (a), (b), (c), (d))
1742extern const ID id_warn, id_warning, id_gets, id_assoc;
1743# define ERR_MESG() STR_NEW2(mesg) /* to bypass Ripper DSL */
1744# define WARN_S_L(s,l) STR_NEW(s,l)
1745# define WARN_S(s) STR_NEW2(s)
1746# define WARN_I(i) INT2NUM(i)
1747# define WARN_ID(i) rb_id2str(i)
1748# define PRIsWARN PRIsVALUE
1749# define WARN_ARGS(fmt,n) p->value, id_warn, n, rb_usascii_str_new_lit(fmt)
1750# define WARN_ARGS_L(l,fmt,n) WARN_ARGS(fmt,n)
1751# ifdef HAVE_VA_ARGS_MACRO
1752# define WARN_CALL(...) rb_funcall(__VA_ARGS__)
1754# define WARN_CALL rb_funcall
1756# define WARNING_ARGS(fmt,n) p->value, id_warning, n, rb_usascii_str_new_lit(fmt)
1757# define WARNING_ARGS_L(l, fmt,n) WARNING_ARGS(fmt,n)
1758# ifdef HAVE_VA_ARGS_MACRO
1759# define WARNING_CALL(...) rb_funcall(__VA_ARGS__)
1761# define WARNING_CALL rb_funcall
1763# define compile_error ripper_compile_error
1765# define WARN_S_L(s,l) s
1768# define WARN_ID(i) rb_id2name(i)
1769# define PRIsWARN PRIsVALUE
1770# define WARN_ARGS(fmt,n) WARN_ARGS_L(p->ruby_sourceline,fmt,n)
1771# define WARN_ARGS_L(l,fmt,n) p->ruby_sourcefile, (l), (fmt)
1772# define WARN_CALL rb_compile_warn
1773# define WARNING_ARGS(fmt,n) WARN_ARGS(fmt,n)
1774# define WARNING_ARGS_L(l,fmt,n) WARN_ARGS_L(l,fmt,n)
1775# define WARNING_CALL rb_compile_warning
1776PRINTF_ARGS(static void parser_compile_error(struct parser_params*, const rb_code_location_t *loc, const char *fmt, ...), 3, 4);
1777# define compile_error(p, ...) parser_compile_error(p, NULL, __VA_ARGS__)
1780#define RNODE_EXITS(node) ((rb_node_exits_t*)(node))
1783add_block_exit(struct parser_params *p, NODE *node)
1786 compile_error(p, "unexpected null node");
1789 switch (nd_type(node)) {
1790 case NODE_BREAK: case NODE_NEXT: case NODE_REDO: break;
1792 compile_error(p, "add_block_exit: unexpected node: %s", parser_node_name(nd_type(node)));
1795 if (!p->ctxt.in_defined) {
1796 rb_node_exits_t *exits = p->exits;
1798 RNODE_EXITS(exits->nd_stts)->nd_chain = node;
1799 exits->nd_stts = node;
1805static rb_node_exits_t *
1806init_block_exit(struct parser_params *p)
1808 rb_node_exits_t *old = p->exits;
1809 rb_node_exits_t *exits = NODE_NEW_INTERNAL(NODE_EXITS, rb_node_exits_t);
1810 exits->nd_chain = 0;
1811 exits->nd_stts = RNODE(exits);
1816static rb_node_exits_t *
1817allow_block_exit(struct parser_params *p)
1819 rb_node_exits_t *exits = p->exits;
1825restore_block_exit(struct parser_params *p, rb_node_exits_t *exits)
1831clear_block_exit(struct parser_params *p, bool error)
1833 rb_node_exits_t *exits = p->exits;
1836 for (NODE *e = RNODE(exits); (e = RNODE_EXITS(e)->nd_chain) != 0; ) {
1837 switch (nd_type(e)) {
1839 yyerror1(&e->nd_loc, "Invalid break");
1842 yyerror1(&e->nd_loc, "Invalid next");
1845 yyerror1(&e->nd_loc, "Invalid redo");
1848 yyerror1(&e->nd_loc, "unexpected node");
1849 goto end_checks; /* no nd_chain */
1854 exits->nd_stts = RNODE(exits);
1855 exits->nd_chain = 0;
1858#define WARN_EOL(tok) \
1859 (looking_at_eol_p(p) ? \
1860 (void)rb_warning0("'" tok "' at the end of line without an expression") : \
1862static int looking_at_eol_p(struct parser_params *p);
1865get_nd_value(struct parser_params *p, NODE *node)
1867 switch (nd_type(node)) {
1869 return RNODE_GASGN(node)->nd_value;
1871 return RNODE_IASGN(node)->nd_value;
1873 return RNODE_LASGN(node)->nd_value;
1875 return RNODE_DASGN(node)->nd_value;
1877 return RNODE_MASGN(node)->nd_value;
1879 return RNODE_CVASGN(node)->nd_value;
1881 return RNODE_CDECL(node)->nd_value;
1883 compile_error(p, "get_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1889set_nd_value(struct parser_params *p, NODE *node, NODE *rhs)
1891 switch (nd_type(node)) {
1893 RNODE_CDECL(node)->nd_value = rhs;
1896 RNODE_GASGN(node)->nd_value = rhs;
1899 RNODE_IASGN(node)->nd_value = rhs;
1902 RNODE_LASGN(node)->nd_value = rhs;
1905 RNODE_DASGN(node)->nd_value = rhs;
1908 RNODE_MASGN(node)->nd_value = rhs;
1911 RNODE_CVASGN(node)->nd_value = rhs;
1914 compile_error(p, "set_nd_value: unexpected node: %s", parser_node_name(nd_type(node)));
1920get_nd_vid(struct parser_params *p, NODE *node)
1922 switch (nd_type(node)) {
1924 return RNODE_CDECL(node)->nd_vid;
1926 return RNODE_GASGN(node)->nd_vid;
1928 return RNODE_IASGN(node)->nd_vid;
1930 return RNODE_LASGN(node)->nd_vid;
1932 return RNODE_DASGN(node)->nd_vid;
1934 return RNODE_CVASGN(node)->nd_vid;
1936 compile_error(p, "get_nd_vid: unexpected node: %s", parser_node_name(nd_type(node)));
1942get_nd_args(struct parser_params *p, NODE *node)
1944 switch (nd_type(node)) {
1946 return RNODE_CALL(node)->nd_args;
1948 return RNODE_OPCALL(node)->nd_args;
1950 return RNODE_FCALL(node)->nd_args;
1952 return RNODE_QCALL(node)->nd_args;
1954 return RNODE_SUPER(node)->nd_args;
1963 compile_error(p, "get_nd_args: unexpected node: %s", parser_node_name(nd_type(node)));
1969djb2(const uint8_t *str, size_t len)
1971 st_index_t hash = 5381;
1973 for (size_t i = 0; i < len; i++) {
1974 hash = ((hash << 5) + hash) + str[i];
1981parser_memhash(const void *ptr, long len)
1983 return djb2(ptr, len);
1986#define PARSER_STRING_PTR(str) (str->ptr)
1987#define PARSER_STRING_LEN(str) (str->len)
1988#define PARSER_STRING_END(str) (&str->ptr[str->len])
1989#define STRING_SIZE(str) ((size_t)str->len + 1)
1990#define STRING_TERM_LEN(str) (1)
1991#define STRING_TERM_FILL(str) (str->ptr[str->len] = '\0')
1992#define PARSER_STRING_RESIZE_CAPA_TERM(p,str,capacity,termlen) do {\
1993 SIZED_REALLOC_N(str->ptr, char, (size_t)total + termlen, STRING_SIZE(str)); \
1996#define STRING_SET_LEN(str, n) do { \
1999#define PARSER_STRING_GETMEM(str, ptrvar, lenvar) \
2000 ((ptrvar) = str->ptr, \
2001 (lenvar) = str->len)
2004parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
2006 return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
2009static rb_parser_string_t *
2010rb_parser_string_new(rb_parser_t *p, const char *ptr, long len)
2012 rb_parser_string_t *str;
2015 rb_bug("negative string size (or size too big): %ld", len);
2018 str = xcalloc(1, sizeof(rb_parser_string_t));
2019 str->ptr = xcalloc(len + 1, sizeof(char));
2022 memcpy(PARSER_STRING_PTR(str), ptr, len);
2024 STRING_SET_LEN(str, len);
2025 STRING_TERM_FILL(str);
2029static rb_parser_string_t *
2030rb_parser_encoding_string_new(rb_parser_t *p, const char *ptr, long len, rb_encoding *enc)
2032 rb_parser_string_t *str = rb_parser_string_new(p, ptr, len);
2033 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2040rb_str_to_parser_string(rb_parser_t *p, VALUE str)
2043 rb_parser_string_t *ret = rb_parser_encoding_string_new(p, RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
2049rb_parser_string_free(rb_parser_t *p, rb_parser_string_t *str)
2052 xfree(PARSER_STRING_PTR(str));
2058rb_parser_str_hash(rb_parser_string_t *str)
2060 return parser_memhash((const void *)PARSER_STRING_PTR(str), PARSER_STRING_LEN(str));
2064rb_char_p_hash(const char *c)
2066 return parser_memhash((const void *)c, strlen(c));
2070rb_parser_str_capacity(rb_parser_string_t *str, const int termlen)
2072 return PARSER_STRING_LEN(str);
2077rb_parser_string_end(rb_parser_string_t *str)
2079 return &str->ptr[str->len];
2084rb_parser_string_set_encoding(rb_parser_string_t *str, rb_encoding *enc)
2090rb_parser_str_get_encoding(rb_parser_string_t *str)
2097PARSER_ENCODING_IS_ASCII8BIT(struct parser_params *p, rb_parser_string_t *str)
2099 return rb_parser_str_get_encoding(str) == rb_ascii8bit_encoding();
2104PARSER_ENC_CODERANGE(rb_parser_string_t *str)
2106 return str->coderange;
2110PARSER_ENC_CODERANGE_SET(rb_parser_string_t *str, int coderange)
2112 str->coderange = coderange;
2116PARSER_ENCODING_CODERANGE_SET(rb_parser_string_t *str, rb_encoding *enc, enum rb_parser_string_coderange_type cr)
2118 rb_parser_string_set_encoding(str, enc);
2119 PARSER_ENC_CODERANGE_SET(str, cr);
2123PARSER_ENC_CODERANGE_CLEAR(rb_parser_string_t *str)
2125 str->coderange = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2129PARSER_ENC_CODERANGE_ASCIIONLY(rb_parser_string_t *str)
2131 return PARSER_ENC_CODERANGE(str) == RB_PARSER_ENC_CODERANGE_7BIT;
2135PARSER_ENC_CODERANGE_CLEAN_P(int cr)
2137 return cr == RB_PARSER_ENC_CODERANGE_7BIT || cr == RB_PARSER_ENC_CODERANGE_VALID;
2141rb_parser_search_nonascii(const char *p, const char *e)
2145 for (; s < e; s++) {
2146 if (*s & 0x80) return s;
2153rb_parser_coderange_scan(struct parser_params *p, const char *ptr, long len, rb_encoding *enc)
2155 const char *e = ptr + len;
2157 if (enc == rb_ascii8bit_encoding()) {
2158 /* enc is ASCII-8BIT. ASCII-8BIT string never be broken. */
2159 ptr = rb_parser_search_nonascii(ptr, e);
2160 return ptr ? RB_PARSER_ENC_CODERANGE_VALID : RB_PARSER_ENC_CODERANGE_7BIT;
2163 /* parser string encoding is always asciicompat */
2164 ptr = rb_parser_search_nonascii(ptr, e);
2165 if (!ptr) return RB_PARSER_ENC_CODERANGE_7BIT;
2167 int ret = rb_enc_precise_mbclen(ptr, e, enc);
2168 if (!MBCLEN_CHARFOUND_P(ret)) return RB_PARSER_ENC_CODERANGE_BROKEN;
2169 ptr += MBCLEN_CHARFOUND_LEN(ret);
2170 if (ptr == e) break;
2171 ptr = rb_parser_search_nonascii(ptr, e);
2175 return RB_PARSER_ENC_CODERANGE_VALID;
2179rb_parser_enc_coderange_scan(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2181 return rb_parser_coderange_scan(p, PARSER_STRING_PTR(str), PARSER_STRING_LEN(str), enc);
2185rb_parser_enc_str_coderange(struct parser_params *p, rb_parser_string_t *str)
2187 int cr = PARSER_ENC_CODERANGE(str);
2189 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2190 cr = rb_parser_enc_coderange_scan(p, str, rb_parser_str_get_encoding(str));
2191 PARSER_ENC_CODERANGE_SET(str, cr);
2197static rb_parser_string_t *
2198rb_parser_enc_associate(struct parser_params *p, rb_parser_string_t *str, rb_encoding *enc)
2200 if (rb_parser_str_get_encoding(str) == enc)
2202 if (!PARSER_ENC_CODERANGE_ASCIIONLY(str)) {
2203 PARSER_ENC_CODERANGE_CLEAR(str);
2205 rb_parser_string_set_encoding(str, enc);
2210rb_parser_is_ascii_string(struct parser_params *p, rb_parser_string_t *str)
2212 return rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_7BIT;
2216rb_parser_enc_compatible(struct parser_params *p, rb_parser_string_t *str1, rb_parser_string_t *str2)
2218 rb_encoding *enc1 = rb_parser_str_get_encoding(str1);
2219 rb_encoding *enc2 = rb_parser_str_get_encoding(str2);
2221 if (enc1 == NULL || enc2 == NULL)
2228 if (PARSER_STRING_LEN(str2) == 0)
2230 if (PARSER_STRING_LEN(str1) == 0)
2231 return rb_parser_is_ascii_string(p, str2) ? enc1 : enc2;
2235 cr1 = rb_parser_enc_str_coderange(p, str1);
2236 cr2 = rb_parser_enc_str_coderange(p, str2);
2239 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) return enc2;
2240 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) return enc1;
2243 if (cr2 == RB_PARSER_ENC_CODERANGE_7BIT) {
2247 if (cr1 == RB_PARSER_ENC_CODERANGE_7BIT) {
2255rb_parser_str_modify(rb_parser_string_t *str)
2257 PARSER_ENC_CODERANGE_CLEAR(str);
2261rb_parser_str_set_len(struct parser_params *p, rb_parser_string_t *str, long len)
2264 const int termlen = STRING_TERM_LEN(str);
2266 if (len > (capa = (long)(rb_parser_str_capacity(str, termlen))) || len < 0) {
2267 rb_bug("probable buffer overflow: %ld for %ld", len, capa);
2270 int cr = PARSER_ENC_CODERANGE(str);
2271 if (cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2272 /* Leave unknown. */
2274 else if (len > PARSER_STRING_LEN(str)) {
2275 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2277 else if (len < PARSER_STRING_LEN(str)) {
2278 if (cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2279 /* ASCII-only string is keeping after truncated. Valid
2280 * and broken may be invalid or valid, leave unknown. */
2281 PARSER_ENC_CODERANGE_SET(str, RB_PARSER_ENC_CODERANGE_UNKNOWN);
2285 STRING_SET_LEN(str, len);
2286 STRING_TERM_FILL(str);
2289static rb_parser_string_t *
2290rb_parser_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len)
2292 rb_parser_str_modify(str);
2293 if (len == 0) return 0;
2295 long total, olen, off = -1;
2297 const int termlen = STRING_TERM_LEN(str);
2299 PARSER_STRING_GETMEM(str, sptr, olen);
2300 if (ptr >= sptr && ptr <= sptr + olen) {
2304 if (olen > LONG_MAX - len) {
2305 compile_error(p, "string sizes too big");
2309 PARSER_STRING_RESIZE_CAPA_TERM(p, str, total, termlen);
2310 sptr = PARSER_STRING_PTR(str);
2314 memcpy(sptr + olen, ptr, len);
2315 STRING_SET_LEN(str, total);
2316 STRING_TERM_FILL(str);
2321#define parser_str_cat(str, ptr, len) rb_parser_str_buf_cat(p, str, ptr, len)
2322#define parser_str_cat_cstr(str, lit) rb_parser_str_buf_cat(p, str, lit, strlen(lit))
2324static rb_parser_string_t *
2325rb_parser_enc_cr_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2326 rb_encoding *ptr_enc, int ptr_cr, int *ptr_cr_ret)
2329 rb_encoding *str_enc, *res_enc;
2331 str_enc = rb_parser_str_get_encoding(str);
2332 str_cr = PARSER_STRING_LEN(str) ? PARSER_ENC_CODERANGE(str) : RB_PARSER_ENC_CODERANGE_7BIT;
2334 if (str_enc == ptr_enc) {
2335 if (str_cr != RB_PARSER_ENC_CODERANGE_UNKNOWN && ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2336 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2340 /* parser string encoding is always asciicompat */
2341 if (ptr_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2342 ptr_cr = rb_parser_coderange_scan(p, ptr, len, ptr_enc);
2344 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2345 if (str_enc == rb_ascii8bit_encoding() || ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2346 str_cr = rb_parser_enc_str_coderange(p, str);
2351 *ptr_cr_ret = ptr_cr;
2353 if (str_enc != ptr_enc &&
2354 str_cr != RB_PARSER_ENC_CODERANGE_7BIT &&
2355 ptr_cr != RB_PARSER_ENC_CODERANGE_7BIT) {
2359 if (str_cr == RB_PARSER_ENC_CODERANGE_UNKNOWN) {
2361 res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2363 else if (str_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2364 if (ptr_cr == RB_PARSER_ENC_CODERANGE_7BIT) {
2366 res_cr = RB_PARSER_ENC_CODERANGE_7BIT;
2373 else if (str_cr == RB_PARSER_ENC_CODERANGE_VALID) {
2375 if (PARSER_ENC_CODERANGE_CLEAN_P(ptr_cr))
2380 else { /* str_cr == RB_PARSER_ENC_CODERANGE_BROKEN */
2383 if (0 < len) res_cr = RB_PARSER_ENC_CODERANGE_UNKNOWN;
2387 compile_error(p, "negative string size (or size too big)");
2389 parser_str_cat(str, ptr, len);
2390 PARSER_ENCODING_CODERANGE_SET(str, res_enc, res_cr);
2394 compile_error(p, "incompatible character encodings: %s and %s",
2395 rb_enc_name(str_enc), rb_enc_name(ptr_enc));
2396 UNREACHABLE_RETURN(0);
2400static rb_parser_string_t *
2401rb_parser_enc_str_buf_cat(struct parser_params *p, rb_parser_string_t *str, const char *ptr, long len,
2402 rb_encoding *ptr_enc)
2404 return rb_parser_enc_cr_str_buf_cat(p, str, ptr, len, ptr_enc, RB_PARSER_ENC_CODERANGE_UNKNOWN, NULL);
2407static rb_parser_string_t *
2408rb_parser_str_buf_append(struct parser_params *p, rb_parser_string_t *str, rb_parser_string_t *str2)
2410 int str2_cr = rb_parser_enc_str_coderange(p, str2);
2412 rb_parser_enc_cr_str_buf_cat(p, str, PARSER_STRING_PTR(str2), PARSER_STRING_LEN(str2),
2413 rb_parser_str_get_encoding(str2), str2_cr, &str2_cr);
2415 PARSER_ENC_CODERANGE_SET(str2, str2_cr);
2420static rb_parser_string_t *
2421rb_parser_str_resize(struct parser_params *p, rb_parser_string_t *str, long len)
2424 rb_bug("negative string size (or size too big)");
2427 long slen = PARSER_STRING_LEN(str);
2429 if (slen > len && PARSER_ENC_CODERANGE(str) != RB_PARSER_ENC_CODERANGE_7BIT) {
2430 PARSER_ENC_CODERANGE_CLEAR(str);
2435 const int termlen = STRING_TERM_LEN(str);
2437 if ((capa = slen) < len) {
2438 SIZED_REALLOC_N(str->ptr, char, (size_t)len + termlen, STRING_SIZE(str));
2440 else if (len == slen) return str;
2441 STRING_SET_LEN(str, len);
2442 STRING_TERM_FILL(str);
2447# define PARSER_ENC_STRING_GETMEM(str, ptrvar, lenvar, encvar) \
2448 ((ptrvar) = str->ptr, \
2449 (lenvar) = str->len, \
2450 (encvar) = str->enc)
2453rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2)
2456 const char *ptr1, *ptr2;
2457 rb_encoding *enc1, *enc2;
2459 PARSER_ENC_STRING_GETMEM(str1, ptr1, len1, enc1);
2460 PARSER_ENC_STRING_GETMEM(str2, ptr2, len2, enc2);
2462 return (len1 != len2 ||
2464 memcmp(ptr1, ptr2, len1) != 0);
2468rb_parser_ary_extend(rb_parser_t *p, rb_parser_ary_t *ary, long len)
2471 if (ary->capa < len) {
2473 ary->data = (rb_parser_ary_data *)xrealloc(ary->data, sizeof(rb_parser_ary_data) * len);
2474 for (i = ary->len; i < len; i++) {
2481 * Do not call this directly.
2482 * Use rb_parser_ary_new_capa_for_XXX() instead.
2484static rb_parser_ary_t *
2485parser_ary_new_capa(rb_parser_t *p, long len)
2488 rb_bug("negative array size (or size too big): %ld", len);
2490 rb_parser_ary_t *ary = xcalloc(1, sizeof(rb_parser_ary_t));
2495 ary->data = (rb_parser_ary_data *)xcalloc(len, sizeof(rb_parser_ary_data));
2504static rb_parser_ary_t *
2505rb_parser_ary_new_capa_for_script_line(rb_parser_t *p, long len)
2507 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2508 ary->data_type = PARSER_ARY_DATA_SCRIPT_LINE;
2512static rb_parser_ary_t *
2513rb_parser_ary_new_capa_for_ast_token(rb_parser_t *p, long len)
2515 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2516 ary->data_type = PARSER_ARY_DATA_AST_TOKEN;
2521static rb_parser_ary_t *
2522rb_parser_ary_new_capa_for_node(rb_parser_t *p, long len)
2524 rb_parser_ary_t *ary = parser_ary_new_capa(p, len);
2525 ary->data_type = PARSER_ARY_DATA_NODE;
2530 * Do not call this directly.
2531 * Use rb_parser_ary_push_XXX() instead.
2533static rb_parser_ary_t *
2534parser_ary_push(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ary_data val)
2536 if (ary->len == ary->capa) {
2537 rb_parser_ary_extend(p, ary, ary->len == 0 ? 1 : ary->len * 2);
2539 ary->data[ary->len++] = val;
2544static rb_parser_ary_t *
2545rb_parser_ary_push_ast_token(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_ast_token_t *val)
2547 if (ary->data_type != PARSER_ARY_DATA_AST_TOKEN) {
2548 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2550 return parser_ary_push(p, ary, val);
2553static rb_parser_ary_t *
2554rb_parser_ary_push_script_line(rb_parser_t *p, rb_parser_ary_t *ary, rb_parser_string_t *val)
2556 if (ary->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
2557 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2559 return parser_ary_push(p, ary, val);
2563static rb_parser_ary_t *
2564rb_parser_ary_push_node(rb_parser_t *p, rb_parser_ary_t *ary, NODE *val)
2566 if (ary->data_type != PARSER_ARY_DATA_NODE) {
2567 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2569 return parser_ary_push(p, ary, val);
2574rb_parser_ast_token_free(rb_parser_t *p, rb_parser_ast_token_t *token)
2577 rb_parser_string_free(p, token->str);
2582rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
2584# define foreach_ary(ptr) \
2585 for (rb_parser_ary_data *ptr = ary->data, *const end_ary_data = ptr + ary->len; \
2586 ptr < end_ary_data; ptr++)
2587 switch (ary->data_type) {
2588 case PARSER_ARY_DATA_AST_TOKEN:
2589 foreach_ary(data) {rb_parser_ast_token_free(p, *data);}
2591 case PARSER_ARY_DATA_SCRIPT_LINE:
2592 foreach_ary(data) {rb_parser_string_free(p, *data);}
2594 case PARSER_ARY_DATA_NODE:
2595 /* Do nothing because nodes are freed when rb_ast_t is freed */
2598 rb_bug("unexpected rb_parser_ary_data_type: %d", ary->data_type);
2611%define parse.error verbose
2613 if ((NODE *)$$ == (NODE *)-1) {
2614 rb_parser_printf(p, "NODE_SPECIAL");
2617 rb_parser_printf(p, "%s", parser_node_name(nd_type(RNODE($$))));
2619} <node> <node_fcall> <node_args> <node_args_aux> <node_opt_arg>
2620 <node_kw_arg> <node_block_pass> <node_masgn> <node_def_temp> <node_exits>
2622 rb_parser_printf(p, "%"PRIsVALUE, rb_id2str($$));
2625 switch (nd_type(RNODE($$))) {
2627 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_integer_literal_val($$));
2630 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_float_literal_val($$));
2633 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_rational_literal_val($$));
2635 case NODE_IMAGINARY:
2636 rb_parser_printf(p, "%+"PRIsVALUE, rb_node_imaginary_literal_val($$));
2641} tINTEGER tFLOAT tRATIONAL tIMAGINARY tSTRING_CONTENT tCHAR
2643 rb_parser_printf(p, "$%ld", RNODE_NTH_REF($$)->nd_nth);
2646 rb_parser_printf(p, "$%c", (int)RNODE_BACK_REF($$)->nd_nth);
2650 if (CASE_LABELS_ENABLED_P($$)) st_free_table($$);
2653%lex-param {struct parser_params *p}
2654%parse-param {struct parser_params *p}
2657 RUBY_SET_YYLLOC_OF_NONE(@$);
2659%after-shift after_shift
2660%before-reduce before_reduce
2661%after-reduce after_reduce
2662%after-shift-error-token after_shift_error_token
2663%after-pop-stack after_pop_stack
2667 rb_node_fcall_t *node_fcall;
2668 rb_node_args_t *node_args;
2669 rb_node_args_aux_t *node_args_aux;
2670 rb_node_opt_arg_t *node_opt_arg;
2671 rb_node_kw_arg_t *node_kw_arg;
2672 rb_node_block_pass_t *node_block_pass;
2673 rb_node_masgn_t *node_masgn;
2674 rb_node_def_temp_t *node_def_temp;
2675 rb_node_exits_t *node_exits;
2680 const struct vtable *vars;
2681 struct rb_strterm_struct *strterm;
2682 struct lex_context ctxt;
2683 enum lex_state_e state;
2687 keyword_class "'class'"
2688 keyword_module "'module'"
2690 keyword_undef "'undef'"
2691 keyword_begin "'begin'"
2692 keyword_rescue "'rescue'"
2693 keyword_ensure "'ensure'"
2696 keyword_unless "'unless'"
2697 keyword_then "'then'"
2698 keyword_elsif "'elsif'"
2699 keyword_else "'else'"
2700 keyword_case "'case'"
2701 keyword_when "'when'"
2702 keyword_while "'while'"
2703 keyword_until "'until'"
2705 keyword_break "'break'"
2706 keyword_next "'next'"
2707 keyword_redo "'redo'"
2708 keyword_retry "'retry'"
2711 keyword_do_cond "'do' for condition"
2712 keyword_do_block "'do' for block"
2713 keyword_do_LAMBDA "'do' for lambda"
2714 keyword_return "'return'"
2715 keyword_yield "'yield'"
2716 keyword_super "'super'"
2717 keyword_self "'self'"
2719 keyword_true "'true'"
2720 keyword_false "'false'"
2724 modifier_if "'if' modifier"
2725 modifier_unless "'unless' modifier"
2726 modifier_while "'while' modifier"
2727 modifier_until "'until' modifier"
2728 modifier_rescue "'rescue' modifier"
2729 keyword_alias "'alias'"
2730 keyword_defined "'defined?'"
2731 keyword_BEGIN "'BEGIN'"
2733 keyword__LINE__ "'__LINE__'"
2734 keyword__FILE__ "'__FILE__'"
2735 keyword__ENCODING__ "'__ENCODING__'"
2737%token <id> tIDENTIFIER "local variable or method"
2738%token <id> tFID "method"
2739%token <id> tGVAR "global variable"
2740%token <id> tIVAR "instance variable"
2741%token <id> tCONSTANT "constant"
2742%token <id> tCVAR "class variable"
2743%token <id> tLABEL "label"
2744%token <node> tINTEGER "integer literal"
2745%token <node> tFLOAT "float literal"
2746%token <node> tRATIONAL "rational literal"
2747%token <node> tIMAGINARY "imaginary literal"
2748%token <node> tCHAR "char literal"
2749%token <node> tNTH_REF "numbered reference"
2750%token <node> tBACK_REF "back reference"
2751%token <node> tSTRING_CONTENT "literal content"
2752%token <num> tREGEXP_END
2753%token <num> tDUMNY_END "dummy end"
2755%type <node> singleton strings string string1 xstring regexp
2756%type <node> string_contents xstring_contents regexp_contents string_content
2757%type <node> words symbols symbol_list qwords qsymbols word_list qword_list qsym_list word
2758%type <node> literal numeric simple_numeric ssym dsym symbol cpath
2759%type <node_def_temp> defn_head defs_head k_def
2760%type <node_exits> block_open k_while k_until k_for allow_exits
2761%type <node> top_compstmt top_stmts top_stmt begin_block endless_arg endless_command
2762%type <node> bodystmt compstmt stmts stmt_or_begin stmt expr arg primary command command_call method_call
2763%type <node> expr_value expr_value_do arg_value primary_value rel_expr
2764%type <node_fcall> fcall
2765%type <node> if_tail opt_else case_body case_args cases opt_rescue exc_list exc_var opt_ensure
2766%type <node> args arg_splat call_args opt_call_args
2767%type <node> paren_args opt_paren_args
2768%type <node_args> args_tail block_args_tail
2769%type <node> command_args aref_args
2770%type <node_block_pass> opt_block_arg block_arg
2771%type <node> var_ref var_lhs
2772%type <node> command_rhs arg_rhs
2773%type <node> command_asgn mrhs mrhs_arg superclass block_call block_command
2774%type <node_args> f_arglist f_opt_paren_args f_paren_args f_args
2775%type <node_args_aux> f_arg f_arg_item
2776%type <node> f_marg f_marg_list f_rest_marg
2777%type <node_masgn> f_margs
2778%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
2779%type <node_args> block_param opt_block_param block_param_def
2780%type <node_kw_arg> f_kw f_block_kw
2781%type <id> bv_decls opt_bv_decl bvar
2782%type <node> lambda lambda_body brace_body do_body
2783%type <node_args> f_larglist
2784%type <node> brace_block cmd_brace_block do_block lhs none fitem
2785%type <node> mlhs_head mlhs_item mlhs_node mlhs_post
2786%type <node_masgn> mlhs mlhs_basic mlhs_inner
2787%type <node> p_case_body p_cases p_top_expr p_top_expr_body
2788%type <node> p_expr p_as p_alt p_expr_basic p_find
2789%type <node> p_args p_args_head p_args_tail p_args_post p_arg p_rest
2790%type <node> p_value p_primitive p_variable p_var_ref p_expr_ref p_const
2791%type <node> p_kwargs p_kwarg p_kw
2792%type <id> keyword_variable user_variable sym operation operation2 operation3
2793%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
2794%type <id> f_kwrest f_label f_arg_asgn call_op call_op2 reswords relop dot_or_colon
2795%type <id> p_kwrest p_kwnorest p_any_kwrest p_kw_label
2796%type <id> f_no_kwarg f_any_kwrest args_forward excessed_comma nonlocal_var def_name
2797%type <ctxt> lex_ctxt begin_defined k_class k_module k_END k_rescue k_ensure after_rescue
2798%type <ctxt> p_in_kwarg
2799%type <tbl> p_lparen p_lbracket p_pktbl p_pvtbl
2800%type <num> max_numparam
2801%type <node> numparam
2803%token END_OF_INPUT 0 "end-of-input"
2806/* escaped chars, should be ignored otherwise */
2807%token <id> '\\' "backslash"
2808%token tSP "escaped space"
2809%token <id> '\t' "escaped horizontal tab"
2810%token <id> '\f' "escaped form feed"
2811%token <id> '\r' "escaped carriage return"
2812%token <id> '\13' "escaped vertical tab"
2813%token tUPLUS RUBY_TOKEN(UPLUS) "unary+"
2814%token tUMINUS RUBY_TOKEN(UMINUS) "unary-"
2815%token tPOW RUBY_TOKEN(POW) "**"
2816%token tCMP RUBY_TOKEN(CMP) "<=>"
2817%token tEQ RUBY_TOKEN(EQ) "=="
2818%token tEQQ RUBY_TOKEN(EQQ) "==="
2819%token tNEQ RUBY_TOKEN(NEQ) "!="
2820%token tGEQ RUBY_TOKEN(GEQ) ">="
2821%token tLEQ RUBY_TOKEN(LEQ) "<="
2822%token tANDOP RUBY_TOKEN(ANDOP) "&&"
2823%token tOROP RUBY_TOKEN(OROP) "||"
2824%token tMATCH RUBY_TOKEN(MATCH) "=~"
2825%token tNMATCH RUBY_TOKEN(NMATCH) "!~"
2826%token tDOT2 RUBY_TOKEN(DOT2) ".."
2827%token tDOT3 RUBY_TOKEN(DOT3) "..."
2828%token tBDOT2 RUBY_TOKEN(BDOT2) "(.."
2829%token tBDOT3 RUBY_TOKEN(BDOT3) "(..."
2830%token tAREF RUBY_TOKEN(AREF) "[]"
2831%token tASET RUBY_TOKEN(ASET) "[]="
2832%token tLSHFT RUBY_TOKEN(LSHFT) "<<"
2833%token tRSHFT RUBY_TOKEN(RSHFT) ">>"
2834%token <id> tANDDOT RUBY_TOKEN(ANDDOT) "&."
2835%token <id> tCOLON2 RUBY_TOKEN(COLON2) "::"
2836%token tCOLON3 ":: at EXPR_BEG"
2837%token <id> tOP_ASGN "operator-assignment" /* +=, -= etc. */
2840%token tLPAREN_ARG "( arg"
2844%token tLBRACE_ARG "{ arg"
2846%token tDSTAR "**arg"
2848%token <num> tLAMBDA "->"
2849%token tSYMBEG "symbol literal"
2850%token tSTRING_BEG "string literal"
2851%token tXSTRING_BEG "backtick literal"
2852%token tREGEXP_BEG "regexp literal"
2853%token tWORDS_BEG "word list"
2854%token tQWORDS_BEG "verbatim word list"
2855%token tSYMBOLS_BEG "symbol list"
2856%token tQSYMBOLS_BEG "verbatim symbol list"
2857%token tSTRING_END "terminator"
2858%token tSTRING_DEND "'}'"
2859%token <state> tSTRING_DBEG "'#{'"
2860%token tSTRING_DVAR tLAMBEG tLABEL_END
2862%token tIGNORED_NL tCOMMENT tEMBDOC_BEG tEMBDOC tEMBDOC_END
2863%token tHEREDOC_BEG tHEREDOC_END k__END__
2870%nonassoc tLBRACE_ARG
2872%nonassoc modifier_if modifier_unless modifier_while modifier_until keyword_in
2873%left keyword_or keyword_and
2875%nonassoc keyword_defined
2877%left modifier_rescue
2879%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3
2882%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
2883%left '>' tGEQ '<' tLEQ
2889%right tUMINUS_NUM tUMINUS
2891%right '!' '~' tUPLUS
2898%rule %inline ident_or_const
2903%rule %inline user_or_keyword_variable
2909 * parameterizing rules
2911%rule f_opt(value) <node_opt_arg>
2912 : f_arg_asgn f_eq value
2914 p->ctxt.in_argdef = 1;
2915 $$ = NEW_OPT_ARG(assignable(p, $1, $3, &@$), &@$);
2916 /*% ripper: [$:$, $:3] %*/
2920%rule f_optarg(value) <node_opt_arg>
2924 /*% ripper: rb_ary_new3(1, $:1) %*/
2926 | f_optarg(value) ',' f_opt(value)
2928 $$ = opt_arg_append($1, $3);
2929 /*% ripper: rb_ary_push($:1, $:3) %*/
2933%rule f_kwarg(kw) <node_kw_arg>
2937 /*% ripper: rb_ary_new3(1, $:1) %*/
2939 | f_kwarg(kw) ',' kw
2941 $$ = kwd_append($1, $3);
2942 /*% ripper: rb_ary_push($:1, $:3) %*/
2946%rule opt_args_tail(tail) <node_args>
2954 $$ = new_args_tail(p, 0, 0, 0, &@0);
2955 /*% ripper: [Qnil, Qnil, Qnil] %*/
2959%rule words(begin, word_list)
2960 : begin ' '+ word_list tSTRING_END
2962 $$ = make_list($3, &@$);
2963 /*% ripper: array!($:3) %*/
2969 SET_LEX_STATE(EXPR_BEG);
2970 local_push(p, ifndef_ripper(1)+0);
2971 /* jumps are possible in the top-level loop. */
2972 if (!ifndef_ripper(p->do_loop) + 0) init_block_exit(p);
2976 if ($2 && !compile_for_eval) {
2978 /* last expression should not be void */
2979 if (nd_type_p(node, NODE_BLOCK)) {
2980 while (RNODE_BLOCK(node)->nd_next) {
2981 node = RNODE_BLOCK(node)->nd_next;
2983 node = RNODE_BLOCK(node)->nd_head;
2985 node = remove_begin(node);
2988 p->eval_tree = NEW_SCOPE(0, block_append(p, p->eval_tree, $2), &@$);
2989 /*% ripper[final]: program!($:2) %*/
2994top_compstmt : top_stmts terms?
2996 void_stmts(p, $$ = $1);
3002 $$ = NEW_BEGIN(0, &@$);
3003 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3007 $$ = newline_node($1);
3008 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3010 | top_stmts terms top_stmt
3012 $$ = block_append(p, $1, newline_node($3));
3013 /*% ripper: stmts_add!($:1, $:3) %*/
3019 clear_block_exit(p, true);
3022 | keyword_BEGIN begin_block
3029block_open : '{' {$$ = init_block_exit(p);};
3031begin_block : block_open top_compstmt '}'
3033 restore_block_exit(p, $block_open);
3034 p->eval_tree_begin = block_append(p, p->eval_tree_begin,
3035 NEW_BEGIN($2, &@$));
3036 $$ = NEW_BEGIN(0, &@$);
3037 /*% ripper: BEGIN!($:2) %*/
3041bodystmt : compstmt[body]
3046 if (!$opt_rescue) yyerror1(&@k_else, "else without rescue is useless");
3047 next_rescue_context(&p->ctxt, &$ctxt, after_else);
3051 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3055 $$ = new_bodystmt(p, $body, $opt_rescue, $elsebody, $opt_ensure, &@$);
3056 /*% ripper: bodystmt!($:body, $:opt_rescue, $:elsebody, $:opt_ensure) %*/
3062 next_rescue_context(&p->ctxt, &$ctxt, after_ensure);
3066 $$ = new_bodystmt(p, $body, $opt_rescue, 0, $opt_ensure, &@$);
3067 /*% ripper: bodystmt!($:body, $:opt_rescue, Qnil, $:opt_ensure) %*/
3071compstmt : stmts terms?
3073 void_stmts(p, $$ = $1);
3079 $$ = NEW_BEGIN(0, &@$);
3080 /*% ripper: stmts_add!(stmts_new!, void_stmt!) %*/
3084 $$ = newline_node($1);
3085 /*% ripper: stmts_add!(stmts_new!, $:1) %*/
3087 | stmts terms stmt_or_begin
3089 $$ = block_append(p, $1, newline_node($3));
3090 /*% ripper: stmts_add!($:1, $:3) %*/
3100 yyerror1(&@1, "BEGIN is permitted only at toplevel");
3108allow_exits : {$$ = allow_block_exit(p);};
3110k_END : keyword_END lex_ctxt
3113 p->ctxt.in_rescue = before_rescue;
3117stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3119 $$ = NEW_ALIAS($2, $4, &@$, &@1);
3120 /*% ripper: alias!($:2, $:4) %*/
3122 | keyword_alias tGVAR tGVAR
3124 $$ = NEW_VALIAS($2, $3, &@$, &@1);
3125 /*% ripper: var_alias!($:2, $:3) %*/
3127 | keyword_alias tGVAR tBACK_REF
3131 buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
3132 $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
3133 /*% ripper: var_alias!($:2, $:3) %*/
3135 | keyword_alias tGVAR tNTH_REF
3137 static const char mesg[] = "can't make alias for the number variables";
3139 yyerror1(&@3, mesg);
3141 $$ = NEW_ERROR(&@$);
3142 /*% ripper[error]: alias_error!(ERR_MESG(), $:3) %*/
3144 | keyword_undef undef_list
3146 nd_set_first_loc($2, @1.beg_pos);
3147 RNODE_UNDEF($2)->keyword_loc = @1;
3149 /*% ripper: undef!($:2) %*/
3151 | stmt modifier_if expr_value
3153 $$ = new_if(p, $3, remove_begin($1), 0, &@$);
3155 /*% ripper: if_mod!($:3, $:1) %*/
3157 | stmt modifier_unless expr_value
3159 $$ = new_unless(p, $3, remove_begin($1), 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
3161 /*% ripper: unless_mod!($:3, $:1) %*/
3163 | stmt modifier_while expr_value
3165 clear_block_exit(p, false);
3166 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3167 $$ = NEW_WHILE(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3170 $$ = NEW_WHILE(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3172 /*% ripper: while_mod!($:3, $:1) %*/
3174 | stmt modifier_until expr_value
3176 clear_block_exit(p, false);
3177 if ($1 && nd_type_p($1, NODE_BEGIN)) {
3178 $$ = NEW_UNTIL(cond(p, $3, &@3), RNODE_BEGIN($1)->nd_body, 0, &@$, &@2, &NULL_LOC);
3181 $$ = NEW_UNTIL(cond(p, $3, &@3), $1, 1, &@$, &@2, &NULL_LOC);
3183 /*% ripper: until_mod!($:3, $:1) %*/
3185 | stmt modifier_rescue after_rescue stmt
3187 p->ctxt.in_rescue = $3.in_rescue;
3189 YYLTYPE loc = code_loc_gen(&@2, &@4);
3190 resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
3191 $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
3192 /*% ripper: rescue_mod!($:1, $:4) %*/
3194 | k_END allow_exits '{' compstmt '}'
3196 if (p->ctxt.in_def) {
3197 rb_warn0("END in method; use at_exit");
3199 restore_block_exit(p, $allow_exits);
3202 NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3203 $$ = NEW_POSTEXE(scope, &@$);
3205 /*% ripper: END!($:compstmt) %*/
3208 | mlhs '=' lex_ctxt command_call
3211 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3212 /*% ripper: massign!($:1, $:4) %*/
3214 | lhs '=' lex_ctxt mrhs
3216 $$ = node_assign(p, $1, $4, $3, &@$);
3217 /*% ripper: assign!($:1, $:4) %*/
3219 | mlhs '=' lex_ctxt mrhs_arg modifier_rescue
3220 after_rescue stmt[resbody]
3222 p->ctxt.in_rescue = $3.in_rescue;
3223 YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3224 $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
3225 loc.beg_pos = @mrhs_arg.beg_pos;
3226 $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
3227 $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
3228 /*% ripper: massign!($:1, rescue_mod!($:4, $:7)) %*/
3230 | mlhs '=' lex_ctxt mrhs_arg
3232 $$ = node_assign(p, (NODE *)$1, $4, $3, &@$);
3233 /*% ripper: massign!($:1, $:4) %*/
3239 $$ = NEW_ERROR(&@$);
3243command_asgn : lhs '=' lex_ctxt command_rhs
3245 $$ = node_assign(p, $1, $4, $3, &@$);
3246 /*% ripper: assign!($:1, $:4) %*/
3248 | var_lhs tOP_ASGN lex_ctxt command_rhs
3250 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3251 /*% ripper: opassign!($:1, $:2, $:4) %*/
3253 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt command_rhs
3255 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3256 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3259 | primary_value call_op ident_or_const tOP_ASGN lex_ctxt command_rhs
3261 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3262 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3264 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt command_rhs
3266 YYLTYPE loc = code_loc_gen(&@1, &@3);
3267 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3268 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3270 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt command_rhs
3272 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3273 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3275 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt command_rhs
3277 YYLTYPE loc = code_loc_gen(&@1, &@2);
3278 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3279 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3281 | defn_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3283 endless_method_name(p, $head->nd_mid, &@head);
3284 restore_defun(p, $head);
3285 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3286 ($$ = $head->nd_def)->nd_loc = @$;
3287 RNODE_DEFN($$)->nd_defn = $bodystmt;
3288 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3289 /*% ripper: def!($:head, $:args, $:$) %*/
3292 | defs_head[head] f_opt_paren_args[args] '=' endless_command[bodystmt]
3294 endless_method_name(p, $head->nd_mid, &@head);
3295 restore_defun(p, $head);
3296 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
3297 ($$ = $head->nd_def)->nd_loc = @$;
3298 RNODE_DEFS($$)->nd_defn = $bodystmt;
3299 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
3300 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
3303 | backref tOP_ASGN lex_ctxt command_rhs
3305 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3306 $$ = NEW_ERROR(&@$);
3307 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3311endless_command : command
3312 | endless_command modifier_rescue after_rescue arg
3314 p->ctxt.in_rescue = $3.in_rescue;
3315 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
3316 /*% ripper: rescue_mod!($:1, $:4) %*/
3318 | keyword_not '\n'? endless_command
3320 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3321 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3325command_rhs : command_call %prec tOP_ASGN
3330 | command_call modifier_rescue after_rescue stmt
3332 p->ctxt.in_rescue = $3.in_rescue;
3333 YYLTYPE loc = code_loc_gen(&@2, &@4);
3335 $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
3336 /*% ripper: rescue_mod!($:1, $:4) %*/
3342 | expr keyword_and expr
3344 $$ = logop(p, idAND, $1, $3, &@2, &@$);
3345 /*% ripper: binary!($:1, ID2VAL(idAND), $:3) %*/
3347 | expr keyword_or expr
3349 $$ = logop(p, idOR, $1, $3, &@2, &@$);
3350 /*% ripper: binary!($:1, ID2VAL(idOR), $:3) %*/
3352 | keyword_not '\n'? expr
3354 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
3355 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
3359 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
3360 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
3366 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3367 p_top_expr_body[body]
3369 pop_pktbl(p, $p_pktbl);
3370 pop_pvtbl(p, $p_pvtbl);
3371 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3372 $$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3373 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3379 p_in_kwarg[ctxt] p_pvtbl p_pktbl
3380 p_top_expr_body[body]
3382 pop_pktbl(p, $p_pktbl);
3383 pop_pvtbl(p, $p_pvtbl);
3384 p->ctxt.in_kwarg = $ctxt.in_kwarg;
3385 $$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3386 /*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
3388 | arg %prec tLBRACE_ARG
3394 numparam_name(p, fname);
3397 p->ctxt.in_rescue = before_rescue;
3398 p->ctxt.cant_return = 0;
3403defn_head : k_def def_name
3405 $$ = def_head_save(p, $k_def);
3406 $$->nd_mid = $def_name;
3407 $$->nd_def = NEW_DEFN($def_name, 0, &@$);
3408 /*% ripper: $:def_name %*/
3412defs_head : k_def singleton dot_or_colon
3414 SET_LEX_STATE(EXPR_FNAME);
3418 SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */
3419 $$ = def_head_save(p, $k_def);
3420 $$->nd_mid = $def_name;
3421 $$->nd_def = NEW_DEFS($singleton, $def_name, 0, &@$);
3422 /*% ripper: [$:singleton, $:dot_or_colon, $:def_name] %*/
3433 $$ = NEW_ERROR(&@$);
3437expr_value_do : {COND_PUSH(1);} expr_value do {COND_POP();}
3444command_call : command
3448block_command : block_call
3449 | block_call call_op2 operation2 command_args
3451 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
3452 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
3456cmd_brace_block : tLBRACE_ARG brace_body '}'
3459 set_embraced_location($$, &@1, &@3);
3466 $$ = NEW_FCALL($1, 0, &@$);
3471command : fcall command_args %prec tLOWEST
3474 nd_set_last_loc($1, @2.end_pos);
3476 /*% ripper: command!($:1, $:2) %*/
3478 | fcall command_args cmd_brace_block
3480 block_dup_check(p, $2, $3);
3482 $$ = method_add_block(p, (NODE *)$1, $3, &@$);
3483 fixpos($$, RNODE($1));
3484 nd_set_last_loc($1, @2.end_pos);
3485 /*% ripper: method_add_block!(command!($:1, $:2), $:3) %*/
3487 | primary_value call_op operation2 command_args %prec tLOWEST
3489 $$ = new_command_qcall(p, $2, $1, $3, $4, 0, &@3, &@$);
3490 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3492 | primary_value call_op operation2 command_args cmd_brace_block
3494 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
3495 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3497 | primary_value tCOLON2 operation2 command_args %prec tLOWEST
3499 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, 0, &@3, &@$);
3500 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
3502 | primary_value tCOLON2 operation2 command_args cmd_brace_block
3504 $$ = new_command_qcall(p, idCOLON2, $1, $3, $4, $5, &@3, &@$);
3505 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
3507 | primary_value tCOLON2 tCONSTANT '{' brace_body '}'
3509 set_embraced_location($5, &@4, &@6);
3510 $$ = new_command_qcall(p, idCOLON2, $1, $3, 0, $5, &@3, &@$);
3511 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, Qnil), $:5) %*/
3513 | keyword_super command_args
3515 $$ = NEW_SUPER($2, &@$);
3517 /*% ripper: super!($:2) %*/
3519 | k_yield command_args
3521 $$ = new_yield(p, $2, &@$);
3523 /*% ripper: yield!($:2) %*/
3525 | k_return call_args
3527 $$ = NEW_RETURN(ret_args(p, $2), &@$, &@1);
3528 /*% ripper: return!($:2) %*/
3530 | keyword_break call_args
3533 args = ret_args(p, $2);
3534 $$ = add_block_exit(p, NEW_BREAK(args, &@$, &@1));
3535 /*% ripper: break!($:2) %*/
3537 | keyword_next call_args
3540 args = ret_args(p, $2);
3541 $$ = add_block_exit(p, NEW_NEXT(args, &@$, &@1));
3542 /*% ripper: next!($:2) %*/
3547 | tLPAREN mlhs_inner rparen
3550 /*% ripper: mlhs_paren!($:2) %*/
3554mlhs_inner : mlhs_basic
3555 | tLPAREN mlhs_inner rparen
3557 $$ = NEW_MASGN(NEW_LIST((NODE *)$2, &@$), 0, &@$);
3558 /*% ripper: mlhs_paren!($:2) %*/
3562mlhs_basic : mlhs_head
3564 $$ = NEW_MASGN($1, 0, &@$);
3567 | mlhs_head mlhs_item
3569 $$ = NEW_MASGN(list_append(p, $1, $2), 0, &@$);
3570 /*% ripper: mlhs_add!($:1, $:2) %*/
3572 | mlhs_head tSTAR mlhs_node
3574 $$ = NEW_MASGN($1, $3, &@$);
3575 /*% ripper: mlhs_add_star!($:1, $:3) %*/
3577 | mlhs_head tSTAR mlhs_node ',' mlhs_post
3579 $$ = NEW_MASGN($1, NEW_POSTARG($3,$5,&@$), &@$);
3580 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
3584 $$ = NEW_MASGN($1, NODE_SPECIAL_NO_NAME_REST, &@$);
3585 /*% ripper: mlhs_add_star!($:1, Qnil) %*/
3587 | mlhs_head tSTAR ',' mlhs_post
3589 $$ = NEW_MASGN($1, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $4, &@$), &@$);
3590 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, Qnil), $:4) %*/
3594 $$ = NEW_MASGN(0, $2, &@$);
3595 /*% ripper: mlhs_add_star!(mlhs_new!, $:2) %*/
3597 | tSTAR mlhs_node ',' mlhs_post
3599 $$ = NEW_MASGN(0, NEW_POSTARG($2,$4,&@$), &@$);
3600 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:2), $:4) %*/
3604 $$ = NEW_MASGN(0, NODE_SPECIAL_NO_NAME_REST, &@$);
3605 /*% ripper: mlhs_add_star!(mlhs_new!, Qnil) %*/
3607 | tSTAR ',' mlhs_post
3609 $$ = NEW_MASGN(0, NEW_POSTARG(NODE_SPECIAL_NO_NAME_REST, $3, &@$), &@$);
3610 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, Qnil), $:3) %*/
3614mlhs_item : mlhs_node
3615 | tLPAREN mlhs_inner rparen
3618 /*% ripper: mlhs_paren!($:2) %*/
3622mlhs_head : mlhs_item ','
3624 $$ = NEW_LIST($1, &@1);
3625 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3627 | mlhs_head mlhs_item ','
3629 $$ = list_append(p, $1, $2);
3630 /*% ripper: mlhs_add!($:1, $:2) %*/
3634mlhs_post : mlhs_item
3636 $$ = NEW_LIST($1, &@$);
3637 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
3639 | mlhs_post ',' mlhs_item
3641 $$ = list_append(p, $1, $3);
3642 /*% ripper: mlhs_add!($:1, $:3) %*/
3646mlhs_node : user_or_keyword_variable
3648 /*% ripper: var_field!($:1) %*/
3649 $$ = assignable(p, $1, 0, &@$);
3651 | primary_value '[' opt_call_args rbracket
3653 $$ = aryset(p, $1, $3, &@$);
3654 /*% ripper: aref_field!($:1, $:3) %*/
3656 | primary_value call_op tIDENTIFIER
3658 anddot_multiple_assignment_check(p, &@2, $2);
3659 $$ = attrset(p, $1, $2, $3, &@$);
3660 /*% ripper: field!($:1, $:2, $:3) %*/
3662 | primary_value tCOLON2 tIDENTIFIER
3664 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3665 /*% ripper: const_path_field!($:1, $:3) %*/
3667 | primary_value call_op tCONSTANT
3669 anddot_multiple_assignment_check(p, &@2, $2);
3670 $$ = attrset(p, $1, $2, $3, &@$);
3671 /*% ripper: field!($:1, $:2, $:3) %*/
3673 | primary_value tCOLON2 tCONSTANT
3675 /*% ripper: const_path_field!($:1, $:3) %*/
3676 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3680 /*% ripper: top_const_field!($:2) %*/
3681 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3685 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3686 $$ = NEW_ERROR(&@$);
3687 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3691lhs : user_or_keyword_variable
3693 /*% ripper: var_field!($:1) %*/
3694 $$ = assignable(p, $1, 0, &@$);
3696 | primary_value '[' opt_call_args rbracket
3698 $$ = aryset(p, $1, $3, &@$);
3699 /*% ripper: aref_field!($:1, $:3) %*/
3701 | primary_value call_op tIDENTIFIER
3703 $$ = attrset(p, $1, $2, $3, &@$);
3704 /*% ripper: field!($:1, $:2, $:3) %*/
3706 | primary_value tCOLON2 tIDENTIFIER
3708 $$ = attrset(p, $1, idCOLON2, $3, &@$);
3709 /*% ripper: field!($:1, $:2, $:3) %*/
3711 | primary_value call_op tCONSTANT
3713 $$ = attrset(p, $1, $2, $3, &@$);
3714 /*% ripper: field!($:1, $:2, $:3) %*/
3716 | primary_value tCOLON2 tCONSTANT
3718 /*% ripper: const_path_field!($:1, $:3) %*/
3719 $$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3723 /*% ripper: top_const_field!($:2) %*/
3724 $$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3728 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3729 $$ = NEW_ERROR(&@$);
3730 /*% ripper[error]: assign_error!(?e, var_field!($:1)) %*/
3736 static const char mesg[] = "class/module name must be CONSTANT";
3738 yyerror1(&@1, mesg);
3740 /*% ripper[error]: class_name_error!(ERR_MESG(), $:1) %*/
3745cpath : tCOLON3 cname
3747 $$ = NEW_COLON3($2, &@$);
3748 /*% ripper: top_const_ref!($:2) %*/
3752 $$ = NEW_COLON2(0, $1, &@$);
3753 /*% ripper: const_ref!($:1) %*/
3755 | primary_value tCOLON2 cname
3757 $$ = NEW_COLON2($1, $3, &@$);
3758 /*% ripper: const_path_ref!($:1, $:3) %*/
3762fname : ident_or_const
3766 SET_LEX_STATE(EXPR_ENDFN);
3774 $$ = NEW_SYM(rb_id2str($1), &@$);
3775 /*% ripper: symbol_literal!($:1) %*/
3782 $$ = NEW_UNDEF($1, &@$);
3783 /*% ripper: rb_ary_new3(1, $:1) %*/
3785 | undef_list ',' {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
3787 nd_set_last_loc($1, @4.end_pos);
3788 rb_parser_ary_push_node(p, RNODE_UNDEF($1)->nd_undefs, $4);
3789 /*% ripper: rb_ary_push($:1, $:4) %*/
3793op : '|' { $$ = '|'; }
3796 | tCMP { $$ = tCMP; }
3798 | tEQQ { $$ = tEQQ; }
3799 | tMATCH { $$ = tMATCH; }
3800 | tNMATCH { $$ = tNMATCH; }
3802 | tGEQ { $$ = tGEQ; }
3804 | tLEQ { $$ = tLEQ; }
3805 | tNEQ { $$ = tNEQ; }
3806 | tLSHFT { $$ = tLSHFT; }
3807 | tRSHFT { $$ = tRSHFT; }
3811 | tSTAR { $$ = '*'; }
3814 | tPOW { $$ = tPOW; }
3815 | tDSTAR { $$ = tDSTAR; }
3818 | tUPLUS { $$ = tUPLUS; }
3819 | tUMINUS { $$ = tUMINUS; }
3820 | tAREF { $$ = tAREF; }
3821 | tASET { $$ = tASET; }
3825reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
3826 | keyword_BEGIN | keyword_END
3827 | keyword_alias | keyword_and | keyword_begin
3828 | keyword_break | keyword_case | keyword_class | keyword_def
3829 | keyword_defined | keyword_do | keyword_else | keyword_elsif
3830 | keyword_end | keyword_ensure | keyword_false
3831 | keyword_for | keyword_in | keyword_module | keyword_next
3832 | keyword_nil | keyword_not | keyword_or | keyword_redo
3833 | keyword_rescue | keyword_retry | keyword_return | keyword_self
3834 | keyword_super | keyword_then | keyword_true | keyword_undef
3835 | keyword_when | keyword_yield | keyword_if | keyword_unless
3836 | keyword_while | keyword_until
3839arg : lhs '=' lex_ctxt arg_rhs
3841 $$ = node_assign(p, $1, $4, $3, &@$);
3842 /*% ripper: assign!($:1, $:4) %*/
3844 | var_lhs tOP_ASGN lex_ctxt arg_rhs
3846 $$ = new_op_assign(p, $1, $2, $4, $3, &@$);
3847 /*% ripper: opassign!($:1, $:2, $:4) %*/
3849 | primary_value '[' opt_call_args rbracket tOP_ASGN lex_ctxt arg_rhs
3851 $$ = new_ary_op_assign(p, $1, $3, $5, $7, &@3, &@$, &NULL_LOC, &@2, &@4, &@5);
3852 /*% ripper: opassign!(aref_field!($:1, $:3), $:5, $:7) %*/
3854 | primary_value call_op tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3856 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3857 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3859 | primary_value call_op tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3861 $$ = new_attr_op_assign(p, $1, $2, $3, $4, $6, &@$, &@2, &@3, &@4);
3862 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3864 | primary_value tCOLON2 tIDENTIFIER tOP_ASGN lex_ctxt arg_rhs
3866 $$ = new_attr_op_assign(p, $1, idCOLON2, $3, $4, $6, &@$, &@2, &@3, &@4);
3867 /*% ripper: opassign!(field!($:1, $:2, $:3), $:4, $:6) %*/
3869 | primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3871 YYLTYPE loc = code_loc_gen(&@1, &@3);
3872 $$ = new_const_op_assign(p, NEW_COLON2($1, $3, &loc), $4, $6, $5, &@$);
3873 /*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
3875 | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt arg_rhs
3877 YYLTYPE loc = code_loc_gen(&@1, &@2);
3878 $$ = new_const_op_assign(p, NEW_COLON3($2, &loc), $3, $5, $4, &@$);
3879 /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
3881 | backref tOP_ASGN lex_ctxt arg_rhs
3883 VALUE MAYBE_UNUSED(e) = rb_backref_error(p, $1);
3884 $$ = NEW_ERROR(&@$);
3885 /*% ripper[error]: assign_error!(?e, opassign!(var_field!($:1), $:2, $:4)) %*/
3891 $$ = NEW_DOT2($1, $3, &@$);
3892 /*% ripper: dot2!($:1, $:3) %*/
3898 $$ = NEW_DOT3($1, $3, &@$);
3899 /*% ripper: dot3!($:1, $:3) %*/
3904 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
3905 /*% ripper: dot2!($:1, Qnil) %*/
3910 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
3911 /*% ripper: dot3!($:1, Qnil) %*/
3916 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
3917 /*% ripper: dot2!(Qnil, $:2) %*/
3922 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
3923 /*% ripper: dot3!(Qnil, $:2) %*/
3927 $$ = call_bin_op(p, $1, '+', $3, &@2, &@$);
3928 /*% ripper: binary!($:1, ID2VAL('\'+\''), $:3) %*/
3932 $$ = call_bin_op(p, $1, '-', $3, &@2, &@$);
3933 /*% ripper: binary!($:1, ID2VAL('\'-\''), $:3) %*/
3937 $$ = call_bin_op(p, $1, '*', $3, &@2, &@$);
3938 /*% ripper: binary!($:1, ID2VAL('\'*\''), $:3) %*/
3942 $$ = call_bin_op(p, $1, '/', $3, &@2, &@$);
3943 /*% ripper: binary!($:1, ID2VAL('\'/\''), $:3) %*/
3947 $$ = call_bin_op(p, $1, '%', $3, &@2, &@$);
3948 /*% ripper: binary!($:1, ID2VAL('\'%\''), $:3) %*/
3952 $$ = call_bin_op(p, $1, idPow, $3, &@2, &@$);
3953 /*% ripper: binary!($:1, ID2VAL(idPow), $:3) %*/
3955 | tUMINUS_NUM simple_numeric tPOW arg
3957 $$ = call_uni_op(p, call_bin_op(p, $2, idPow, $4, &@2, &@$), idUMinus, &@1, &@$);
3958 /*% ripper: unary!(ID2VAL(idUMinus), binary!($:2, ID2VAL(idPow), $:4)) %*/
3962 $$ = call_uni_op(p, $2, idUPlus, &@1, &@$);
3963 /*% ripper: unary!(ID2VAL(idUPlus), $:2) %*/
3967 $$ = call_uni_op(p, $2, idUMinus, &@1, &@$);
3968 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
3972 $$ = call_bin_op(p, $1, '|', $3, &@2, &@$);
3973 /*% ripper: binary!($:1, ID2VAL('\'|\''), $:3) %*/
3977 $$ = call_bin_op(p, $1, '^', $3, &@2, &@$);
3978 /*% ripper: binary!($:1, ID2VAL('\'^\''), $:3) %*/
3982 $$ = call_bin_op(p, $1, '&', $3, &@2, &@$);
3983 /*% ripper: binary!($:1, ID2VAL('\'&\''), $:3) %*/
3987 $$ = call_bin_op(p, $1, idCmp, $3, &@2, &@$);
3988 /*% ripper: binary!($:1, ID2VAL(idCmp), $:3) %*/
3990 | rel_expr %prec tCMP
3993 $$ = call_bin_op(p, $1, idEq, $3, &@2, &@$);
3994 /*% ripper: binary!($:1, ID2VAL(idEq), $:3) %*/
3998 $$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
3999 /*% ripper: binary!($:1, ID2VAL(idEqq), $:3) %*/
4003 $$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
4004 /*% ripper: binary!($:1, ID2VAL(idNeq), $:3) %*/
4008 $$ = match_op(p, $1, $3, &@2, &@$);
4009 /*% ripper: binary!($:1, ID2VAL(idEqTilde), $:3) %*/
4013 $$ = call_bin_op(p, $1, idNeqTilde, $3, &@2, &@$);
4014 /*% ripper: binary!($:1, ID2VAL(idNeqTilde), $:3) %*/
4018 $$ = call_uni_op(p, method_cond(p, $2, &@2), '!', &@1, &@$);
4019 /*% ripper: unary!(ID2VAL('\'!\''), $:2) %*/
4023 $$ = call_uni_op(p, $2, '~', &@1, &@$);
4024 /*% ripper: unary!(ID2VAL('\'~\''), $:2) %*/
4028 $$ = call_bin_op(p, $1, idLTLT, $3, &@2, &@$);
4029 /*% ripper: binary!($:1, ID2VAL(idLTLT), $:3) %*/
4033 $$ = call_bin_op(p, $1, idGTGT, $3, &@2, &@$);
4034 /*% ripper: binary!($:1, ID2VAL(idGTGT), $:3) %*/
4038 $$ = logop(p, idANDOP, $1, $3, &@2, &@$);
4039 /*% ripper: binary!($:1, ID2VAL(idANDOP), $:3) %*/
4043 $$ = logop(p, idOROP, $1, $3, &@2, &@$);
4044 /*% ripper: binary!($:1, ID2VAL(idOROP), $:3) %*/
4046 | keyword_defined '\n'? begin_defined arg
4048 p->ctxt.in_defined = $3.in_defined;
4049 $$ = new_defined(p, $4, &@$);
4050 /*% ripper: defined!($:4) %*/
4052 | arg '?' arg '\n'? ':' arg
4055 $$ = new_if(p, $1, $3, $6, &@$);
4057 /*% ripper: ifop!($:1, $:3, $:6) %*/
4059 | defn_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4061 endless_method_name(p, $head->nd_mid, &@head);
4062 restore_defun(p, $head);
4063 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4064 ($$ = $head->nd_def)->nd_loc = @$;
4065 RNODE_DEFN($$)->nd_defn = $bodystmt;
4066 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4067 /*% ripper: def!($:head, $:args, $:$) %*/
4070 | defs_head[head] f_opt_paren_args[args] '=' endless_arg[bodystmt]
4072 endless_method_name(p, $head->nd_mid, &@head);
4073 restore_defun(p, $head);
4074 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4075 ($$ = $head->nd_def)->nd_loc = @$;
4076 RNODE_DEFS($$)->nd_defn = $bodystmt;
4077 /*% ripper: bodystmt!($:bodystmt, Qnil, Qnil, Qnil) %*/
4078 /*% ripper: defs!(*$:head[0..2], $:args, $:$) %*/
4087endless_arg : arg %prec modifier_rescue
4088 | endless_arg modifier_rescue after_rescue arg
4090 p->ctxt.in_rescue = $3.in_rescue;
4091 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4092 /*% ripper: rescue_mod!($:1, $:4) %*/
4094 | keyword_not '\n'? endless_arg
4096 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4097 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4101relop : '>' {$$ = '>';}
4107rel_expr : arg relop arg %prec '>'
4109 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4110 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4112 | rel_expr relop arg %prec '>'
4114 rb_warning1("comparison '%s' after comparison", WARN_ID($2));
4115 $$ = call_bin_op(p, $1, $2, $3, &@2, &@$);
4116 /*% ripper: binary!($:1, ID2VAL($2), $:3) %*/
4126begin_defined : lex_ctxt
4128 p->ctxt.in_defined = 1;
4133after_rescue : lex_ctxt
4135 p->ctxt.in_rescue = after_rescue;
4152 | args ',' assocs trailer
4154 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4155 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4159 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@$) : 0;
4160 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4164arg_rhs : arg %prec tOP_ASGN
4169 | arg modifier_rescue after_rescue arg
4171 p->ctxt.in_rescue = $3.in_rescue;
4173 $$ = rescued_expr(p, $1, $4, &@1, &@2, &@4);
4174 /*% ripper: rescue_mod!($:1, $:4) %*/
4178paren_args : '(' opt_call_args rparen
4181 /*% ripper: arg_paren!($:2) %*/
4183 | '(' args ',' args_forward rparen
4185 if (!check_forwarding_args(p)) {
4189 $$ = new_args_forward_call(p, $2, &@4, &@$);
4190 /*% ripper: arg_paren!(args_add!($:2, $:4)) %*/
4193 | '(' args_forward rparen
4195 if (!check_forwarding_args(p)) {
4199 $$ = new_args_forward_call(p, 0, &@2, &@$);
4200 /*% ripper: arg_paren!($:2) %*/
4205opt_paren_args : none
4208 $$ = $1 ? $1 : NODE_SPECIAL_EMPTY_ARGS;
4218 | args ',' assocs ','
4220 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4221 /*% ripper: args_add!($:1, bare_assoc_hash!($:3)) %*/
4225 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4226 /*% ripper: args_add!(args_new!, bare_assoc_hash!($:1)) %*/
4233 $$ = NEW_LIST($1, &@$);
4234 /*% ripper: args_add!(args_new!, $:1) %*/
4236 | args opt_block_arg
4238 $$ = arg_blk_pass($1, $2);
4239 /*% ripper: args_add_block!($:1, $:2) %*/
4241 | assocs opt_block_arg
4243 $$ = $1 ? NEW_LIST(new_hash(p, $1, &@1), &@1) : 0;
4244 $$ = arg_blk_pass($$, $2);
4245 /*% ripper: args_add_block!(args_add!(args_new!, bare_assoc_hash!($:1)), $:2) %*/
4247 | args ',' assocs opt_block_arg
4249 $$ = $3 ? arg_append(p, $1, new_hash(p, $3, &@3), &@$) : $1;
4250 $$ = arg_blk_pass($$, $4);
4251 /*% ripper: args_add_block!(args_add!($:1, bare_assoc_hash!($:3)), $:4) %*/
4254 /*% ripper: args_add_block!(args_new!, $:1) %*/
4258 /* If call_args starts with a open paren '(' or '[',
4259 * look-ahead reading of the letters calls CMDARG_PUSH(0),
4260 * but the push must be done after CMDARG_PUSH(1).
4261 * So this code makes them consistent by first cancelling
4262 * the premature CMDARG_PUSH(0), doing CMDARG_PUSH(1),
4263 * and finally redoing CMDARG_PUSH(0).
4267 case '(': case tLPAREN: case tLPAREN_ARG: case '[': case tLBRACK:
4270 if (lookahead) CMDARG_POP();
4272 if (lookahead) CMDARG_PUSH(0);
4276 /* call_args can be followed by tLBRACE_ARG (that does CMDARG_PUSH(0) in the lexer)
4277 * but the push must be done after CMDARG_POP() in the parser.
4278 * So this code does CMDARG_POP() to pop 0 pushed by tLBRACE_ARG,
4279 * CMDARG_POP() to pop 1 pushed by command_args,
4280 * and CMDARG_PUSH(0) to restore back the flag set by tLBRACE_ARG.
4287 if (lookahead) CMDARG_POP();
4289 if (lookahead) CMDARG_PUSH(0);
4295block_arg : tAMPER arg_value
4297 $$ = NEW_BLOCK_PASS($2, &@$, &@1);
4302 forwarding_arg_check(p, idFWD_BLOCK, idFWD_ALL, "block");
4303 $$ = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, &@1), &@$, &@1);
4304 /*% ripper: Qnil %*/
4308opt_block_arg : ',' block_arg
4316 /*% ripper: Qfalse %*/
4323 $$ = NEW_LIST($arg_value, &@$);
4324 /*% ripper: args_add!(args_new!, $:arg_value) %*/
4329 /*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
4331 | args[non_last_args] ',' arg_value
4333 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
4334 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
4336 | args[non_last_args] ',' arg_splat
4338 $$ = rest_arg_append(p, $non_last_args, RNODE_SPLAT($arg_splat)->nd_head, &@$);
4339 /*% ripper: args_add_star!($:non_last_args, $:arg_splat) %*/
4344arg_splat : tSTAR arg_value
4346 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4347 /*% ripper: $:arg_value %*/
4351 forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
4352 $$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@tSTAR), &@$, &@tSTAR);
4353 /*% ripper: Qnil %*/
4363mrhs : args ',' arg_value
4365 $$ = last_arg_append(p, $args, $arg_value, &@$);
4366 /*% ripper: mrhs_add!(mrhs_new_from_args!($:args), $:arg_value) %*/
4368 | args ',' tSTAR arg_value
4370 $$ = rest_arg_append(p, $args, $arg_value, &@$);
4371 /*% ripper: mrhs_add_star!(mrhs_new_from_args!($:args), $:arg_value) %*/
4375 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
4376 /*% ripper: mrhs_add_star!(mrhs_new!, $:arg_value) %*/
4380%rule %inline inline_primary
4391primary : inline_primary
4396 $$ = (NODE *)NEW_FCALL($1, 0, &@$);
4397 /*% ripper: method_add_arg!(fcall!($:1), args_new!) %*/
4407 set_line_body($3, @1.end_pos.lineno);
4408 $$ = NEW_BEGIN($3, &@$);
4409 nd_set_line($$, @1.end_pos.lineno);
4410 /*% ripper: begin!($:3) %*/
4412 | tLPAREN_ARG compstmt {SET_LEX_STATE(EXPR_ENDARG);} ')'
4414 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4416 /*% ripper: paren!($:2) %*/
4418 | tLPAREN compstmt ')'
4420 if (nd_type_p($2, NODE_SELF)) RNODE_SELF($2)->nd_state = 0;
4421 $$ = NEW_BLOCK($2, &@$);
4422 /*% ripper: paren!($:2) %*/
4424 | primary_value tCOLON2 tCONSTANT
4426 $$ = NEW_COLON2($1, $3, &@$);
4427 /*% ripper: const_path_ref!($:1, $:3) %*/
4431 $$ = NEW_COLON3($2, &@$);
4432 /*% ripper: top_const_ref!($:2) %*/
4434 | tLBRACK aref_args ']'
4436 $$ = make_list($2, &@$);
4437 /*% ripper: array!($:2) %*/
4439 | tLBRACE assoc_list '}'
4441 $$ = new_hash(p, $2, &@$);
4442 RNODE_HASH($$)->nd_brace = TRUE;
4443 /*% ripper: hash!($:2) %*/
4447 $$ = NEW_RETURN(0, &@$, &@1);
4448 /*% ripper: return0! %*/
4450 | k_yield '(' call_args rparen
4452 $$ = new_yield(p, $3, &@$);
4453 /*% ripper: yield!(paren!($:3)) %*/
4455 | k_yield '(' rparen
4457 $$ = NEW_YIELD(0, &@$);
4458 /*% ripper: yield!(paren!(args_new!)) %*/
4462 $$ = NEW_YIELD(0, &@$);
4463 /*% ripper: yield0! %*/
4465 | keyword_defined '\n'? '(' begin_defined expr rparen
4467 p->ctxt.in_defined = $4.in_defined;
4468 $$ = new_defined(p, $5, &@$);
4469 /*% ripper: defined!($:5) %*/
4471 | keyword_not '(' expr rparen
4473 $$ = call_uni_op(p, method_cond(p, $3, &@3), METHOD_NOT, &@1, &@$);
4474 /*% ripper: unary!(ID2VAL(idNOT), $:3) %*/
4476 | keyword_not '(' rparen
4478 $$ = call_uni_op(p, method_cond(p, new_nil(&@2), &@2), METHOD_NOT, &@1, &@$);
4479 /*% ripper: unary!(ID2VAL(idNOT), Qnil) %*/
4483 $$ = method_add_block(p, (NODE *)$1, $2, &@$);
4484 /*% ripper: method_add_block!(method_add_arg!(fcall!($:1), args_new!), $:2) %*/
4487 | method_call brace_block
4489 block_dup_check(p, get_nd_args(p, $1), $2);
4490 $$ = method_add_block(p, $1, $2, &@$);
4491 /*% ripper: method_add_block!($:1, $:2) %*/
4494 | k_if expr_value then
4499 $$ = new_if(p, $2, $4, $5, &@$);
4501 /*% ripper: if!($:2, $:4, $:5) %*/
4503 | k_unless expr_value then
4508 $$ = new_unless(p, $2, $4, $5, &@$, &@1, &@3, &@6);
4510 /*% ripper: unless!($:2, $:4, $:5) %*/
4512 | k_while expr_value_do
4516 restore_block_exit(p, $1);
4517 $$ = NEW_WHILE(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4519 /*% ripper: while!($:2, $:3) %*/
4521 | k_until expr_value_do
4525 restore_block_exit(p, $1);
4526 $$ = NEW_UNTIL(cond(p, $2, &@2), $3, 1, &@$, &@1, &@4);
4528 /*% ripper: until!($:2, $:3) %*/
4530 | k_case expr_value terms?
4532 $$ = p->case_labels;
4533 p->case_labels = CHECK_LITERAL_WHEN;
4538 if (CASE_LABELS_ENABLED_P(p->case_labels)) st_free_table(p->case_labels);
4539 p->case_labels = $4;
4540 $$ = NEW_CASE($2, $5, &@$, &@1, &@6);
4542 /*% ripper: case!($:2, $:5) %*/
4546 $$ = p->case_labels;
4552 if (p->case_labels) st_free_table(p->case_labels);
4553 p->case_labels = $3;
4554 $$ = NEW_CASE2($4, &@$, &@1, &@5);
4555 /*% ripper: case!(Qnil, $:4) %*/
4557 | k_case expr_value terms?
4561 $$ = NEW_CASE3($2, $4, &@$, &@1, &@5);
4562 /*% ripper: case!($:2, $:4) %*/
4564 | k_for for_var keyword_in expr_value_do
4568 restore_block_exit(p, $1);
4572 * e.each{|*x| a, b, c = x}
4576 * e.each{|x| a, = x}
4578 ID id = internal_id(p);
4579 rb_node_args_aux_t *m = NEW_ARGS_AUX(0, 0, &NULL_LOC);
4580 rb_node_args_t *args;
4581 NODE *scope, *internal_var = NEW_DVAR(id, &@2);
4582 rb_ast_id_table_t *tbl = rb_ast_new_local_table(p->ast, 1);
4583 tbl->ids[0] = id; /* internal id */
4585 switch (nd_type($2)) {
4587 case NODE_DASGN: /* e.each {|internal_var| a = internal_var; ... } */
4588 set_nd_value(p, $2, internal_var);
4593 case NODE_MASGN: /* e.each {|*internal_var| a, b, c = (internal_var.length == 1 && Array === (tmp = internal_var[0]) ? tmp : internal_var); ... } */
4594 m->nd_next = node_assign(p, $2, NEW_FOR_MASGN(internal_var, &@2), NO_LEX_CTXT, &@2);
4596 default: /* e.each {|*internal_var| @a, B, c[1], d.attr = internal_val; ... } */
4597 m->nd_next = node_assign(p, (NODE *)NEW_MASGN(NEW_LIST($2, &@2), 0, &@2), internal_var, NO_LEX_CTXT, &@2);
4599 /* {|*internal_id| <m> = internal_id; ... } */
4600 args = new_args(p, m, 0, id, 0, new_args_tail(p, 0, 0, 0, &@2), &@2);
4601 scope = NEW_SCOPE2(tbl, args, $5, &@$);
4602 $$ = NEW_FOR($4, scope, &@$);
4604 /*% ripper: for!($:2, $:4, $:5) %*/
4606 | k_class cpath superclass
4608 begin_definition("class", &@k_class, &@cpath);
4613 $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
4614 nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
4615 set_line_body($bodystmt, @superclass.end_pos.lineno);
4616 nd_set_line($$, @superclass.end_pos.lineno);
4617 /*% ripper: class!($:cpath, $:superclass, $:bodystmt) %*/
4619 p->ctxt.in_class = $k_class.in_class;
4620 p->ctxt.cant_return = $k_class.cant_return;
4621 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4623 | k_class tLSHFT expr_value
4625 begin_definition("", &@k_class, &@tLSHFT);
4631 $$ = NEW_SCLASS($expr_value, $bodystmt, &@$);
4632 nd_set_line(RNODE_SCLASS($$)->nd_body, @k_end.end_pos.lineno);
4633 set_line_body($bodystmt, nd_line($expr_value));
4634 fixpos($$, $expr_value);
4635 /*% ripper: sclass!($:expr_value, $:bodystmt) %*/
4637 p->ctxt.in_def = $k_class.in_def;
4638 p->ctxt.in_class = $k_class.in_class;
4639 p->ctxt.cant_return = $k_class.cant_return;
4640 p->ctxt.shareable_constant_value = $k_class.shareable_constant_value;
4644 begin_definition("module", &@k_module, &@cpath);
4649 $$ = NEW_MODULE($cpath, $bodystmt, &@$);
4650 nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
4651 set_line_body($bodystmt, @cpath.end_pos.lineno);
4652 nd_set_line($$, @cpath.end_pos.lineno);
4653 /*% ripper: module!($:cpath, $:bodystmt) %*/
4655 p->ctxt.in_class = $k_module.in_class;
4656 p->ctxt.cant_return = $k_module.cant_return;
4657 p->ctxt.shareable_constant_value = $k_module.shareable_constant_value;
4662 push_end_expect_token_locations(p, &@head.beg_pos);
4667 restore_defun(p, $head);
4668 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4669 ($$ = $head->nd_def)->nd_loc = @$;
4670 RNODE_DEFN($$)->nd_defn = $bodystmt;
4671 /*% ripper: def!($:head, $:args, $:bodystmt) %*/
4677 push_end_expect_token_locations(p, &@head.beg_pos);
4682 restore_defun(p, $head);
4683 $bodystmt = new_scope_body(p, $args, $bodystmt, &@$);
4684 ($$ = $head->nd_def)->nd_loc = @$;
4685 RNODE_DEFS($$)->nd_defn = $bodystmt;
4686 /*% ripper: defs!(*$:head[0..2], $:args, $:bodystmt) %*/
4691 $$ = add_block_exit(p, NEW_BREAK(0, &@$, &@1));
4692 /*% ripper: break!(args_new!) %*/
4696 $$ = add_block_exit(p, NEW_NEXT(0, &@$, &@1));
4697 /*% ripper: next!(args_new!) %*/
4701 $$ = add_block_exit(p, NEW_REDO(&@$, &@1));
4702 /*% ripper: redo! %*/
4706 if (!p->ctxt.in_defined) {
4707 switch (p->ctxt.in_rescue) {
4708 case before_rescue: yyerror1(&@1, "Invalid retry without rescue"); break;
4709 case after_rescue: /* ok */ break;
4710 case after_else: yyerror1(&@1, "Invalid retry after else"); break;
4711 case after_ensure: yyerror1(&@1, "Invalid retry after ensure"); break;
4714 $$ = NEW_RETRY(&@$);
4715 /*% ripper: retry! %*/
4719primary_value : primary
4726k_begin : keyword_begin
4728 token_info_push(p, "begin", &@$);
4729 push_end_expect_token_locations(p, &@1.beg_pos);
4736 token_info_push(p, "if", &@$);
4737 if (p->token_info && p->token_info->nonspc &&
4738 p->token_info->next && !strcmp(p->token_info->next->token, "else")) {
4739 const char *tok = p->lex.ptok - rb_strlen_lit("if");
4740 const char *beg = p->lex.pbeg + p->token_info->next->beg.column;
4741 beg += rb_strlen_lit("else");
4742 while (beg < tok && ISSPACE(*beg)) beg++;
4744 p->token_info->nonspc = 0;
4747 push_end_expect_token_locations(p, &@1.beg_pos);
4751k_unless : keyword_unless
4753 token_info_push(p, "unless", &@$);
4754 push_end_expect_token_locations(p, &@1.beg_pos);
4758k_while : keyword_while allow_exits
4761 token_info_push(p, "while", &@$);
4762 push_end_expect_token_locations(p, &@1.beg_pos);
4766k_until : keyword_until allow_exits
4769 token_info_push(p, "until", &@$);
4770 push_end_expect_token_locations(p, &@1.beg_pos);
4774k_case : keyword_case
4776 token_info_push(p, "case", &@$);
4777 push_end_expect_token_locations(p, &@1.beg_pos);
4781k_for : keyword_for allow_exits
4784 token_info_push(p, "for", &@$);
4785 push_end_expect_token_locations(p, &@1.beg_pos);
4789k_class : keyword_class
4791 token_info_push(p, "class", &@$);
4793 p->ctxt.in_rescue = before_rescue;
4794 push_end_expect_token_locations(p, &@1.beg_pos);
4798k_module : keyword_module
4800 token_info_push(p, "module", &@$);
4802 p->ctxt.in_rescue = before_rescue;
4803 push_end_expect_token_locations(p, &@1.beg_pos);
4809 token_info_push(p, "def", &@$);
4810 $$ = NEW_DEF_TEMP(&@$);
4811 p->ctxt.in_argdef = 1;
4817 token_info_push(p, "do", &@$);
4818 push_end_expect_token_locations(p, &@1.beg_pos);
4822k_do_block : keyword_do_block
4824 token_info_push(p, "do", &@$);
4825 push_end_expect_token_locations(p, &@1.beg_pos);
4829k_rescue : keyword_rescue
4831 token_info_warn(p, "rescue", p->token_info, 1, &@$);
4833 p->ctxt.in_rescue = after_rescue;
4837k_ensure : keyword_ensure
4839 token_info_warn(p, "ensure", p->token_info, 1, &@$);
4844k_when : keyword_when
4846 token_info_warn(p, "when", p->token_info, 0, &@$);
4850k_else : keyword_else
4852 token_info *ptinfo_beg = p->token_info;
4853 int same = ptinfo_beg && strcmp(ptinfo_beg->token, "case") != 0;
4854 token_info_warn(p, "else", p->token_info, same, &@$);
4857 e.next = ptinfo_beg->next;
4859 token_info_setup(&e, p->lex.pbeg, &@$);
4860 if (!e.nonspc) *ptinfo_beg = e;
4865k_elsif : keyword_elsif
4868 token_info_warn(p, "elsif", p->token_info, 1, &@$);
4874 token_info_pop(p, "end", &@$);
4875 pop_end_expect_token_locations(p);
4879 compile_error(p, "syntax error, unexpected end-of-input");
4883k_return : keyword_return
4885 if (p->ctxt.cant_return && !dyna_in_block(p))
4886 yyerror1(&@1, "Invalid return in class/module body");
4890k_yield : keyword_yield
4892 if (!p->ctxt.in_defined && !p->ctxt.in_def && !compile_for_eval)
4893 yyerror1(&@1, "Invalid yield");
4907 | k_elsif expr_value then
4911 $$ = new_if(p, $2, $4, $5, &@$);
4913 /*% ripper: elsif!($:2, $:4, $:5) %*/
4921 /*% ripper: else!($:2) %*/
4931 $$ = assignable(p, $1, 0, &@$);
4932 mark_lvar_used(p, $$);
4934 | tLPAREN f_margs rparen
4937 /*% ripper: mlhs_paren!($:2) %*/
4943 $$ = NEW_LIST($1, &@$);
4944 /*% ripper: mlhs_add!(mlhs_new!, $:1) %*/
4946 | f_marg_list ',' f_marg
4948 $$ = list_append(p, $1, $3);
4949 /*% ripper: mlhs_add!($:1, $:3) %*/
4953f_margs : f_marg_list
4955 $$ = NEW_MASGN($1, 0, &@$);
4958 | f_marg_list ',' f_rest_marg
4960 $$ = NEW_MASGN($1, $3, &@$);
4961 /*% ripper: mlhs_add_star!($:1, $:3) %*/
4963 | f_marg_list ',' f_rest_marg ',' f_marg_list
4965 $$ = NEW_MASGN($1, NEW_POSTARG($3, $5, &@$), &@$);
4966 /*% ripper: mlhs_add_post!(mlhs_add_star!($:1, $:3), $:5) %*/
4970 $$ = NEW_MASGN(0, $1, &@$);
4971 /*% ripper: mlhs_add_star!(mlhs_new!, $:1) %*/
4973 | f_rest_marg ',' f_marg_list
4975 $$ = NEW_MASGN(0, NEW_POSTARG($1, $3, &@$), &@$);
4976 /*% ripper: mlhs_add_post!(mlhs_add_star!(mlhs_new!, $:1), $:3) %*/
4980f_rest_marg : tSTAR f_norm_arg
4983 $$ = assignable(p, $2, 0, &@$);
4984 mark_lvar_used(p, $$);
4988 $$ = NODE_SPECIAL_NO_NAME_REST;
4989 /*% ripper: Qnil %*/
4993f_any_kwrest : f_kwrest
4997 /*% ripper: ID2VAL(idNil) %*/
5001f_eq : {p->ctxt.in_argdef = 0;} '=';
5003block_args_tail : f_kwarg(f_block_kw) ',' f_kwrest opt_f_block_arg
5005 $$ = new_args_tail(p, $1, $3, $4, &@3);
5006 /*% ripper: [$:1, $:3, $:4] %*/
5008 | f_kwarg(f_block_kw) opt_f_block_arg
5010 $$ = new_args_tail(p, $1, 0, $2, &@1);
5011 /*% ripper: [$:1, Qnil, $:2] %*/
5013 | f_any_kwrest opt_f_block_arg
5015 $$ = new_args_tail(p, 0, $1, $2, &@1);
5016 /*% ripper: [Qnil, $:1, $:2] %*/
5020 $$ = new_args_tail(p, 0, 0, $1, &@1);
5021 /*% ripper: [Qnil, Qnil, $:1] %*/
5027 /* magic number for rest_id in iseq_set_arguments() */
5028 $$ = NODE_SPECIAL_EXCESSIVE_COMMA;
5029 /*% ripper: excessed_comma! %*/
5033block_param : f_arg ',' f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5035 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
5036 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
5038 | f_arg ',' f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5040 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
5041 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
5043 | f_arg ',' f_optarg(primary_value) opt_args_tail(block_args_tail)
5045 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
5046 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
5048 | f_arg ',' f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5050 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
5051 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
5053 | f_arg ',' f_rest_arg opt_args_tail(block_args_tail)
5055 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
5056 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
5058 | f_arg excessed_comma
5060 $$ = new_args_tail(p, 0, 0, 0, &@2);
5061 $$ = new_args(p, $1, 0, $2, 0, $$, &@$);
5062 /*% ripper: params!($:1, Qnil, $:2, Qnil, Qnil, Qnil, Qnil) %*/
5064 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5066 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
5067 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
5069 | f_arg opt_args_tail(block_args_tail)
5071 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
5072 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
5074 | f_optarg(primary_value) ',' f_rest_arg opt_args_tail(block_args_tail)
5076 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
5077 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
5079 | f_optarg(primary_value) ',' f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5081 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
5082 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
5084 | f_optarg(primary_value) opt_args_tail(block_args_tail)
5086 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
5087 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
5089 | f_optarg(primary_value) ',' f_arg opt_args_tail(block_args_tail)
5091 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
5092 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
5094 | f_rest_arg opt_args_tail(block_args_tail)
5096 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
5097 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
5099 | f_rest_arg ',' f_arg opt_args_tail(block_args_tail)
5101 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
5102 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
5106 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
5107 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
5111opt_block_param : none
5114 p->command_start = TRUE;
5118block_param_def : '|' opt_bv_decl '|'
5120 p->max_numparam = ORDINAL_PARAM;
5121 p->ctxt.in_argdef = 0;
5123 /*% ripper: block_var!(params!(Qnil,Qnil,Qnil,Qnil,Qnil,Qnil,Qnil), $:2) %*/
5125 | '|' block_param opt_bv_decl '|'
5127 p->max_numparam = ORDINAL_PARAM;
5128 p->ctxt.in_argdef = 0;
5130 /*% ripper: block_var!($:2, $:3) %*/
5138 /*% ripper: Qfalse %*/
5140 | '\n'? ';' bv_decls '\n'?
5148 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
5150 /*% ripper[brace]: rb_ary_push($:1, $:3) %*/
5165 $$ = p->max_numparam;
5166 p->max_numparam = 0;
5171 $$ = numparam_push(p);
5181lambda : tLAMBDA[lpar]
5183 token_info_push(p, "->", &@1);
5186 max_numparam numparam it_id allow_exits
5193 int max_numparam = p->max_numparam;
5194 ID it_id = p->it_id;
5195 p->lex.lpar_beg = $lpar;
5196 p->max_numparam = $max_numparam;
5198 restore_block_exit(p, $allow_exits);
5200 $args = args_with_numbered(p, $args, max_numparam, it_id);
5202 YYLTYPE loc = code_loc_gen(&@args, &@body);
5203 $$ = NEW_LAMBDA($args, $body, &loc);
5204 nd_set_line(RNODE_LAMBDA($$)->nd_body, @body.end_pos.lineno);
5205 nd_set_line($$, @args.end_pos.lineno);
5206 nd_set_first_loc($$, @1.beg_pos);
5208 /*% ripper: lambda!($:args, $:body) %*/
5209 numparam_pop(p, $numparam);
5214f_larglist : '(' f_args opt_bv_decl ')'
5216 p->ctxt.in_argdef = 0;
5218 p->max_numparam = ORDINAL_PARAM;
5219 /*% ripper: paren!($:2) %*/
5223 p->ctxt.in_argdef = 0;
5224 if (!args_info_empty_p(&$1->nd_ainfo))
5225 p->max_numparam = ORDINAL_PARAM;
5230lambda_body : tLAMBEG compstmt '}'
5232 token_info_pop(p, "}", &@3);
5238 push_end_expect_token_locations(p, &@1.beg_pos);
5247do_block : k_do_block do_body k_end
5250 set_embraced_location($$, &@1, &@3);
5255block_call : command do_block
5257 if (nd_type_p($1, NODE_YIELD)) {
5258 compile_error(p, "block given to yield");
5261 block_dup_check(p, get_nd_args(p, $1), $2);
5263 $$ = method_add_block(p, $1, $2, &@$);
5265 /*% ripper: method_add_block!($:1, $:2) %*/
5267 | block_call call_op2 operation2 opt_paren_args
5269 bool has_args = $4 != 0;
5270 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5271 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5272 /*% ripper: call!($:1, $:2, $:3) %*/
5274 /*% ripper: method_add_arg!($:$, $:4) %*/
5277 | block_call call_op2 operation2 opt_paren_args brace_block
5279 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5280 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5281 /*% ripper: command_call!($:1, $:2, $:3, $:4) %*/
5283 /*% ripper: method_add_block!($:$, $:5) %*/
5286 | block_call call_op2 operation2 command_args do_block
5288 $$ = new_command_qcall(p, $2, $1, $3, $4, $5, &@3, &@$);
5289 /*% ripper: method_add_block!(command_call!($:1, $:2, $:3, $:4), $:5) %*/
5293method_call : fcall paren_args
5297 nd_set_last_loc($1, @2.end_pos);
5298 /*% ripper: method_add_arg!(fcall!($:1), $:2) %*/
5300 | primary_value call_op operation2 opt_paren_args
5302 bool has_args = $4 != 0;
5303 if (NODE_EMPTY_ARGS_P($4)) $4 = 0;
5304 $$ = new_qcall(p, $2, $1, $3, $4, &@3, &@$);
5305 nd_set_line($$, @3.end_pos.lineno);
5306 /*% ripper: call!($:1, $:2, $:3) %*/
5308 /*% ripper: method_add_arg!($:$, $:4) %*/
5311 | primary_value tCOLON2 operation2 paren_args
5313 $$ = new_qcall(p, idCOLON2, $1, $3, $4, &@3, &@$);
5314 nd_set_line($$, @3.end_pos.lineno);
5315 /*% ripper: method_add_arg!(call!($:1, $:2, $:3), $:4) %*/
5317 | primary_value tCOLON2 operation3
5319 $$ = new_qcall(p, idCOLON2, $1, $3, 0, &@3, &@$);
5320 /*% ripper: call!($:1, $:2, $:3) %*/
5322 | primary_value call_op paren_args
5324 $$ = new_qcall(p, $2, $1, idCall, $3, &@2, &@$);
5325 nd_set_line($$, @2.end_pos.lineno);
5326 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5328 | primary_value tCOLON2 paren_args
5330 $$ = new_qcall(p, idCOLON2, $1, idCall, $3, &@2, &@$);
5331 nd_set_line($$, @2.end_pos.lineno);
5332 /*% ripper: method_add_arg!(call!($:1, $:2, ID2VAL(idCall)), $:3) %*/
5334 | keyword_super paren_args
5336 $$ = NEW_SUPER($2, &@$);
5337 /*% ripper: super!($:2) %*/
5341 $$ = NEW_ZSUPER(&@$);
5342 /*% ripper: zsuper! %*/
5344 | primary_value '[' opt_call_args rbracket
5346 $$ = NEW_CALL($1, tAREF, $3, &@$);
5348 /*% ripper: aref!($:1, $:3) %*/
5352brace_block : '{' brace_body '}'
5355 set_embraced_location($$, &@1, &@3);
5358 | k_do do_body k_end
5361 set_embraced_location($$, &@1, &@3);
5366brace_body : {$$ = dyna_push(p);}[dyna]<vars>
5367 max_numparam numparam it_id allow_exits
5368 opt_block_param[args] compstmt
5370 int max_numparam = p->max_numparam;
5371 ID it_id = p->it_id;
5372 p->max_numparam = $max_numparam;
5374 $args = args_with_numbered(p, $args, max_numparam, it_id);
5375 $$ = NEW_ITER($args, $compstmt, &@$);
5376 /*% ripper: brace_block!($:args, $:compstmt) %*/
5377 restore_block_exit(p, $allow_exits);
5378 numparam_pop(p, $numparam);
5387 max_numparam numparam it_id allow_exits
5388 opt_block_param[args] bodystmt
5390 int max_numparam = p->max_numparam;
5391 ID it_id = p->it_id;
5392 p->max_numparam = $max_numparam;
5394 $args = args_with_numbered(p, $args, max_numparam, it_id);
5395 $$ = NEW_ITER($args, $bodystmt, &@$);
5396 /*% ripper: do_block!($:args, $:bodystmt) %*/
5398 restore_block_exit(p, $allow_exits);
5399 numparam_pop(p, $numparam);
5404case_args : arg_value
5406 check_literal_when(p, $arg_value, &@arg_value);
5407 $$ = NEW_LIST($arg_value, &@$);
5408 /*% ripper: args_add!(args_new!, $:arg_value) %*/
5412 $$ = NEW_SPLAT($arg_value, &@$, &@tSTAR);
5413 /*% ripper: args_add_star!(args_new!, $:arg_value) %*/
5415 | case_args[non_last_args] ',' arg_value
5417 check_literal_when(p, $arg_value, &@arg_value);
5418 $$ = last_arg_append(p, $non_last_args, $arg_value, &@$);
5419 /*% ripper: args_add!($:non_last_args, $:arg_value) %*/
5421 | case_args[non_last_args] ',' tSTAR arg_value
5423 $$ = rest_arg_append(p, $non_last_args, $arg_value, &@$);
5424 /*% ripper: args_add_star!($:non_last_args, $:arg_value) %*/
5428case_body : k_when case_args then
5432 $$ = NEW_WHEN($2, $4, $5, &@$, &@1, &@3);
5434 /*% ripper: when!($:2, $:4, $:5) %*/
5442p_pvtbl : {$$ = p->pvtbl; p->pvtbl = st_init_numtable();};
5443p_pktbl : {$$ = p->pktbl; p->pktbl = 0;};
5447 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
5448 p->command_start = FALSE;
5449 p->ctxt.in_kwarg = 1;
5453p_case_body : keyword_in
5454 p_in_kwarg[ctxt] p_pvtbl p_pktbl
5455 p_top_expr[expr] then
5457 pop_pktbl(p, $p_pktbl);
5458 pop_pvtbl(p, $p_pvtbl);
5459 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5464 $$ = NEW_IN($expr, $compstmt, $cases, &@$);
5465 /*% ripper: in!($:expr, $:compstmt, $:cases) %*/
5473p_top_expr : p_top_expr_body
5474 | p_top_expr_body modifier_if expr_value
5476 $$ = new_if(p, $3, $1, 0, &@$);
5478 /*% ripper: if_mod!($:3, $:1) %*/
5480 | p_top_expr_body modifier_unless expr_value
5482 $$ = new_unless(p, $3, $1, 0, &@$, &@2, &NULL_LOC, &NULL_LOC);
5484 /*% ripper: unless_mod!($:3, $:1) %*/
5488p_top_expr_body : p_expr
5491 $$ = new_array_pattern_tail(p, 0, 1, 0, 0, &@$);
5492 $$ = new_array_pattern(p, 0, $1, $$, &@$);
5493 /*% ripper: aryptn!(Qnil, [$:1], Qnil, Qnil) %*/
5497 $$ = new_array_pattern(p, 0, $1, $3, &@$);
5498 nd_set_first_loc($$, @1.beg_pos);
5499 /*% ripper: aryptn!(Qnil, aryptn_pre_args(p, $:1, $:3[0]), *$:3[1..2]) %*/
5503 $$ = new_find_pattern(p, 0, $1, &@$);
5504 /*% ripper: fndptn!(Qnil, *$:1[0..2]) %*/
5508 $$ = new_array_pattern(p, 0, 0, $1, &@$);
5509 /*% ripper: aryptn!(Qnil, *$:1[0..2]) %*/
5513 $$ = new_hash_pattern(p, 0, $1, &@$);
5514 /*% ripper: hshptn!(Qnil, *$:1[0..1]) %*/
5521p_as : p_expr tASSOC p_variable
5523 NODE *n = NEW_LIST($1, &@$);
5524 n = list_append(p, n, $3);
5525 $$ = new_hash(p, n, &@$);
5526 /*% ripper: binary!($:1, ID2VAL((id_assoc)), $:3) %*/
5531p_alt : p_alt '|' p_expr_basic
5533 $$ = NEW_OR($1, $3, &@$, &@2);
5534 /*% ripper: binary!($:1, ID2VAL(idOr), $:3) %*/
5539p_lparen : '(' p_pktbl
5546p_lbracket : '[' p_pktbl
5553p_expr_basic : p_value
5555 | p_const p_lparen[p_pktbl] p_args rparen
5557 pop_pktbl(p, $p_pktbl);
5558 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5559 nd_set_first_loc($$, @p_const.beg_pos);
5560 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5562 | p_const p_lparen[p_pktbl] p_find rparen
5564 pop_pktbl(p, $p_pktbl);
5565 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5566 nd_set_first_loc($$, @p_const.beg_pos);
5567 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5569 | p_const p_lparen[p_pktbl] p_kwargs rparen
5571 pop_pktbl(p, $p_pktbl);
5572 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5573 nd_set_first_loc($$, @p_const.beg_pos);
5574 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5576 | p_const '(' rparen
5578 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5579 $$ = new_array_pattern(p, $p_const, 0, $$, &@$);
5580 /*% ripper: aryptn!($:p_const, Qnil, Qnil, Qnil) %*/
5582 | p_const p_lbracket[p_pktbl] p_args rbracket
5584 pop_pktbl(p, $p_pktbl);
5585 $$ = new_array_pattern(p, $p_const, 0, $p_args, &@$);
5586 nd_set_first_loc($$, @p_const.beg_pos);
5587 /*% ripper: aryptn!($:p_const, *$:p_args[0..2]) %*/
5589 | p_const p_lbracket[p_pktbl] p_find rbracket
5591 pop_pktbl(p, $p_pktbl);
5592 $$ = new_find_pattern(p, $p_const, $p_find, &@$);
5593 nd_set_first_loc($$, @p_const.beg_pos);
5594 /*% ripper: fndptn!($:p_const, *$:p_find[0..2]) %*/
5596 | p_const p_lbracket[p_pktbl] p_kwargs rbracket
5598 pop_pktbl(p, $p_pktbl);
5599 $$ = new_hash_pattern(p, $p_const, $p_kwargs, &@$);
5600 nd_set_first_loc($$, @p_const.beg_pos);
5601 /*% ripper: hshptn!($:p_const, *$:p_kwargs[0..1]) %*/
5603 | p_const '[' rbracket
5605 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5606 $$ = new_array_pattern(p, $1, 0, $$, &@$);
5607 /*% ripper: aryptn!($:1, Qnil, Qnil, Qnil) %*/
5609 | tLBRACK p_args rbracket
5611 $$ = new_array_pattern(p, 0, 0, $p_args, &@$);
5612 /*% ripper: aryptn!(Qnil, *$:p_args[0..2]) %*/
5614 | tLBRACK p_find rbracket
5616 $$ = new_find_pattern(p, 0, $p_find, &@$);
5617 /*% ripper: fndptn!(Qnil, *$:p_find[0..2]) %*/
5621 $$ = new_array_pattern_tail(p, 0, 0, 0, 0, &@$);
5622 $$ = new_array_pattern(p, 0, 0, $$, &@$);
5623 /*% ripper: aryptn!(Qnil, Qnil, Qnil, Qnil) %*/
5625 | tLBRACE p_pktbl lex_ctxt[ctxt]
5627 p->ctxt.in_kwarg = 0;
5631 pop_pktbl(p, $p_pktbl);
5632 p->ctxt.in_kwarg = $ctxt.in_kwarg;
5633 $$ = new_hash_pattern(p, 0, $p_kwargs, &@$);
5634 /*% ripper: hshptn!(Qnil, *$:p_kwargs[0..1]) %*/
5638 $$ = new_hash_pattern_tail(p, 0, 0, &@$);
5639 $$ = new_hash_pattern(p, 0, $$, &@$);
5640 /*% ripper: hshptn!(Qnil, Qnil, Qnil) %*/
5642 | tLPAREN p_pktbl p_expr rparen
5644 pop_pktbl(p, $p_pktbl);
5646 /*% ripper: $:p_expr %*/
5652 NODE *pre_args = NEW_LIST($1, &@$);
5653 $$ = new_array_pattern_tail(p, pre_args, 0, 0, 0, &@$);
5654 /*% ripper: [[$:1], Qnil, Qnil] %*/
5658 $$ = new_array_pattern_tail(p, $1, 1, 0, 0, &@$);
5659 /*% ripper: [$:1, Qnil, Qnil] %*/
5663 $$ = new_array_pattern_tail(p, list_concat($1, $2), 0, 0, 0, &@$);
5664 /*% ripper: [rb_ary_concat($:1, $:2), Qnil, Qnil] %*/
5666 | p_args_head p_rest
5668 $$ = new_array_pattern_tail(p, $1, 1, $2, 0, &@$);
5669 /*% ripper: [$:1, $:2, Qnil] %*/
5671 | p_args_head p_rest ',' p_args_post
5673 $$ = new_array_pattern_tail(p, $1, 1, $2, $4, &@$);
5674 /*% ripper: [$:1, $:2, $:4] %*/
5679p_args_head : p_arg ','
5683 | p_args_head p_arg ','
5685 $$ = list_concat($1, $2);
5686 /*% ripper: rb_ary_concat($:1, $:2) %*/
5692 $$ = new_array_pattern_tail(p, 0, 1, $1, 0, &@$);
5693 /*% ripper: [Qnil, $:1, Qnil] %*/
5695 | p_rest ',' p_args_post
5697 $$ = new_array_pattern_tail(p, 0, 1, $1, $3, &@$);
5698 /*% ripper: [Qnil, $:1, $:3] %*/
5702p_find : p_rest ',' p_args_post ',' p_rest
5704 $$ = new_find_pattern_tail(p, $1, $3, $5, &@$);
5705 /*% ripper: [$:1, $:3, $:5] %*/
5710p_rest : tSTAR tIDENTIFIER
5712 error_duplicate_pattern_variable(p, $2, &@2);
5713 /*% ripper: var_field!($:2) %*/
5714 $$ = assignable(p, $2, 0, &@$);
5719 /*% ripper: var_field!(Qnil) %*/
5724 | p_args_post ',' p_arg
5726 $$ = list_concat($1, $3);
5727 /*% ripper: rb_ary_concat($:1, $:3) %*/
5733 $$ = NEW_LIST($1, &@$);
5734 /*% ripper: [$:1] %*/
5738p_kwargs : p_kwarg ',' p_any_kwrest
5740 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), $3, &@$);
5741 /*% ripper: [$:1, $:3] %*/
5745 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5746 /*% ripper: [$:1, Qnil] %*/
5750 $$ = new_hash_pattern_tail(p, new_unique_key_hash(p, $1, &@$), 0, &@$);
5751 /*% ripper: [$:1, Qnil] %*/
5755 $$ = new_hash_pattern_tail(p, new_hash(p, 0, &@$), $1, &@$);
5756 /*% ripper: [[], $:1] %*/
5761 /*% ripper[brace]: [$:1] %*/
5764 $$ = list_concat($1, $3);
5765 /*% ripper: rb_ary_push($:1, $:3) %*/
5769p_kw : p_kw_label p_expr
5771 error_duplicate_pattern_key(p, $1, &@1);
5772 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
5773 /*% ripper: [$:1, $:2] %*/
5777 error_duplicate_pattern_key(p, $1, &@1);
5778 if ($1 && !is_local_id($1)) {
5779 yyerror1(&@1, "key must be valid as local variables");
5781 error_duplicate_pattern_variable(p, $1, &@1);
5782 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@$), &@$), assignable(p, $1, 0, &@$));
5783 /*% ripper: [$:1, Qnil] %*/
5788 | tSTRING_BEG string_contents tLABEL_END
5790 YYLTYPE loc = code_loc_gen(&@1, &@3);
5791 if (!$2 || nd_type_p($2, NODE_STR)) {
5792 NODE *node = dsym_node(p, $2, &loc);
5793 $$ = rb_sym2id(rb_node_sym_string_val(node));
5796 yyerror1(&loc, "symbol literal with interpolation is not allowed");
5797 $$ = rb_intern_str(STR_NEW0());
5803p_kwrest : kwrest_mark tIDENTIFIER
5806 /*% ripper: var_field!($:2) %*/
5811 /*% ripper: Qnil %*/
5815p_kwnorest : kwrest_mark keyword_nil
5821p_any_kwrest : p_kwrest
5825 /*% ripper: var_field!(ID2VAL(idNil)) %*/
5829p_value : p_primitive
5830 | p_primitive tDOT2 p_primitive
5834 $$ = NEW_DOT2($1, $3, &@$);
5835 /*% ripper: dot2!($:1, $:3) %*/
5837 | p_primitive tDOT3 p_primitive
5841 $$ = NEW_DOT3($1, $3, &@$);
5842 /*% ripper: dot3!($:1, $:3) %*/
5847 $$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
5848 /*% ripper: dot2!($:1, Qnil) %*/
5853 $$ = NEW_DOT3($1, new_nil_at(p, &@2.end_pos), &@$);
5854 /*% ripper: dot3!($:1, Qnil) %*/
5859 | tBDOT2 p_primitive
5862 $$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
5863 /*% ripper: dot2!(Qnil, $:2) %*/
5865 | tBDOT3 p_primitive
5868 $$ = NEW_DOT3(new_nil_at(p, &@1.beg_pos), $2, &@$);
5869 /*% ripper: dot3!(Qnil, $:2) %*/
5873p_primitive : inline_primary
5876 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
5877 /*% ripper: var_ref!($:1) %*/
5882p_variable : tIDENTIFIER
5884 error_duplicate_pattern_variable(p, $1, &@1);
5885 /*% ripper: var_field!($:1) %*/
5886 $$ = assignable(p, $1, 0, &@$);
5890p_var_ref : '^' tIDENTIFIER
5892 NODE *n = gettable(p, $2, &@$);
5896 else if (!(nd_type_p(n, NODE_LVAR) || nd_type_p(n, NODE_DVAR))) {
5897 compile_error(p, "%"PRIsVALUE": no such local variable", rb_id2str($2));
5900 /*% ripper: var_ref!($:2) %*/
5904 if (!($$ = gettable(p, $2, &@$))) $$ = NEW_ERROR(&@$);
5905 /*% ripper: var_ref!($:2) %*/
5909p_expr_ref : '^' tLPAREN expr_value rparen
5911 $$ = NEW_BLOCK($3, &@$);
5912 /*% ripper: begin!($:3) %*/
5916p_const : tCOLON3 cname
5918 $$ = NEW_COLON3($2, &@$);
5919 /*% ripper: top_const_ref!($:2) %*/
5921 | p_const tCOLON2 cname
5923 $$ = NEW_COLON2($1, $3, &@$);
5924 /*% ripper: const_path_ref!($:1, $:3) %*/
5928 $$ = gettable(p, $1, &@$);
5929 /*% ripper: var_ref!($:1) %*/
5933opt_rescue : k_rescue exc_list exc_var then
5939 err = NEW_ERRINFO(&@3);
5940 err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5942 $$ = NEW_RESBODY($2, $3, $5, $6, &@$);
5952 /*% ripper: rescue!($:2, $:3, $:5, $:6) %*/
5959 $$ = NEW_LIST($1, &@$);
5960 /*% ripper: rb_ary_new3(1, $:1) %*/
5964 if (!($$ = splat_array($1))) $$ = $1;
5977opt_ensure : k_ensure stmts terms?
5979 p->ctxt.in_rescue = $1.in_rescue;
5981 void_expr(p, void_stmts(p, $$));
5982 /*% ripper: ensure!($:2) %*/
5995 node = NEW_STR(STRING_NEW0(), &@$);
5998 node = evstr2dstr(p, node);
6009 $$ = literal_concat(p, $1, $2, &@$);
6010 /*% ripper: string_concat!($:1, $:2) %*/
6014string1 : tSTRING_BEG string_contents tSTRING_END
6016 $$ = heredoc_dedent(p, $2);
6017 if ($$) nd_set_loc($$, &@$);
6019 if (p->heredoc_indent > 0) {
6020 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6021 p->heredoc_indent = 0;
6023 /*% ripper: string_literal!($:$) %*/
6027xstring : tXSTRING_BEG xstring_contents tSTRING_END
6029 $$ = new_xstring(p, heredoc_dedent(p, $2), &@$);
6031 if (p->heredoc_indent > 0) {
6032 /*% ripper: heredoc_dedent!($:$, INT2NUM(%{p->heredoc_indent})) %*/
6033 p->heredoc_indent = 0;
6035 /*% ripper: xstring_literal!($:$) %*/
6039regexp : tREGEXP_BEG regexp_contents tREGEXP_END
6041 $$ = new_regexp(p, $2, $3, &@$);
6042 /*% ripper: regexp_literal!($:2, $:3) %*/
6046words : words(tWORDS_BEG, word_list) <node>
6049word_list : /* none */
6052 /*% ripper: words_new! %*/
6054 | word_list word ' '+
6056 $$ = list_append(p, $1, evstr2dstr(p, $2));
6057 /*% ripper: words_add!($:1, $:2) %*/
6061word : string_content
6062 /*% ripper[brace]: word_add!(word_new!, $:1) %*/
6063 | word string_content
6065 $$ = literal_concat(p, $1, $2, &@$);
6066 /*% ripper: word_add!($:1, $:2) %*/
6070symbols : words(tSYMBOLS_BEG, symbol_list) <node>
6073symbol_list : /* none */
6076 /*% ripper: symbols_new! %*/
6078 | symbol_list word ' '+
6080 $$ = symbol_append(p, $1, evstr2dstr(p, $2));
6081 /*% ripper: symbols_add!($:1, $:2) %*/
6085qwords : words(tQWORDS_BEG, qword_list) <node>
6088qsymbols : words(tQSYMBOLS_BEG, qsym_list) <node>
6091qword_list : /* none */
6094 /*% ripper: qwords_new! %*/
6096 | qword_list tSTRING_CONTENT ' '+
6098 $$ = list_append(p, $1, $2);
6099 /*% ripper: qwords_add!($:1, $:2) %*/
6103qsym_list : /* none */
6106 /*% ripper: qsymbols_new! %*/
6108 | qsym_list tSTRING_CONTENT ' '+
6110 $$ = symbol_append(p, $1, $2);
6111 /*% ripper: qsymbols_add!($:1, $:2) %*/
6115string_contents : /* none */
6118 /*% ripper: string_content! %*/
6120 | string_contents string_content
6122 $$ = literal_concat(p, $1, $2, &@$);
6123 /*% ripper: string_add!($:1, $:2) %*/
6127xstring_contents: /* none */
6130 /*% ripper: xstring_new! %*/
6132 | xstring_contents string_content
6134 $$ = literal_concat(p, $1, $2, &@$);
6135 /*% ripper: xstring_add!($:1, $:2) %*/
6139regexp_contents: /* none */
6142 /*% ripper: regexp_new! %*/
6144 | regexp_contents string_content
6146 NODE *head = $1, *tail = $2;
6154 switch (nd_type(head)) {
6156 head = str2dstr(p, head);
6161 head = list_append(p, NEW_DSTR(0, &@$), head);
6164 $$ = list_append(p, head, tail);
6166 /*% ripper: regexp_add!($:1, $:2) %*/
6170string_content : tSTRING_CONTENT
6171 /*% ripper[brace]: $:1 %*/
6174 /* need to backup p->lex.strterm so that a string literal `%&foo,#$&,bar&` can be parsed */
6175 $$ = p->lex.strterm;
6177 SET_LEX_STATE(EXPR_BEG);
6181 p->lex.strterm = $2;
6182 $$ = NEW_EVSTR($3, &@$);
6183 nd_set_line($$, @3.end_pos.lineno);
6184 /*% ripper: string_dvar!($:3) %*/
6186 | tSTRING_DBEG[state]
6190 /* need to backup p->lex.strterm so that a string literal `%!foo,#{ !0 },bar!` can be parsed */
6191 $$ = p->lex.strterm;
6193 SET_LEX_STATE(EXPR_BEG);
6196 $$ = p->lex.brace_nest;
6197 p->lex.brace_nest = 0;
6200 $$ = p->heredoc_indent;
6201 p->heredoc_indent = 0;
6203 compstmt string_dend
6207 p->lex.strterm = $term;
6208 SET_LEX_STATE($state);
6209 p->lex.brace_nest = $brace;
6210 p->heredoc_indent = $indent;
6211 p->heredoc_line_indent = -1;
6212 if ($compstmt) nd_unset_fl_newline($compstmt);
6213 $$ = new_evstr(p, $compstmt, &@$);
6214 /*% ripper: string_embexpr!($:compstmt) %*/
6218string_dend : tSTRING_DEND
6222string_dvar : nonlocal_var
6224 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6225 /*% ripper: var_ref!($:1) %*/
6236 SET_LEX_STATE(EXPR_END);
6237 VALUE str = rb_id2str($2);
6240 * set_yylval_noname sets invalid id to yylval.
6241 * This branch can be removed once yylval is changed to
6242 * hold lexed string.
6244 if (!str) str = STR_NEW0();
6245 $$ = NEW_SYM(str, &@$);
6246 /*% ripper: symbol_literal!(symbol!($:2)) %*/
6254dsym : tSYMBEG string_contents tSTRING_END
6256 SET_LEX_STATE(EXPR_END);
6257 $$ = dsym_node(p, $2, &@$);
6258 /*% ripper: dyna_symbol!($:2) %*/
6262numeric : simple_numeric
6263 | tUMINUS_NUM simple_numeric %prec tLOWEST
6267 /*% ripper: unary!(ID2VAL(idUMinus), $:2) %*/
6271simple_numeric : tINTEGER
6282user_variable : ident_or_const
6286keyword_variable: keyword_nil {$$ = KWD2EID(nil, $1);}
6287 | keyword_self {$$ = KWD2EID(self, $1);}
6288 | keyword_true {$$ = KWD2EID(true, $1);}
6289 | keyword_false {$$ = KWD2EID(false, $1);}
6290 | keyword__FILE__ {$$ = KWD2EID(_FILE__, $1);}
6291 | keyword__LINE__ {$$ = KWD2EID(_LINE__, $1);}
6292 | keyword__ENCODING__ {$$ = KWD2EID(_ENCODING__, $1);}
6295var_ref : user_variable
6297 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6298 if (ifdef_ripper(id_is_var(p, $1), false)) {
6299 /*% ripper: var_ref!($:1) %*/
6302 /*% ripper: vcall!($:1) %*/
6307 if (!($$ = gettable(p, $1, &@$))) $$ = NEW_ERROR(&@$);
6308 /*% ripper: var_ref!($:1) %*/
6312var_lhs : user_or_keyword_variable
6314 /*% ripper: var_field!($:1) %*/
6315 $$ = assignable(p, $1, 0, &@$);
6325 SET_LEX_STATE(EXPR_BEG);
6326 p->command_start = TRUE;
6336 /*% ripper: Qnil %*/
6340f_opt_paren_args: f_paren_args
6343 p->ctxt.in_argdef = 0;
6344 $$ = new_args_tail(p, 0, 0, 0, &@0);
6345 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6346 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6350f_paren_args : '(' f_args rparen
6353 /*% ripper: paren!($:2) %*/
6354 SET_LEX_STATE(EXPR_BEG);
6355 p->command_start = TRUE;
6356 p->ctxt.in_argdef = 0;
6360f_arglist : f_paren_args
6363 p->ctxt.in_kwarg = 1;
6364 p->ctxt.in_argdef = 1;
6365 SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */
6369 p->ctxt.in_kwarg = $1.in_kwarg;
6370 p->ctxt.in_argdef = 0;
6372 SET_LEX_STATE(EXPR_BEG);
6373 p->command_start = TRUE;
6378args_tail : f_kwarg(f_kw) ',' f_kwrest opt_f_block_arg
6380 $$ = new_args_tail(p, $1, $3, $4, &@3);
6381 /*% ripper: [$:1, $:3, $:4] %*/
6383 | f_kwarg(f_kw) opt_f_block_arg
6385 $$ = new_args_tail(p, $1, 0, $2, &@1);
6386 /*% ripper: [$:1, Qnil, $:2] %*/
6388 | f_any_kwrest opt_f_block_arg
6390 $$ = new_args_tail(p, 0, $1, $2, &@1);
6391 /*% ripper: [Qnil, $:1, $:2] %*/
6395 $$ = new_args_tail(p, 0, 0, $1, &@1);
6396 /*% ripper: [Qnil, Qnil, $:1] %*/
6400 ID fwd = $args_forward;
6401 if (lambda_beginning_p() ||
6402 (p->lex.lpar_beg >= 0 && p->lex.lpar_beg+1 == p->lex.paren_nest)) {
6403 yyerror0("unexpected ... in lambda argument");
6407 add_forwarding_args(p);
6409 $$ = new_args_tail(p, 0, fwd, arg_FWD_BLOCK, &@1);
6410 $$->nd_ainfo.forwarding = 1;
6411 /*% ripper: [Qnil, $:1, Qnil] %*/
6415f_args : f_arg ',' f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6417 $$ = new_args(p, $1, $3, $5, 0, $6, &@$);
6418 /*% ripper: params!($:1, $:3, $:5, Qnil, *$:6[0..2]) %*/
6420 | f_arg ',' f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6422 $$ = new_args(p, $1, $3, $5, $7, $8, &@$);
6423 /*% ripper: params!($:1, $:3, $:5, $:7, *$:8[0..2]) %*/
6425 | f_arg ',' f_optarg(arg_value) opt_args_tail(args_tail)
6427 $$ = new_args(p, $1, $3, 0, 0, $4, &@$);
6428 /*% ripper: params!($:1, $:3, Qnil, Qnil, *$:4[0..2]) %*/
6430 | f_arg ',' f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6432 $$ = new_args(p, $1, $3, 0, $5, $6, &@$);
6433 /*% ripper: params!($:1, $:3, Qnil, $:5, *$:6[0..2]) %*/
6435 | f_arg ',' f_rest_arg opt_args_tail(args_tail)
6437 $$ = new_args(p, $1, 0, $3, 0, $4, &@$);
6438 /*% ripper: params!($:1, Qnil, $:3, Qnil, *$:4[0..2]) %*/
6440 | f_arg ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6442 $$ = new_args(p, $1, 0, $3, $5, $6, &@$);
6443 /*% ripper: params!($:1, Qnil, $:3, $:5, *$:6[0..2]) %*/
6445 | f_arg opt_args_tail(args_tail)
6447 $$ = new_args(p, $1, 0, 0, 0, $2, &@$);
6448 /*% ripper: params!($:1, Qnil, Qnil, Qnil, *$:2[0..2]) %*/
6450 | f_optarg(arg_value) ',' f_rest_arg opt_args_tail(args_tail)
6452 $$ = new_args(p, 0, $1, $3, 0, $4, &@$);
6453 /*% ripper: params!(Qnil, $:1, $:3, Qnil, *$:4[0..2]) %*/
6455 | f_optarg(arg_value) ',' f_rest_arg ',' f_arg opt_args_tail(args_tail)
6457 $$ = new_args(p, 0, $1, $3, $5, $6, &@$);
6458 /*% ripper: params!(Qnil, $:1, $:3, $:5, *$:6[0..2]) %*/
6460 | f_optarg(arg_value) opt_args_tail(args_tail)
6462 $$ = new_args(p, 0, $1, 0, 0, $2, &@$);
6463 /*% ripper: params!(Qnil, $:1, Qnil, Qnil, *$:2[0..2]) %*/
6465 | f_optarg(arg_value) ',' f_arg opt_args_tail(args_tail)
6467 $$ = new_args(p, 0, $1, 0, $3, $4, &@$);
6468 /*% ripper: params!(Qnil, $:1, Qnil, $:3, *$:4[0..2]) %*/
6470 | f_rest_arg opt_args_tail(args_tail)
6472 $$ = new_args(p, 0, 0, $1, 0, $2, &@$);
6473 /*% ripper: params!(Qnil, Qnil, $:1, Qnil, *$:2[0..2]) %*/
6475 | f_rest_arg ',' f_arg opt_args_tail(args_tail)
6477 $$ = new_args(p, 0, 0, $1, $3, $4, &@$);
6478 /*% ripper: params!(Qnil, Qnil, $:1, $:3, *$:4[0..2]) %*/
6482 $$ = new_args(p, 0, 0, 0, 0, $1, &@$);
6483 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, *$:1[0..2]) %*/
6487 $$ = new_args_tail(p, 0, 0, 0, &@0);
6488 $$ = new_args(p, 0, 0, 0, 0, $$, &@0);
6489 /*% ripper: params!(Qnil, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil) %*/
6493args_forward : tBDOT3
6495#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
6500 /*% ripper: args_forward! %*/
6504f_bad_arg : tCONSTANT
6506 static const char mesg[] = "formal argument cannot be a constant";
6508 yyerror1(&@1, mesg);
6511 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6515 static const char mesg[] = "formal argument cannot be an instance variable";
6517 yyerror1(&@1, mesg);
6520 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6524 static const char mesg[] = "formal argument cannot be a global variable";
6526 yyerror1(&@1, mesg);
6529 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6533 static const char mesg[] = "formal argument cannot be a class variable";
6535 yyerror1(&@1, mesg);
6538 /*% ripper[error]: param_error!(ERR_MESG(), $:1) %*/
6542f_norm_arg : f_bad_arg
6545 VALUE e = formal_argument_error(p, $$ = $1);
6547 /*% ripper[error]: param_error!(?e, $:1) %*/
6549 p->max_numparam = ORDINAL_PARAM;
6553f_arg_asgn : f_norm_arg
6561f_arg_item : f_arg_asgn
6563 $$ = NEW_ARGS_AUX($1, 1, &NULL_LOC);
6566 | tLPAREN f_margs rparen
6568 ID tid = internal_id(p);
6570 loc.beg_pos = @2.beg_pos;
6571 loc.end_pos = @2.beg_pos;
6573 if (dyna_in_block(p)) {
6574 $2->nd_value = NEW_DVAR(tid, &loc);
6577 $2->nd_value = NEW_LVAR(tid, &loc);
6579 $$ = NEW_ARGS_AUX(tid, 1, &NULL_LOC);
6580 $$->nd_next = (NODE *)$2;
6581 /*% ripper: mlhs_paren!($:2) %*/
6586 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6587 | f_arg ',' f_arg_item
6591 $$->nd_next = block_append(p, $$->nd_next, $3->nd_next);
6592 rb_discard_node(p, (NODE *)$3);
6593 /*% ripper: rb_ary_push($:1, $:3) %*/
6600 VALUE e = formal_argument_error(p, $$ = $1);
6603 /*% ripper[error]: param_error!(?e, $:1) %*/
6606 * Workaround for Prism::ParseTest#test_filepath for
6607 * "unparser/corpus/literal/def.txt"
6609 * See the discussion on https://github.com/ruby/ruby/pull/9923
6611 arg_var(p, ifdef_ripper(0, $1));
6613 p->max_numparam = ORDINAL_PARAM;
6614 p->ctxt.in_argdef = 0;
6618f_kw : f_label arg_value
6620 p->ctxt.in_argdef = 1;
6621 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6622 /*% ripper: [$:$, $:2] %*/
6626 p->ctxt.in_argdef = 1;
6627 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6628 /*% ripper: [$:$, 0] %*/
6632f_block_kw : f_label primary_value
6634 p->ctxt.in_argdef = 1;
6635 $$ = new_kw_arg(p, assignable(p, $1, $2, &@$), &@$);
6636 /*% ripper: [$:$, $:2] %*/
6640 p->ctxt.in_argdef = 1;
6641 $$ = new_kw_arg(p, assignable(p, $1, NODE_SPECIAL_REQUIRED_KEYWORD, &@$), &@$);
6642 /*% ripper: [$:$, 0] %*/
6650f_no_kwarg : p_kwnorest
6652 /*% ripper: nokw_param!(Qnil) %*/
6656f_kwrest : kwrest_mark tIDENTIFIER
6658 arg_var(p, shadowing_lvar(p, $2));
6660 /*% ripper: kwrest_param!($:2) %*/
6664 arg_var(p, idFWD_KWREST);
6666 /*% ripper: kwrest_param!(Qnil) %*/
6674f_rest_arg : restarg_mark tIDENTIFIER
6676 arg_var(p, shadowing_lvar(p, $2));
6678 /*% ripper: rest_param!($:2) %*/
6682 arg_var(p, idFWD_REST);
6684 /*% ripper: rest_param!(Qnil) %*/
6692f_block_arg : blkarg_mark tIDENTIFIER
6694 arg_var(p, shadowing_lvar(p, $2));
6696 /*% ripper: blockarg!($:2) %*/
6700 arg_var(p, idFWD_BLOCK);
6702 /*% ripper: blockarg!(Qnil) %*/
6706opt_f_block_arg : ',' f_block_arg
6714 /*% ripper: Qnil %*/
6725 SET_LEX_STATE(EXPR_BEG);
6726 p->ctxt.in_argdef = 0;
6730 p->ctxt.in_argdef = 1;
6731 NODE *expr = last_expr_node($3);
6732 switch (nd_type(expr)) {
6746 case NODE_IMAGINARY:
6750 yyerror1(&expr->nd_loc, "can't define singleton method for literals");
6757 /*% ripper: paren!($:3) %*/
6765 /*% ripper: assoclist_from_args!($:1) %*/
6770 /*% ripper[brace]: rb_ary_new3(1, $:1) %*/
6779 if (RNODE_LIST(assocs)->nd_head) {
6780 NODE *n = RNODE_LIST(tail)->nd_next;
6781 if (!RNODE_LIST(tail)->nd_head && nd_type_p(n, NODE_LIST) &&
6782 nd_type_p((n = RNODE_LIST(n)->nd_head), NODE_HASH)) {
6784 tail = RNODE_HASH(n)->nd_head;
6788 assocs = list_concat(assocs, tail);
6792 /*% ripper: rb_ary_push($:1, $:3) %*/
6796assoc : arg_value tASSOC arg_value
6798 $$ = list_append(p, NEW_LIST($1, &@$), $3);
6799 /*% ripper: assoc_new!($:1, $:3) %*/
6803 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), $2);
6804 /*% ripper: assoc_new!($:1, $:2) %*/
6808 NODE *val = gettable(p, $1, &@$);
6809 if (!val) val = NEW_ERROR(&@$);
6810 $$ = list_append(p, NEW_LIST(NEW_SYM(rb_id2str($1), &@1), &@$), val);
6811 /*% ripper: assoc_new!($:1, Qnil) %*/
6813 | tSTRING_BEG string_contents tLABEL_END arg_value
6815 YYLTYPE loc = code_loc_gen(&@1, &@3);
6816 $$ = list_append(p, NEW_LIST(dsym_node(p, $2, &loc), &loc), $4);
6817 /*% ripper: assoc_new!(dyna_symbol!($:2), $:4) %*/
6821 $$ = list_append(p, NEW_LIST(0, &@$), $2);
6822 /*% ripper: assoc_splat!($:2) %*/
6826 forwarding_arg_check(p, idFWD_KWREST, idFWD_ALL, "keyword rest");
6827 $$ = list_append(p, NEW_LIST(0, &@$),
6828 NEW_LVAR(idFWD_KWREST, &@$));
6829 /*% ripper: assoc_splat!(Qnil) %*/
6833operation : ident_or_const
6837operation2 : operation
6841operation3 : tIDENTIFIER
6871term : ';' {yyerrok;token_flush(p);}
6874 @$.end_pos = @$.beg_pos;
6880 | terms ';' {yyerrok;}
6892# define yylval (*p->lval)
6894static int regx_options(struct parser_params*);
6895static int tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**,rb_encoding**);
6896static void tokaddmbc(struct parser_params *p, int c, rb_encoding *enc);
6897static enum yytokentype parse_string(struct parser_params*,rb_strterm_literal_t*);
6898static enum yytokentype here_document(struct parser_params*,rb_strterm_heredoc_t*);
6900#define set_parser_s_value(x) (ifdef_ripper(p->s_value = (x), (void)0))
6902# define set_yylval_node(x) { \
6904 rb_parser_set_location(p, &_cur_loc); \
6905 yylval.node = (x); \
6906 set_parser_s_value(STR_NEW(p->lex.ptok, p->lex.pcur-p->lex.ptok)); \
6908# define set_yylval_str(x) \
6910 set_yylval_node(NEW_STR(x, &_cur_loc)); \
6911 set_parser_s_value(rb_str_new_mutable_parser_string(x)); \
6913# define set_yylval_num(x) { \
6915 set_parser_s_value(x); \
6917# define set_yylval_id(x) (yylval.id = (x))
6918# define set_yylval_name(x) { \
6919 (yylval.id = (x)); \
6920 set_parser_s_value(ID2SYM(x)); \
6922# define yylval_id() (yylval.id)
6924#define set_yylval_noname() set_yylval_id(keyword_nil)
6925#define has_delayed_token(p) (p->delayed.token != NULL)
6928#define literal_flush(p, ptr) ((p)->lex.ptok = (ptr))
6929#define dispatch_scan_event(p, t) parser_dispatch_scan_event(p, t, __LINE__)
6932parser_has_token(struct parser_params *p)
6934 const char *const pcur = p->lex.pcur;
6935 const char *const ptok = p->lex.ptok;
6936 if (p->keep_tokens && (pcur < ptok)) {
6937 rb_bug("lex.pcur < lex.ptok. (line: %d) %"PRIdPTRDIFF"|%"PRIdPTRDIFF"|%"PRIdPTRDIFF"",
6938 p->ruby_sourceline, ptok - p->lex.pbeg, pcur - ptok, p->lex.pend - pcur);
6947 case '"': return "\\\"";
6948 case '\\': return "\\\\";
6949 case '\0': return "\\0";
6950 case '\n': return "\\n";
6951 case '\r': return "\\r";
6952 case '\t': return "\\t";
6953 case '\f': return "\\f";
6954 case '\013': return "\\v";
6955 case '\010': return "\\b";
6956 case '\007': return "\\a";
6957 case '\033': return "\\e";
6958 case '\x7f': return "\\c?";
6963static rb_parser_string_t *
6964rb_parser_str_escape(struct parser_params *p, rb_parser_string_t *str)
6966 rb_encoding *enc = p->enc;
6967 const char *ptr = str->ptr;
6968 const char *pend = ptr + str->len;
6969 const char *prev = ptr;
6970 char charbuf[5] = {'\\', 'x', 0, 0, 0};
6971 rb_parser_string_t * result = rb_parser_string_new(p, 0, 0);
6973 while (ptr < pend) {
6976 int n = rb_enc_precise_mbclen(ptr, pend, enc);
6977 if (!MBCLEN_CHARFOUND_P(n)) {
6978 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
6979 n = rb_enc_mbminlen(enc);
6981 n = (int)(pend - ptr);
6983 c = *ptr & 0xf0 >> 4;
6984 charbuf[2] = (c < 10) ? '0' + c : 'A' + c - 10;
6986 charbuf[3] = (c < 10) ? '0' + c : 'A' + c - 10;
6987 parser_str_cat(result, charbuf, 4);
6992 n = MBCLEN_CHARFOUND_LEN(n);
6993 c = rb_enc_mbc_to_codepoint(ptr, pend, enc);
6995 cc = escaped_char(c);
6997 if (ptr - n > prev) parser_str_cat(result, prev, ptr - n - prev);
6998 parser_str_cat_cstr(result, cc);
7001 else if (rb_enc_isascii(c, enc) && ISPRINT(c)) {
7004 if (ptr - n > prev) {
7005 parser_str_cat(result, prev, ptr - n - prev);
7008 parser_str_cat(result, prev, ptr - prev);
7012 if (ptr > prev) parser_str_cat(result, prev, ptr - prev);
7018parser_append_tokens(struct parser_params *p, rb_parser_string_t *str, enum yytokentype t, int line)
7020 rb_parser_ast_token_t *token = xcalloc(1, sizeof(rb_parser_ast_token_t));
7021 token->id = p->token_id;
7022 token->type_name = parser_token2char(p, t);
7024 token->loc.beg_pos = p->yylloc->beg_pos;
7025 token->loc.end_pos = p->yylloc->end_pos;
7026 rb_parser_ary_push_ast_token(p, p->tokens, token);
7030 rb_parser_string_t *str_escaped = rb_parser_str_escape(p, str);
7031 rb_parser_printf(p, "Append tokens (line: %d) [%d, :%s, \"%s\", [%d, %d, %d, %d]]\n",
7032 line, token->id, token->type_name, str_escaped->ptr,
7033 token->loc.beg_pos.lineno, token->loc.beg_pos.column,
7034 token->loc.end_pos.lineno, token->loc.end_pos.column);
7035 rb_parser_string_free(p, str_escaped);
7040parser_dispatch_scan_event(struct parser_params *p, enum yytokentype t, int line)
7042 debug_token_line(p, "parser_dispatch_scan_event", line);
7044 if (!parser_has_token(p)) return;
7046 RUBY_SET_YYLLOC(*p->yylloc);
7048 if (p->keep_tokens) {
7049 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pcur - p->lex.ptok, p->enc);
7050 parser_append_tokens(p, str, t, line);
7056#define dispatch_delayed_token(p, t) parser_dispatch_delayed_token(p, t, __LINE__)
7058parser_dispatch_delayed_token(struct parser_params *p, enum yytokentype t, int line)
7060 debug_token_line(p, "parser_dispatch_delayed_token", line);
7062 if (!has_delayed_token(p)) return;
7064 RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(*p->yylloc);
7066 if (p->keep_tokens) {
7067 /* p->delayed.token is freed by rb_parser_tokens_free */
7068 parser_append_tokens(p, p->delayed.token, t, line);
7070 rb_parser_string_free(p, p->delayed.token);
7073 p->delayed.token = NULL;
7076#define literal_flush(p, ptr) ((void)(ptr))
7079ripper_has_scan_event(struct parser_params *p)
7081 if (p->lex.pcur < p->lex.ptok) rb_raise(rb_eRuntimeError, "lex.pcur < lex.ptok");
7082 return p->lex.pcur > p->lex.ptok;
7086ripper_scan_event_val(struct parser_params *p, enum yytokentype t)
7088 VALUE str = STR_NEW(p->lex.ptok, p->lex.pcur - p->lex.ptok);
7089 VALUE rval = ripper_dispatch1(p, ripper_token2eventid(t), str);
7090 RUBY_SET_YYLLOC(*p->yylloc);
7096ripper_dispatch_scan_event(struct parser_params *p, enum yytokentype t)
7098 if (!ripper_has_scan_event(p)) return;
7100 set_parser_s_value(ripper_scan_event_val(p, t));
7102#define dispatch_scan_event(p, t) ripper_dispatch_scan_event(p, t)
7105ripper_dispatch_delayed_token(struct parser_params *p, enum yytokentype t)
7107 /* save and adjust the location to delayed token for callbacks */
7108 int saved_line = p->ruby_sourceline;
7109 const char *saved_tokp = p->lex.ptok;
7112 if (!has_delayed_token(p)) return;
7113 p->ruby_sourceline = p->delayed.beg_line;
7114 p->lex.ptok = p->lex.pbeg + p->delayed.beg_col;
7115 str = rb_str_new_mutable_parser_string(p->delayed.token);
7116 rb_parser_string_free(p, p->delayed.token);
7117 s_value = ripper_dispatch1(p, ripper_token2eventid(t), str);
7118 set_parser_s_value(s_value);
7119 p->delayed.token = NULL;
7120 p->ruby_sourceline = saved_line;
7121 p->lex.ptok = saved_tokp;
7123#define dispatch_delayed_token(p, t) ripper_dispatch_delayed_token(p, t)
7127is_identchar(struct parser_params *p, const char *ptr, const char *MAYBE_UNUSED(ptr_end), rb_encoding *enc)
7129 return rb_enc_isalnum((unsigned char)*ptr, enc) || *ptr == '_' || !ISASCII(*ptr);
7133parser_is_identchar(struct parser_params *p)
7135 return !(p)->eofp && is_identchar(p, p->lex.pcur-1, p->lex.pend, p->enc);
7139parser_isascii(struct parser_params *p)
7141 return ISASCII(*(p->lex.pcur-1));
7145token_info_setup(token_info *ptinfo, const char *ptr, const rb_code_location_t *loc)
7147 int column = 1, nonspc = 0, i;
7148 for (i = 0; i < loc->beg_pos.column; i++, ptr++) {
7150 column = (((column - 1) / TAB_WIDTH) + 1) * TAB_WIDTH;
7153 if (*ptr != ' ' && *ptr != '\t') {
7158 ptinfo->beg = loc->beg_pos;
7159 ptinfo->indent = column;
7160 ptinfo->nonspc = nonspc;
7164token_info_push(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7168 if (!p->token_info_enabled) return;
7169 ptinfo = ALLOC(token_info);
7170 ptinfo->token = token;
7171 ptinfo->next = p->token_info;
7172 token_info_setup(ptinfo, p->lex.pbeg, loc);
7174 p->token_info = ptinfo;
7178token_info_pop(struct parser_params *p, const char *token, const rb_code_location_t *loc)
7180 token_info *ptinfo_beg = p->token_info;
7182 if (!ptinfo_beg) return;
7184 /* indentation check of matched keywords (begin..end, if..end, etc.) */
7185 token_info_warn(p, token, ptinfo_beg, 1, loc);
7187 p->token_info = ptinfo_beg->next;
7188 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7192token_info_drop(struct parser_params *p, const char *token, rb_code_position_t beg_pos)
7194 token_info *ptinfo_beg = p->token_info;
7196 if (!ptinfo_beg) return;
7197 p->token_info = ptinfo_beg->next;
7199 if (ptinfo_beg->beg.lineno != beg_pos.lineno ||
7200 ptinfo_beg->beg.column != beg_pos.column ||
7201 strcmp(ptinfo_beg->token, token)) {
7202 compile_error(p, "token position mismatch: %d:%d:%s expected but %d:%d:%s",
7203 beg_pos.lineno, beg_pos.column, token,
7204 ptinfo_beg->beg.lineno, ptinfo_beg->beg.column,
7208 ruby_sized_xfree(ptinfo_beg, sizeof(*ptinfo_beg));
7212token_info_warn(struct parser_params *p, const char *token, token_info *ptinfo_beg, int same, const rb_code_location_t *loc)
7214 token_info ptinfo_end_body, *ptinfo_end = &ptinfo_end_body;
7215 if (!p->token_info_enabled) return;
7216 if (!ptinfo_beg) return;
7217 token_info_setup(ptinfo_end, p->lex.pbeg, loc);
7218 if (ptinfo_beg->beg.lineno == ptinfo_end->beg.lineno) return; /* ignore one-line block */
7219 if (ptinfo_beg->nonspc || ptinfo_end->nonspc) return; /* ignore keyword in the middle of a line */
7220 if (ptinfo_beg->indent == ptinfo_end->indent) return; /* the indents are matched */
7221 if (!same && ptinfo_beg->indent < ptinfo_end->indent) return;
7222 rb_warn3L(ptinfo_end->beg.lineno,
7223 "mismatched indentations at '%s' with '%s' at %d",
7224 WARN_S(token), WARN_S(ptinfo_beg->token), WARN_I(ptinfo_beg->beg.lineno));
7228parser_precise_mbclen(struct parser_params *p, const char *ptr)
7230 int len = rb_enc_precise_mbclen(ptr, p->lex.pend, p->enc);
7231 if (!MBCLEN_CHARFOUND_P(len)) {
7232 compile_error(p, "invalid multibyte char (%s)", rb_enc_name(p->enc));
7240parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7242 rb_parser_string_t *str;
7243 int lineno = p->ruby_sourceline;
7247 else if (yylloc->beg_pos.lineno == lineno) {
7248 str = p->lex.lastline;
7253 ruby_show_error_line(p, p->error_buffer, yylloc, lineno, str);
7257parser_yyerror(struct parser_params *p, const rb_code_location_t *yylloc, const char *msg)
7263 yylloc = RUBY_SET_YYLLOC(current);
7265 else if ((p->ruby_sourceline != yylloc->beg_pos.lineno &&
7266 p->ruby_sourceline != yylloc->end_pos.lineno)) {
7270 parser_compile_error(p, yylloc, "%s", msg);
7271 parser_show_error_line(p, yylloc);
7276parser_yyerror0(struct parser_params *p, const char *msg)
7279 return parser_yyerror(p, RUBY_SET_YYLLOC(current), msg);
7283ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yylloc, int lineno, rb_parser_string_t *str)
7286 const int max_line_margin = 30;
7287 const char *ptr, *ptr_end, *pt, *pb;
7288 const char *pre = "", *post = "", *pend;
7289 const char *code = "", *caret = "";
7291 const char *const pbeg = PARSER_STRING_PTR(str);
7296 if (!yylloc) return;
7297 pend = rb_parser_string_end(str);
7298 if (pend > pbeg && pend[-1] == '\n') {
7299 if (--pend > pbeg && pend[-1] == '\r') --pend;
7303 if (lineno == yylloc->end_pos.lineno &&
7304 (pend - pbeg) > yylloc->end_pos.column) {
7305 pt = pbeg + yylloc->end_pos.column;
7309 lim = ptr - pbeg > max_line_margin ? ptr - max_line_margin : pbeg;
7310 while ((lim < ptr) && (*(ptr-1) != '\n')) ptr--;
7312 lim = pend - ptr_end > max_line_margin ? ptr_end + max_line_margin : pend;
7313 while ((ptr_end < lim) && (*ptr_end != '\n') && (*ptr_end != '\r')) ptr_end++;
7315 len = ptr_end - ptr;
7318 ptr = rb_enc_prev_char(pbeg, ptr, pt, rb_parser_str_get_encoding(str));
7319 if (ptr > pbeg) pre = "...";
7321 if (ptr_end < pend) {
7322 ptr_end = rb_enc_prev_char(pt, ptr_end, pend, rb_parser_str_get_encoding(str));
7323 if (ptr_end < pend) post = "...";
7327 if (lineno == yylloc->beg_pos.lineno) {
7328 pb += yylloc->beg_pos.column;
7329 if (pb > pt) pb = pt;
7331 if (pb < ptr) pb = ptr;
7332 if (len <= 4 && yylloc->beg_pos.lineno == yylloc->end_pos.lineno) {
7335 if (RTEST(errbuf)) {
7336 mesg = rb_attr_get(errbuf, idMesg);
7337 if (char_at_end(p, mesg, '\n') != '\n')
7338 rb_str_cat_cstr(mesg, "\n");
7341 mesg = rb_enc_str_new(0, 0, rb_parser_str_get_encoding(str));
7343 if (!errbuf && rb_stderr_tty_p()) {
7344#define CSI_BEGIN "\033["
7347 CSI_BEGIN""CSI_SGR"%s" /* pre */
7348 CSI_BEGIN"1"CSI_SGR"%.*s"
7349 CSI_BEGIN"1;4"CSI_SGR"%.*s"
7350 CSI_BEGIN";1"CSI_SGR"%.*s"
7351 CSI_BEGIN""CSI_SGR"%s" /* post */
7354 (int)(pb - ptr), ptr,
7356 (int)(ptr_end - pt), pt,
7362 len = ptr_end - ptr;
7363 lim = pt < pend ? pt : pend;
7364 i = (int)(lim - ptr);
7365 buf = ALLOCA_N(char, i+2);
7370 *p2++ = *ptr++ == '\t' ? '\t' : ' ';
7376 memset(p2, '~', (lim - ptr));
7380 rb_str_catf(mesg, "%s%.*s%s\n""%s%s\n",
7381 pre, (int)len, code, post,
7384 if (!errbuf) rb_write_error_str(mesg);
7389parser_yyerror(struct parser_params *p, const YYLTYPE *yylloc, const char *msg)
7391 const char *pcur = 0, *ptok = 0;
7392 if (p->ruby_sourceline == yylloc->beg_pos.lineno &&
7393 p->ruby_sourceline == yylloc->end_pos.lineno) {
7396 p->lex.ptok = p->lex.pbeg + yylloc->beg_pos.column;
7397 p->lex.pcur = p->lex.pbeg + yylloc->end_pos.column;
7399 parser_yyerror0(p, msg);
7408parser_yyerror0(struct parser_params *p, const char *msg)
7410 dispatch1(parse_error, STR_NEW2(msg));
7416parser_show_error_line(struct parser_params *p, const YYLTYPE *yylloc)
7422vtable_size(const struct vtable *tbl)
7424 if (!DVARS_TERMINAL_P(tbl)) {
7432static struct vtable *
7433vtable_alloc_gen(struct parser_params *p, int line, struct vtable *prev)
7435 struct vtable *tbl = ALLOC(struct vtable);
7438 tbl->tbl = ALLOC_N(ID, tbl->capa);
7442 rb_parser_printf(p, "vtable_alloc:%d: %p\n", line, (void *)tbl);
7447#define vtable_alloc(prev) vtable_alloc_gen(p, __LINE__, prev)
7450vtable_free_gen(struct parser_params *p, int line, const char *name,
7455 rb_parser_printf(p, "vtable_free:%d: %s(%p)\n", line, name, (void *)tbl);
7458 if (!DVARS_TERMINAL_P(tbl)) {
7460 ruby_sized_xfree(tbl->tbl, tbl->capa * sizeof(ID));
7462 ruby_sized_xfree(tbl, sizeof(*tbl));
7465#define vtable_free(tbl) vtable_free_gen(p, __LINE__, #tbl, tbl)
7468vtable_add_gen(struct parser_params *p, int line, const char *name,
7469 struct vtable *tbl, ID id)
7473 rb_parser_printf(p, "vtable_add:%d: %s(%p), %s\n",
7474 line, name, (void *)tbl, rb_id2name(id));
7477 if (DVARS_TERMINAL_P(tbl)) {
7478 rb_parser_fatal(p, "vtable_add: vtable is not allocated (%p)", (void *)tbl);
7481 if (tbl->pos == tbl->capa) {
7482 tbl->capa = tbl->capa * 2;
7483 SIZED_REALLOC_N(tbl->tbl, ID, tbl->capa, tbl->pos);
7485 tbl->tbl[tbl->pos++] = id;
7487#define vtable_add(tbl, id) vtable_add_gen(p, __LINE__, #tbl, tbl, id)
7490vtable_pop_gen(struct parser_params *p, int line, const char *name,
7491 struct vtable *tbl, int n)
7494 rb_parser_printf(p, "vtable_pop:%d: %s(%p), %d\n",
7495 line, name, (void *)tbl, n);
7498 rb_parser_fatal(p, "vtable_pop: unreachable (%d < %d)", tbl->pos, n);
7503#define vtable_pop(tbl, n) vtable_pop_gen(p, __LINE__, #tbl, tbl, n)
7506vtable_included(const struct vtable * tbl, ID id)
7510 if (!DVARS_TERMINAL_P(tbl)) {
7511 for (i = 0; i < tbl->pos; i++) {
7512 if (tbl->tbl[i] == id) {
7520static void parser_prepare(struct parser_params *p);
7523e_option_supplied(struct parser_params *p)
7525 return strcmp(p->ruby_sourcefile, "-e") == 0;
7529static NODE *parser_append_options(struct parser_params *p, NODE *node);
7532yycompile0(VALUE arg)
7536 struct parser_params *p = (struct parser_params *)arg;
7539 if (!compile_for_eval && !NIL_P(p->ruby_sourcefile_string) && !e_option_supplied(p)) {
7543 if (p->debug_lines) {
7544 p->ast->body.script_lines = p->debug_lines;
7548#define RUBY_DTRACE_PARSE_HOOK(name) \
7549 if (RUBY_DTRACE_PARSE_##name##_ENABLED()) { \
7550 RUBY_DTRACE_PARSE_##name(p->ruby_sourcefile, p->ruby_sourceline); \
7552 RUBY_DTRACE_PARSE_HOOK(BEGIN);
7554 RUBY_DTRACE_PARSE_HOOK(END);
7558 xfree(p->lex.strterm);
7560 p->lex.pcur = p->lex.pbeg = p->lex.pend = 0;
7561 if (n || p->error_p) {
7562 VALUE mesg = p->error_buffer;
7564 mesg = syntax_error_new();
7566 if (!p->error_tolerant) {
7567 rb_set_errinfo(mesg);
7571 tree = p->eval_tree;
7573 tree = NEW_NIL(&NULL_LOC);
7576 rb_parser_ary_t *tokens = p->tokens;
7578 NODE *body = parser_append_options(p, RNODE_SCOPE(tree)->nd_body);
7579 prelude = block_append(p, p->eval_tree_begin, body);
7580 RNODE_SCOPE(tree)->nd_body = prelude;
7581 p->ast->body.frozen_string_literal = p->frozen_string_literal;
7582 p->ast->body.coverage_enabled = cov;
7583 if (p->keep_tokens) {
7584 p->ast->node_buffer->tokens = tokens;
7588 p->ast->body.root = tree;
7589 p->ast->body.line_count = p->line_count;
7594yycompile(struct parser_params *p, VALUE fname, int line)
7598 p->ruby_sourcefile_string = Qnil;
7599 p->ruby_sourcefile = "(none)";
7602 p->ruby_sourcefile_string = rb_str_to_interned_str(fname);
7603 p->ruby_sourcefile = StringValueCStr(fname);
7605 p->ruby_sourceline = line - 1;
7609 p->ast = ast = rb_ast_new();
7610 compile_callback(yycompile0, (VALUE)p);
7622must_be_ascii_compatible(struct parser_params *p, rb_parser_string_t *s)
7624 rb_encoding *enc = rb_parser_str_get_encoding(s);
7625 if (!rb_enc_asciicompat(enc)) {
7626 rb_raise(rb_eArgError, "invalid source encoding");
7631static rb_parser_string_t *
7632lex_getline(struct parser_params *p)
7634 rb_parser_string_t *line = (*p->lex.gets)(p, p->lex.input, p->line_count);
7635 if (!line) return 0;
7637 string_buffer_append(p, line);
7638 must_be_ascii_compatible(p, line);
7644rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
7647 p->lex.input = input;
7648 p->lex.pbeg = p->lex.pcur = p->lex.pend = 0;
7650 return yycompile(p, fname, line);
7654#define STR_FUNC_ESCAPE 0x01
7655#define STR_FUNC_EXPAND 0x02
7656#define STR_FUNC_REGEXP 0x04
7657#define STR_FUNC_QWORDS 0x08
7658#define STR_FUNC_SYMBOL 0x10
7659#define STR_FUNC_INDENT 0x20
7660#define STR_FUNC_LABEL 0x40
7661#define STR_FUNC_LIST 0x4000
7662#define STR_FUNC_TERM 0x8000
7665 str_label = STR_FUNC_LABEL,
7667 str_dquote = (STR_FUNC_EXPAND),
7668 str_xquote = (STR_FUNC_EXPAND),
7669 str_regexp = (STR_FUNC_REGEXP|STR_FUNC_ESCAPE|STR_FUNC_EXPAND),
7670 str_sword = (STR_FUNC_QWORDS|STR_FUNC_LIST),
7671 str_dword = (STR_FUNC_QWORDS|STR_FUNC_EXPAND|STR_FUNC_LIST),
7672 str_ssym = (STR_FUNC_SYMBOL),
7673 str_dsym = (STR_FUNC_SYMBOL|STR_FUNC_EXPAND)
7676static rb_parser_string_t *
7677parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *enc, int func, rb_encoding *enc0)
7679 rb_parser_string_t *pstr;
7681 pstr = rb_parser_encoding_string_new(p, ptr, len, enc);
7683 if (!(func & STR_FUNC_REGEXP)) {
7684 if (rb_parser_is_ascii_string(p, pstr)) {
7686 else if (rb_is_usascii_enc((void *)enc0) && enc != rb_utf8_encoding()) {
7687 /* everything is valid in ASCII-8BIT */
7688 enc = rb_ascii8bit_encoding();
7689 PARSER_ENCODING_CODERANGE_SET(pstr, enc, RB_PARSER_ENC_CODERANGE_VALID);
7697strterm_is_heredoc(rb_strterm_t *strterm)
7699 return strterm->heredoc;
7702static rb_strterm_t *
7703new_strterm(struct parser_params *p, int func, int term, int paren)
7705 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7706 strterm->u.literal.func = func;
7707 strterm->u.literal.term = term;
7708 strterm->u.literal.paren = paren;
7712static rb_strterm_t *
7713new_heredoc(struct parser_params *p)
7715 rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7716 strterm->heredoc = true;
7720#define peek(p,c) peek_n(p, (c), 0)
7721#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
7722#define peekc(p) peekc_n(p, 0)
7723#define peekc_n(p,n) (lex_eol_n_p(p, n) ? -1 : (unsigned char)(p)->lex.pcur[n])
7725#define add_delayed_token(p, tok, end) parser_add_delayed_token(p, tok, end, __LINE__)
7727parser_add_delayed_token(struct parser_params *p, const char *tok, const char *end, int line)
7729 debug_token_line(p, "add_delayed_token", line);
7732 if (has_delayed_token(p)) {
7733 bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
7734 int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
7735 int end_col = (next_line ? 0 : p->delayed.end_col);
7736 if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
7737 dispatch_delayed_token(p, tSTRING_CONTENT);
7740 if (!has_delayed_token(p)) {
7741 p->delayed.token = rb_parser_string_new(p, 0, 0);
7742 rb_parser_enc_associate(p, p->delayed.token, p->enc);
7743 p->delayed.beg_line = p->ruby_sourceline;
7744 p->delayed.beg_col = rb_long2int(tok - p->lex.pbeg);
7746 parser_str_cat(p->delayed.token, tok, end - tok);
7747 p->delayed.end_line = p->ruby_sourceline;
7748 p->delayed.end_col = rb_long2int(end - p->lex.pbeg);
7754set_lastline(struct parser_params *p, rb_parser_string_t *str)
7756 p->lex.pbeg = p->lex.pcur = PARSER_STRING_PTR(str);
7757 p->lex.pend = p->lex.pcur + PARSER_STRING_LEN(str);
7758 p->lex.lastline = str;
7762nextline(struct parser_params *p, int set_encoding)
7764 rb_parser_string_t *str = p->lex.nextline;
7765 p->lex.nextline = 0;
7770 if (!lex_eol_ptr_p(p, p->lex.pbeg) && *(p->lex.pend-1) != '\n') {
7774 if (!p->lex.input || !(str = lex_getline(p))) {
7781 if (p->debug_lines) {
7782 if (set_encoding) rb_parser_enc_associate(p, str, p->enc);
7783 rb_parser_string_t *copy = rb_parser_string_deep_copy(p, str);
7784 rb_parser_ary_push_script_line(p, p->debug_lines, copy);
7789 else if (str == AFTER_HEREDOC_WITHOUT_TERMINTOR) {
7790 /* after here-document without terminator */
7793 add_delayed_token(p, p->lex.ptok, p->lex.pend);
7794 if (p->heredoc_end > 0) {
7795 p->ruby_sourceline = p->heredoc_end;
7798 p->ruby_sourceline++;
7799 set_lastline(p, str);
7805parser_cr(struct parser_params *p, int c)
7807 if (peek(p, '\n')) {
7815nextc0(struct parser_params *p, int set_encoding)
7819 if (UNLIKELY(lex_eol_p(p) || p->eofp || p->lex.nextline > AFTER_HEREDOC_WITHOUT_TERMINTOR)) {
7820 if (nextline(p, set_encoding)) return -1;
7822 c = (unsigned char)*p->lex.pcur++;
7823 if (UNLIKELY(c == '\r')) {
7824 c = parser_cr(p, c);
7829#define nextc(p) nextc0(p, TRUE)
7832pushback(struct parser_params *p, int c)
7834 if (c == -1) return;
7837 if (p->lex.pcur > p->lex.pbeg && p->lex.pcur[0] == '\n' && p->lex.pcur[-1] == '\r') {
7842#define was_bol(p) ((p)->lex.pcur == (p)->lex.pbeg + 1)
7844#define tokfix(p) ((p)->tokenbuf[(p)->tokidx]='\0')
7845#define tok(p) (p)->tokenbuf
7846#define toklen(p) (p)->tokidx
7849looking_at_eol_p(struct parser_params *p)
7851 const char *ptr = p->lex.pcur;
7852 while (!lex_eol_ptr_p(p, ptr)) {
7853 int c = (unsigned char)*ptr++;
7854 int eol = (c == '\n' || c == '#');
7855 if (eol || !ISSPACE(c)) {
7863newtok(struct parser_params *p)
7868 p->tokenbuf = ALLOC_N(char, 60);
7870 if (p->toksiz > 4096) {
7872 REALLOC_N(p->tokenbuf, char, 60);
7878tokspace(struct parser_params *p, int n)
7882 if (p->tokidx >= p->toksiz) {
7883 do {p->toksiz *= 2;} while (p->toksiz < p->tokidx);
7884 REALLOC_N(p->tokenbuf, char, p->toksiz);
7886 return &p->tokenbuf[p->tokidx-n];
7890tokadd(struct parser_params *p, int c)
7892 p->tokenbuf[p->tokidx++] = (char)c;
7893 if (p->tokidx >= p->toksiz) {
7895 REALLOC_N(p->tokenbuf, char, p->toksiz);
7900tok_hex(struct parser_params *p, size_t *numlen)
7904 c = (int)ruby_scan_hex(p->lex.pcur, 2, numlen);
7906 flush_string_content(p, p->enc, rb_strlen_lit("\\x"));
7907 yyerror0("invalid hex escape");
7908 dispatch_scan_event(p, tSTRING_CONTENT);
7911 p->lex.pcur += *numlen;
7915#define tokcopy(p, n) memcpy(tokspace(p, n), (p)->lex.pcur - (n), (n))
7918escaped_control_code(int c)
7944#define WARN_SPACE_CHAR(c, prefix) \
7945 rb_warn1("invalid character syntax; use "prefix"\\%c", WARN_I(c))
7948tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
7949 int regexp_literal, const char *begin)
7951 const int wide = !begin;
7953 int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
7955 p->lex.pcur += numlen;
7956 if (p->lex.strterm == NULL ||
7957 strterm_is_heredoc(p->lex.strterm) ||
7958 (p->lex.strterm->u.literal.func != str_regexp)) {
7959 if (!begin) begin = p->lex.pcur;
7960 if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
7961 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7962 yyerror0("invalid Unicode escape");
7963 dispatch_scan_event(p, tSTRING_CONTENT);
7964 return wide && numlen > 0;
7966 if (codepoint > 0x10ffff) {
7967 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7968 yyerror0("invalid Unicode codepoint (too large)");
7969 dispatch_scan_event(p, tSTRING_CONTENT);
7972 if ((codepoint & 0xfffff800) == 0xd800) {
7973 flush_string_content(p, rb_utf8_encoding(), p->lex.pcur - begin);
7974 yyerror0("invalid Unicode codepoint");
7975 dispatch_scan_event(p, tSTRING_CONTENT);
7979 if (regexp_literal) {
7980 tokcopy(p, (int)numlen);
7982 else if (codepoint >= 0x80) {
7983 rb_encoding *utf8 = rb_utf8_encoding();
7984 if (*encp && utf8 != *encp) {
7985 YYLTYPE loc = RUBY_INIT_YYLLOC();
7986 compile_error(p, "UTF-8 mixed within %s source", rb_enc_name(*encp));
7987 parser_show_error_line(p, &loc);
7991 tokaddmbc(p, codepoint, *encp);
7994 tokadd(p, codepoint);
7999static int tokadd_mbchar(struct parser_params *p, int c);
8002tokskip_mbchar(struct parser_params *p)
8004 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8006 p->lex.pcur += len - 1;
8011/* return value is for ?\u3042 */
8013tokadd_utf8(struct parser_params *p, rb_encoding **encp,
8014 int term, int symbol_literal, int regexp_literal)
8017 * If `term` is not -1, then we allow multiple codepoints in \u{}
8018 * upto `term` byte, otherwise we're parsing a character literal.
8019 * And then add the codepoints to the current token.
8021 static const char multiple_codepoints[] = "Multiple codepoints at single character literal";
8023 const int open_brace = '{', close_brace = '}';
8025 if (regexp_literal) { tokadd(p, '\\'); tokadd(p, 'u'); }
8027 if (peek(p, open_brace)) { /* handle \u{...} form */
8028 if (regexp_literal && p->lex.strterm->u.literal.func == str_regexp) {
8030 * Skip parsing validation code and copy bytes as-is until term or
8031 * closing brace, in order to correctly handle extended regexps where
8032 * invalid unicode escapes are allowed in comments. The regexp parser
8033 * does its own validation and will catch any issues.
8035 tokadd(p, open_brace);
8036 while (!lex_eol_ptr_p(p, ++p->lex.pcur)) {
8038 if (c == close_brace) {
8043 else if (c == term) {
8046 if (c == '\\' && !lex_eol_n_p(p, 1)) {
8050 tokadd_mbchar(p, c);
8054 const char *second = NULL;
8055 int c, last = nextc(p);
8056 if (lex_eol_p(p)) goto unterminated;
8057 while (ISSPACE(c = peekc(p)) && !lex_eol_ptr_p(p, ++p->lex.pcur));
8058 while (c != close_brace) {
8059 if (c == term) goto unterminated;
8060 if (second == multiple_codepoints)
8061 second = p->lex.pcur;
8062 if (regexp_literal) tokadd(p, last);
8063 if (!tokadd_codepoint(p, encp, regexp_literal, NULL)) {
8066 while (ISSPACE(c = peekc(p))) {
8067 if (lex_eol_ptr_p(p, ++p->lex.pcur)) goto unterminated;
8070 if (term == -1 && !second)
8071 second = multiple_codepoints;
8074 if (c != close_brace) {
8076 flush_string_content(p, rb_utf8_encoding(), 0);
8077 yyerror0("unterminated Unicode escape");
8078 dispatch_scan_event(p, tSTRING_CONTENT);
8081 if (second && second != multiple_codepoints) {
8082 const char *pcur = p->lex.pcur;
8083 p->lex.pcur = second;
8084 dispatch_scan_event(p, tSTRING_CONTENT);
8087 yyerror0(multiple_codepoints);
8091 if (regexp_literal) tokadd(p, close_brace);
8095 else { /* handle \uxxxx form */
8096 if (!tokadd_codepoint(p, encp, regexp_literal, p->lex.pcur - rb_strlen_lit("\\u"))) {
8103#define ESCAPE_CONTROL 1
8104#define ESCAPE_META 2
8107read_escape(struct parser_params *p, int flags, const char *begin)
8112 switch (c = nextc(p)) {
8113 case '\\': /* Backslash */
8116 case 'n': /* newline */
8119 case 't': /* horizontal tab */
8122 case 'r': /* carriage-return */
8125 case 'f': /* form-feed */
8128 case 'v': /* vertical tab */
8131 case 'a': /* alarm(bell) */
8134 case 'e': /* escape */
8137 case '0': case '1': case '2': case '3': /* octal constant */
8138 case '4': case '5': case '6': case '7':
8140 c = (int)ruby_scan_oct(p->lex.pcur, 3, &numlen);
8141 p->lex.pcur += numlen;
8144 case 'x': /* hex constant */
8145 c = tok_hex(p, &numlen);
8146 if (numlen == 0) return 0;
8149 case 'b': /* backspace */
8152 case 's': /* space */
8156 if (flags & ESCAPE_META) goto eof;
8157 if ((c = nextc(p)) != '-') {
8160 if ((c = nextc(p)) == '\\') {
8166 return read_escape(p, flags|ESCAPE_META, begin) | 0x80;
8168 else if (c == -1) goto eof;
8169 else if (!ISASCII(c)) {
8174 int c2 = escaped_control_code(c);
8176 if (ISCNTRL(c) || !(flags & ESCAPE_CONTROL)) {
8177 WARN_SPACE_CHAR(c2, "\\M-");
8180 WARN_SPACE_CHAR(c2, "\\C-\\M-");
8183 else if (ISCNTRL(c)) goto eof;
8184 return ((c & 0xff) | 0x80);
8188 if ((c = nextc(p)) != '-') {
8192 if (flags & ESCAPE_CONTROL) goto eof;
8193 if ((c = nextc(p))== '\\') {
8199 c = read_escape(p, flags|ESCAPE_CONTROL, begin);
8203 else if (c == -1) goto eof;
8204 else if (!ISASCII(c)) {
8209 int c2 = escaped_control_code(c);
8212 if (flags & ESCAPE_META) {
8213 WARN_SPACE_CHAR(c2, "\\M-");
8216 WARN_SPACE_CHAR(c2, "");
8220 if (flags & ESCAPE_META) {
8221 WARN_SPACE_CHAR(c2, "\\M-\\C-");
8224 WARN_SPACE_CHAR(c2, "\\C-");
8228 else if (ISCNTRL(c)) goto eof;
8234 flush_string_content(p, p->enc, p->lex.pcur - begin);
8235 yyerror0("Invalid escape character syntax");
8236 dispatch_scan_event(p, tSTRING_CONTENT);
8245tokaddmbc(struct parser_params *p, int c, rb_encoding *enc)
8247 int len = rb_enc_codelen(c, enc);
8248 rb_enc_mbcput(c, tokspace(p, len), enc);
8252tokadd_escape(struct parser_params *p)
8256 const char *begin = p->lex.pcur;
8258 switch (c = nextc(p)) {
8260 return 0; /* just ignore */
8262 case '0': case '1': case '2': case '3': /* octal constant */
8263 case '4': case '5': case '6': case '7':
8265 ruby_scan_oct(--p->lex.pcur, 3, &numlen);
8266 if (numlen == 0) goto eof;
8267 p->lex.pcur += numlen;
8268 tokcopy(p, (int)numlen + 1);
8272 case 'x': /* hex constant */
8274 tok_hex(p, &numlen);
8275 if (numlen == 0) return -1;
8276 tokcopy(p, (int)numlen + 2);
8282 flush_string_content(p, p->enc, p->lex.pcur - begin);
8283 yyerror0("Invalid escape character syntax");
8295char_to_option(int c)
8301 val = RE_ONIG_OPTION_IGNORECASE;
8304 val = RE_ONIG_OPTION_EXTEND;
8307 val = RE_ONIG_OPTION_MULTILINE;
8316#define ARG_ENCODING_FIXED 16
8317#define ARG_ENCODING_NONE 32
8318#define ENC_ASCII8BIT 1
8320#define ENC_Windows_31J 3
8324char_to_option_kcode(int c, int *option, int *kcode)
8330 *kcode = ENC_ASCII8BIT;
8331 return (*option = ARG_ENCODING_NONE);
8333 *kcode = ENC_EUC_JP;
8336 *kcode = ENC_Windows_31J;
8343 return (*option = char_to_option(c));
8345 *option = ARG_ENCODING_FIXED;
8350regx_options(struct parser_params *p)
8358 while (c = nextc(p), ISALPHA(c)) {
8360 options |= RE_OPTION_ONCE;
8362 else if (char_to_option_kcode(c, &opt, &kc)) {
8364 if (kc != ENC_ASCII8BIT) kcode = c;
8378 YYLTYPE loc = RUBY_INIT_YYLLOC();
8380 compile_error(p, "unknown regexp option%s - %*s",
8381 toklen(p) > 1 ? "s" : "", toklen(p), tok(p));
8382 parser_show_error_line(p, &loc);
8384 return options | RE_OPTION_ENCODING(kcode);
8388tokadd_mbchar(struct parser_params *p, int c)
8390 int len = parser_precise_mbclen(p, p->lex.pcur-1);
8391 if (len < 0) return -1;
8393 p->lex.pcur += --len;
8394 if (len > 0) tokcopy(p, len);
8399simple_re_meta(int c)
8402 case '$': case '*': case '+': case '.':
8403 case '?': case '^': case '|':
8404 case ')': case ']': case '}': case '>':
8412parser_update_heredoc_indent(struct parser_params *p, int c)
8414 if (p->heredoc_line_indent == -1) {
8415 if (c == '\n') p->heredoc_line_indent = 0;
8419 p->heredoc_line_indent++;
8422 else if (c == '\t') {
8423 int w = (p->heredoc_line_indent / TAB_WIDTH) + 1;
8424 p->heredoc_line_indent = w * TAB_WIDTH;
8427 else if (c != '\n') {
8428 if (p->heredoc_indent > p->heredoc_line_indent) {
8429 p->heredoc_indent = p->heredoc_line_indent;
8431 p->heredoc_line_indent = -1;
8434 /* Whitespace only line has no indentation */
8435 p->heredoc_line_indent = 0;
8442parser_mixed_error(struct parser_params *p, rb_encoding *enc1, rb_encoding *enc2)
8444 YYLTYPE loc = RUBY_INIT_YYLLOC();
8445 const char *n1 = rb_enc_name(enc1), *n2 = rb_enc_name(enc2);
8446 compile_error(p, "%s mixed within %s source", n1, n2);
8447 parser_show_error_line(p, &loc);
8451parser_mixed_escape(struct parser_params *p, const char *beg, rb_encoding *enc1, rb_encoding *enc2)
8453 const char *pos = p->lex.pcur;
8455 parser_mixed_error(p, enc1, enc2);
8460nibble_char_upper(unsigned int c)
8463 return c + (c < 10 ? '0' : 'A' - 10);
8467tokadd_string(struct parser_params *p,
8468 int func, int term, int paren, long *nest,
8469 rb_encoding **encp, rb_encoding **enc)
8474 const int heredoc_end = (p->heredoc_end ? p->heredoc_end + 1 : 0);
8475 int top_of_line = FALSE;
8478#define mixed_error(enc1, enc2) \
8479 (void)(erred || (parser_mixed_error(p, enc1, enc2), erred = true))
8480#define mixed_escape(beg, enc1, enc2) \
8481 (void)(erred || (parser_mixed_escape(p, beg, enc1, enc2), erred = true))
8483 while ((c = nextc(p)) != -1) {
8484 if (p->heredoc_indent > 0) {
8485 parser_update_heredoc_indent(p, c);
8488 if (top_of_line && heredoc_end == p->ruby_sourceline) {
8494 if (paren && c == paren) {
8497 else if (c == term) {
8498 if (!nest || !*nest) {
8504 else if ((func & STR_FUNC_EXPAND) && c == '#' && !lex_eol_p(p)) {
8505 unsigned char c2 = *p->lex.pcur;
8506 if (c2 == '$' || c2 == '@' || c2 == '{') {
8511 else if (c == '\\') {
8515 if (func & STR_FUNC_QWORDS) break;
8516 if (func & STR_FUNC_EXPAND) {
8517 if (!(func & STR_FUNC_INDENT) || (p->heredoc_indent < 0))
8528 if (func & STR_FUNC_ESCAPE) tokadd(p, c);
8532 if ((func & STR_FUNC_EXPAND) == 0) {
8536 tokadd_utf8(p, enc, term,
8537 func & STR_FUNC_SYMBOL,
8538 func & STR_FUNC_REGEXP);
8542 if (c == -1) return -1;
8544 if ((func & STR_FUNC_EXPAND) == 0) tokadd(p, '\\');
8547 if (func & STR_FUNC_REGEXP) {
8553 c = read_escape(p, 0, p->lex.pcur - 1);
8555 char *t = tokspace(p, rb_strlen_lit("\\x00"));
8558 *t++ = nibble_char_upper(c >> 4);
8559 *t++ = nibble_char_upper(c);
8564 if (c == term && !simple_re_meta(c)) {
8569 if ((c = tokadd_escape(p)) < 0)
8571 if (*enc && *enc != *encp) {
8572 mixed_escape(p->lex.ptok+2, *enc, *encp);
8576 else if (func & STR_FUNC_EXPAND) {
8578 if (func & STR_FUNC_ESCAPE) tokadd(p, '\\');
8579 c = read_escape(p, 0, p->lex.pcur - 1);
8581 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8582 /* ignore backslashed spaces in %w */
8584 else if (c != term && !(paren && c == paren)) {
8591 else if (!parser_isascii(p)) {
8596 else if (*enc != *encp) {
8597 mixed_error(*enc, *encp);
8600 if (tokadd_mbchar(p, c) == -1) return -1;
8603 else if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8611 else if (*enc != *encp) {
8612 mixed_error(*enc, *encp);
8618 top_of_line = (c == '\n');
8622 if (*enc) *encp = *enc;
8626#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
8629flush_string_content(struct parser_params *p, rb_encoding *enc, size_t back)
8631 p->lex.pcur -= back;
8632 if (has_delayed_token(p)) {
8633 ptrdiff_t len = p->lex.pcur - p->lex.ptok;
8635 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
8636 p->delayed.end_line = p->ruby_sourceline;
8637 p->delayed.end_col = rb_long2int(p->lex.pcur - p->lex.pbeg);
8639 dispatch_delayed_token(p, tSTRING_CONTENT);
8640 p->lex.ptok = p->lex.pcur;
8642 dispatch_scan_event(p, tSTRING_CONTENT);
8643 p->lex.pcur += back;
8646/* this can be shared with ripper, since it's independent from struct
8649#define BIT(c, idx) (((c) / 32 - 1 == idx) ? (1U << ((c) % 32)) : 0)
8650#define SPECIAL_PUNCT(idx) ( \
8651 BIT('~', idx) | BIT('*', idx) | BIT('$', idx) | BIT('?', idx) | \
8652 BIT('!', idx) | BIT('@', idx) | BIT('/', idx) | BIT('\\', idx) | \
8653 BIT(';', idx) | BIT(',', idx) | BIT('.', idx) | BIT('=', idx) | \
8654 BIT(':', idx) | BIT('<', idx) | BIT('>', idx) | BIT('\"', idx) | \
8655 BIT('&', idx) | BIT('`', idx) | BIT('\'', idx) | BIT('+', idx) | \
8657const uint_least32_t ruby_global_name_punct_bits[] = {
8666static enum yytokentype
8667parser_peek_variable_name(struct parser_params *p)
8670 const char *ptr = p->lex.pcur;
8672 if (lex_eol_ptr_n_p(p, ptr, 1)) return 0;
8676 if ((c = *ptr) == '-') {
8677 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8680 else if (is_global_name_punct(c) || ISDIGIT(c)) {
8681 return tSTRING_DVAR;
8685 if ((c = *ptr) == '@') {
8686 if (lex_eol_ptr_p(p, ++ptr)) return 0;
8692 p->command_start = TRUE;
8693 yylval.state = p->lex.state;
8694 return tSTRING_DBEG;
8698 if (!ISASCII(c) || c == '_' || ISALPHA(c))
8699 return tSTRING_DVAR;
8703#define IS_ARG() IS_lex_state(EXPR_ARG_ANY)
8704#define IS_END() IS_lex_state(EXPR_END_ANY)
8705#define IS_BEG() (IS_lex_state(EXPR_BEG_ANY) || IS_lex_state_all(EXPR_ARG|EXPR_LABELED))
8706#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c))
8707#define IS_LABEL_POSSIBLE() (\
8708 (IS_lex_state(EXPR_LABEL|EXPR_ENDFN) && !cmd_state) || \
8710#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1))
8711#define IS_AFTER_OPERATOR() IS_lex_state(EXPR_FNAME | EXPR_DOT)
8713static inline enum yytokentype
8714parser_string_term(struct parser_params *p, int func)
8716 xfree(p->lex.strterm);
8718 if (func & STR_FUNC_REGEXP) {
8719 set_yylval_num(regx_options(p));
8720 dispatch_scan_event(p, tREGEXP_END);
8721 SET_LEX_STATE(EXPR_END);
8724 if ((func & STR_FUNC_LABEL) && IS_LABEL_SUFFIX(0)) {
8726 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
8729 SET_LEX_STATE(EXPR_END);
8733static enum yytokentype
8734parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
8736 int func = quote->func;
8737 int term = quote->term;
8738 int paren = quote->paren;
8740 rb_encoding *enc = p->enc;
8741 rb_encoding *base_enc = 0;
8742 rb_parser_string_t *lit;
8744 if (func & STR_FUNC_TERM) {
8745 if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
8746 SET_LEX_STATE(EXPR_END);
8747 xfree(p->lex.strterm);
8749 return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
8752 if ((func & STR_FUNC_QWORDS) && ISSPACE(c)) {
8753 while (c != '\n' && ISSPACE(c = nextc(p)));
8756 if (func & STR_FUNC_LIST) {
8757 quote->func &= ~STR_FUNC_LIST;
8760 if (c == term && !quote->nest) {
8761 if (func & STR_FUNC_QWORDS) {
8762 quote->func |= STR_FUNC_TERM;
8763 pushback(p, c); /* dispatch the term at tSTRING_END */
8764 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8767 return parser_string_term(p, func);
8770 if (!ISSPACE(c)) pushback(p, c);
8771 add_delayed_token(p, p->lex.ptok, p->lex.pcur);
8775 if ((func & STR_FUNC_EXPAND) && c == '#') {
8776 enum yytokentype t = parser_peek_variable_name(p);
8782 if (tokadd_string(p, func, term, paren, "e->nest,
8783 &enc, &base_enc) == -1) {
8786# define unterminated_literal(mesg) yyerror0(mesg)
8788# define unterminated_literal(mesg) compile_error(p, mesg)
8790 literal_flush(p, p->lex.pcur);
8791 if (func & STR_FUNC_QWORDS) {
8792 /* no content to add, bailing out here */
8793 unterminated_literal("unterminated list meets end of file");
8794 xfree(p->lex.strterm);
8798 if (func & STR_FUNC_REGEXP) {
8799 unterminated_literal("unterminated regexp meets end of file");
8802 unterminated_literal("unterminated string meets end of file");
8804 quote->func |= STR_FUNC_TERM;
8809 lit = STR_NEW3(tok(p), toklen(p), enc, func);
8810 set_yylval_str(lit);
8811 flush_string_content(p, enc, 0);
8813 return tSTRING_CONTENT;
8816static enum yytokentype
8817heredoc_identifier(struct parser_params *p)
8820 * term_len is length of `<<"END"` except `END`,
8821 * in this case term_len is 4 (<, <, " and ").
8823 long len, offset = p->lex.pcur - p->lex.pbeg;
8824 int c = nextc(p), term, func = 0, quote = 0;
8825 enum yytokentype token = tSTRING_BEG;
8830 func = STR_FUNC_INDENT;
8833 else if (c == '~') {
8835 func = STR_FUNC_INDENT;
8841 func |= str_squote; goto quoted;
8843 func |= str_dquote; goto quoted;
8845 token = tXSTRING_BEG;
8846 func |= str_xquote; goto quoted;
8853 while ((c = nextc(p)) != term) {
8854 if (c == -1 || c == '\r' || c == '\n') {
8855 yyerror0("unterminated here document identifier");
8862 if (!parser_is_identchar(p)) {
8864 if (func & STR_FUNC_INDENT) {
8865 pushback(p, indent > 0 ? '~' : '-');
8871 int n = parser_precise_mbclen(p, p->lex.pcur-1);
8872 if (n < 0) return 0;
8874 } while ((c = nextc(p)) != -1 && parser_is_identchar(p));
8879 len = p->lex.pcur - (p->lex.pbeg + offset) - quote;
8880 if ((unsigned long)len >= HERETERM_LENGTH_MAX)
8881 yyerror0("too long here document identifier");
8882 dispatch_scan_event(p, tHEREDOC_BEG);
8885 p->lex.strterm = new_heredoc(p);
8886 rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
8887 here->offset = offset;
8888 here->sourceline = p->ruby_sourceline;
8889 here->length = (unsigned)len;
8890 here->quote = quote;
8892 here->lastline = p->lex.lastline;
8895 p->heredoc_indent = indent;
8896 p->heredoc_line_indent = 0;
8901heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
8903 rb_parser_string_t *line;
8904 rb_strterm_t *term = p->lex.strterm;
8907 line = here->lastline;
8908 p->lex.lastline = line;
8909 p->lex.pbeg = PARSER_STRING_PTR(line);
8910 p->lex.pend = p->lex.pbeg + PARSER_STRING_LEN(line);
8911 p->lex.pcur = p->lex.pbeg + here->offset + here->length + here->quote;
8912 p->lex.ptok = p->lex.pbeg + here->offset - here->quote;
8913 p->heredoc_end = p->ruby_sourceline;
8914 p->ruby_sourceline = (int)here->sourceline;
8915 if (p->eofp) p->lex.nextline = AFTER_HEREDOC_WITHOUT_TERMINTOR;
8921dedent_string_column(const char *str, long len, int width)
8925 for (i = 0; i < len && col < width; i++) {
8926 if (str[i] == ' ') {
8929 else if (str[i] == '\t') {
8930 int n = TAB_WIDTH * (col / TAB_WIDTH + 1);
8931 if (n > width) break;
8943dedent_string(struct parser_params *p, rb_parser_string_t *string, int width)
8949 len = PARSER_STRING_LEN(string);
8950 str = PARSER_STRING_PTR(string);
8952 i = dedent_string_column(str, len, width);
8955 rb_parser_str_modify(string);
8956 str = PARSER_STRING_PTR(string);
8957 if (PARSER_STRING_LEN(string) != len)
8958 rb_fatal("literal string changed: %s", PARSER_STRING_PTR(string));
8959 MEMMOVE(str, str + i, char, len - i);
8960 rb_parser_str_set_len(p, string, len - i);
8965heredoc_dedent(struct parser_params *p, NODE *root)
8967 NODE *node, *str_node, *prev_node;
8968 int indent = p->heredoc_indent;
8969 rb_parser_string_t *prev_lit = 0;
8971 if (indent <= 0) return root;
8972 if (!root) return root;
8974 prev_node = node = str_node = root;
8975 if (nd_type_p(root, NODE_LIST)) str_node = RNODE_LIST(root)->nd_head;
8978 rb_parser_string_t *lit = RNODE_STR(str_node)->string;
8979 if (nd_fl_newline(str_node)) {
8980 dedent_string(p, lit, indent);
8985 else if (!literal_concat0(p, prev_lit, lit)) {
8989 NODE *end = RNODE_LIST(node)->as.nd_end;
8990 node = RNODE_LIST(prev_node)->nd_next = RNODE_LIST(node)->nd_next;
8992 if (nd_type_p(prev_node, NODE_DSTR))
8993 nd_set_type(prev_node, NODE_STR);
8996 RNODE_LIST(node)->as.nd_end = end;
9001 while ((nd_type_p(node, NODE_LIST) || nd_type_p(node, NODE_DSTR)) && (node = RNODE_LIST(prev_node = node)->nd_next) != 0) {
9003 if (!nd_type_p(node, NODE_LIST)) break;
9004 if ((str_node = RNODE_LIST(node)->nd_head) != 0) {
9005 enum node_type type = nd_type(str_node);
9006 if (type == NODE_STR || type == NODE_DSTR) break;
9016whole_match_p(struct parser_params *p, const char *eos, long len, int indent)
9018 const char *beg = p->lex.pbeg;
9019 const char *ptr = p->lex.pend;
9021 if (ptr - beg < len) return FALSE;
9022 if (ptr > beg && ptr[-1] == '\n') {
9023 if (--ptr > beg && ptr[-1] == '\r') --ptr;
9024 if (ptr - beg < len) return FALSE;
9026 if (strncmp(eos, ptr -= len, len)) return FALSE;
9028 while (beg < ptr && ISSPACE(*beg)) beg++;
9034word_match_p(struct parser_params *p, const char *word, long len)
9036 if (strncmp(p->lex.pcur, word, len)) return 0;
9037 if (lex_eol_n_p(p, len)) return 1;
9038 int c = (unsigned char)p->lex.pcur[len];
9039 if (ISSPACE(c)) return 1;
9041 case '\0': case '\004': case '\032': return 1;
9046#define NUM_SUFFIX_R (1<<0)
9047#define NUM_SUFFIX_I (1<<1)
9048#define NUM_SUFFIX_ALL 3
9051number_literal_suffix(struct parser_params *p, int mask)
9054 const char *lastp = p->lex.pcur;
9056 while ((c = nextc(p)) != -1) {
9057 if ((mask & NUM_SUFFIX_I) && c == 'i') {
9058 result |= (mask & NUM_SUFFIX_I);
9059 mask &= ~NUM_SUFFIX_I;
9060 /* r after i, rational of complex is disallowed */
9061 mask &= ~NUM_SUFFIX_R;
9064 if ((mask & NUM_SUFFIX_R) && c == 'r') {
9065 result |= (mask & NUM_SUFFIX_R);
9066 mask &= ~NUM_SUFFIX_R;
9069 if (!ISASCII(c) || ISALPHA(c) || c == '_') {
9070 p->lex.pcur = lastp;
9071 literal_flush(p, p->lex.pcur);
9080static enum yytokentype
9081set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, int base, int seen_point)
9083 enum rb_numeric_type numeric_type = integer_literal;
9085 if (type == tFLOAT) {
9086 numeric_type = float_literal;
9089 if (suffix & NUM_SUFFIX_R) {
9091 numeric_type = rational_literal;
9093 if (suffix & NUM_SUFFIX_I) {
9099 set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc));
9102 set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc));
9105 set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc));
9108 set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc));
9109 (void)numeric_type; /* for ripper */
9112 rb_bug("unexpected token: %d", type);
9114 SET_LEX_STATE(EXPR_END);
9118#define dispatch_heredoc_end(p) parser_dispatch_heredoc_end(p, __LINE__)
9120parser_dispatch_heredoc_end(struct parser_params *p, int line)
9122 if (has_delayed_token(p))
9123 dispatch_delayed_token(p, tSTRING_CONTENT);
9126 VALUE str = STR_NEW(p->lex.ptok, p->lex.pend - p->lex.ptok);
9127 ripper_dispatch1(p, ripper_token2eventid(tHEREDOC_END), str);
9129 if (p->keep_tokens) {
9130 rb_parser_string_t *str = rb_parser_encoding_string_new(p, p->lex.ptok, p->lex.pend - p->lex.ptok, p->enc);
9131 RUBY_SET_YYLLOC_OF_HEREDOC_END(*p->yylloc);
9132 parser_append_tokens(p, str, tHEREDOC_END, line);
9136 RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*p->yylloc);
9141static enum yytokentype
9142here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
9144 int c, func, indent = 0;
9145 const char *eos, *ptr, *ptr_end;
9147 rb_parser_string_t *str = 0;
9148 rb_encoding *enc = p->enc;
9149 rb_encoding *base_enc = 0;
9155 eos = PARSER_STRING_PTR(here->lastline) + here->offset;
9157 indent = (func = here->func) & STR_FUNC_INDENT;
9159 if ((c = nextc(p)) == -1) {
9162 if (!has_delayed_token(p)) {
9163 dispatch_scan_event(p, tSTRING_CONTENT);
9166 if ((len = p->lex.pcur - p->lex.ptok) > 0) {
9167 if (!(func & STR_FUNC_REGEXP)) {
9168 int cr = ENC_CODERANGE_UNKNOWN;
9169 rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
9170 if (cr != ENC_CODERANGE_7BIT &&
9171 rb_is_usascii_enc(p->enc) &&
9172 enc != rb_utf8_encoding()) {
9173 enc = rb_ascii8bit_encoding();
9176 rb_parser_enc_str_buf_cat(p, p->delayed.token, p->lex.ptok, len, enc);
9178 dispatch_delayed_token(p, tSTRING_CONTENT);
9182 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9183 compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
9186 SET_LEX_STATE(EXPR_END);
9191 /* not beginning of line, cannot be the terminator */
9193 else if (p->heredoc_line_indent == -1) {
9194 /* `heredoc_line_indent == -1` means
9195 * - "after an interpolation in the same line", or
9196 * - "in a continuing line"
9198 p->heredoc_line_indent = 0;
9200 else if (whole_match_p(p, eos, len, indent)) {
9201 dispatch_heredoc_end(p);
9203 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9205 SET_LEX_STATE(EXPR_END);
9209 if (!(func & STR_FUNC_EXPAND)) {
9211 ptr = PARSER_STRING_PTR(p->lex.lastline);
9212 ptr_end = p->lex.pend;
9213 if (ptr_end > ptr) {
9214 switch (ptr_end[-1]) {
9216 if (--ptr_end == ptr || ptr_end[-1] != '\r') {
9225 if (p->heredoc_indent > 0) {
9227 while (ptr + i < ptr_end && parser_update_heredoc_indent(p, ptr[i]))
9229 p->heredoc_line_indent = 0;
9233 parser_str_cat(str, ptr, ptr_end - ptr);
9235 str = rb_parser_encoding_string_new(p, ptr, ptr_end - ptr, enc);
9236 if (!lex_eol_ptr_p(p, ptr_end)) parser_str_cat_cstr(str, "\n");
9238 if (p->heredoc_indent > 0) {
9241 if (nextc(p) == -1) {
9243 rb_parser_string_free(p, str);
9248 } while (!whole_match_p(p, eos, len, indent));
9251 /* int mb = ENC_CODERANGE_7BIT, *mbp = &mb;*/
9254 enum yytokentype t = parser_peek_variable_name(p);
9255 if (p->heredoc_line_indent != -1) {
9256 if (p->heredoc_indent > p->heredoc_line_indent) {
9257 p->heredoc_indent = p->heredoc_line_indent;
9259 p->heredoc_line_indent = -1;
9268 if ((c = tokadd_string(p, func, '\n', 0, NULL, &enc, &base_enc)) == -1) {
9269 if (p->eofp) goto error;
9273 if (c == '\\') p->heredoc_line_indent = -1;
9275 str = STR_NEW3(tok(p), toklen(p), enc, func);
9277 set_yylval_str(str);
9279 if (bol) nd_set_fl_newline(yylval.node);
9281 flush_string_content(p, enc, 0);
9282 return tSTRING_CONTENT;
9284 tokadd(p, nextc(p));
9285 if (p->heredoc_indent > 0) {
9289 /* if (mbp && mb == ENC_CODERANGE_UNKNOWN) mbp = 0;*/
9290 if ((c = nextc(p)) == -1) goto error;
9291 } while (!whole_match_p(p, eos, len, indent));
9292 str = STR_NEW3(tok(p), toklen(p), enc, func);
9294 dispatch_heredoc_end(p);
9295 heredoc_restore(p, &p->lex.strterm->u.heredoc);
9297 p->lex.strterm = NEW_STRTERM(func | STR_FUNC_TERM, 0, 0);
9299 /* Preserve s_value for set_yylval_str */
9300 s_value = p->s_value;
9302 set_yylval_str(str);
9304 set_parser_s_value(s_value);
9308 if (bol) nd_set_fl_newline(yylval.node);
9310 return tSTRING_CONTENT;
9316arg_ambiguous(struct parser_params *p, char c)
9320 rb_warning1("ambiguity between regexp and two divisions: wrap regexp in parentheses or add a space after '%c' operator", WARN_I(c));
9323 rb_warning1("ambiguous first argument; put parentheses or a space even after '%c' operator", WARN_I(c));
9326 dispatch1(arg_ambiguous, rb_usascii_str_new(&c, 1));
9331/* returns true value if formal argument error;
9332 * Qtrue, or error message if ripper */
9334formal_argument_error(struct parser_params *p, ID id)
9336 switch (id_type(id)) {
9340# define ERR(mesg) (yyerror0(mesg), Qtrue)
9342# define ERR(mesg) WARN_S(mesg)
9345 return ERR("formal argument cannot be a constant");
9347 return ERR("formal argument cannot be an instance variable");
9349 return ERR("formal argument cannot be a global variable");
9351 return ERR("formal argument cannot be a class variable");
9353 return ERR("formal argument must be local variable");
9356 shadowing_lvar(p, id);
9362lvar_defined(struct parser_params *p, ID id)
9364 return (dyna_in_block(p) && dvar_defined(p, id)) || local_id(p, id);
9367/* emacsen -*- hack */
9369parser_encode_length(struct parser_params *p, const char *name, long len)
9373 if (len > 5 && name[nlen = len - 5] == '-') {
9374 if (rb_memcicmp(name + nlen + 1, "unix", 4) == 0)
9377 if (len > 4 && name[nlen = len - 4] == '-') {
9378 if (rb_memcicmp(name + nlen + 1, "dos", 3) == 0)
9380 if (rb_memcicmp(name + nlen + 1, "mac", 3) == 0 &&
9381 !(len == 8 && rb_memcicmp(name, "utf8-mac", len) == 0))
9382 /* exclude UTF8-MAC because the encoding named "UTF8" doesn't exist in Ruby */
9389parser_set_encode(struct parser_params *p, const char *name)
9395 const char *wrong = 0;
9397 case 'e': case 'E': wrong = "external"; break;
9398 case 'i': case 'I': wrong = "internal"; break;
9399 case 'f': case 'F': wrong = "filesystem"; break;
9400 case 'l': case 'L': wrong = "locale"; break;
9402 if (wrong && STRCASECMP(name, wrong) == 0) goto unknown;
9403 idx = rb_enc_find_index(name);
9406 excargs[1] = rb_sprintf("unknown encoding name: %s", name);
9408 excargs[0] = rb_eArgError;
9409 excargs[2] = rb_make_backtrace();
9410 rb_ary_unshift(excargs[2], rb_sprintf("%"PRIsVALUE":%d", p->ruby_sourcefile_string, p->ruby_sourceline));
9411 VALUE exc = rb_make_exception(3, excargs);
9412 ruby_show_error_line(p, exc, &(YYLTYPE)RUBY_INIT_YYLLOC(), p->ruby_sourceline, p->lex.lastline);
9414 rb_ast_free(p->ast);
9419 enc = rb_enc_from_index(idx);
9420 if (!rb_enc_asciicompat(enc)) {
9421 excargs[1] = rb_sprintf("%s is not ASCII compatible", rb_enc_name(enc));
9426 if (p->debug_lines) {
9428 for (i = 0; i < p->debug_lines->len; i++) {
9429 rb_parser_enc_associate(p, p->debug_lines->data[i], enc);
9436comment_at_top(struct parser_params *p)
9438 if (p->token_seen) return false;
9439 return (p->line_count == (p->has_shebang ? 2 : 1));
9442typedef long (*rb_magic_comment_length_t)(struct parser_params *p, const char *name, long len);
9443typedef void (*rb_magic_comment_setter_t)(struct parser_params *p, const char *name, const char *val);
9445static int parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val);
9448magic_comment_encoding(struct parser_params *p, const char *name, const char *val)
9450 if (!comment_at_top(p)) {
9453 parser_set_encode(p, val);
9457parser_get_bool(struct parser_params *p, const char *name, const char *val)
9461 if (STRCASECMP(val, "true") == 0) {
9466 if (STRCASECMP(val, "false") == 0) {
9471 return parser_invalid_pragma_value(p, name, val);
9475parser_invalid_pragma_value(struct parser_params *p, const char *name, const char *val)
9477 rb_warning2("invalid value for %s: %s", WARN_S(name), WARN_S(val));
9482parser_set_token_info(struct parser_params *p, const char *name, const char *val)
9484 int b = parser_get_bool(p, name, val);
9485 if (b >= 0) p->token_info_enabled = b;
9489parser_set_frozen_string_literal(struct parser_params *p, const char *name, const char *val)
9493 if (p->token_seen) {
9494 rb_warning1("'%s' is ignored after any tokens", WARN_S(name));
9498 b = parser_get_bool(p, name, val);
9501 p->frozen_string_literal = b;
9505parser_set_shareable_constant_value(struct parser_params *p, const char *name, const char *val)
9507 for (const char *s = p->lex.pbeg, *e = p->lex.pcur; s < e; ++s) {
9508 if (*s == ' ' || *s == '\t') continue;
9509 if (*s == '#') break;
9510 rb_warning1("'%s' is ignored unless in comment-only line", WARN_S(name));
9516 if (STRCASECMP(val, "none") == 0) {
9517 p->ctxt.shareable_constant_value = rb_parser_shareable_none;
9522 if (STRCASECMP(val, "literal") == 0) {
9523 p->ctxt.shareable_constant_value = rb_parser_shareable_literal;
9528 if (STRCASECMP(val, "experimental_copy") == 0) {
9529 p->ctxt.shareable_constant_value = rb_parser_shareable_copy;
9532 if (STRCASECMP(val, "experimental_everything") == 0) {
9533 p->ctxt.shareable_constant_value = rb_parser_shareable_everything;
9538 parser_invalid_pragma_value(p, name, val);
9543parser_set_past_scope(struct parser_params *p, const char *name, const char *val)
9545 int b = parser_get_bool(p, name, val);
9546 if (b >= 0) p->past_scope_enabled = b;
9550struct magic_comment {
9552 rb_magic_comment_setter_t func;
9553 rb_magic_comment_length_t length;
9556static const struct magic_comment magic_comments[] = {
9557 {"coding", magic_comment_encoding, parser_encode_length},
9558 {"encoding", magic_comment_encoding, parser_encode_length},
9559 {"frozen_string_literal", parser_set_frozen_string_literal},
9560 {"shareable_constant_value", parser_set_shareable_constant_value},
9561 {"warn_indent", parser_set_token_info},
9563 {"warn_past_scope", parser_set_past_scope},
9568magic_comment_marker(const char *str, long len)
9575 if (str[i-1] == '*' && str[i-2] == '-') {
9581 if (i + 1 >= len) return 0;
9582 if (str[i+1] != '-') {
9585 else if (str[i-1] != '-') {
9601parser_magic_comment(struct parser_params *p, const char *str, long len)
9604 VALUE name = 0, val = 0;
9605 const char *beg, *end, *vbeg, *vend;
9606#define str_copy(_s, _p, _n) ((_s) \
9607 ? (void)(rb_str_resize((_s), (_n)), \
9608 MEMCPY(RSTRING_PTR(_s), (_p), char, (_n)), (_s)) \
9609 : (void)((_s) = STR_NEW((_p), (_n))))
9611 if (len <= 7) return FALSE;
9612 if (!!(beg = magic_comment_marker(str, len))) {
9613 if (!(end = magic_comment_marker(beg, str + len - beg)))
9617 len = end - beg - 3;
9620 /* %r"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*" */
9622 const struct magic_comment *mc = magic_comments;
9627 for (; len > 0 && *str; str++, --len) {
9629 case '\'': case '"': case ':': case ';':
9632 if (!ISSPACE(*str)) break;
9634 for (beg = str; len > 0; str++, --len) {
9636 case '\'': case '"': case ':': case ';':
9639 if (ISSPACE(*str)) break;
9644 for (end = str; len > 0 && ISSPACE(*str); str++, --len);
9647 if (!indicator) return FALSE;
9651 do str++; while (--len > 0 && ISSPACE(*str));
9653 const char *tok_beg = str;
9655 for (vbeg = ++str; --len > 0 && *str != '"'; str++) {
9668 for (vbeg = str; len > 0 && *str != '"' && *str != ';' && !ISSPACE(*str); --len, str++);
9671 const char *tok_end = str;
9673 while (len > 0 && (*str == ';' || ISSPACE(*str))) --len, str++;
9676 while (len > 0 && (ISSPACE(*str))) --len, str++;
9677 if (len) return FALSE;
9681 str_copy(name, beg, n);
9682 s = RSTRING_PTR(name);
9683 for (i = 0; i < n; ++i) {
9684 if (s[i] == '-') s[i] = '_';
9687 if (STRNCASECMP(mc->name, s, n) == 0 && !mc->name[n]) {
9690 n = (*mc->length)(p, vbeg, n);
9692 str_copy(val, vbeg, n);
9693 p->lex.ptok = tok_beg;
9694 p->lex.pcur = tok_end;
9695 (*mc->func)(p, mc->name, RSTRING_PTR(val));
9698 } while (++mc < magic_comments + numberof(magic_comments));
9700 str_copy(val, vbeg, vend - vbeg);
9701 dispatch2(magic_comment, name, val);
9709set_file_encoding(struct parser_params *p, const char *str, const char *send)
9712 const char *beg = str;
9716 if (send - str <= 6) return;
9718 case 'C': case 'c': str += 6; continue;
9719 case 'O': case 'o': str += 5; continue;
9720 case 'D': case 'd': str += 4; continue;
9721 case 'I': case 'i': str += 3; continue;
9722 case 'N': case 'n': str += 2; continue;
9723 case 'G': case 'g': str += 1; continue;
9730 if (ISSPACE(*str)) break;
9733 if (STRNCASECMP(str-6, "coding", 6) == 0) break;
9738 if (++str >= send) return;
9739 } while (ISSPACE(*str));
9741 if (*str != '=' && *str != ':') return;
9746 while ((*str == '-' || *str == '_' || ISALNUM(*str)) && ++str < send);
9747 s = rb_str_new(beg, parser_encode_length(p, beg, str - beg));
9750 parser_set_encode(p, RSTRING_PTR(s));
9751 rb_str_resize(s, 0);
9755parser_prepare(struct parser_params *p)
9757 int c = nextc0(p, FALSE);
9758 p->token_info_enabled = !compile_for_eval && RTEST(ruby_verbose);
9761 if (peek(p, '!')) p->has_shebang = 1;
9763 case 0xef: /* UTF-8 BOM marker */
9764 if (!lex_eol_n_p(p, 2) &&
9765 (unsigned char)p->lex.pcur[0] == 0xbb &&
9766 (unsigned char)p->lex.pcur[1] == 0xbf) {
9767 p->enc = rb_utf8_encoding();
9770 if (p->debug_lines) {
9771 rb_parser_string_set_encoding(p->lex.lastline, p->enc);
9774 p->lex.pbeg = p->lex.pcur;
9779 case -1: /* end of script. */
9783 p->enc = rb_parser_str_get_encoding(p->lex.lastline);
9787#define ambiguous_operator(tok, op, syn) ( \
9788 rb_warning0("'"op"' after local variable or literal is interpreted as binary operator"), \
9789 rb_warning0("even though it seems like "syn""))
9791#define ambiguous_operator(tok, op, syn) \
9792 dispatch2(operator_ambiguous, TOKEN2VAL(tok), rb_str_new_cstr(syn))
9794#define warn_balanced(tok, op, syn) ((void) \
9795 (!IS_lex_state_for(last_state, EXPR_CLASS|EXPR_DOT|EXPR_FNAME|EXPR_ENDFN) && \
9796 space_seen && !ISSPACE(c) && \
9797 (ambiguous_operator(tok, op, syn), 0)), \
9798 (enum yytokentype)(tok))
9800static enum yytokentype
9801no_digits(struct parser_params *p)
9803 yyerror0("numeric literal without digits");
9804 if (peek(p, '_')) nextc(p);
9805 /* dummy 0, for tUMINUS_NUM at numeric */
9806 return set_number_literal(p, tINTEGER, 0, 10, 0);
9809static enum yytokentype
9810parse_numeric(struct parser_params *p, int c)
9812 int is_float, seen_point, seen_e, nondigit;
9815 is_float = seen_point = seen_e = nondigit = 0;
9816 SET_LEX_STATE(EXPR_END);
9818 if (c == '-' || c == '+') {
9823 int start = toklen(p);
9825 if (c == 'x' || c == 'X') {
9828 if (c != -1 && ISXDIGIT(c)) {
9831 if (nondigit) break;
9835 if (!ISXDIGIT(c)) break;
9838 } while ((c = nextc(p)) != -1);
9842 if (toklen(p) == start) {
9843 return no_digits(p);
9845 else if (nondigit) goto trailing_uc;
9846 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9847 return set_number_literal(p, tINTEGER, suffix, 16, 0);
9849 if (c == 'b' || c == 'B') {
9852 if (c == '0' || c == '1') {
9855 if (nondigit) break;
9859 if (c != '0' && c != '1') break;
9862 } while ((c = nextc(p)) != -1);
9866 if (toklen(p) == start) {
9867 return no_digits(p);
9869 else if (nondigit) goto trailing_uc;
9870 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9871 return set_number_literal(p, tINTEGER, suffix, 2, 0);
9873 if (c == 'd' || c == 'D') {
9876 if (c != -1 && ISDIGIT(c)) {
9879 if (nondigit) break;
9883 if (!ISDIGIT(c)) break;
9886 } while ((c = nextc(p)) != -1);
9890 if (toklen(p) == start) {
9891 return no_digits(p);
9893 else if (nondigit) goto trailing_uc;
9894 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9895 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9901 if (c == 'o' || c == 'O') {
9902 /* prefixed octal */
9904 if (c == -1 || c == '_' || !ISDIGIT(c)) {
9906 return no_digits(p);
9909 if (c >= '0' && c <= '7') {
9914 if (nondigit) break;
9918 if (c < '0' || c > '9') break;
9919 if (c > '7') goto invalid_octal;
9922 } while ((c = nextc(p)) != -1);
9923 if (toklen(p) > start) {
9926 if (nondigit) goto trailing_uc;
9927 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9928 return set_number_literal(p, tINTEGER, suffix, 8, 0);
9935 if (c > '7' && c <= '9') {
9937 yyerror0("Invalid octal digit");
9939 else if (c == '.' || c == 'e' || c == 'E') {
9945 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
9946 return set_number_literal(p, tINTEGER, suffix, 10, 0);
9952 case '0': case '1': case '2': case '3': case '4':
9953 case '5': case '6': case '7': case '8': case '9':
9959 if (nondigit) goto trailing_uc;
9960 if (seen_point || seen_e) {
9965 if (c0 == -1 || !ISDIGIT(c0)) {
9971 seen_point = toklen(p);
9990 if (c != '-' && c != '+' && !ISDIGIT(c)) {
9996 tokadd(p, nondigit);
10000 nondigit = (c == '-' || c == '+') ? c : 0;
10003 case '_': /* `_' in number just ignored */
10004 if (nondigit) goto decode_num;
10018 literal_flush(p, p->lex.pcur - 1);
10019 YYLTYPE loc = RUBY_INIT_YYLLOC();
10020 compile_error(p, "trailing '%c' in number", nondigit);
10021 parser_show_error_line(p, &loc);
10025 enum yytokentype type = tFLOAT;
10027 suffix = number_literal_suffix(p, seen_e ? NUM_SUFFIX_I : NUM_SUFFIX_ALL);
10028 if (suffix & NUM_SUFFIX_R) {
10033 if (errno == ERANGE) {
10034 rb_warning1("Float %s out of range", WARN_S(tok(p)));
10038 return set_number_literal(p, type, suffix, 0, seen_point);
10040 suffix = number_literal_suffix(p, NUM_SUFFIX_ALL);
10041 return set_number_literal(p, tINTEGER, suffix, 10, 0);
10044static enum yytokentype
10045parse_qmark(struct parser_params *p, int space_seen)
10049 rb_parser_string_t *lit;
10050 const char *start = p->lex.pcur;
10053 SET_LEX_STATE(EXPR_VALUE);
10058 compile_error(p, "incomplete character syntax");
10061 if (rb_enc_isspace(c, p->enc)) {
10063 int c2 = escaped_control_code(c);
10065 WARN_SPACE_CHAR(c2, "?");
10070 SET_LEX_STATE(EXPR_VALUE);
10075 int w = parser_precise_mbclen(p, start);
10076 if (is_identchar(p, start, p->lex.pend, p->enc) &&
10077 !(lex_eol_ptr_n_p(p, start, w) || !is_identchar(p, start + w, p->lex.pend, p->enc))) {
10079 const char *ptr = start;
10081 int n = parser_precise_mbclen(p, ptr);
10082 if (n < 0) return -1;
10084 } while (!lex_eol_ptr_p(p, ptr) && is_identchar(p, ptr, p->lex.pend, p->enc));
10085 rb_warn2("'?' just followed by '%.*s' is interpreted as" \
10086 " a conditional operator, put a space after '?'",
10087 WARN_I((int)(ptr - start)), WARN_S_L(start, (ptr - start)));
10091 else if (c == '\\') {
10092 if (peek(p, 'u')) {
10094 enc = rb_utf8_encoding();
10095 tokadd_utf8(p, &enc, -1, 0, 0);
10097 else if (!ISASCII(c = peekc(p)) && c != -1) {
10099 if (tokadd_mbchar(p, c) == -1) return 0;
10102 c = read_escape(p, 0, p->lex.pcur - rb_strlen_lit("?\\"));
10107 if (tokadd_mbchar(p, c) == -1) return 0;
10110 lit = STR_NEW3(tok(p), toklen(p), enc, 0);
10111 set_yylval_str(lit);
10112 SET_LEX_STATE(EXPR_END);
10116static enum yytokentype
10117parse_percent(struct parser_params *p, const int space_seen, const enum lex_state_e last_state)
10120 const char *ptok = p->lex.pcur;
10128 if (c == -1) goto unterminated;
10131 if (!ISASCII(c)) goto unknown;
10136 if (rb_enc_isalnum(term, p->enc) || !parser_isascii(p)) {
10139 c = parser_precise_mbclen(p, p->lex.pcur);
10140 if (c < 0) return 0;
10142 yyerror0("unknown type of %string");
10148 compile_error(p, "unterminated quoted string meets end of file");
10152 if (term == '(') term = ')';
10153 else if (term == '[') term = ']';
10154 else if (term == '{') term = '}';
10155 else if (term == '<') term = '>';
10158 p->lex.ptok = ptok-1;
10161 p->lex.strterm = NEW_STRTERM(str_dquote, term, paren);
10162 return tSTRING_BEG;
10165 p->lex.strterm = NEW_STRTERM(str_squote, term, paren);
10166 return tSTRING_BEG;
10169 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10173 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10174 return tQWORDS_BEG;
10177 p->lex.strterm = NEW_STRTERM(str_dword, term, paren);
10178 return tSYMBOLS_BEG;
10181 p->lex.strterm = NEW_STRTERM(str_sword, term, paren);
10182 return tQSYMBOLS_BEG;
10185 p->lex.strterm = NEW_STRTERM(str_xquote, term, paren);
10186 return tXSTRING_BEG;
10189 p->lex.strterm = NEW_STRTERM(str_regexp, term, paren);
10190 return tREGEXP_BEG;
10193 p->lex.strterm = NEW_STRTERM(str_ssym, term, paren);
10194 SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);
10198 yyerror0("unknown type of %string");
10202 if ((c = nextc(p)) == '=') {
10203 set_yylval_id('%');
10204 SET_LEX_STATE(EXPR_BEG);
10207 if (IS_SPCARG(c) || (IS_lex_state(EXPR_FITEM) && c == 's')) {
10210 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10212 return warn_balanced('%', "%%", "string literal");
10216tokadd_ident(struct parser_params *p, int c)
10219 if (tokadd_mbchar(p, c) == -1) return -1;
10221 } while (parser_is_identchar(p));
10227tokenize_ident(struct parser_params *p)
10229 ID ident = TOK_INTERN();
10231 set_yylval_name(ident);
10237parse_numvar(struct parser_params *p)
10241 unsigned long n = ruby_scan_digits(tok(p)+1, toklen(p)-1, 10, &len, &overflow);
10242 const unsigned long nth_ref_max =
10243 ((FIXNUM_MAX < INT_MAX) ? FIXNUM_MAX : INT_MAX) >> 1;
10244 /* NTH_REF is left-shifted to be ORed with back-ref flag and
10245 * turned into a Fixnum, in compile.c */
10247 if (overflow || n > nth_ref_max) {
10248 /* compile_error()? */
10249 rb_warn1("'%s' is too big for a number variable, always nil", WARN_S(tok(p)));
10250 return 0; /* $0 is $PROGRAM_NAME, not NTH_REF */
10257static enum yytokentype
10258parse_gvar(struct parser_params *p, const enum lex_state_e last_state)
10260 const char *ptr = p->lex.pcur;
10263 SET_LEX_STATE(EXPR_END);
10264 p->lex.ptok = ptr - 1; /* from '$' */
10268 case '_': /* $_: last read line string */
10270 if (parser_is_identchar(p)) {
10278 case '~': /* $~: match-data */
10279 case '*': /* $*: argv */
10280 case '$': /* $$: pid */
10281 case '?': /* $?: last status */
10282 case '!': /* $!: error string */
10283 case '@': /* $@: error position */
10284 case '/': /* $/: input record separator */
10285 case '\\': /* $\: output record separator */
10286 case ';': /* $;: field separator */
10287 case ',': /* $,: output field separator */
10288 case '.': /* $.: last read line number */
10289 case '=': /* $=: ignorecase */
10290 case ':': /* $:: load path */
10291 case '<': /* $<: default input handle */
10292 case '>': /* $>: default output handle */
10293 case '\"': /* $": already loaded files */
10302 if (parser_is_identchar(p)) {
10303 if (tokadd_mbchar(p, c) == -1) return 0;
10314 case '&': /* $&: last match */
10315 case '`': /* $`: string before last match */
10316 case '\'': /* $': string after last match */
10317 case '+': /* $+: string matches last paren. */
10318 if (IS_lex_state_for(last_state, EXPR_FNAME)) {
10323 set_yylval_node(NEW_BACK_REF(c, &_cur_loc));
10326 case '1': case '2': case '3':
10327 case '4': case '5': case '6':
10328 case '7': case '8': case '9':
10333 } while (c != -1 && ISDIGIT(c));
10335 if (IS_lex_state_for(last_state, EXPR_FNAME)) goto gvar;
10337 c = parse_numvar(p);
10338 set_yylval_node(NEW_NTH_REF(c, &_cur_loc));
10342 if (!parser_is_identchar(p)) {
10343 YYLTYPE loc = RUBY_INIT_YYLLOC();
10344 if (c == -1 || ISSPACE(c)) {
10345 compile_error(p, "'$' without identifiers is not allowed as a global variable name");
10349 compile_error(p, "'$%c' is not allowed as a global variable name", c);
10351 parser_show_error_line(p, &loc);
10352 set_yylval_noname();
10360 if (tokadd_ident(p, c)) return 0;
10361 SET_LEX_STATE(EXPR_END);
10362 if (VALID_SYMNAME_P(tok(p), toklen(p), p->enc, ID_GLOBAL)) {
10366 compile_error(p, "'%.*s' is not allowed as a global variable name", toklen(p), tok(p));
10367 set_yylval_noname();
10373parser_numbered_param(struct parser_params *p, int n)
10375 if (n < 0) return false;
10377 if (DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev)) {
10380 if (p->max_numparam == ORDINAL_PARAM) {
10381 compile_error(p, "ordinary parameter is defined");
10384 struct vtable *args = p->lvtbl->args;
10385 if (p->max_numparam < n) {
10386 p->max_numparam = n;
10388 while (n > args->pos) {
10389 vtable_add(args, NUMPARAM_IDX_TO_ID(args->pos+1));
10394static enum yytokentype
10395parse_atmark(struct parser_params *p, const enum lex_state_e last_state)
10397 const char *ptr = p->lex.pcur;
10398 enum yytokentype result = tIVAR;
10399 register int c = nextc(p);
10402 p->lex.ptok = ptr - 1; /* from '@' */
10410 SET_LEX_STATE(IS_lex_state_for(last_state, EXPR_FNAME) ? EXPR_ENDFN : EXPR_END);
10411 if (c == -1 || !parser_is_identchar(p)) {
10413 RUBY_SET_YYLLOC(loc);
10414 if (result == tIVAR) {
10415 compile_error(p, "'@' without identifiers is not allowed as an instance variable name");
10418 compile_error(p, "'@@' without identifiers is not allowed as a class variable name");
10420 parser_show_error_line(p, &loc);
10421 set_yylval_noname();
10422 SET_LEX_STATE(EXPR_END);
10425 else if (ISDIGIT(c)) {
10427 RUBY_SET_YYLLOC(loc);
10428 if (result == tIVAR) {
10429 compile_error(p, "'@%c' is not allowed as an instance variable name", c);
10432 compile_error(p, "'@@%c' is not allowed as a class variable name", c);
10434 parser_show_error_line(p, &loc);
10435 set_yylval_noname();
10436 SET_LEX_STATE(EXPR_END);
10440 if (tokadd_ident(p, c)) return 0;
10445static enum yytokentype
10446parse_ident(struct parser_params *p, int c, int cmd_state)
10448 enum yytokentype result;
10449 bool is_ascii = true;
10450 const enum lex_state_e last_state = p->lex.state;
10452 int enforce_keyword_end = 0;
10455 if (!ISASCII(c)) is_ascii = false;
10456 if (tokadd_mbchar(p, c) == -1) return 0;
10458 } while (parser_is_identchar(p));
10459 if ((c == '!' || c == '?') && !peek(p, '=')) {
10463 else if (c == '=' && IS_lex_state(EXPR_FNAME) &&
10464 (!peek(p, '~') && !peek(p, '>') && (!peek(p, '=') || (peek_n(p, '>', 1))))) {
10465 result = tIDENTIFIER;
10469 result = tCONSTANT; /* assume provisionally */
10474 if (IS_LABEL_POSSIBLE()) {
10475 if (IS_LABEL_SUFFIX(0)) {
10476 SET_LEX_STATE(EXPR_ARG|EXPR_LABELED);
10484 if (peek_end_expect_token_locations(p)) {
10485 const rb_code_position_t *end_pos;
10486 int lineno, column;
10487 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
10489 end_pos = peek_end_expect_token_locations(p)->pos;
10490 lineno = end_pos->lineno;
10491 column = end_pos->column;
10494 rb_parser_printf(p, "enforce_keyword_end check. current: (%d, %d), peek: (%d, %d)\n",
10495 p->ruby_sourceline, beg_pos, lineno, column);
10498 if ((p->ruby_sourceline > lineno) && (beg_pos <= column)) {
10499 const struct kwtable *kw;
10501 if ((IS_lex_state(EXPR_DOT)) && (kw = rb_reserved_word(tok(p), toklen(p))) && (kw && kw->id[0] == keyword_end)) {
10502 if (p->debug) rb_parser_printf(p, "enforce_keyword_end is enabled\n");
10503 enforce_keyword_end = 1;
10509 if (is_ascii && (!IS_lex_state(EXPR_DOT) || enforce_keyword_end)) {
10510 const struct kwtable *kw;
10512 /* See if it is a reserved word. */
10513 kw = rb_reserved_word(tok(p), toklen(p));
10515 enum lex_state_e state = p->lex.state;
10516 if (IS_lex_state_for(state, EXPR_FNAME)) {
10517 SET_LEX_STATE(EXPR_ENDFN);
10518 set_yylval_name(rb_intern2(tok(p), toklen(p)));
10521 SET_LEX_STATE(kw->state);
10522 if (IS_lex_state(EXPR_BEG)) {
10523 p->command_start = TRUE;
10525 if (kw->id[0] == keyword_do) {
10526 if (lambda_beginning_p()) {
10527 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE in the body of "-> do ... end" */
10528 return keyword_do_LAMBDA;
10530 if (COND_P()) return keyword_do_cond;
10531 if (CMDARG_P() && !IS_lex_state_for(state, EXPR_CMDARG))
10532 return keyword_do_block;
10535 if (IS_lex_state_for(state, (EXPR_BEG | EXPR_LABELED | EXPR_CLASS)))
10538 if (kw->id[0] != kw->id[1])
10539 SET_LEX_STATE(EXPR_BEG | EXPR_LABEL);
10545 if (IS_lex_state(EXPR_BEG_ANY | EXPR_ARG_ANY | EXPR_DOT)) {
10547 SET_LEX_STATE(EXPR_CMDARG);
10550 SET_LEX_STATE(EXPR_ARG);
10553 else if (p->lex.state == EXPR_FNAME) {
10554 SET_LEX_STATE(EXPR_ENDFN);
10557 SET_LEX_STATE(EXPR_END);
10560 ident = tokenize_ident(p);
10561 if (result == tCONSTANT && is_local_id(ident)) result = tIDENTIFIER;
10562 if (!IS_lex_state_for(last_state, EXPR_DOT|EXPR_FNAME) &&
10563 (result == tIDENTIFIER) && /* not EXPR_FNAME, not attrasgn */
10564 (lvar_defined(p, ident) || NUMPARAM_ID_P(ident))) {
10565 SET_LEX_STATE(EXPR_END|EXPR_LABEL);
10571warn_cr(struct parser_params *p)
10575 /* carried over with p->lex.nextline for nextc() */
10576 rb_warn0("encountered \\r in middle of line, treated as a mere space");
10580static enum yytokentype
10581parser_yylex(struct parser_params *p)
10584 int space_seen = 0;
10587 enum lex_state_e last_state;
10588 int fallthru = FALSE;
10589 int token_seen = p->token_seen;
10591 if (p->lex.strterm) {
10592 if (strterm_is_heredoc(p->lex.strterm)) {
10594 return here_document(p, &p->lex.strterm->u.heredoc);
10598 return parse_string(p, &p->lex.strterm->u.literal);
10601 cmd_state = p->command_start;
10602 p->command_start = FALSE;
10603 p->token_seen = TRUE;
10608 last_state = p->lex.state;
10609 switch (c = nextc(p)) {
10610 case '\0': /* NUL */
10611 case '\004': /* ^D */
10612 case '\032': /* ^Z */
10613 case -1: /* end of script. */
10616 if (p->end_expect_token_locations) {
10617 pop_end_expect_token_locations(p);
10618 RUBY_SET_YYLLOC_OF_DUMMY_END(*p->yylloc);
10622 /* Set location for end-of-input because dispatch_scan_event is not called. */
10623 RUBY_SET_YYLLOC(*p->yylloc);
10624 return END_OF_INPUT;
10630 case ' ': case '\t': case '\f':
10631 case '\13': /* '\v' */
10633 while ((c = nextc(p))) {
10638 case ' ': case '\t': case '\f':
10639 case '\13': /* '\v' */
10647 dispatch_scan_event(p, tSP);
10653 case '#': /* it's a comment */
10654 p->token_seen = token_seen;
10655 const char *const pcur = p->lex.pcur, *const ptok = p->lex.ptok;
10656 /* no magic_comment in shebang line */
10657 if (!parser_magic_comment(p, p->lex.pcur, p->lex.pend - p->lex.pcur)) {
10658 if (comment_at_top(p)) {
10659 set_file_encoding(p, p->lex.pcur, p->lex.pend);
10662 p->lex.pcur = pcur, p->lex.ptok = ptok;
10664 dispatch_scan_event(p, tCOMMENT);
10668 p->token_seen = token_seen;
10669 rb_parser_string_t *prevline = p->lex.lastline;
10670 c = (IS_lex_state(EXPR_BEG|EXPR_CLASS|EXPR_FNAME|EXPR_DOT) &&
10671 !IS_lex_state(EXPR_LABELED));
10672 if (c || IS_lex_state_all(EXPR_ARG|EXPR_LABELED)) {
10674 dispatch_scan_event(p, tIGNORED_NL);
10677 if (!c && p->ctxt.in_kwarg) {
10678 goto normal_newline;
10683 switch (c = nextc(p)) {
10684 case ' ': case '\t': case '\f': case '\r':
10685 case '\13': /* '\v' */
10691 dispatch_scan_event(p, tSP);
10697 dispatch_delayed_token(p, tIGNORED_NL);
10698 if (peek(p, '.') == (c == '&')) {
10700 dispatch_scan_event(p, tSP);
10705 p->ruby_sourceline--;
10706 p->lex.nextline = p->lex.lastline;
10707 set_lastline(p, prevline);
10708 case -1: /* EOF no decrement*/
10709 if (c == -1 && space_seen) {
10710 dispatch_scan_event(p, tSP);
10715 RUBY_SET_YYLLOC(*p->yylloc);
10717 goto normal_newline;
10721 p->command_start = TRUE;
10722 SET_LEX_STATE(EXPR_BEG);
10726 if ((c = nextc(p)) == '*') {
10727 if ((c = nextc(p)) == '=') {
10728 set_yylval_id(idPow);
10729 SET_LEX_STATE(EXPR_BEG);
10733 if (IS_SPCARG(c)) {
10734 rb_warning0("'**' interpreted as argument prefix");
10737 else if (IS_BEG()) {
10741 c = warn_balanced((enum ruby_method_ids)tPOW, "**", "argument prefix");
10746 set_yylval_id('*');
10747 SET_LEX_STATE(EXPR_BEG);
10751 if (IS_SPCARG(c)) {
10752 rb_warning0("'*' interpreted as argument prefix");
10755 else if (IS_BEG()) {
10759 c = warn_balanced('*', "*", "argument prefix");
10762 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10767 if (IS_AFTER_OPERATOR()) {
10768 SET_LEX_STATE(EXPR_ARG);
10774 SET_LEX_STATE(EXPR_BEG);
10787 /* skip embedded rd document */
10788 if (word_match_p(p, "begin", 5)) {
10789 int first_p = TRUE;
10792 dispatch_scan_event(p, tEMBDOC_BEG);
10796 dispatch_scan_event(p, tEMBDOC);
10801 compile_error(p, "embedded document meets end of file");
10802 return END_OF_INPUT;
10804 if (c == '=' && word_match_p(p, "end", 3)) {
10810 dispatch_scan_event(p, tEMBDOC_END);
10815 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10816 if ((c = nextc(p)) == '=') {
10817 if ((c = nextc(p)) == '=') {
10826 else if (c == '>') {
10835 !IS_lex_state(EXPR_DOT | EXPR_CLASS) &&
10837 (!IS_ARG() || IS_lex_state(EXPR_LABELED) || space_seen)) {
10838 enum yytokentype token = heredoc_identifier(p);
10839 if (token) return token < 0 ? 0 : token;
10841 if (IS_AFTER_OPERATOR()) {
10842 SET_LEX_STATE(EXPR_ARG);
10845 if (IS_lex_state(EXPR_CLASS))
10846 p->command_start = TRUE;
10847 SET_LEX_STATE(EXPR_BEG);
10850 if ((c = nextc(p)) == '>') {
10857 if ((c = nextc(p)) == '=') {
10858 set_yylval_id(idLTLT);
10859 SET_LEX_STATE(EXPR_BEG);
10863 return warn_balanced((enum ruby_method_ids)tLSHFT, "<<", "here document");
10869 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10870 if ((c = nextc(p)) == '=') {
10874 if ((c = nextc(p)) == '=') {
10875 set_yylval_id(idGTGT);
10876 SET_LEX_STATE(EXPR_BEG);
10886 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10887 p->lex.strterm = NEW_STRTERM(str_dquote | label, '"', 0);
10888 p->lex.ptok = p->lex.pcur-1;
10889 return tSTRING_BEG;
10892 if (IS_lex_state(EXPR_FNAME)) {
10893 SET_LEX_STATE(EXPR_ENDFN);
10896 if (IS_lex_state(EXPR_DOT)) {
10898 SET_LEX_STATE(EXPR_CMDARG);
10900 SET_LEX_STATE(EXPR_ARG);
10903 p->lex.strterm = NEW_STRTERM(str_xquote, '`', 0);
10904 return tXSTRING_BEG;
10907 label = (IS_LABEL_POSSIBLE() ? str_label : 0);
10908 p->lex.strterm = NEW_STRTERM(str_squote | label, '\'', 0);
10909 p->lex.ptok = p->lex.pcur-1;
10910 return tSTRING_BEG;
10913 return parse_qmark(p, space_seen);
10916 if ((c = nextc(p)) == '&') {
10917 SET_LEX_STATE(EXPR_BEG);
10918 if ((c = nextc(p)) == '=') {
10919 set_yylval_id(idANDOP);
10920 SET_LEX_STATE(EXPR_BEG);
10926 else if (c == '=') {
10927 set_yylval_id('&');
10928 SET_LEX_STATE(EXPR_BEG);
10931 else if (c == '.') {
10932 set_yylval_id(idANDDOT);
10933 SET_LEX_STATE(EXPR_DOT);
10937 if (IS_SPCARG(c)) {
10939 (c = peekc_n(p, 1)) == -1 ||
10940 !(c == '\'' || c == '"' ||
10941 is_identchar(p, (p->lex.pcur+1), p->lex.pend, p->enc))) {
10942 rb_warning0("'&' interpreted as argument prefix");
10946 else if (IS_BEG()) {
10950 c = warn_balanced('&', "&", "argument prefix");
10952 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
10956 if ((c = nextc(p)) == '|') {
10957 SET_LEX_STATE(EXPR_BEG);
10958 if ((c = nextc(p)) == '=') {
10959 set_yylval_id(idOROP);
10960 SET_LEX_STATE(EXPR_BEG);
10964 if (IS_lex_state_for(last_state, EXPR_BEG)) {
10972 set_yylval_id('|');
10973 SET_LEX_STATE(EXPR_BEG);
10976 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG|EXPR_LABEL);
10982 if (IS_AFTER_OPERATOR()) {
10983 SET_LEX_STATE(EXPR_ARG);
10991 set_yylval_id('+');
10992 SET_LEX_STATE(EXPR_BEG);
10995 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '+'))) {
10996 SET_LEX_STATE(EXPR_BEG);
10998 if (c != -1 && ISDIGIT(c)) {
10999 return parse_numeric(p, '+');
11003 SET_LEX_STATE(EXPR_BEG);
11005 return warn_balanced('+', "+", "unary operator");
11009 if (IS_AFTER_OPERATOR()) {
11010 SET_LEX_STATE(EXPR_ARG);
11018 set_yylval_id('-');
11019 SET_LEX_STATE(EXPR_BEG);
11023 SET_LEX_STATE(EXPR_ENDFN);
11024 yylval.num = p->lex.lpar_beg;
11025 p->lex.lpar_beg = p->lex.paren_nest;
11028 if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p, '-'))) {
11029 SET_LEX_STATE(EXPR_BEG);
11031 if (c != -1 && ISDIGIT(c)) {
11032 return tUMINUS_NUM;
11036 SET_LEX_STATE(EXPR_BEG);
11038 return warn_balanced('-', "-", "unary operator");
11041 int is_beg = IS_BEG();
11042 SET_LEX_STATE(EXPR_BEG);
11043 if ((c = nextc(p)) == '.') {
11044 if ((c = nextc(p)) == '.') {
11045 if (p->ctxt.in_argdef || IS_LABEL_POSSIBLE()) {
11046 SET_LEX_STATE(EXPR_ENDARG);
11049 if (p->lex.paren_nest == 0 && looking_at_eol_p(p)) {
11050 rb_warn0("... at EOL, should be parenthesized?");
11052 return is_beg ? tBDOT3 : tDOT3;
11055 return is_beg ? tBDOT2 : tDOT2;
11058 if (c != -1 && ISDIGIT(c)) {
11059 char prev = p->lex.pcur-1 > p->lex.pbeg ? *(p->lex.pcur-2) : 0;
11060 parse_numeric(p, '.');
11061 if (ISDIGIT(prev)) {
11062 yyerror0("unexpected fraction part after numeric literal");
11065 yyerror0("no .<digit> floating literal anymore; put 0 before dot");
11067 SET_LEX_STATE(EXPR_END);
11068 p->lex.ptok = p->lex.pcur;
11071 set_yylval_id('.');
11072 SET_LEX_STATE(EXPR_DOT);
11076 case '0': case '1': case '2': case '3': case '4':
11077 case '5': case '6': case '7': case '8': case '9':
11078 return parse_numeric(p, c);
11083 SET_LEX_STATE(EXPR_ENDFN);
11084 p->lex.paren_nest--;
11090 SET_LEX_STATE(EXPR_END);
11091 p->lex.paren_nest--;
11095 /* tSTRING_DEND does COND_POP and CMDARG_POP in the yacc's rule */
11096 if (!p->lex.brace_nest--) return tSTRING_DEND;
11099 SET_LEX_STATE(EXPR_END);
11100 p->lex.paren_nest--;
11106 if (IS_BEG() || IS_lex_state(EXPR_CLASS) || IS_SPCARG(-1)) {
11107 SET_LEX_STATE(EXPR_BEG);
11110 set_yylval_id(idCOLON2);
11111 SET_LEX_STATE(EXPR_DOT);
11114 if (IS_END() || ISSPACE(c) || c == '#') {
11116 c = warn_balanced(':', ":", "symbol literal");
11117 SET_LEX_STATE(EXPR_BEG);
11122 p->lex.strterm = NEW_STRTERM(str_ssym, c, 0);
11125 p->lex.strterm = NEW_STRTERM(str_dsym, c, 0);
11131 SET_LEX_STATE(EXPR_FNAME);
11136 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11137 return tREGEXP_BEG;
11139 if ((c = nextc(p)) == '=') {
11140 set_yylval_id('/');
11141 SET_LEX_STATE(EXPR_BEG);
11145 if (IS_SPCARG(c)) {
11146 arg_ambiguous(p, '/');
11147 p->lex.strterm = NEW_STRTERM(str_regexp, '/', 0);
11148 return tREGEXP_BEG;
11150 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11151 return warn_balanced('/', "/", "regexp literal");
11154 if ((c = nextc(p)) == '=') {
11155 set_yylval_id('^');
11156 SET_LEX_STATE(EXPR_BEG);
11159 SET_LEX_STATE(IS_AFTER_OPERATOR() ? EXPR_ARG : EXPR_BEG);
11164 SET_LEX_STATE(EXPR_BEG);
11165 p->command_start = TRUE;
11169 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11173 if (IS_AFTER_OPERATOR()) {
11174 if ((c = nextc(p)) != '@') {
11177 SET_LEX_STATE(EXPR_ARG);
11180 SET_LEX_STATE(EXPR_BEG);
11188 else if (!space_seen) {
11189 /* foo( ... ) => method call, no ambiguity */
11191 else if (IS_ARG() || IS_lex_state_all(EXPR_END|EXPR_LABEL)) {
11194 else if (IS_lex_state(EXPR_ENDFN) && !lambda_beginning_p()) {
11195 rb_warning0("parentheses after method name is interpreted as "
11196 "an argument list, not a decomposed argument");
11198 p->lex.paren_nest++;
11201 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11205 p->lex.paren_nest++;
11206 if (IS_AFTER_OPERATOR()) {
11207 if ((c = nextc(p)) == ']') {
11208 p->lex.paren_nest--;
11209 SET_LEX_STATE(EXPR_ARG);
11210 if ((c = nextc(p)) == '=') {
11217 SET_LEX_STATE(EXPR_ARG|EXPR_LABEL);
11220 else if (IS_BEG()) {
11223 else if (IS_ARG() && (space_seen || IS_lex_state(EXPR_LABELED))) {
11226 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11232 ++p->lex.brace_nest;
11233 if (lambda_beginning_p())
11235 else if (IS_lex_state(EXPR_LABELED))
11236 c = tLBRACE; /* hash */
11237 else if (IS_lex_state(EXPR_ARG_ANY | EXPR_END | EXPR_ENDFN))
11238 c = '{'; /* block (primary) */
11239 else if (IS_lex_state(EXPR_ENDARG))
11240 c = tLBRACE_ARG; /* block (expr) */
11242 c = tLBRACE; /* hash */
11243 if (c != tLBRACE) {
11244 p->command_start = TRUE;
11245 SET_LEX_STATE(EXPR_BEG);
11248 SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
11250 ++p->lex.paren_nest; /* after lambda_beginning_p() */
11259 dispatch_scan_event(p, tSP);
11260 goto retry; /* skip \\n */
11262 if (c == ' ') return tSP;
11263 if (ISSPACE(c)) return c;
11268 return parse_percent(p, space_seen, last_state);
11271 return parse_gvar(p, last_state);
11274 return parse_atmark(p, last_state);
11277 if (was_bol(p) && whole_match_p(p, "__END__", 7, 0)) {
11278 p->ruby__end__seen = 1;
11282 dispatch_scan_event(p, k__END__);
11284 return END_OF_INPUT;
11290 if (!parser_is_identchar(p)) {
11291 compile_error(p, "Invalid char '\\x%02X' in expression", c);
11300 return parse_ident(p, c, cmd_state);
11303static enum yytokentype
11304yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)
11306 enum yytokentype t;
11310 p->yylloc = yylloc;
11312 t = parser_yylex(p);
11314 if (has_delayed_token(p))
11315 dispatch_delayed_token(p, t);
11316 else if (t != END_OF_INPUT)
11317 dispatch_scan_event(p, t);
11322#define LVAR_USED ((ID)1 << (sizeof(ID) * CHAR_BIT - 1))
11325node_new_internal(struct parser_params *p, enum node_type type, size_t size, size_t alignment)
11327 NODE *n = rb_ast_newnode(p->ast, type, size, alignment);
11329 rb_node_init(n, type);
11334nd_set_loc(NODE *nd, const YYLTYPE *loc)
11337 nd_set_line(nd, loc->beg_pos.lineno);
11342node_newnode(struct parser_params *p, enum node_type type, size_t size, size_t alignment, const rb_code_location_t *loc)
11344 NODE *n = node_new_internal(p, type, size, alignment);
11346 nd_set_loc(n, loc);
11347 nd_set_node_id(n, parser_get_node_id(p));
11351#define NODE_NEWNODE(node_type, type, loc) (type *)(node_newnode(p, node_type, sizeof(type), RUBY_ALIGNOF(type), loc))
11353static rb_node_scope_t *
11354rb_node_scope_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11356 rb_ast_id_table_t *nd_tbl;
11357 nd_tbl = local_tbl(p);
11358 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11359 n->nd_tbl = nd_tbl;
11360 n->nd_body = nd_body;
11361 n->nd_args = nd_args;
11366static rb_node_scope_t *
11367rb_node_scope_new2(struct parser_params *p, rb_ast_id_table_t *nd_tbl, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11369 rb_node_scope_t *n = NODE_NEWNODE(NODE_SCOPE, rb_node_scope_t, loc);
11370 n->nd_tbl = nd_tbl;
11371 n->nd_body = nd_body;
11372 n->nd_args = nd_args;
11377static rb_node_defn_t *
11378rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11380 rb_node_defn_t *n = NODE_NEWNODE(NODE_DEFN, rb_node_defn_t, loc);
11381 n->nd_mid = nd_mid;
11382 n->nd_defn = nd_defn;
11387static rb_node_defs_t *
11388rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc)
11390 rb_node_defs_t *n = NODE_NEWNODE(NODE_DEFS, rb_node_defs_t, loc);
11391 n->nd_recv = nd_recv;
11392 n->nd_mid = nd_mid;
11393 n->nd_defn = nd_defn;
11398static rb_node_block_t *
11399rb_node_block_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11401 rb_node_block_t *n = NODE_NEWNODE(NODE_BLOCK, rb_node_block_t, loc);
11402 n->nd_head = nd_head;
11403 n->nd_end = (NODE *)n;
11409static rb_node_for_t *
11410rb_node_for_new(struct parser_params *p, NODE *nd_iter, NODE *nd_body, const YYLTYPE *loc)
11412 rb_node_for_t *n = NODE_NEWNODE(NODE_FOR, rb_node_for_t, loc);
11413 n->nd_body = nd_body;
11414 n->nd_iter = nd_iter;
11419static rb_node_for_masgn_t *
11420rb_node_for_masgn_new(struct parser_params *p, NODE *nd_var, const YYLTYPE *loc)
11422 rb_node_for_masgn_t *n = NODE_NEWNODE(NODE_FOR_MASGN, rb_node_for_masgn_t, loc);
11423 n->nd_var = nd_var;
11428static rb_node_retry_t *
11429rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc)
11431 rb_node_retry_t *n = NODE_NEWNODE(NODE_RETRY, rb_node_retry_t, loc);
11436static rb_node_begin_t *
11437rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
11439 rb_node_begin_t *n = NODE_NEWNODE(NODE_BEGIN, rb_node_begin_t, loc);
11440 n->nd_body = nd_body;
11445static rb_node_rescue_t *
11446rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc)
11448 rb_node_rescue_t *n = NODE_NEWNODE(NODE_RESCUE, rb_node_rescue_t, loc);
11449 n->nd_head = nd_head;
11450 n->nd_resq = nd_resq;
11451 n->nd_else = nd_else;
11456static rb_node_resbody_t *
11457rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11459 rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
11460 n->nd_args = nd_args;
11461 n->nd_exc_var = nd_exc_var;
11462 n->nd_body = nd_body;
11463 n->nd_next = nd_next;
11468static rb_node_ensure_t *
11469rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc)
11471 rb_node_ensure_t *n = NODE_NEWNODE(NODE_ENSURE, rb_node_ensure_t, loc);
11472 n->nd_head = nd_head;
11473 n->nd_ensr = nd_ensr;
11478static rb_node_and_t *
11479rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11481 rb_node_and_t *n = NODE_NEWNODE(NODE_AND, rb_node_and_t, loc);
11482 n->nd_1st = nd_1st;
11483 n->nd_2nd = nd_2nd;
11484 n->operator_loc = *operator_loc;
11489static rb_node_or_t *
11490rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *operator_loc)
11492 rb_node_or_t *n = NODE_NEWNODE(NODE_OR, rb_node_or_t, loc);
11493 n->nd_1st = nd_1st;
11494 n->nd_2nd = nd_2nd;
11495 n->operator_loc = *operator_loc;
11500static rb_node_return_t *
11501rb_node_return_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
11503 rb_node_return_t *n = NODE_NEWNODE(NODE_RETURN, rb_node_return_t, loc);
11504 n->nd_stts = nd_stts;
11505 n->keyword_loc = *keyword_loc;
11509static rb_node_yield_t *
11510rb_node_yield_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11512 rb_node_yield_t *n = NODE_NEWNODE(NODE_YIELD, rb_node_yield_t, loc);
11513 n->nd_head = nd_head;
11518static rb_node_if_t *
11519rb_node_if_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc)
11521 rb_node_if_t *n = NODE_NEWNODE(NODE_IF, rb_node_if_t, loc);
11522 n->nd_cond = nd_cond;
11523 n->nd_body = nd_body;
11524 n->nd_else = nd_else;
11529static rb_node_unless_t *
11530rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *nd_else, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc)
11532 rb_node_unless_t *n = NODE_NEWNODE(NODE_UNLESS, rb_node_unless_t, loc);
11533 n->nd_cond = nd_cond;
11534 n->nd_body = nd_body;
11535 n->nd_else = nd_else;
11536 n->keyword_loc = *keyword_loc;
11537 n->then_keyword_loc = *then_keyword_loc;
11538 n->end_keyword_loc = *end_keyword_loc;
11543static rb_node_class_t *
11544rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc)
11546 /* Keep the order of node creation */
11547 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11548 rb_node_class_t *n = NODE_NEWNODE(NODE_CLASS, rb_node_class_t, loc);
11549 n->nd_cpath = nd_cpath;
11550 n->nd_body = scope;
11551 n->nd_super = nd_super;
11556static rb_node_sclass_t *
11557rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc)
11559 /* Keep the order of node creation */
11560 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11561 rb_node_sclass_t *n = NODE_NEWNODE(NODE_SCLASS, rb_node_sclass_t, loc);
11562 n->nd_recv = nd_recv;
11563 n->nd_body = scope;
11568static rb_node_module_t *
11569rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11571 /* Keep the order of node creation */
11572 NODE *scope = NEW_SCOPE(0, nd_body, loc);
11573 rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
11574 n->nd_cpath = nd_cpath;
11575 n->nd_body = scope;
11580static rb_node_iter_t *
11581rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11583 /* Keep the order of node creation */
11584 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11585 rb_node_iter_t *n = NODE_NEWNODE(NODE_ITER, rb_node_iter_t, loc);
11586 n->nd_body = scope;
11592static rb_node_lambda_t *
11593rb_node_lambda_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc)
11595 /* Keep the order of node creation */
11596 NODE *scope = NEW_SCOPE(nd_args, nd_body, loc);
11597 rb_node_lambda_t *n = NODE_NEWNODE(NODE_LAMBDA, rb_node_lambda_t, loc);
11598 n->nd_body = scope;
11603static rb_node_case_t *
11604rb_node_case_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11606 rb_node_case_t *n = NODE_NEWNODE(NODE_CASE, rb_node_case_t, loc);
11607 n->nd_head = nd_head;
11608 n->nd_body = nd_body;
11609 n->case_keyword_loc = *case_keyword_loc;
11610 n->end_keyword_loc = *end_keyword_loc;
11615static rb_node_case2_t *
11616rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11618 rb_node_case2_t *n = NODE_NEWNODE(NODE_CASE2, rb_node_case2_t, loc);
11620 n->nd_body = nd_body;
11621 n->case_keyword_loc = *case_keyword_loc;
11622 n->end_keyword_loc = *end_keyword_loc;
11627static rb_node_case3_t *
11628rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc)
11630 rb_node_case3_t *n = NODE_NEWNODE(NODE_CASE3, rb_node_case3_t, loc);
11631 n->nd_head = nd_head;
11632 n->nd_body = nd_body;
11633 n->case_keyword_loc = *case_keyword_loc;
11634 n->end_keyword_loc = *end_keyword_loc;
11639static rb_node_when_t *
11640rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc)
11642 rb_node_when_t *n = NODE_NEWNODE(NODE_WHEN, rb_node_when_t, loc);
11643 n->nd_head = nd_head;
11644 n->nd_body = nd_body;
11645 n->nd_next = nd_next;
11646 n->keyword_loc = *keyword_loc;
11647 n->then_keyword_loc = *then_keyword_loc;
11652static rb_node_in_t *
11653rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11655 rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
11656 n->nd_head = nd_head;
11657 n->nd_body = nd_body;
11658 n->nd_next = nd_next;
11663static rb_node_while_t *
11664rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc)
11666 rb_node_while_t *n = NODE_NEWNODE(NODE_WHILE, rb_node_while_t, loc);
11667 n->nd_cond = nd_cond;
11668 n->nd_body = nd_body;
11669 n->nd_state = nd_state;
11670 n->keyword_loc = *keyword_loc;
11671 n->closing_loc = *closing_loc;
11676static rb_node_until_t *
11677rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc)
11679 rb_node_until_t *n = NODE_NEWNODE(NODE_UNTIL, rb_node_until_t, loc);
11680 n->nd_cond = nd_cond;
11681 n->nd_body = nd_body;
11682 n->nd_state = nd_state;
11683 n->keyword_loc = *keyword_loc;
11684 n->closing_loc = *closing_loc;
11689static rb_node_colon2_t *
11690rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11692 rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
11693 n->nd_head = nd_head;
11694 n->nd_mid = nd_mid;
11699static rb_node_colon3_t *
11700rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11702 rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
11703 n->nd_mid = nd_mid;
11708static rb_node_dot2_t *
11709rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11711 rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
11712 n->nd_beg = nd_beg;
11713 n->nd_end = nd_end;
11718static rb_node_dot3_t *
11719rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
11721 rb_node_dot3_t *n = NODE_NEWNODE(NODE_DOT3, rb_node_dot3_t, loc);
11722 n->nd_beg = nd_beg;
11723 n->nd_end = nd_end;
11728static rb_node_self_t *
11729rb_node_self_new(struct parser_params *p, const YYLTYPE *loc)
11731 rb_node_self_t *n = NODE_NEWNODE(NODE_SELF, rb_node_self_t, loc);
11737static rb_node_nil_t *
11738rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc)
11740 rb_node_nil_t *n = NODE_NEWNODE(NODE_NIL, rb_node_nil_t, loc);
11745static rb_node_true_t *
11746rb_node_true_new(struct parser_params *p, const YYLTYPE *loc)
11748 rb_node_true_t *n = NODE_NEWNODE(NODE_TRUE, rb_node_true_t, loc);
11753static rb_node_false_t *
11754rb_node_false_new(struct parser_params *p, const YYLTYPE *loc)
11756 rb_node_false_t *n = NODE_NEWNODE(NODE_FALSE, rb_node_false_t, loc);
11761static rb_node_super_t *
11762rb_node_super_new(struct parser_params *p, NODE *nd_args, const YYLTYPE *loc)
11764 rb_node_super_t *n = NODE_NEWNODE(NODE_SUPER, rb_node_super_t, loc);
11765 n->nd_args = nd_args;
11770static rb_node_zsuper_t *
11771rb_node_zsuper_new(struct parser_params *p, const YYLTYPE *loc)
11773 rb_node_zsuper_t *n = NODE_NEWNODE(NODE_ZSUPER, rb_node_zsuper_t, loc);
11778static rb_node_match2_t *
11779rb_node_match2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11781 rb_node_match2_t *n = NODE_NEWNODE(NODE_MATCH2, rb_node_match2_t, loc);
11782 n->nd_recv = nd_recv;
11783 n->nd_value = nd_value;
11789static rb_node_match3_t *
11790rb_node_match3_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, const YYLTYPE *loc)
11792 rb_node_match3_t *n = NODE_NEWNODE(NODE_MATCH3, rb_node_match3_t, loc);
11793 n->nd_recv = nd_recv;
11794 n->nd_value = nd_value;
11799/* TODO: Use union for NODE_LIST2 */
11800static rb_node_list_t *
11801rb_node_list_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11803 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11804 n->nd_head = nd_head;
11811static rb_node_list_t *
11812rb_node_list_new2(struct parser_params *p, NODE *nd_head, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
11814 rb_node_list_t *n = NODE_NEWNODE(NODE_LIST, rb_node_list_t, loc);
11815 n->nd_head = nd_head;
11816 n->as.nd_alen = nd_alen;
11817 n->nd_next = nd_next;
11822static rb_node_zlist_t *
11823rb_node_zlist_new(struct parser_params *p, const YYLTYPE *loc)
11825 rb_node_zlist_t *n = NODE_NEWNODE(NODE_ZLIST, rb_node_zlist_t, loc);
11830static rb_node_hash_t *
11831rb_node_hash_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
11833 rb_node_hash_t *n = NODE_NEWNODE(NODE_HASH, rb_node_hash_t, loc);
11834 n->nd_head = nd_head;
11840static rb_node_masgn_t *
11841rb_node_masgn_new(struct parser_params *p, NODE *nd_head, NODE *nd_args, const YYLTYPE *loc)
11843 rb_node_masgn_t *n = NODE_NEWNODE(NODE_MASGN, rb_node_masgn_t, loc);
11844 n->nd_head = nd_head;
11846 n->nd_args = nd_args;
11851static rb_node_gasgn_t *
11852rb_node_gasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11854 rb_node_gasgn_t *n = NODE_NEWNODE(NODE_GASGN, rb_node_gasgn_t, loc);
11855 n->nd_vid = nd_vid;
11856 n->nd_value = nd_value;
11861static rb_node_lasgn_t *
11862rb_node_lasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11864 rb_node_lasgn_t *n = NODE_NEWNODE(NODE_LASGN, rb_node_lasgn_t, loc);
11865 n->nd_vid = nd_vid;
11866 n->nd_value = nd_value;
11871static rb_node_dasgn_t *
11872rb_node_dasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11874 rb_node_dasgn_t *n = NODE_NEWNODE(NODE_DASGN, rb_node_dasgn_t, loc);
11875 n->nd_vid = nd_vid;
11876 n->nd_value = nd_value;
11881static rb_node_iasgn_t *
11882rb_node_iasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11884 rb_node_iasgn_t *n = NODE_NEWNODE(NODE_IASGN, rb_node_iasgn_t, loc);
11885 n->nd_vid = nd_vid;
11886 n->nd_value = nd_value;
11891static rb_node_cvasgn_t *
11892rb_node_cvasgn_new(struct parser_params *p, ID nd_vid, NODE *nd_value, const YYLTYPE *loc)
11894 rb_node_cvasgn_t *n = NODE_NEWNODE(NODE_CVASGN, rb_node_cvasgn_t, loc);
11895 n->nd_vid = nd_vid;
11896 n->nd_value = nd_value;
11901static rb_node_op_asgn1_t *
11902rb_node_op_asgn1_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *index, NODE *rvalue, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
11904 rb_node_op_asgn1_t *n = NODE_NEWNODE(NODE_OP_ASGN1, rb_node_op_asgn1_t, loc);
11905 n->nd_recv = nd_recv;
11906 n->nd_mid = nd_mid;
11907 n->nd_index = index;
11908 n->nd_rvalue = rvalue;
11909 n->call_operator_loc = *call_operator_loc;
11910 n->opening_loc = *opening_loc;
11911 n->closing_loc = *closing_loc;
11912 n->binary_operator_loc = *binary_operator_loc;
11917static rb_node_op_asgn2_t *
11918rb_node_op_asgn2_new(struct parser_params *p, NODE *nd_recv, NODE *nd_value, ID nd_vid, ID nd_mid, bool nd_aid, const YYLTYPE *loc, const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
11920 rb_node_op_asgn2_t *n = NODE_NEWNODE(NODE_OP_ASGN2, rb_node_op_asgn2_t, loc);
11921 n->nd_recv = nd_recv;
11922 n->nd_value = nd_value;
11923 n->nd_vid = nd_vid;
11924 n->nd_mid = nd_mid;
11925 n->nd_aid = nd_aid;
11926 n->call_operator_loc = *call_operator_loc;
11927 n->message_loc = *message_loc;
11928 n->binary_operator_loc = *binary_operator_loc;
11933static rb_node_op_asgn_or_t *
11934rb_node_op_asgn_or_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11936 rb_node_op_asgn_or_t *n = NODE_NEWNODE(NODE_OP_ASGN_OR, rb_node_op_asgn_or_t, loc);
11937 n->nd_head = nd_head;
11938 n->nd_value = nd_value;
11943static rb_node_op_asgn_and_t *
11944rb_node_op_asgn_and_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, const YYLTYPE *loc)
11946 rb_node_op_asgn_and_t *n = NODE_NEWNODE(NODE_OP_ASGN_AND, rb_node_op_asgn_and_t, loc);
11947 n->nd_head = nd_head;
11948 n->nd_value = nd_value;
11953static rb_node_gvar_t *
11954rb_node_gvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11956 rb_node_gvar_t *n = NODE_NEWNODE(NODE_GVAR, rb_node_gvar_t, loc);
11957 n->nd_vid = nd_vid;
11962static rb_node_lvar_t *
11963rb_node_lvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11965 rb_node_lvar_t *n = NODE_NEWNODE(NODE_LVAR, rb_node_lvar_t, loc);
11966 n->nd_vid = nd_vid;
11971static rb_node_dvar_t *
11972rb_node_dvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11974 rb_node_dvar_t *n = NODE_NEWNODE(NODE_DVAR, rb_node_dvar_t, loc);
11975 n->nd_vid = nd_vid;
11980static rb_node_ivar_t *
11981rb_node_ivar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11983 rb_node_ivar_t *n = NODE_NEWNODE(NODE_IVAR, rb_node_ivar_t, loc);
11984 n->nd_vid = nd_vid;
11989static rb_node_const_t *
11990rb_node_const_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
11992 rb_node_const_t *n = NODE_NEWNODE(NODE_CONST, rb_node_const_t, loc);
11993 n->nd_vid = nd_vid;
11998static rb_node_cvar_t *
11999rb_node_cvar_new(struct parser_params *p, ID nd_vid, const YYLTYPE *loc)
12001 rb_node_cvar_t *n = NODE_NEWNODE(NODE_CVAR, rb_node_cvar_t, loc);
12002 n->nd_vid = nd_vid;
12007static rb_node_nth_ref_t *
12008rb_node_nth_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12010 rb_node_nth_ref_t *n = NODE_NEWNODE(NODE_NTH_REF, rb_node_nth_ref_t, loc);
12011 n->nd_nth = nd_nth;
12016static rb_node_back_ref_t *
12017rb_node_back_ref_new(struct parser_params *p, long nd_nth, const YYLTYPE *loc)
12019 rb_node_back_ref_t *n = NODE_NEWNODE(NODE_BACK_REF, rb_node_back_ref_t, loc);
12020 n->nd_nth = nd_nth;
12025static rb_node_integer_t *
12026rb_node_integer_new(struct parser_params *p, char* val, int base, const YYLTYPE *loc)
12028 rb_node_integer_t *n = NODE_NEWNODE(NODE_INTEGER, rb_node_integer_t, loc);
12036static rb_node_float_t *
12037rb_node_float_new(struct parser_params *p, char* val, const YYLTYPE *loc)
12039 rb_node_float_t *n = NODE_NEWNODE(NODE_FLOAT, rb_node_float_t, loc);
12046static rb_node_rational_t *
12047rb_node_rational_new(struct parser_params *p, char* val, int base, int seen_point, const YYLTYPE *loc)
12049 rb_node_rational_t *n = NODE_NEWNODE(NODE_RATIONAL, rb_node_rational_t, loc);
12053 n->seen_point = seen_point;
12058static rb_node_imaginary_t *
12059rb_node_imaginary_new(struct parser_params *p, char* val, int base, int seen_point, enum rb_numeric_type numeric_type, const YYLTYPE *loc)
12061 rb_node_imaginary_t *n = NODE_NEWNODE(NODE_IMAGINARY, rb_node_imaginary_t, loc);
12065 n->seen_point = seen_point;
12066 n->type = numeric_type;
12071static rb_node_str_t *
12072rb_node_str_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12074 rb_node_str_t *n = NODE_NEWNODE(NODE_STR, rb_node_str_t, loc);
12075 n->string = string;
12080/* TODO; Use union for NODE_DSTR2 */
12081static rb_node_dstr_t *
12082rb_node_dstr_new0(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12084 rb_node_dstr_t *n = NODE_NEWNODE(NODE_DSTR, rb_node_dstr_t, loc);
12085 n->string = string;
12086 n->as.nd_alen = nd_alen;
12087 n->nd_next = (rb_node_list_t *)nd_next;
12092static rb_node_dstr_t *
12093rb_node_dstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12095 return rb_node_dstr_new0(p, string, 1, 0, loc);
12098static rb_node_xstr_t *
12099rb_node_xstr_new(struct parser_params *p, rb_parser_string_t *string, const YYLTYPE *loc)
12101 rb_node_xstr_t *n = NODE_NEWNODE(NODE_XSTR, rb_node_xstr_t, loc);
12102 n->string = string;
12107static rb_node_dxstr_t *
12108rb_node_dxstr_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12110 rb_node_dxstr_t *n = NODE_NEWNODE(NODE_DXSTR, rb_node_dxstr_t, loc);
12111 n->string = string;
12112 n->as.nd_alen = nd_alen;
12113 n->nd_next = (rb_node_list_t *)nd_next;
12118static rb_node_sym_t *
12119rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12121 rb_node_sym_t *n = NODE_NEWNODE(NODE_SYM, rb_node_sym_t, loc);
12122 n->string = rb_str_to_parser_string(p, str);
12127static rb_node_dsym_t *
12128rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc)
12130 rb_node_dsym_t *n = NODE_NEWNODE(NODE_DSYM, rb_node_dsym_t, loc);
12131 n->string = string;
12132 n->as.nd_alen = nd_alen;
12133 n->nd_next = (rb_node_list_t *)nd_next;
12138static rb_node_evstr_t *
12139rb_node_evstr_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12141 rb_node_evstr_t *n = NODE_NEWNODE(NODE_EVSTR, rb_node_evstr_t, loc);
12142 n->nd_body = nd_body;
12147static rb_node_regx_t *
12148rb_node_regx_new(struct parser_params *p, rb_parser_string_t *string, int options, const YYLTYPE *loc)
12150 rb_node_regx_t *n = NODE_NEWNODE(NODE_REGX, rb_node_regx_t, loc);
12151 n->string = string;
12152 n->options = options & RE_OPTION_MASK;
12157static rb_node_call_t *
12158rb_node_call_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12160 rb_node_call_t *n = NODE_NEWNODE(NODE_CALL, rb_node_call_t, loc);
12161 n->nd_recv = nd_recv;
12162 n->nd_mid = nd_mid;
12163 n->nd_args = nd_args;
12168static rb_node_opcall_t *
12169rb_node_opcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12171 rb_node_opcall_t *n = NODE_NEWNODE(NODE_OPCALL, rb_node_opcall_t, loc);
12172 n->nd_recv = nd_recv;
12173 n->nd_mid = nd_mid;
12174 n->nd_args = nd_args;
12179static rb_node_fcall_t *
12180rb_node_fcall_new(struct parser_params *p, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12182 rb_node_fcall_t *n = NODE_NEWNODE(NODE_FCALL, rb_node_fcall_t, loc);
12183 n->nd_mid = nd_mid;
12184 n->nd_args = nd_args;
12189static rb_node_qcall_t *
12190rb_node_qcall_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12192 rb_node_qcall_t *n = NODE_NEWNODE(NODE_QCALL, rb_node_qcall_t, loc);
12193 n->nd_recv = nd_recv;
12194 n->nd_mid = nd_mid;
12195 n->nd_args = nd_args;
12200static rb_node_vcall_t *
12201rb_node_vcall_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
12203 rb_node_vcall_t *n = NODE_NEWNODE(NODE_VCALL, rb_node_vcall_t, loc);
12204 n->nd_mid = nd_mid;
12209static rb_node_once_t *
12210rb_node_once_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12212 rb_node_once_t *n = NODE_NEWNODE(NODE_ONCE, rb_node_once_t, loc);
12213 n->nd_body = nd_body;
12218static rb_node_args_t *
12219rb_node_args_new(struct parser_params *p, const YYLTYPE *loc)
12221 rb_node_args_t *n = NODE_NEWNODE(NODE_ARGS, rb_node_args_t, loc);
12222 MEMZERO(&n->nd_ainfo, struct rb_args_info, 1);
12227static rb_node_args_aux_t *
12228rb_node_args_aux_new(struct parser_params *p, ID nd_pid, int nd_plen, const YYLTYPE *loc)
12230 rb_node_args_aux_t *n = NODE_NEWNODE(NODE_ARGS_AUX, rb_node_args_aux_t, loc);
12231 n->nd_pid = nd_pid;
12232 n->nd_plen = nd_plen;
12238static rb_node_opt_arg_t *
12239rb_node_opt_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12241 rb_node_opt_arg_t *n = NODE_NEWNODE(NODE_OPT_ARG, rb_node_opt_arg_t, loc);
12242 n->nd_body = nd_body;
12248static rb_node_kw_arg_t *
12249rb_node_kw_arg_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12251 rb_node_kw_arg_t *n = NODE_NEWNODE(NODE_KW_ARG, rb_node_kw_arg_t, loc);
12252 n->nd_body = nd_body;
12258static rb_node_postarg_t *
12259rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc)
12261 rb_node_postarg_t *n = NODE_NEWNODE(NODE_POSTARG, rb_node_postarg_t, loc);
12262 n->nd_1st = nd_1st;
12263 n->nd_2nd = nd_2nd;
12268static rb_node_argscat_t *
12269rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12271 rb_node_argscat_t *n = NODE_NEWNODE(NODE_ARGSCAT, rb_node_argscat_t, loc);
12272 n->nd_head = nd_head;
12273 n->nd_body = nd_body;
12278static rb_node_argspush_t *
12279rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc)
12281 rb_node_argspush_t *n = NODE_NEWNODE(NODE_ARGSPUSH, rb_node_argspush_t, loc);
12282 n->nd_head = nd_head;
12283 n->nd_body = nd_body;
12288static rb_node_splat_t *
12289rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12291 rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
12292 n->nd_head = nd_head;
12293 n->operator_loc = *operator_loc;
12298static rb_node_block_pass_t *
12299rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_loc)
12301 rb_node_block_pass_t *n = NODE_NEWNODE(NODE_BLOCK_PASS, rb_node_block_pass_t, loc);
12304 n->nd_body = nd_body;
12305 n->operator_loc = *operator_loc;
12310static rb_node_alias_t *
12311rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12313 rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
12314 n->nd_1st = nd_1st;
12315 n->nd_2nd = nd_2nd;
12316 n->keyword_loc = *keyword_loc;
12321static rb_node_valias_t *
12322rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12324 rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
12325 n->nd_alias = nd_alias;
12326 n->nd_orig = nd_orig;
12327 n->keyword_loc = *keyword_loc;
12332static rb_node_undef_t *
12333rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc)
12335 rb_node_undef_t *n = NODE_NEWNODE(NODE_UNDEF, rb_node_undef_t, loc);
12336 n->nd_undefs = rb_parser_ary_new_capa_for_node(p, 1);
12337 n->keyword_loc = NULL_LOC;
12338 rb_parser_ary_push_node(p, n->nd_undefs, nd_undef);
12343static rb_node_errinfo_t *
12344rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc)
12346 rb_node_errinfo_t *n = NODE_NEWNODE(NODE_ERRINFO, rb_node_errinfo_t, loc);
12351static rb_node_defined_t *
12352rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
12354 rb_node_defined_t *n = NODE_NEWNODE(NODE_DEFINED, rb_node_defined_t, loc);
12355 n->nd_head = nd_head;
12360static rb_node_postexe_t *
12361rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12363 rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
12364 n->nd_body = nd_body;
12369static rb_node_attrasgn_t *
12370rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc)
12372 rb_node_attrasgn_t *n = NODE_NEWNODE(NODE_ATTRASGN, rb_node_attrasgn_t, loc);
12373 n->nd_recv = nd_recv;
12374 n->nd_mid = nd_mid;
12375 n->nd_args = nd_args;
12380static rb_node_aryptn_t *
12381rb_node_aryptn_new(struct parser_params *p, NODE *pre_args, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
12383 rb_node_aryptn_t *n = NODE_NEWNODE(NODE_ARYPTN, rb_node_aryptn_t, loc);
12385 n->pre_args = pre_args;
12386 n->rest_arg = rest_arg;
12387 n->post_args = post_args;
12392static rb_node_hshptn_t *
12393rb_node_hshptn_new(struct parser_params *p, NODE *nd_pconst, NODE *nd_pkwargs, NODE *nd_pkwrestarg, const YYLTYPE *loc)
12395 rb_node_hshptn_t *n = NODE_NEWNODE(NODE_HSHPTN, rb_node_hshptn_t, loc);
12396 n->nd_pconst = nd_pconst;
12397 n->nd_pkwargs = nd_pkwargs;
12398 n->nd_pkwrestarg = nd_pkwrestarg;
12403static rb_node_fndptn_t *
12404rb_node_fndptn_new(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
12406 rb_node_fndptn_t *n = NODE_NEWNODE(NODE_FNDPTN, rb_node_fndptn_t, loc);
12408 n->pre_rest_arg = pre_rest_arg;
12410 n->post_rest_arg = post_rest_arg;
12415static rb_node_line_t *
12416rb_node_line_new(struct parser_params *p, const YYLTYPE *loc)
12418 rb_node_line_t *n = NODE_NEWNODE(NODE_LINE, rb_node_line_t, loc);
12423static rb_node_file_t *
12424rb_node_file_new(struct parser_params *p, VALUE str, const YYLTYPE *loc)
12426 rb_node_file_t *n = NODE_NEWNODE(NODE_FILE, rb_node_file_t, loc);
12427 n->path = rb_str_to_parser_string(p, str);
12432static rb_node_encoding_t *
12433rb_node_encoding_new(struct parser_params *p, const YYLTYPE *loc)
12435 rb_node_encoding_t *n = NODE_NEWNODE(NODE_ENCODING, rb_node_encoding_t, loc);
12441static rb_node_cdecl_t *
12442rb_node_cdecl_new(struct parser_params *p, ID nd_vid, NODE *nd_value, NODE *nd_else, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12444 rb_node_cdecl_t *n = NODE_NEWNODE(NODE_CDECL, rb_node_cdecl_t, loc);
12445 n->nd_vid = nd_vid;
12446 n->nd_value = nd_value;
12447 n->nd_else = nd_else;
12448 n->shareability = shareability;
12453static rb_node_op_cdecl_t *
12454rb_node_op_cdecl_new(struct parser_params *p, NODE *nd_head, NODE *nd_value, ID nd_aid, enum rb_parser_shareability shareability, const YYLTYPE *loc)
12456 rb_node_op_cdecl_t *n = NODE_NEWNODE(NODE_OP_CDECL, rb_node_op_cdecl_t, loc);
12457 n->nd_head = nd_head;
12458 n->nd_value = nd_value;
12459 n->nd_aid = nd_aid;
12460 n->shareability = shareability;
12465static rb_node_error_t *
12466rb_node_error_new(struct parser_params *p, const YYLTYPE *loc)
12468 rb_node_error_t *n = NODE_NEWNODE(NODE_ERROR, rb_node_error_t, loc);
12473static rb_node_break_t *
12474rb_node_break_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12476 rb_node_break_t *n = NODE_NEWNODE(NODE_BREAK, rb_node_break_t, loc);
12477 n->nd_stts = nd_stts;
12479 n->keyword_loc = *keyword_loc;
12484static rb_node_next_t *
12485rb_node_next_new(struct parser_params *p, NODE *nd_stts, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12487 rb_node_next_t *n = NODE_NEWNODE(NODE_NEXT, rb_node_next_t, loc);
12488 n->nd_stts = nd_stts;
12490 n->keyword_loc = *keyword_loc;
12495static rb_node_redo_t *
12496rb_node_redo_new(struct parser_params *p, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
12498 rb_node_redo_t *n = NODE_NEWNODE(NODE_REDO, rb_node_redo_t, loc);
12500 n->keyword_loc = *keyword_loc;
12505static rb_node_def_temp_t *
12506rb_node_def_temp_new(struct parser_params *p, const YYLTYPE *loc)
12508 rb_node_def_temp_t *n = NODE_NEWNODE((enum node_type)NODE_DEF_TEMP, rb_node_def_temp_t, loc);
12509 n->save.numparam_save = 0;
12510 n->save.max_numparam = 0;
12511 n->save.ctxt = p->ctxt;
12518static rb_node_def_temp_t *
12519def_head_save(struct parser_params *p, rb_node_def_temp_t *n)
12521 n->save.numparam_save = numparam_push(p);
12522 n->save.max_numparam = p->max_numparam;
12527static enum node_type
12528nodetype(NODE *node) /* for debug */
12530 return (enum node_type)nd_type(node);
12534nodeline(NODE *node)
12536 return nd_line(node);
12541newline_node(NODE *node)
12544 node = remove_begin(node);
12545 nd_set_fl_newline(node);
12551fixpos(NODE *node, NODE *orig)
12555 nd_set_line(node, nd_line(orig));
12559block_append(struct parser_params *p, NODE *head, NODE *tail)
12561 NODE *end, *h = head, *nd;
12563 if (tail == 0) return head;
12565 if (h == 0) return tail;
12566 switch (nd_type(h)) {
12568 h = end = NEW_BLOCK(head, &head->nd_loc);
12572 end = RNODE_BLOCK(h)->nd_end;
12576 nd = RNODE_BLOCK(end)->nd_head;
12577 switch (nd_type(nd)) {
12583 rb_warning0L(nd_line(tail), "statement not reached");
12590 if (!nd_type_p(tail, NODE_BLOCK)) {
12591 tail = NEW_BLOCK(tail, &tail->nd_loc);
12593 RNODE_BLOCK(end)->nd_next = tail;
12594 RNODE_BLOCK(h)->nd_end = RNODE_BLOCK(tail)->nd_end;
12595 nd_set_last_loc(head, nd_last_loc(tail));
12599/* append item to the list */
12601list_append(struct parser_params *p, NODE *list, NODE *item)
12605 if (list == 0) return NEW_LIST(item, &item->nd_loc);
12606 if (RNODE_LIST(list)->nd_next) {
12607 last = RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end;
12613 RNODE_LIST(list)->as.nd_alen += 1;
12614 RNODE_LIST(last)->nd_next = NEW_LIST(item, &item->nd_loc);
12615 RNODE_LIST(RNODE_LIST(list)->nd_next)->as.nd_end = RNODE_LIST(last)->nd_next;
12617 nd_set_last_loc(list, nd_last_loc(item));
12622/* concat two lists */
12624list_concat(NODE *head, NODE *tail)
12628 if (RNODE_LIST(head)->nd_next) {
12629 last = RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end;
12635 RNODE_LIST(head)->as.nd_alen += RNODE_LIST(tail)->as.nd_alen;
12636 RNODE_LIST(last)->nd_next = tail;
12637 if (RNODE_LIST(tail)->nd_next) {
12638 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = RNODE_LIST(RNODE_LIST(tail)->nd_next)->as.nd_end;
12641 RNODE_LIST(RNODE_LIST(head)->nd_next)->as.nd_end = tail;
12644 nd_set_last_loc(head, nd_last_loc(tail));
12650literal_concat0(struct parser_params *p, rb_parser_string_t *head, rb_parser_string_t *tail)
12652 if (!tail) return 1;
12653 if (!rb_parser_enc_compatible(p, head, tail)) {
12654 compile_error(p, "string literal encodings differ (%s / %s)",
12655 rb_enc_name(rb_parser_str_get_encoding(head)),
12656 rb_enc_name(rb_parser_str_get_encoding(tail)));
12657 rb_parser_str_resize(p, head, 0);
12658 rb_parser_str_resize(p, tail, 0);
12661 rb_parser_str_buf_append(p, head, tail);
12665static rb_parser_string_t *
12666string_literal_head(struct parser_params *p, enum node_type htype, NODE *head)
12668 if (htype != NODE_DSTR) return NULL;
12669 if (RNODE_DSTR(head)->nd_next) {
12670 head = RNODE_LIST(RNODE_LIST(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_head;
12671 if (!head || !nd_type_p(head, NODE_STR)) return NULL;
12673 rb_parser_string_t *lit = RNODE_DSTR(head)->string;
12679static rb_parser_string_t *
12680rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *orig)
12682 rb_parser_string_t *copy;
12683 if (!orig) return NULL;
12684 copy = rb_parser_string_new(p, PARSER_STRING_PTR(orig), PARSER_STRING_LEN(orig));
12685 copy->coderange = orig->coderange;
12686 copy->enc = orig->enc;
12691/* concat two string literals */
12693literal_concat(struct parser_params *p, NODE *head, NODE *tail, const YYLTYPE *loc)
12695 enum node_type htype;
12696 rb_parser_string_t *lit;
12698 if (!head) return tail;
12699 if (!tail) return head;
12701 htype = nd_type(head);
12702 if (htype == NODE_EVSTR) {
12703 head = new_dstr(p, head, loc);
12706 if (p->heredoc_indent > 0) {
12709 head = str2dstr(p, head);
12711 return list_append(p, head, tail);
12716 switch (nd_type(tail)) {
12718 if ((lit = string_literal_head(p, htype, head)) != false) {
12722 lit = RNODE_DSTR(head)->string;
12724 if (htype == NODE_STR) {
12725 if (!literal_concat0(p, lit, RNODE_STR(tail)->string)) {
12727 rb_discard_node(p, head);
12728 rb_discard_node(p, tail);
12731 rb_discard_node(p, tail);
12734 list_append(p, head, tail);
12739 if (htype == NODE_STR) {
12740 if (!literal_concat0(p, RNODE_STR(head)->string, RNODE_DSTR(tail)->string))
12742 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12743 RNODE_DSTR(tail)->string = RNODE_STR(head)->string;
12744 RNODE_STR(head)->string = NULL;
12745 rb_discard_node(p, head);
12748 else if (!RNODE_DSTR(tail)->string) {
12750 RNODE_DSTR(head)->as.nd_alen += RNODE_DSTR(tail)->as.nd_alen - 1;
12751 if (!RNODE_DSTR(head)->nd_next) {
12752 RNODE_DSTR(head)->nd_next = RNODE_DSTR(tail)->nd_next;
12754 else if (RNODE_DSTR(tail)->nd_next) {
12755 RNODE_DSTR(RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end)->nd_next = RNODE_DSTR(tail)->nd_next;
12756 RNODE_DSTR(RNODE_DSTR(head)->nd_next)->as.nd_end = RNODE_DSTR(RNODE_DSTR(tail)->nd_next)->as.nd_end;
12758 rb_discard_node(p, tail);
12760 else if ((lit = string_literal_head(p, htype, head)) != false) {
12761 if (!literal_concat0(p, lit, RNODE_DSTR(tail)->string))
12763 rb_parser_string_free(p, RNODE_DSTR(tail)->string);
12764 RNODE_DSTR(tail)->string = 0;
12768 list_concat(head, NEW_LIST2(NEW_STR(RNODE_DSTR(tail)->string, loc), RNODE_DSTR(tail)->as.nd_alen, (NODE *)RNODE_DSTR(tail)->nd_next, loc));
12769 RNODE_DSTR(tail)->string = 0;
12774 if (htype == NODE_STR) {
12775 head = str2dstr(p, head);
12776 RNODE_DSTR(head)->as.nd_alen = 1;
12778 list_append(p, head, tail);
12785nd_copy_flag(NODE *new_node, NODE *old_node)
12787 if (nd_fl_newline(old_node)) nd_set_fl_newline(new_node);
12788 nd_set_line(new_node, nd_line(old_node));
12789 new_node->nd_loc = old_node->nd_loc;
12790 new_node->node_id = old_node->node_id;
12794str2dstr(struct parser_params *p, NODE *node)
12796 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_DSTR, rb_node_dstr_t);
12797 nd_copy_flag(new_node, node);
12798 RNODE_DSTR(new_node)->string = RNODE_STR(node)->string;
12799 RNODE_DSTR(new_node)->as.nd_alen = 0;
12800 RNODE_DSTR(new_node)->nd_next = 0;
12801 RNODE_STR(node)->string = 0;
12807str2regx(struct parser_params *p, NODE *node, int options)
12809 NODE *new_node = (NODE *)NODE_NEW_INTERNAL(NODE_REGX, rb_node_regx_t);
12810 nd_copy_flag(new_node, node);
12811 RNODE_REGX(new_node)->string = RNODE_STR(node)->string;
12812 RNODE_REGX(new_node)->options = options;
12813 RNODE_STR(node)->string = 0;
12819evstr2dstr(struct parser_params *p, NODE *node)
12821 if (nd_type_p(node, NODE_EVSTR)) {
12822 node = new_dstr(p, node, &node->nd_loc);
12828new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12833 switch (nd_type(node)) {
12835 return str2dstr(p, node);
12842 return NEW_EVSTR(head, loc);
12846new_dstr(struct parser_params *p, NODE *node, const YYLTYPE *loc)
12848 NODE *dstr = NEW_DSTR(STRING_NEW0(), loc);
12849 return list_append(p, dstr, node);
12853call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1,
12854 const YYLTYPE *op_loc, const YYLTYPE *loc)
12859 expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc);
12860 nd_set_line(expr, op_loc->beg_pos.lineno);
12865call_uni_op(struct parser_params *p, NODE *recv, ID id, const YYLTYPE *op_loc, const YYLTYPE *loc)
12869 opcall = NEW_OPCALL(recv, id, 0, loc);
12870 nd_set_line(opcall, op_loc->beg_pos.lineno);
12875new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc)
12877 NODE *qcall = NEW_QCALL(atype, recv, mid, args, loc);
12878 nd_set_line(qcall, op_loc->beg_pos.lineno);
12883new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc)
12886 if (block) block_dup_check(p, args, block);
12887 ret = new_qcall(p, atype, recv, mid, args, op_loc, loc);
12888 if (block) ret = method_add_block(p, ret, block, loc);
12893#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
12896last_expr_once_body(NODE *node)
12898 if (!node) return 0;
12899 return nd_once_body(node);
12903match_op(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *op_loc, const YYLTYPE *loc)
12906 int line = op_loc->beg_pos.lineno;
12911 if ((n = last_expr_once_body(node1)) != 0) {
12912 switch (nd_type(n)) {
12915 NODE *match = NEW_MATCH2(node1, node2, loc);
12916 nd_set_line(match, line);
12922 const VALUE lit = rb_node_regx_string_val(n);
12924 NODE *match = NEW_MATCH2(node1, node2, loc);
12925 RNODE_MATCH2(match)->nd_args = reg_named_capture_assign(p, lit, loc, assignable);
12926 nd_set_line(match, line);
12933 if ((n = last_expr_once_body(node2)) != 0) {
12936 switch (nd_type(n)) {
12938 match3 = NEW_MATCH3(node2, node1, loc);
12943 n = NEW_CALL(node1, tMATCH, NEW_LIST(node2, &node2->nd_loc), loc);
12944 nd_set_line(n, line);
12948# if WARN_PAST_SCOPE
12950past_dvar_p(struct parser_params *p, ID id)
12952 struct vtable *past = p->lvtbl->past;
12954 if (vtable_included(past, id)) return 1;
12962numparam_nested_p(struct parser_params *p)
12964 struct local_vars *local = p->lvtbl;
12965 NODE *outer = local->numparam.outer;
12966 NODE *inner = local->numparam.inner;
12967 if (outer || inner) {
12968 NODE *used = outer ? outer : inner;
12969 compile_error(p, "numbered parameter is already used in\n"
12970 "%s:%d: %s block here",
12971 p->ruby_sourcefile, nd_line(used),
12972 outer ? "outer" : "inner");
12973 parser_show_error_line(p, &used->nd_loc);
12980numparam_used_p(struct parser_params *p)
12982 NODE *numparam = p->lvtbl->numparam.current;
12984 compile_error(p, "numbered parameter is already used in\n"
12985 "%s:%d: current block here",
12986 p->ruby_sourcefile, nd_line(numparam));
12987 parser_show_error_line(p, &numparam->nd_loc);
12994it_used_p(struct parser_params *p)
12996 NODE *it = p->lvtbl->it;
12998 compile_error(p, "'it' is already used in\n"
12999 "%s:%d: current block here",
13000 p->ruby_sourcefile, nd_line(it));
13001 parser_show_error_line(p, &it->nd_loc);
13008gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
13014 return NEW_SELF(loc);
13016 return NEW_NIL(loc);
13018 return NEW_TRUE(loc);
13019 case keyword_false:
13020 return NEW_FALSE(loc);
13021 case keyword__FILE__:
13023 VALUE file = p->ruby_sourcefile_string;
13025 file = rb_str_new(0, 0);
13026 node = NEW_FILE(file, loc);
13029 case keyword__LINE__:
13030 return NEW_LINE(loc);
13031 case keyword__ENCODING__:
13032 return NEW_ENCODING(loc);
13035 switch (id_type(id)) {
13037 if (dyna_in_block(p) && dvar_defined_ref(p, id, &vidp)) {
13038 if (NUMPARAM_ID_P(id) && (numparam_nested_p(p) || it_used_p(p))) return 0;
13039 if (vidp) *vidp |= LVAR_USED;
13040 node = NEW_DVAR(id, loc);
13043 if (local_id_ref(p, id, &vidp)) {
13044 if (vidp) *vidp |= LVAR_USED;
13045 node = NEW_LVAR(id, loc);
13048 if (dyna_in_block(p) && NUMPARAM_ID_P(id) &&
13049 parser_numbered_param(p, NUMPARAM_ID_TO_IDX(id))) {
13050 if (numparam_nested_p(p) || it_used_p(p)) return 0;
13051 node = NEW_DVAR(id, loc);
13052 struct local_vars *local = p->lvtbl;
13053 if (!local->numparam.current) local->numparam.current = node;
13056# if WARN_PAST_SCOPE
13057 if (!p->ctxt.in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) {
13058 rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id));
13061 /* method call without arguments */
13062 if (dyna_in_block(p) && id == rb_intern("it") && !(DVARS_TERMINAL_P(p->lvtbl->args) || DVARS_TERMINAL_P(p->lvtbl->args->prev))) {
13063 if (numparam_used_p(p)) return 0;
13064 if (p->max_numparam == ORDINAL_PARAM) {
13065 compile_error(p, "ordinary parameter is defined");
13069 p->it_id = internal_id(p);
13070 vtable_add(p->lvtbl->args, p->it_id);
13072 NODE *node = NEW_DVAR(p->it_id, loc);
13073 if (!p->lvtbl->it) p->lvtbl->it = node;
13076 return NEW_VCALL(id, loc);
13078 return NEW_GVAR(id, loc);
13080 return NEW_IVAR(id, loc);
13082 return NEW_CONST(id, loc);
13084 return NEW_CVAR(id, loc);
13086 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13090static rb_node_opt_arg_t *
13091opt_arg_append(rb_node_opt_arg_t *opt_list, rb_node_opt_arg_t *opt)
13093 rb_node_opt_arg_t *opts = opt_list;
13094 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13096 while (opts->nd_next) {
13097 opts = opts->nd_next;
13098 RNODE(opts)->nd_loc.end_pos = RNODE(opt)->nd_loc.end_pos;
13100 opts->nd_next = opt;
13105static rb_node_kw_arg_t *
13106kwd_append(rb_node_kw_arg_t *kwlist, rb_node_kw_arg_t *kw)
13109 /* Assume rb_node_kw_arg_t and rb_node_opt_arg_t has same structure */
13110 opt_arg_append(RNODE_OPT_ARG(kwlist), RNODE_OPT_ARG(kw));
13116new_defined(struct parser_params *p, NODE *expr, const YYLTYPE *loc)
13120 if (nd_type_p(n, NODE_BEGIN)) {
13121 n = RNODE_BEGIN(n)->nd_body;
13123 else if (nd_type_p(n, NODE_BLOCK) && RNODE_BLOCK(n)->nd_end == n) {
13124 n = RNODE_BLOCK(n)->nd_head;
13130 return NEW_DEFINED(n, loc);
13134str_to_sym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13137 rb_parser_string_t *str = RNODE_STR(node)->string;
13138 if (rb_parser_enc_str_coderange(p, str) == RB_PARSER_ENC_CODERANGE_BROKEN) {
13139 yyerror1(loc, "invalid symbol");
13143 lit = rb_str_new_parser_string(str);
13145 return NEW_SYM(lit, loc);
13149symbol_append(struct parser_params *p, NODE *symbols, NODE *symbol)
13151 enum node_type type = nd_type(symbol);
13154 nd_set_type(symbol, NODE_DSYM);
13157 symbol = str_to_sym_node(p, symbol, &RNODE(symbol)->nd_loc);
13160 compile_error(p, "unexpected node as symbol: %s", parser_node_name(type));
13162 return list_append(p, symbols, symbol);
13166new_regexp(struct parser_params *p, NODE *node, int options, const YYLTYPE *loc)
13168 struct RNode_LIST *list;
13172 /* Check string is valid regex */
13173 rb_parser_string_t *str = STRING_NEW0();
13174 reg_compile(p, str, options);
13175 node = NEW_REGX(str, options, loc);
13178 switch (nd_type(node)) {
13181 /* Check string is valid regex */
13182 reg_compile(p, RNODE_STR(node)->string, options);
13183 node = str2regx(p, node, options);
13187 node = NEW_DSTR0(STRING_NEW0(), 1, NEW_LIST(node, loc), loc);
13190 nd_set_type(node, NODE_DREGX);
13191 nd_set_loc(node, loc);
13192 rb_node_dregx_t *const dreg = RNODE_DREGX(node);
13193 dreg->as.nd_cflag = options & RE_OPTION_MASK;
13194 if (dreg->string) reg_fragment_check(p, dreg->string, options);
13196 for (list = dreg->nd_next; list; list = RNODE_LIST(list->nd_next)) {
13197 NODE *frag = list->nd_head;
13198 enum node_type type = nd_type(frag);
13199 if (type == NODE_STR || (type == NODE_DSTR && !RNODE_DSTR(frag)->nd_next)) {
13200 rb_parser_string_t *tail = RNODE_STR(frag)->string;
13201 if (reg_fragment_check(p, tail, options) && prev && RNODE_DREGX(prev)->string) {
13202 rb_parser_string_t *lit = prev == node ? dreg->string : RNODE_STR(RNODE_LIST(prev)->nd_head)->string;
13203 if (!literal_concat0(p, lit, tail)) {
13204 return NEW_NIL(loc); /* dummy node on error */
13206 rb_parser_str_resize(p, tail, 0);
13207 RNODE_LIST(prev)->nd_next = list->nd_next;
13208 rb_discard_node(p, list->nd_head);
13209 rb_discard_node(p, (NODE *)list);
13210 list = RNODE_LIST(prev);
13213 prev = (NODE *)list;
13220 if (!dreg->nd_next) {
13221 /* Check string is valid regex */
13222 reg_compile(p, dreg->string, options);
13224 if (options & RE_OPTION_ONCE) {
13225 node = NEW_ONCE(node, loc);
13232static rb_node_kw_arg_t *
13233new_kw_arg(struct parser_params *p, NODE *k, const YYLTYPE *loc)
13236 return NEW_KW_ARG((k), loc);
13240new_xstring(struct parser_params *p, NODE *node, const YYLTYPE *loc)
13243 NODE *xstr = NEW_XSTR(STRING_NEW0(), loc);
13246 switch (nd_type(node)) {
13248 nd_set_type(node, NODE_XSTR);
13249 nd_set_loc(node, loc);
13252 nd_set_type(node, NODE_DXSTR);
13253 nd_set_loc(node, loc);
13256 node = NEW_DXSTR(0, 1, NEW_LIST(node, loc), loc);
13263struct st_hash_type literal_type = {
13268static int nd_type_st_key_enable_p(NODE *node);
13271check_literal_when(struct parser_params *p, NODE *arg, const YYLTYPE *loc)
13273 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
13274 if (!arg || !p->case_labels) return;
13275 if (!nd_type_st_key_enable_p(arg)) return;
13277 if (p->case_labels == CHECK_LITERAL_WHEN) {
13278 p->case_labels = st_init_table(&literal_type);
13282 if (st_lookup(p->case_labels, (st_data_t)arg, &line)) {
13283 rb_warning2("'when' clause on line %d duplicates 'when' clause on line %d and is ignored",
13284 WARN_I((int)nd_line(arg)), WARN_I((int)line));
13288 st_insert(p->case_labels, (st_data_t)arg, (st_data_t)p->ruby_sourceline);
13293id_is_var(struct parser_params *p, ID id)
13295 if (is_notop_id(id)) {
13296 switch (id & ID_SCOPE_MASK) {
13297 case ID_GLOBAL: case ID_INSTANCE: case ID_CONST: case ID_CLASS:
13300 if (dyna_in_block(p)) {
13301 if (NUMPARAM_ID_P(id) || dvar_defined(p, id)) return 1;
13303 if (local_id(p, id)) return 1;
13304 /* method call without arguments */
13308 compile_error(p, "identifier %"PRIsVALUE" is not valid to get", rb_id2str(id));
13313static inline enum lex_state_e
13314parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line)
13317 ls = rb_parser_trace_lex_state(p, p->lex.state, ls, line);
13319 return p->lex.state = ls;
13324flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str)
13326 VALUE mesg = p->debug_buffer;
13328 if (!NIL_P(mesg) && RSTRING_LEN(mesg)) {
13329 p->debug_buffer = Qnil;
13330 rb_io_puts(1, &mesg, out);
13332 if (!NIL_P(str) && RSTRING_LEN(str)) {
13333 rb_io_write(p->debug_output, str);
13337static const char rb_parser_lex_state_names[][8] = {
13338 "BEG", "END", "ENDARG", "ENDFN", "ARG",
13339 "CMDARG", "MID", "FNAME", "DOT", "CLASS",
13340 "LABEL", "LABELED","FITEM",
13344append_lex_state_name(struct parser_params *p, enum lex_state_e state, VALUE buf)
13347 unsigned int mask = 1;
13348 static const char none[] = "NONE";
13350 for (i = 0; i < EXPR_MAX_STATE; ++i, mask <<= 1) {
13351 if ((unsigned)state & mask) {
13353 rb_str_cat(buf, "|", 1);
13356 rb_str_cat_cstr(buf, rb_parser_lex_state_names[i]);
13360 rb_str_cat(buf, none, sizeof(none)-1);
13366rb_parser_trace_lex_state(struct parser_params *p, enum lex_state_e from,
13367 enum lex_state_e to, int line)
13370 mesg = rb_str_new_cstr("lex_state: ");
13371 append_lex_state_name(p, from, mesg);
13372 rb_str_cat_cstr(mesg, " -> ");
13373 append_lex_state_name(p, to, mesg);
13374 rb_str_catf(mesg, " at line %d\n", line);
13375 flush_debug_buffer(p, p->debug_output, mesg);
13380rb_parser_lex_state_name(struct parser_params *p, enum lex_state_e state)
13382 return rb_str_to_interned_str(append_lex_state_name(p, state, rb_str_new(0, 0)));
13386append_bitstack_value(struct parser_params *p, stack_type stack, VALUE mesg)
13389 rb_str_cat_cstr(mesg, "0");
13392 stack_type mask = (stack_type)1U << (CHAR_BIT * sizeof(stack_type) - 1);
13393 for (; mask && !(stack & mask); mask >>= 1) continue;
13394 for (; mask; mask >>= 1) rb_str_cat(mesg, stack & mask ? "1" : "0", 1);
13399rb_parser_show_bitstack(struct parser_params *p, stack_type stack,
13400 const char *name, int line)
13402 VALUE mesg = rb_sprintf("%s: ", name);
13403 append_bitstack_value(p, stack, mesg);
13404 rb_str_catf(mesg, " at line %d\n", line);
13405 flush_debug_buffer(p, p->debug_output, mesg);
13409rb_parser_fatal(struct parser_params *p, const char *fmt, ...)
13412 VALUE mesg = rb_str_new_cstr("internal parser error: ");
13415 rb_str_vcatf(mesg, fmt, ap);
13417 yyerror0(RSTRING_PTR(mesg));
13420 mesg = rb_str_new(0, 0);
13421 append_lex_state_name(p, p->lex.state, mesg);
13422 compile_error(p, "lex.state: %"PRIsVALUE, mesg);
13423 rb_str_resize(mesg, 0);
13424 append_bitstack_value(p, p->cond_stack, mesg);
13425 compile_error(p, "cond_stack: %"PRIsVALUE, mesg);
13426 rb_str_resize(mesg, 0);
13427 append_bitstack_value(p, p->cmdarg_stack, mesg);
13428 compile_error(p, "cmdarg_stack: %"PRIsVALUE, mesg);
13429 if (p->debug_output == rb_ractor_stdout())
13430 p->debug_output = rb_ractor_stderr();
13435rb_parser_set_pos(YYLTYPE *yylloc, int sourceline, int beg_pos, int end_pos)
13437 yylloc->beg_pos.lineno = sourceline;
13438 yylloc->beg_pos.column = beg_pos;
13439 yylloc->end_pos.lineno = sourceline;
13440 yylloc->end_pos.column = end_pos;
13445rb_parser_set_location_from_strterm_heredoc(struct parser_params *p, rb_strterm_heredoc_t *here, YYLTYPE *yylloc)
13447 int sourceline = here->sourceline;
13448 int beg_pos = (int)here->offset - here->quote
13449 - (rb_strlen_lit("<<-") - !(here->func & STR_FUNC_INDENT));
13450 int end_pos = (int)here->offset + here->length + here->quote;
13452 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13456rb_parser_set_location_of_delayed_token(struct parser_params *p, YYLTYPE *yylloc)
13458 yylloc->beg_pos.lineno = p->delayed.beg_line;
13459 yylloc->beg_pos.column = p->delayed.beg_col;
13460 yylloc->end_pos.lineno = p->delayed.end_line;
13461 yylloc->end_pos.column = p->delayed.end_col;
13467rb_parser_set_location_of_heredoc_end(struct parser_params *p, YYLTYPE *yylloc)
13469 int sourceline = p->ruby_sourceline;
13470 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13471 int end_pos = (int)(p->lex.pend - p->lex.pbeg);
13472 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13476rb_parser_set_location_of_dummy_end(struct parser_params *p, YYLTYPE *yylloc)
13478 yylloc->end_pos = yylloc->beg_pos;
13484rb_parser_set_location_of_none(struct parser_params *p, YYLTYPE *yylloc)
13486 int sourceline = p->ruby_sourceline;
13487 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13488 int end_pos = (int)(p->lex.ptok - p->lex.pbeg);
13489 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13493rb_parser_set_location(struct parser_params *p, YYLTYPE *yylloc)
13495 int sourceline = p->ruby_sourceline;
13496 int beg_pos = (int)(p->lex.ptok - p->lex.pbeg);
13497 int end_pos = (int)(p->lex.pcur - p->lex.pbeg);
13498 return rb_parser_set_pos(yylloc, sourceline, beg_pos, end_pos);
13500#endif /* !RIPPER */
13503assignable0(struct parser_params *p, ID id, const char **err)
13505 if (!id) return -1;
13508 *err = "Can't change the value of self";
13511 *err = "Can't assign to nil";
13514 *err = "Can't assign to true";
13516 case keyword_false:
13517 *err = "Can't assign to false";
13519 case keyword__FILE__:
13520 *err = "Can't assign to __FILE__";
13522 case keyword__LINE__:
13523 *err = "Can't assign to __LINE__";
13525 case keyword__ENCODING__:
13526 *err = "Can't assign to __ENCODING__";
13529 switch (id_type(id)) {
13531 if (dyna_in_block(p)) {
13532 if (p->max_numparam > NO_PARAM && NUMPARAM_ID_P(id)) {
13533 compile_error(p, "Can't assign to numbered parameter _%d",
13534 NUMPARAM_ID_TO_IDX(id));
13537 if (dvar_curr(p, id)) return NODE_DASGN;
13538 if (dvar_defined(p, id)) return NODE_DASGN;
13539 if (local_id(p, id)) return NODE_LASGN;
13544 if (!local_id(p, id)) local_var(p, id);
13548 case ID_GLOBAL: return NODE_GASGN;
13549 case ID_INSTANCE: return NODE_IASGN;
13551 if (!p->ctxt.in_def) return NODE_CDECL;
13552 *err = "dynamic constant assignment";
13554 case ID_CLASS: return NODE_CVASGN;
13556 compile_error(p, "identifier %"PRIsVALUE" is not valid to set", rb_id2str(id));
13562assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
13564 const char *err = 0;
13565 int node_type = assignable0(p, id, &err);
13566 switch (node_type) {
13567 case NODE_DASGN: return NEW_DASGN(id, val, loc);
13568 case NODE_LASGN: return NEW_LASGN(id, val, loc);
13569 case NODE_GASGN: return NEW_GASGN(id, val, loc);
13570 case NODE_IASGN: return NEW_IASGN(id, val, loc);
13571 case NODE_CDECL: return NEW_CDECL(id, val, 0, p->ctxt.shareable_constant_value, loc);
13572 case NODE_CVASGN: return NEW_CVASGN(id, val, loc);
13576 if (err) yyerror1(loc, err);
13578 if (err) set_value(assign_error(p, err, p->s_lvalue));
13580 return NEW_ERROR(loc);
13584is_private_local_id(struct parser_params *p, ID name)
13587 if (name == idUScore) return 1;
13588 if (!is_local_id(name)) return 0;
13589 s = rb_id2str(name);
13591 return RSTRING_PTR(s)[0] == '_';
13595shadowing_lvar_0(struct parser_params *p, ID name)
13597 if (dyna_in_block(p)) {
13598 if (dvar_curr(p, name)) {
13599 if (is_private_local_id(p, name)) return 1;
13600 yyerror0("duplicated argument name");
13602 else if (dvar_defined(p, name) || local_id(p, name)) {
13603 vtable_add(p->lvtbl->vars, name);
13604 if (p->lvtbl->used) {
13605 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline | LVAR_USED);
13611 if (local_id(p, name)) {
13612 if (is_private_local_id(p, name)) return 1;
13613 yyerror0("duplicated argument name");
13620shadowing_lvar(struct parser_params *p, ID name)
13622 shadowing_lvar_0(p, name);
13627new_bv(struct parser_params *p, ID name)
13630 if (!is_local_id(name)) {
13631 compile_error(p, "invalid local variable - %"PRIsVALUE,
13635 if (!shadowing_lvar_0(p, name)) return;
13638 if (dvar_defined_ref(p, name, &vidp)) {
13639 if (vidp) *vidp |= LVAR_USED;
13644aryset_check(struct parser_params *p, NODE *args)
13646 NODE *block = 0, *kwds = 0;
13647 if (args && nd_type_p(args, NODE_BLOCK_PASS)) {
13648 block = RNODE_BLOCK_PASS(args)->nd_body;
13649 args = RNODE_BLOCK_PASS(args)->nd_head;
13651 if (args && nd_type_p(args, NODE_ARGSCAT)) {
13652 args = RNODE_ARGSCAT(args)->nd_body;
13654 if (args && nd_type_p(args, NODE_ARGSPUSH)) {
13655 kwds = RNODE_ARGSPUSH(args)->nd_body;
13658 for (NODE *next = args; next && nd_type_p(next, NODE_LIST);
13659 next = RNODE_LIST(next)->nd_next) {
13660 kwds = RNODE_LIST(next)->nd_head;
13663 if (kwds && nd_type_p(kwds, NODE_HASH) && !RNODE_HASH(kwds)->nd_brace) {
13664 yyerror1(&kwds->nd_loc, "keyword arg given in index assignment");
13667 yyerror1(&block->nd_loc, "block arg given in index assignment");
13672aryset(struct parser_params *p, NODE *recv, NODE *idx, const YYLTYPE *loc)
13674 aryset_check(p, idx);
13675 return NEW_ATTRASGN(recv, tASET, idx, loc);
13679block_dup_check(struct parser_params *p, NODE *node1, NODE *node2)
13681 if (node2 && node1 && nd_type_p(node1, NODE_BLOCK_PASS)) {
13682 compile_error(p, "both block arg and actual block given");
13687attrset(struct parser_params *p, NODE *recv, ID atype, ID id, const YYLTYPE *loc)
13689 if (!CALL_Q_P(atype)) id = rb_id_attrset(id);
13690 return NEW_ATTRASGN(recv, id, 0, loc);
13694rb_backref_error(struct parser_params *p, NODE *node)
13697# define ERR(...) (compile_error(p, __VA_ARGS__), Qtrue)
13699# define ERR(...) rb_sprintf(__VA_ARGS__)
13701 switch (nd_type(node)) {
13703 return ERR("Can't set variable $%ld", RNODE_NTH_REF(node)->nd_nth);
13704 case NODE_BACK_REF:
13705 return ERR("Can't set variable $%c", (int)RNODE_BACK_REF(node)->nd_nth);
13708 UNREACHABLE_RETURN(Qfalse); /* only called on syntax error */
13712arg_append(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13714 if (!node1) return NEW_LIST(node2, &node2->nd_loc);
13715 switch (nd_type(node1)) {
13717 return list_append(p, node1, node2);
13718 case NODE_BLOCK_PASS:
13719 RNODE_BLOCK_PASS(node1)->nd_head = arg_append(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13720 node1->nd_loc.end_pos = RNODE_BLOCK_PASS(node1)->nd_head->nd_loc.end_pos;
13722 case NODE_ARGSPUSH:
13723 RNODE_ARGSPUSH(node1)->nd_body = list_append(p, NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, &RNODE_ARGSPUSH(node1)->nd_body->nd_loc), node2);
13724 node1->nd_loc.end_pos = RNODE_ARGSPUSH(node1)->nd_body->nd_loc.end_pos;
13725 nd_set_type(node1, NODE_ARGSCAT);
13728 if (!nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13729 RNODE_ARGSCAT(node1)->nd_body = list_append(p, RNODE_ARGSCAT(node1)->nd_body, node2);
13730 node1->nd_loc.end_pos = RNODE_ARGSCAT(node1)->nd_body->nd_loc.end_pos;
13733 return NEW_ARGSPUSH(node1, node2, loc);
13737arg_concat(struct parser_params *p, NODE *node1, NODE *node2, const YYLTYPE *loc)
13739 if (!node2) return node1;
13740 switch (nd_type(node1)) {
13741 case NODE_BLOCK_PASS:
13742 if (RNODE_BLOCK_PASS(node1)->nd_head)
13743 RNODE_BLOCK_PASS(node1)->nd_head = arg_concat(p, RNODE_BLOCK_PASS(node1)->nd_head, node2, loc);
13745 RNODE_LIST(node1)->nd_head = NEW_LIST(node2, loc);
13747 case NODE_ARGSPUSH:
13748 if (!nd_type_p(node2, NODE_LIST)) break;
13749 RNODE_ARGSPUSH(node1)->nd_body = list_concat(NEW_LIST(RNODE_ARGSPUSH(node1)->nd_body, loc), node2);
13750 nd_set_type(node1, NODE_ARGSCAT);
13753 if (!nd_type_p(node2, NODE_LIST) ||
13754 !nd_type_p(RNODE_ARGSCAT(node1)->nd_body, NODE_LIST)) break;
13755 RNODE_ARGSCAT(node1)->nd_body = list_concat(RNODE_ARGSCAT(node1)->nd_body, node2);
13758 return NEW_ARGSCAT(node1, node2, loc);
13762last_arg_append(struct parser_params *p, NODE *args, NODE *last_arg, const YYLTYPE *loc)
13765 if ((n1 = splat_array(args)) != 0) {
13766 return list_append(p, n1, last_arg);
13768 return arg_append(p, args, last_arg, loc);
13772rest_arg_append(struct parser_params *p, NODE *args, NODE *rest_arg, const YYLTYPE *loc)
13775 if ((nd_type_p(rest_arg, NODE_LIST)) && (n1 = splat_array(args)) != 0) {
13776 return list_concat(n1, rest_arg);
13778 return arg_concat(p, args, rest_arg, loc);
13782splat_array(NODE* node)
13784 if (nd_type_p(node, NODE_SPLAT)) node = RNODE_SPLAT(node)->nd_head;
13785 if (nd_type_p(node, NODE_LIST)) return node;
13790mark_lvar_used(struct parser_params *p, NODE *rhs)
13794 switch (nd_type(rhs)) {
13796 if (local_id_ref(p, RNODE_LASGN(rhs)->nd_vid, &vidp)) {
13797 if (vidp) *vidp |= LVAR_USED;
13801 if (dvar_defined_ref(p, RNODE_DASGN(rhs)->nd_vid, &vidp)) {
13802 if (vidp) *vidp |= LVAR_USED;
13807 for (rhs = rhs->nd_head; rhs; rhs = rhs->nd_next) {
13808 mark_lvar_used(p, rhs->nd_head);
13815static int is_static_content(NODE *node);
13818node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
13820 if (!lhs) return 0;
13822 switch (nd_type(lhs)) {
13830 set_nd_value(p, lhs, rhs);
13831 nd_set_loc(lhs, loc);
13834 case NODE_ATTRASGN:
13835 RNODE_ATTRASGN(lhs)->nd_args = arg_append(p, RNODE_ATTRASGN(lhs)->nd_args, rhs, loc);
13836 nd_set_loc(lhs, loc);
13840 /* should not happen */
13848value_expr_check(struct parser_params *p, NODE *node)
13850 NODE *void_node = 0, *vn;
13853 rb_warning0("empty expression");
13856 switch (nd_type(node)) {
13858 vn = RNODE_ENSURE(node)->nd_head;
13859 node = RNODE_ENSURE(node)->nd_ensr;
13860 /* nd_ensr should not be NULL, check it out next */
13861 if (vn && (vn = value_expr_check(p, vn))) {
13867 /* void only if all children are void */
13868 vn = RNODE_RESCUE(node)->nd_head;
13869 if (!vn || !(vn = value_expr_check(p, vn))) return NULL;
13870 if (!void_node) void_node = vn;
13871 for (NODE *r = RNODE_RESCUE(node)->nd_resq; r; r = RNODE_RESBODY(r)->nd_next) {
13872 if (!nd_type_p(r, NODE_RESBODY)) {
13873 compile_error(p, "unexpected node");
13876 if (!(vn = value_expr_check(p, RNODE_RESBODY(r)->nd_body))) {
13880 if (!void_node) void_node = vn;
13882 node = RNODE_RESCUE(node)->nd_else;
13883 if (!node) return void_node;
13894 if (!RNODE_CASE3(node)->nd_body || !nd_type_p(RNODE_CASE3(node)->nd_body, NODE_IN)) {
13895 compile_error(p, "unexpected node");
13898 if (RNODE_IN(RNODE_CASE3(node)->nd_body)->nd_body) {
13901 /* single line pattern matching with "=>" operator */
13905 while (RNODE_BLOCK(node)->nd_next) {
13906 node = RNODE_BLOCK(node)->nd_next;
13908 node = RNODE_BLOCK(node)->nd_head;
13912 node = RNODE_BEGIN(node)->nd_body;
13917 if (!RNODE_IF(node)->nd_body) {
13920 else if (!RNODE_IF(node)->nd_else) {
13923 vn = value_expr_check(p, RNODE_IF(node)->nd_body);
13924 if (!vn) return NULL;
13925 if (!void_node) void_node = vn;
13926 node = RNODE_IF(node)->nd_else;
13931 node = RNODE_AND(node)->nd_1st;
13937 mark_lvar_used(p, node);
13948 /* return the first found node */
13949 return void_node ? void_node : node;
13953value_expr_gen(struct parser_params *p, NODE *node)
13955 NODE *void_node = value_expr_check(p, node);
13957 yyerror1(&void_node->nd_loc, "void value expression");
13958 /* or "control never reach"? */
13965void_expr(struct parser_params *p, NODE *node)
13967 const char *useless = 0;
13969 if (!RTEST(ruby_verbose)) return;
13971 if (!node || !(node = nd_once_body(node))) return;
13972 switch (nd_type(node)) {
13974 switch (RNODE_OPCALL(node)->nd_mid) {
13993 useless = rb_id2name(RNODE_OPCALL(node)->nd_mid);
14004 case NODE_BACK_REF:
14005 useless = "a variable";
14008 useless = "a constant";
14013 case NODE_ENCODING:
14016 case NODE_RATIONAL:
14017 case NODE_IMAGINARY:
14022 useless = "a literal";
14047 useless = "defined?";
14052 rb_warn1L(nd_line(node), "possibly useless use of %s in void context", WARN_S(useless));
14056/* warns useless use of block and returns the last statement node */
14058void_stmts(struct parser_params *p, NODE *node)
14060 NODE *const n = node;
14061 if (!RTEST(ruby_verbose)) return n;
14062 if (!node) return n;
14063 if (!nd_type_p(node, NODE_BLOCK)) return n;
14065 while (RNODE_BLOCK(node)->nd_next) {
14066 void_expr(p, RNODE_BLOCK(node)->nd_head);
14067 node = RNODE_BLOCK(node)->nd_next;
14069 return RNODE_BLOCK(node)->nd_head;
14073remove_begin(NODE *node)
14075 NODE **n = &node, *n1 = node;
14076 while (n1 && nd_type_p(n1, NODE_BEGIN) && RNODE_BEGIN(n1)->nd_body) {
14077 *n = n1 = RNODE_BEGIN(n1)->nd_body;
14083reduce_nodes(struct parser_params *p, NODE **body)
14085 NODE *node = *body;
14088 *body = NEW_NIL(&NULL_LOC);
14091#define subnodes(type, n1, n2) \
14092 ((!type(node)->n1) ? (type(node)->n2 ? (body = &type(node)->n2, 1) : 0) : \
14093 (!type(node)->n2) ? (body = &type(node)->n1, 1) : \
14094 (reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))
14097 int newline = (int)nd_fl_newline(node);
14098 switch (nd_type(node)) {
14104 *body = node = RNODE_BEGIN(node)->nd_body;
14105 if (newline && node) nd_set_fl_newline(node);
14108 body = &RNODE_BLOCK(RNODE_BLOCK(node)->nd_end)->nd_head;
14112 if (subnodes(RNODE_IF, nd_body, nd_else)) break;
14115 body = &RNODE_CASE(node)->nd_body;
14118 if (!subnodes(RNODE_WHEN, nd_body, nd_next)) goto end;
14121 body = &RNODE_ENSURE(node)->nd_head;
14124 newline = 0; // RESBODY should not be a NEWLINE
14125 if (RNODE_RESCUE(node)->nd_else) {
14126 body = &RNODE_RESCUE(node)->nd_resq;
14129 if (!subnodes(RNODE_RESCUE, nd_head, nd_resq)) goto end;
14135 if (newline && node) nd_set_fl_newline(node);
14142is_static_content(NODE *node)
14144 if (!node) return 1;
14145 switch (nd_type(node)) {
14147 if (!(node = RNODE_HASH(node)->nd_head)) break;
14150 if (!is_static_content(RNODE_LIST(node)->nd_head)) return 0;
14151 } while ((node = RNODE_LIST(node)->nd_next) != 0);
14156 case NODE_ENCODING:
14159 case NODE_RATIONAL:
14160 case NODE_IMAGINARY:
14174assign_in_cond(struct parser_params *p, NODE *node)
14176 switch (nd_type(node)) {
14190 if (!get_nd_value(p, node)) return 1;
14191 if (is_static_content(get_nd_value(p, node))) {
14192 /* reports always */
14193 rb_warn0L(nd_line(get_nd_value(p, node)), "found '= literal' in conditional, should be ==");
14204#define SWITCH_BY_COND_TYPE(t, w, arg) do { \
14206 case COND_IN_OP: break; \
14207 case COND_IN_COND: rb_##w##0(arg "literal in condition"); break; \
14208 case COND_IN_FF: rb_##w##0(arg "literal in flip-flop"); break; \
14212static NODE *cond0(struct parser_params*,NODE*,enum cond_type,const YYLTYPE*,bool);
14215range_op(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14217 enum node_type type;
14219 if (node == 0) return 0;
14221 type = nd_type(node);
14223 if (type == NODE_INTEGER) {
14224 if (!e_option_supplied(p)) rb_warn0L(nd_line(node), "integer literal in flip-flop");
14225 ID lineno = rb_intern("$.");
14226 return NEW_CALL(node, tEQ, NEW_LIST(NEW_GVAR(lineno, loc), loc), loc);
14228 return cond0(p, node, COND_IN_FF, loc, true);
14232cond0(struct parser_params *p, NODE *node, enum cond_type type, const YYLTYPE *loc, bool top)
14234 if (node == 0) return 0;
14235 if (!(node = nd_once_body(node))) return 0;
14236 assign_in_cond(p, node);
14238 switch (nd_type(node)) {
14240 RNODE_BEGIN(node)->nd_body = cond0(p, RNODE_BEGIN(node)->nd_body, type, loc, top);
14247 SWITCH_BY_COND_TYPE(type, warn, "string ");
14251 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warn, "regex ");
14252 nd_set_type(node, NODE_MATCH);
14256 if (!e_option_supplied(p)) SWITCH_BY_COND_TYPE(type, warning, "regex ");
14258 return NEW_MATCH2(node, NEW_GVAR(idLASTLINE, loc), loc);
14262 NODE *end = RNODE_BLOCK(node)->nd_end;
14263 NODE **expr = &RNODE_BLOCK(end)->nd_head;
14264 if (top) top = node == end;
14265 *expr = cond0(p, *expr, type, loc, top);
14271 RNODE_AND(node)->nd_1st = cond0(p, RNODE_AND(node)->nd_1st, COND_IN_COND, loc, true);
14272 RNODE_AND(node)->nd_2nd = cond0(p, RNODE_AND(node)->nd_2nd, COND_IN_COND, loc, true);
14278 RNODE_DOT2(node)->nd_beg = range_op(p, RNODE_DOT2(node)->nd_beg, loc);
14279 RNODE_DOT2(node)->nd_end = range_op(p, RNODE_DOT2(node)->nd_end, loc);
14280 switch (nd_type(node)) {
14282 nd_set_type(node,NODE_FLIP2);
14283 rb_node_flip2_t *flip2 = RNODE_FLIP2(node); /* for debug info */
14287 nd_set_type(node, NODE_FLIP3);
14288 rb_node_flip3_t *flip3 = RNODE_FLIP3(node); /* for debug info */
14296 SWITCH_BY_COND_TYPE(type, warning, "symbol ");
14300 SWITCH_BY_COND_TYPE(type, warning, "");
14303 case NODE_ENCODING:
14304 SWITCH_BY_COND_TYPE(type, warning, "");
14309 case NODE_RATIONAL:
14310 case NODE_IMAGINARY:
14311 SWITCH_BY_COND_TYPE(type, warning, "");
14321cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14323 if (node == 0) return 0;
14324 return cond0(p, node, COND_IN_COND, loc, true);
14328method_cond(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14330 if (node == 0) return 0;
14331 return cond0(p, node, COND_IN_OP, loc, true);
14335new_nil_at(struct parser_params *p, const rb_code_position_t *pos)
14337 YYLTYPE loc = {*pos, *pos};
14338 return NEW_NIL(&loc);
14342new_if(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc)
14344 if (!cc) return right;
14345 cc = cond0(p, cc, COND_IN_COND, loc, true);
14346 return newline_node(NEW_IF(cc, left, right, loc));
14350new_unless(struct parser_params *p, NODE *cc, NODE *left, NODE *right, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *end_keyword_loc)
14352 if (!cc) return right;
14353 cc = cond0(p, cc, COND_IN_COND, loc, true);
14354 return newline_node(NEW_UNLESS(cc, left, right, loc, keyword_loc, then_keyword_loc, end_keyword_loc));
14357#define NEW_AND_OR(type, f, s, loc, op_loc) (type == NODE_AND ? NEW_AND(f,s,loc,op_loc) : NEW_OR(f,s,loc,op_loc))
14360logop(struct parser_params *p, ID id, NODE *left, NODE *right,
14361 const YYLTYPE *op_loc, const YYLTYPE *loc)
14363 enum node_type type = id == idAND || id == idANDOP ? NODE_AND : NODE_OR;
14366 if (left && nd_type_p(left, type)) {
14367 NODE *node = left, *second;
14368 while ((second = RNODE_AND(node)->nd_2nd) != 0 && nd_type_p(second, type)) {
14371 RNODE_AND(node)->nd_2nd = NEW_AND_OR(type, second, right, loc, op_loc);
14372 nd_set_line(RNODE_AND(node)->nd_2nd, op_loc->beg_pos.lineno);
14373 left->nd_loc.end_pos = loc->end_pos;
14376 op = NEW_AND_OR(type, left, right, loc, op_loc);
14377 nd_set_line(op, op_loc->beg_pos.lineno);
14384no_blockarg(struct parser_params *p, NODE *node)
14386 if (nd_type_p(node, NODE_BLOCK_PASS)) {
14387 compile_error(p, "block argument should not be given");
14392ret_args(struct parser_params *p, NODE *node)
14395 no_blockarg(p, node);
14396 if (nd_type_p(node, NODE_LIST) && !RNODE_LIST(node)->nd_next) {
14397 node = RNODE_LIST(node)->nd_head;
14404new_yield(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14406 if (node) no_blockarg(p, node);
14408 return NEW_YIELD(node, loc);
14412negate_lit(struct parser_params *p, NODE* node)
14414 switch (nd_type(node)) {
14416 RNODE_INTEGER(node)->minus = TRUE;
14419 RNODE_FLOAT(node)->minus = TRUE;
14421 case NODE_RATIONAL:
14422 RNODE_RATIONAL(node)->minus = TRUE;
14424 case NODE_IMAGINARY:
14425 RNODE_IMAGINARY(node)->minus = TRUE;
14432arg_blk_pass(NODE *node1, rb_node_block_pass_t *node2)
14435 if (!node1) return (NODE *)node2;
14436 node2->nd_head = node1;
14437 nd_set_first_lineno(node2, nd_first_lineno(node1));
14438 nd_set_first_column(node2, nd_first_column(node1));
14439 return (NODE *)node2;
14445args_info_empty_p(struct rb_args_info *args)
14447 if (args->pre_args_num) return false;
14448 if (args->post_args_num) return false;
14449 if (args->rest_arg) return false;
14450 if (args->opt_args) return false;
14451 if (args->block_arg) return false;
14452 if (args->kw_args) return false;
14453 if (args->kw_rest_arg) return false;
14457static rb_node_args_t *
14458new_args(struct parser_params *p, rb_node_args_aux_t *pre_args, rb_node_opt_arg_t *opt_args, ID rest_arg, rb_node_args_aux_t *post_args, rb_node_args_t *tail, const YYLTYPE *loc)
14460 struct rb_args_info *args = &tail->nd_ainfo;
14462 if (args->forwarding) {
14464 yyerror1(&RNODE(tail)->nd_loc, "... after rest argument");
14467 rest_arg = idFWD_REST;
14470 args->pre_args_num = pre_args ? pre_args->nd_plen : 0;
14471 args->pre_init = pre_args ? pre_args->nd_next : 0;
14473 args->post_args_num = post_args ? post_args->nd_plen : 0;
14474 args->post_init = post_args ? post_args->nd_next : 0;
14475 args->first_post_arg = post_args ? post_args->nd_pid : 0;
14477 args->rest_arg = rest_arg;
14479 args->opt_args = opt_args;
14481#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
14482 args->ruby2_keywords = args->forwarding;
14484 args->ruby2_keywords = 0;
14487 nd_set_loc(RNODE(tail), loc);
14492static rb_node_args_t *
14493new_args_tail(struct parser_params *p, rb_node_kw_arg_t *kw_args, ID kw_rest_arg, ID block, const YYLTYPE *kw_rest_loc)
14495 rb_node_args_t *node = NEW_ARGS(&NULL_LOC);
14496 struct rb_args_info *args = &node->nd_ainfo;
14497 if (p->error_p) return node;
14499 args->block_arg = block;
14500 args->kw_args = kw_args;
14504 * def foo(k1: 1, kr1:, k2: 2, **krest, &b)
14505 * variable order: k1, kr1, k2, &b, internal_id, krest
14507 * variable order: kr1, k1, k2, internal_id, krest, &b
14509 ID kw_bits = internal_id(p), *required_kw_vars, *kw_vars;
14510 struct vtable *vtargs = p->lvtbl->args;
14511 rb_node_kw_arg_t *kwn = kw_args;
14513 if (block) block = vtargs->tbl[vtargs->pos-1];
14514 vtable_pop(vtargs, !!block + !!kw_rest_arg);
14515 required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
14517 if (!NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body)))
14519 --required_kw_vars;
14520 kwn = kwn->nd_next;
14523 for (kwn = kw_args; kwn; kwn = kwn->nd_next) {
14524 ID vid = get_nd_vid(p, kwn->nd_body);
14525 if (NODE_REQUIRED_KEYWORD_P(get_nd_value(p, kwn->nd_body))) {
14526 *required_kw_vars++ = vid;
14533 arg_var(p, kw_bits);
14534 if (kw_rest_arg) arg_var(p, kw_rest_arg);
14535 if (block) arg_var(p, block);
14537 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14539 else if (kw_rest_arg == idNil) {
14540 args->no_kwarg = 1;
14542 else if (kw_rest_arg) {
14543 args->kw_rest_arg = NEW_DVAR(kw_rest_arg, kw_rest_loc);
14549static rb_node_args_t *
14550args_with_numbered(struct parser_params *p, rb_node_args_t *args, int max_numparam, ID it_id)
14552 if (max_numparam > NO_PARAM || it_id) {
14554 YYLTYPE loc = RUBY_INIT_YYLLOC();
14555 args = new_args_tail(p, 0, 0, 0, 0);
14556 nd_set_loc(RNODE(args), &loc);
14558 args->nd_ainfo.pre_args_num = it_id ? 1 : max_numparam;
14564new_array_pattern(struct parser_params *p, NODE *constant, NODE *pre_arg, NODE *aryptn, const YYLTYPE *loc)
14566 RNODE_ARYPTN(aryptn)->nd_pconst = constant;
14569 NODE *pre_args = NEW_LIST(pre_arg, loc);
14570 if (RNODE_ARYPTN(aryptn)->pre_args) {
14571 RNODE_ARYPTN(aryptn)->pre_args = list_concat(pre_args, RNODE_ARYPTN(aryptn)->pre_args);
14574 RNODE_ARYPTN(aryptn)->pre_args = pre_args;
14581new_array_pattern_tail(struct parser_params *p, NODE *pre_args, int has_rest, NODE *rest_arg, NODE *post_args, const YYLTYPE *loc)
14584 rest_arg = rest_arg ? rest_arg : NODE_SPECIAL_NO_NAME_REST;
14589 NODE *node = NEW_ARYPTN(pre_args, rest_arg, post_args, loc);
14595new_find_pattern(struct parser_params *p, NODE *constant, NODE *fndptn, const YYLTYPE *loc)
14597 RNODE_FNDPTN(fndptn)->nd_pconst = constant;
14603new_find_pattern_tail(struct parser_params *p, NODE *pre_rest_arg, NODE *args, NODE *post_rest_arg, const YYLTYPE *loc)
14605 pre_rest_arg = pre_rest_arg ? pre_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14606 post_rest_arg = post_rest_arg ? post_rest_arg : NODE_SPECIAL_NO_NAME_REST;
14607 NODE *node = NEW_FNDPTN(pre_rest_arg, args, post_rest_arg, loc);
14613new_hash_pattern(struct parser_params *p, NODE *constant, NODE *hshptn, const YYLTYPE *loc)
14615 RNODE_HSHPTN(hshptn)->nd_pconst = constant;
14620new_hash_pattern_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, const YYLTYPE *loc)
14622 NODE *node, *kw_rest_arg_node;
14624 if (kw_rest_arg == idNil) {
14625 kw_rest_arg_node = NODE_SPECIAL_NO_REST_KEYWORD;
14627 else if (kw_rest_arg) {
14628 kw_rest_arg_node = assignable(p, kw_rest_arg, 0, loc);
14631 kw_rest_arg_node = NULL;
14634 node = NEW_HSHPTN(0, kw_args, kw_rest_arg_node, loc);
14640dsym_node(struct parser_params *p, NODE *node, const YYLTYPE *loc)
14643 return NEW_SYM(STR_NEW0(), loc);
14646 switch (nd_type(node)) {
14648 nd_set_type(node, NODE_DSYM);
14649 nd_set_loc(node, loc);
14652 node = str_to_sym_node(p, node, loc);
14655 node = NEW_DSYM(0, 1, NEW_LIST(node, loc), loc);
14662nd_type_st_key_enable_p(NODE *node)
14664 switch (nd_type(node)) {
14667 case NODE_RATIONAL:
14668 case NODE_IMAGINARY:
14674 case NODE_ENCODING:
14682nd_value(struct parser_params *p, NODE *node)
14684 switch (nd_type(node)) {
14686 return rb_node_str_string_val(node);
14688 return rb_node_integer_literal_val(node);
14690 return rb_node_float_literal_val(node);
14691 case NODE_RATIONAL:
14692 return rb_node_rational_literal_val(node);
14693 case NODE_IMAGINARY:
14694 return rb_node_imaginary_literal_val(node);
14696 return rb_node_sym_string_val(node);
14698 return rb_node_regx_string_val(node);
14700 return rb_node_line_lineno_val(node);
14701 case NODE_ENCODING:
14702 return rb_node_encoding_val(node);
14704 return rb_node_file_path_val(node);
14706 rb_bug("unexpected node: %s", ruby_node_name(nd_type(node)));
14707 UNREACHABLE_RETURN(0);
14712warn_duplicate_keys(struct parser_params *p, NODE *hash)
14714 /* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
14715 p->warn_duplicate_keys_table = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
14716 while (hash && RNODE_LIST(hash)->nd_next) {
14717 NODE *head = RNODE_LIST(hash)->nd_head;
14718 NODE *value = RNODE_LIST(hash)->nd_next;
14719 NODE *next = RNODE_LIST(value)->nd_next;
14723 /* keyword splat, e.g. {k: 1, **z, k: 2} */
14728 if (nd_type_st_key_enable_p(head)) {
14729 key = (st_data_t)head;
14731 if (st_delete(p->warn_duplicate_keys_table, &key, &data)) {
14732 rb_warn2L(nd_line((NODE *)data),
14733 "key %+"PRIsWARN" is duplicated and overwritten on line %d",
14734 nd_value(p, head), WARN_I(nd_line(head)));
14736 st_insert(p->warn_duplicate_keys_table, (st_data_t)key, (st_data_t)hash);
14740 st_free_table(p->warn_duplicate_keys_table);
14741 p->warn_duplicate_keys_table = NULL;
14745new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14747 if (hash) warn_duplicate_keys(p, hash);
14748 return NEW_HASH(hash, loc);
14752error_duplicate_pattern_variable(struct parser_params *p, ID id, const YYLTYPE *loc)
14754 if (is_private_local_id(p, id)) {
14757 if (st_is_member(p->pvtbl, id)) {
14758 yyerror1(loc, "duplicated variable name");
14761 st_insert(p->pvtbl, (st_data_t)id, 0);
14766error_duplicate_pattern_key(struct parser_params *p, VALUE key, const YYLTYPE *loc)
14769 p->pktbl = st_init_numtable();
14771 else if (st_is_member(p->pktbl, key)) {
14772 yyerror1(loc, "duplicated key name");
14775 st_insert(p->pktbl, (st_data_t)key, 0);
14779new_unique_key_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
14781 return NEW_HASH(hash, loc);
14785new_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14790 ID vid = get_nd_vid(p, lhs);
14791 YYLTYPE lhs_loc = lhs->nd_loc;
14793 set_nd_value(p, lhs, rhs);
14794 nd_set_loc(lhs, loc);
14795 asgn = NEW_OP_ASGN_OR(gettable(p, vid, &lhs_loc), lhs, loc);
14797 else if (op == tANDOP) {
14798 set_nd_value(p, lhs, rhs);
14799 nd_set_loc(lhs, loc);
14800 asgn = NEW_OP_ASGN_AND(gettable(p, vid, &lhs_loc), lhs, loc);
14804 rhs = NEW_CALL(gettable(p, vid, &lhs_loc), op, NEW_LIST(rhs, &rhs->nd_loc), loc);
14805 set_nd_value(p, asgn, rhs);
14806 nd_set_loc(asgn, loc);
14810 asgn = NEW_ERROR(loc);
14816new_ary_op_assign(struct parser_params *p, NODE *ary,
14817 NODE *args, ID op, NODE *rhs, const YYLTYPE *args_loc, const YYLTYPE *loc,
14818 const YYLTYPE *call_operator_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc, const YYLTYPE *binary_operator_loc)
14822 aryset_check(p, args);
14823 args = make_list(args, args_loc);
14824 asgn = NEW_OP_ASGN1(ary, op, args, rhs, loc, call_operator_loc, opening_loc, closing_loc, binary_operator_loc);
14830new_attr_op_assign(struct parser_params *p, NODE *lhs,
14831 ID atype, ID attr, ID op, NODE *rhs, const YYLTYPE *loc,
14832 const YYLTYPE *call_operator_loc, const YYLTYPE *message_loc, const YYLTYPE *binary_operator_loc)
14836 asgn = NEW_OP_ASGN2(lhs, CALL_Q_P(atype), attr, op, rhs, loc, call_operator_loc, message_loc, binary_operator_loc);
14842new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, struct lex_context ctxt, const YYLTYPE *loc)
14847 asgn = NEW_OP_CDECL(lhs, op, rhs, ctxt.shareable_constant_value, loc);
14850 asgn = NEW_ERROR(loc);
14857const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc)
14859 if (p->ctxt.in_def) {
14861 yyerror1(loc, "dynamic constant assignment");
14863 set_value(assign_error(p, "dynamic constant assignment", p->s_lvalue));
14866 return NEW_CDECL(0, 0, (path), p->ctxt.shareable_constant_value, loc);
14871assign_error(struct parser_params *p, const char *mesg, VALUE a)
14873 a = dispatch2(assign_error, ERR_MESG(), a);
14880new_bodystmt(struct parser_params *p, NODE *head, NODE *rescue, NODE *rescue_else, NODE *ensure, const YYLTYPE *loc)
14882 NODE *result = head;
14884 NODE *tmp = rescue_else ? rescue_else : rescue;
14885 YYLTYPE rescue_loc = code_loc_gen(&head->nd_loc, &tmp->nd_loc);
14887 result = NEW_RESCUE(head, rescue, rescue_else, &rescue_loc);
14888 nd_set_line(result, rescue->nd_loc.beg_pos.lineno);
14891 result = NEW_ENSURE(result, ensure, loc);
14893 fixpos(result, head);
14898warn_unused_var(struct parser_params *p, struct local_vars *local)
14902 if (!local->used) return;
14903 cnt = local->used->pos;
14904 if (cnt != local->vars->pos) {
14905 rb_parser_fatal(p, "local->used->pos != local->vars->pos");
14908 ID *v = local->vars->tbl;
14909 ID *u = local->used->tbl;
14910 for (int i = 0; i < cnt; ++i) {
14911 if (!v[i] || (u[i] & LVAR_USED)) continue;
14912 if (is_private_local_id(p, v[i])) continue;
14913 rb_warn1L((int)u[i], "assigned but unused variable - %"PRIsWARN, rb_id2str(v[i]));
14919local_push(struct parser_params *p, int toplevel_scope)
14921 struct local_vars *local;
14922 int inherits_dvars = toplevel_scope && compile_for_eval;
14923 int warn_unused_vars = RTEST(ruby_verbose);
14925 local = ALLOC(struct local_vars);
14926 local->prev = p->lvtbl;
14927 local->args = vtable_alloc(0);
14928 local->vars = vtable_alloc(inherits_dvars ? DVARS_INHERIT : DVARS_TOPSCOPE);
14930 if (toplevel_scope && compile_for_eval) warn_unused_vars = 0;
14931 if (toplevel_scope && e_option_supplied(p)) warn_unused_vars = 0;
14933 local->numparam.outer = 0;
14934 local->numparam.inner = 0;
14935 local->numparam.current = 0;
14937 local->used = warn_unused_vars ? vtable_alloc(0) : 0;
14939# if WARN_PAST_SCOPE
14948vtable_chain_free(struct parser_params *p, struct vtable *table)
14950 while (!DVARS_TERMINAL_P(table)) {
14951 struct vtable *cur_table = table;
14952 table = cur_table->prev;
14953 vtable_free(cur_table);
14958local_free(struct parser_params *p, struct local_vars *local)
14960 vtable_chain_free(p, local->used);
14962# if WARN_PAST_SCOPE
14963 vtable_chain_free(p, local->past);
14966 vtable_chain_free(p, local->args);
14967 vtable_chain_free(p, local->vars);
14969 ruby_sized_xfree(local, sizeof(struct local_vars));
14973local_pop(struct parser_params *p)
14975 struct local_vars *local = p->lvtbl->prev;
14976 if (p->lvtbl->used) {
14977 warn_unused_var(p, p->lvtbl);
14980 local_free(p, p->lvtbl);
14987static rb_ast_id_table_t *
14988local_tbl(struct parser_params *p)
14990 int cnt_args = vtable_size(p->lvtbl->args);
14991 int cnt_vars = vtable_size(p->lvtbl->vars);
14992 int cnt = cnt_args + cnt_vars;
14994 rb_ast_id_table_t *tbl;
14996 if (cnt <= 0) return 0;
14997 tbl = rb_ast_new_local_table(p->ast, cnt);
14998 MEMCPY(tbl->ids, p->lvtbl->args->tbl, ID, cnt_args);
14999 /* remove IDs duplicated to warn shadowing */
15000 for (i = 0, j = cnt_args; i < cnt_vars; ++i) {
15001 ID id = p->lvtbl->vars->tbl[i];
15002 if (!vtable_included(p->lvtbl->args, id)) {
15003 tbl->ids[j++] = id;
15007 tbl = rb_ast_resize_latest_local_table(p->ast, j);
15014numparam_name(struct parser_params *p, ID id)
15016 if (!NUMPARAM_ID_P(id)) return;
15017 compile_error(p, "_%d is reserved for numbered parameter",
15018 NUMPARAM_ID_TO_IDX(id));
15022arg_var(struct parser_params *p, ID id)
15024 numparam_name(p, id);
15025 vtable_add(p->lvtbl->args, id);
15029local_var(struct parser_params *p, ID id)
15031 numparam_name(p, id);
15032 vtable_add(p->lvtbl->vars, id);
15033 if (p->lvtbl->used) {
15034 vtable_add(p->lvtbl->used, (ID)p->ruby_sourceline);
15040rb_parser_local_defined(struct parser_params *p, ID id, const struct rb_iseq_struct *iseq)
15042 return rb_local_defined(id, iseq);
15047local_id_ref(struct parser_params *p, ID id, ID **vidrefp)
15049 struct vtable *vars, *args, *used;
15051 vars = p->lvtbl->vars;
15052 args = p->lvtbl->args;
15053 used = p->lvtbl->used;
15055 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15058 if (used) used = used->prev;
15061 if (vars && vars->prev == DVARS_INHERIT) {
15062 return rb_parser_local_defined(p, id, p->parent_iseq);
15064 else if (vtable_included(args, id)) {
15068 int i = vtable_included(vars, id);
15069 if (i && used && vidrefp) *vidrefp = &used->tbl[i-1];
15075local_id(struct parser_params *p, ID id)
15077 return local_id_ref(p, id, NULL);
15081check_forwarding_args(struct parser_params *p)
15083 if (local_id(p, idFWD_ALL)) return TRUE;
15084 compile_error(p, "unexpected ...");
15089add_forwarding_args(struct parser_params *p)
15091 arg_var(p, idFWD_REST);
15092#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15093 arg_var(p, idFWD_KWREST);
15095 arg_var(p, idFWD_BLOCK);
15096 arg_var(p, idFWD_ALL);
15100forwarding_arg_check(struct parser_params *p, ID arg, ID all, const char *var)
15102 bool conflict = false;
15104 struct vtable *vars, *args;
15106 vars = p->lvtbl->vars;
15107 args = p->lvtbl->args;
15109 while (vars && !DVARS_TERMINAL_P(vars->prev)) {
15110 conflict |= (vtable_included(args, arg) && !(all && vtable_included(args, all)));
15115 bool found = false;
15116 if (vars && vars->prev == DVARS_INHERIT && !found) {
15117 found = (rb_parser_local_defined(p, arg, p->parent_iseq) &&
15118 !(all && rb_parser_local_defined(p, all, p->parent_iseq)));
15121 found = (vtable_included(args, arg) &&
15122 !(all && vtable_included(args, all)));
15126 compile_error(p, "no anonymous %s parameter", var);
15128 else if (conflict) {
15129 compile_error(p, "anonymous %s parameter is also used within block", var);
15134new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc)
15136 NODE *rest = NEW_LVAR(idFWD_REST, loc);
15137#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15138 NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
15140 rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
15141 NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
15142 block->forwarding = TRUE;
15143#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
15144 args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);
15146 return arg_blk_pass(args, block);
15150numparam_push(struct parser_params *p)
15152 struct local_vars *local = p->lvtbl;
15153 NODE *inner = local->numparam.inner;
15154 if (!local->numparam.outer) {
15155 local->numparam.outer = local->numparam.current;
15157 local->numparam.inner = 0;
15158 local->numparam.current = 0;
15164numparam_pop(struct parser_params *p, NODE *prev_inner)
15166 struct local_vars *local = p->lvtbl;
15168 /* prefer first one */
15169 local->numparam.inner = prev_inner;
15171 else if (local->numparam.current) {
15172 /* current and inner are exclusive */
15173 local->numparam.inner = local->numparam.current;
15175 if (p->max_numparam > NO_PARAM) {
15176 /* current and outer are exclusive */
15177 local->numparam.current = local->numparam.outer;
15178 local->numparam.outer = 0;
15181 /* no numbered parameter */
15182 local->numparam.current = 0;
15187static const struct vtable *
15188dyna_push(struct parser_params *p)
15190 p->lvtbl->args = vtable_alloc(p->lvtbl->args);
15191 p->lvtbl->vars = vtable_alloc(p->lvtbl->vars);
15192 if (p->lvtbl->used) {
15193 p->lvtbl->used = vtable_alloc(p->lvtbl->used);
15195 return p->lvtbl->args;
15199dyna_pop_vtable(struct parser_params *p, struct vtable **vtblp)
15201 struct vtable *tmp = *vtblp;
15202 *vtblp = tmp->prev;
15203# if WARN_PAST_SCOPE
15204 if (p->past_scope_enabled) {
15205 tmp->prev = p->lvtbl->past;
15206 p->lvtbl->past = tmp;
15214dyna_pop_1(struct parser_params *p)
15216 struct vtable *tmp;
15218 if ((tmp = p->lvtbl->used) != 0) {
15219 warn_unused_var(p, p->lvtbl);
15220 p->lvtbl->used = p->lvtbl->used->prev;
15223 dyna_pop_vtable(p, &p->lvtbl->args);
15224 dyna_pop_vtable(p, &p->lvtbl->vars);
15228dyna_pop(struct parser_params *p, const struct vtable *lvargs)
15230 while (p->lvtbl->args != lvargs) {
15232 if (!p->lvtbl->args) {
15233 struct local_vars *local = p->lvtbl->prev;
15234 ruby_sized_xfree(p->lvtbl, sizeof(*p->lvtbl));
15242dyna_in_block(struct parser_params *p)
15244 return !DVARS_TERMINAL_P(p->lvtbl->vars) && p->lvtbl->vars->prev != DVARS_TOPSCOPE;
15249dvar_defined_ref(struct parser_params *p, ID id, ID **vidrefp)
15251 struct vtable *vars, *args, *used;
15254 args = p->lvtbl->args;
15255 vars = p->lvtbl->vars;
15256 used = p->lvtbl->used;
15258 while (!DVARS_TERMINAL_P(vars)) {
15259 if (vtable_included(args, id)) {
15262 if ((i = vtable_included(vars, id)) != 0) {
15263 if (used && vidrefp) *vidrefp = &used->tbl[i-1];
15268 if (!vidrefp) used = 0;
15269 if (used) used = used->prev;
15272 if (vars == DVARS_INHERIT && !NUMPARAM_ID_P(id)) {
15273 return rb_dvar_defined(id, p->parent_iseq);
15281dvar_defined(struct parser_params *p, ID id)
15283 return dvar_defined_ref(p, id, NULL);
15287dvar_curr(struct parser_params *p, ID id)
15289 return (vtable_included(p->lvtbl->args, id) ||
15290 vtable_included(p->lvtbl->vars, id));
15294reg_fragment_enc_error(struct parser_params* p, rb_parser_string_t *str, int c)
15297 "regexp encoding option '%c' differs from source encoding '%s'",
15298 c, rb_enc_name(rb_parser_str_get_encoding(str)));
15302static rb_encoding *
15303find_enc(struct parser_params* p, const char *name)
15305 int idx = rb_enc_find_index(name);
15307 rb_bug("unknown encoding name: %s", name);
15310 return rb_enc_from_index(idx);
15313static rb_encoding *
15314kcode_to_enc(struct parser_params* p, int kcode)
15319 case ENC_ASCII8BIT:
15320 enc = rb_ascii8bit_encoding();
15323 enc = find_enc(p, "EUC-JP");
15325 case ENC_Windows_31J:
15326 enc = find_enc(p, "Windows-31J");
15329 enc = rb_utf8_encoding();
15340rb_reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15342 int c = RE_OPTION_ENCODING_IDX(options);
15348 char_to_option_kcode(c, &opt, &idx);
15349 enc = kcode_to_enc(p, idx);
15350 if (enc != rb_parser_str_get_encoding(str) &&
15351 !rb_parser_is_ascii_string(p, str)) {
15354 rb_parser_string_set_encoding(str, enc);
15356 else if (RE_OPTION_ENCODING_NONE(options)) {
15357 if (!PARSER_ENCODING_IS_ASCII8BIT(p, str) &&
15358 !rb_parser_is_ascii_string(p, str)) {
15362 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15364 else if (rb_is_usascii_enc(p->enc)) {
15365 if (!rb_parser_is_ascii_string(p, str)) {
15366 /* raise in re.c */
15367 rb_parser_enc_associate(p, str, rb_usascii_encoding());
15370 rb_parser_enc_associate(p, str, rb_ascii8bit_encoding());
15381reg_fragment_setenc(struct parser_params* p, rb_parser_string_t *str, int options)
15383 int c = rb_reg_fragment_setenc(p, str, options);
15384 if (c) reg_fragment_enc_error(p, str, c);
15388reg_fragment_error(struct parser_params* p, VALUE err)
15390 compile_error(p, "%"PRIsVALUE, err);
15395rb_parser_reg_fragment_check(struct parser_params* p, rb_parser_string_t *str, int options, rb_parser_reg_fragment_error_func error)
15398 reg_fragment_setenc(p, str, options);
15400 str2 = rb_str_new_parser_string(str);
15401 err = rb_reg_check_preprocess(str2);
15403 err = rb_obj_as_string(err);
15411#ifndef UNIVERSAL_PARSER
15413 struct parser_params* parser;
15416 const YYLTYPE *loc;
15417 rb_parser_assignable_func assignable;
15418} reg_named_capture_assign_t;
15421reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
15422 int back_num, int *back_refs, OnigRegex regex, void *arg0)
15424 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
15425 struct parser_params* p = arg->parser;
15426 rb_encoding *enc = arg->enc;
15427 long len = name_end - name;
15428 const char *s = (const char *)name;
15430 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, arg->loc, arg->assignable);
15434reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc, rb_parser_assignable_func assignable)
15436 reg_named_capture_assign_t arg;
15439 arg.enc = rb_enc_get(regexp);
15440 arg.succ_block = 0;
15442 arg.assignable = assignable;
15443 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
15445 if (!arg.succ_block) return 0;
15446 return RNODE_BLOCK(arg.succ_block)->nd_next;
15452rb_parser_assignable(struct parser_params *p, ID id, NODE *val, const YYLTYPE *loc)
15454 return assignable(p, id, val, loc);
15458rb_reg_named_capture_assign_iter_impl(struct parser_params *p, const char *s, long len,
15459 rb_encoding *enc, NODE **succ_block, const rb_code_location_t *loc, rb_parser_assignable_func assignable)
15464 if (!len) return ST_CONTINUE;
15465 if (!VALID_SYMNAME_P(s, len, enc, ID_LOCAL))
15466 return ST_CONTINUE;
15468 var = intern_cstr(s, len, enc);
15469 if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
15470 if (!lvar_defined(p, var)) return ST_CONTINUE;
15472 node = node_assign(p, assignable(p, var, 0, loc), NEW_SYM(rb_id2str(var), loc), NO_LEX_CTXT, loc);
15473 succ = *succ_block;
15474 if (!succ) succ = NEW_ERROR(loc);
15475 succ = block_append(p, succ, node);
15476 *succ_block = succ;
15477 return ST_CONTINUE;
15482parser_reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15485 reg_fragment_setenc(p, str, options);
15486 str2 = rb_str_new_parser_string(str);
15487 return rb_parser_reg_compile(p, str2, options);
15492rb_parser_reg_compile(struct parser_params* p, VALUE str, int options)
15494 return rb_reg_compile(str, options & RE_OPTION_MASK, p->ruby_sourcefile, p->ruby_sourceline);
15499reg_compile(struct parser_params* p, rb_parser_string_t *str, int options)
15504 err = rb_errinfo();
15505 re = parser_reg_compile(p, str, options);
15507 VALUE m = rb_attr_get(rb_errinfo(), idMesg);
15508 rb_set_errinfo(err);
15509 reg_fragment_error(p, m);
15517rb_ruby_parser_set_options(struct parser_params *p, int print, int loop, int chomp, int split)
15519 p->do_print = print;
15521 p->do_chomp = chomp;
15522 p->do_split = split;
15526parser_append_options(struct parser_params *p, NODE *node)
15528 static const YYLTYPE default_location = {{1, 0}, {1, 0}};
15529 const YYLTYPE *const LOC = &default_location;
15532 NODE *print = (NODE *)NEW_FCALL(rb_intern("print"),
15533 NEW_LIST(NEW_GVAR(idLASTLINE, LOC), LOC),
15535 node = block_append(p, node, print);
15539 NODE *irs = NEW_LIST(NEW_GVAR(rb_intern("$/"), LOC), LOC);
15542 ID ifs = rb_intern("$;");
15543 ID fields = rb_intern("$F");
15544 NODE *args = NEW_LIST(NEW_GVAR(ifs, LOC), LOC);
15545 NODE *split = NEW_GASGN(fields,
15546 NEW_CALL(NEW_GVAR(idLASTLINE, LOC),
15547 rb_intern("split"), args, LOC),
15549 node = block_append(p, split, node);
15552 NODE *chomp = NEW_SYM(rb_str_new_cstr("chomp"), LOC);
15553 chomp = list_append(p, NEW_LIST(chomp, LOC), NEW_TRUE(LOC));
15554 irs = list_append(p, irs, NEW_HASH(chomp, LOC));
15557 node = NEW_WHILE((NODE *)NEW_FCALL(idGets, irs, LOC), node, 1, LOC, &NULL_LOC, &NULL_LOC);
15566 /* just to suppress unused-function warnings */
15572internal_id(struct parser_params *p)
15574 return rb_make_temporary_id(vtable_size(p->lvtbl->args) + vtable_size(p->lvtbl->vars));
15576#endif /* !RIPPER */
15579parser_initialize(struct parser_params *p)
15581 /* note: we rely on TypedData_Make_Struct to set most fields to 0 */
15582 p->command_start = TRUE;
15583 p->ruby_sourcefile_string = Qnil;
15584 p->lex.lpar_beg = -1; /* make lambda_beginning_p() == FALSE at first */
15585 string_buffer_init(p);
15587 p->delayed.token = NULL;
15588 p->frozen_string_literal = -1; /* not specified */
15590 p->error_buffer = Qfalse;
15591 p->end_expect_token_locations = NULL;
15596 p->parsing_thread = Qnil;
15598 p->s_lvalue = Qnil;
15599 p->s_value_stack = rb_ary_new();
15601 p->debug_buffer = Qnil;
15602 p->debug_output = rb_ractor_stdout();
15603 p->enc = rb_utf8_encoding();
15608#define rb_ruby_parser_mark ripper_parser_mark
15609#define rb_ruby_parser_free ripper_parser_free
15610#define rb_ruby_parser_memsize ripper_parser_memsize
15614rb_ruby_parser_mark(void *ptr)
15616 struct parser_params *p = (struct parser_params*)ptr;
15618 rb_gc_mark(p->ruby_sourcefile_string);
15620 rb_gc_mark(p->error_buffer);
15622 rb_gc_mark(p->value);
15623 rb_gc_mark(p->result);
15624 rb_gc_mark(p->parsing_thread);
15625 rb_gc_mark(p->s_value);
15626 rb_gc_mark(p->s_lvalue);
15627 rb_gc_mark(p->s_value_stack);
15629 rb_gc_mark(p->debug_buffer);
15630 rb_gc_mark(p->debug_output);
15634rb_ruby_parser_free(void *ptr)
15636 struct parser_params *p = (struct parser_params*)ptr;
15637 struct local_vars *local, *prev;
15640 rb_ast_free(p->ast);
15643 if (p->warn_duplicate_keys_table) {
15644 st_free_table(p->warn_duplicate_keys_table);
15649 rb_parser_ary_free(p, p->tokens);
15654 ruby_sized_xfree(p->tokenbuf, p->toksiz);
15657 for (local = p->lvtbl; local; local = prev) {
15658 prev = local->prev;
15659 local_free(p, local);
15663 token_info *ptinfo;
15664 while ((ptinfo = p->token_info) != 0) {
15665 p->token_info = ptinfo->next;
15669 string_buffer_free(p);
15672 st_free_table(p->pvtbl);
15675 if (CASE_LABELS_ENABLED_P(p->case_labels)) {
15676 st_free_table(p->case_labels);
15679 xfree(p->lex.strterm);
15680 p->lex.strterm = 0;
15686rb_ruby_parser_memsize(const void *ptr)
15688 struct parser_params *p = (struct parser_params*)ptr;
15689 struct local_vars *local;
15690 size_t size = sizeof(*p);
15693 for (local = p->lvtbl; local; local = local->prev) {
15694 size += sizeof(*local);
15695 if (local->vars) size += local->vars->capa * sizeof(ID);
15701#undef rb_reserved_word
15703const struct kwtable *
15704rb_reserved_word(const char *str, unsigned int len)
15706 return reserved_word(str, len);
15709#ifdef UNIVERSAL_PARSER
15711rb_ruby_parser_allocate(const rb_parser_config_t *config)
15713 /* parser_initialize expects fields to be set to 0 */
15714 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15715 p->config = config;
15720rb_ruby_parser_new(const rb_parser_config_t *config)
15722 /* parser_initialize expects fields to be set to 0 */
15723 rb_parser_t *p = rb_ruby_parser_allocate(config);
15724 parser_initialize(p);
15729rb_ruby_parser_allocate(void)
15731 /* parser_initialize expects fields to be set to 0 */
15732 rb_parser_t *p = (rb_parser_t *)ruby_xcalloc(1, sizeof(rb_parser_t));
15737rb_ruby_parser_new(void)
15739 /* parser_initialize expects fields to be set to 0 */
15740 rb_parser_t *p = rb_ruby_parser_allocate();
15741 parser_initialize(p);
15747rb_ruby_parser_set_context(rb_parser_t *p, const struct rb_iseq_struct *base, int main)
15749 p->error_buffer = main ? Qfalse : Qnil;
15750 p->parent_iseq = base;
15755rb_ruby_parser_set_script_lines(rb_parser_t *p)
15757 p->debug_lines = rb_parser_ary_new_capa_for_script_line(p, 10);
15761rb_ruby_parser_error_tolerant(rb_parser_t *p)
15763 p->error_tolerant = 1;
15767rb_ruby_parser_keep_tokens(rb_parser_t *p)
15769 p->keep_tokens = 1;
15770 p->tokens = rb_parser_ary_new_capa_for_ast_token(p, 10);
15774rb_ruby_parser_encoding(rb_parser_t *p)
15780rb_ruby_parser_end_seen_p(rb_parser_t *p)
15782 return p->ruby__end__seen;
15786rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag)
15791#endif /* !RIPPER */
15795rb_ruby_parser_get_yydebug(rb_parser_t *p)
15801rb_ruby_parser_set_value(rb_parser_t *p, VALUE value)
15807rb_ruby_parser_error_p(rb_parser_t *p)
15813rb_ruby_parser_debug_output(rb_parser_t *p)
15815 return p->debug_output;
15819rb_ruby_parser_set_debug_output(rb_parser_t *p, VALUE output)
15821 p->debug_output = output;
15825rb_ruby_parser_parsing_thread(rb_parser_t *p)
15827 return p->parsing_thread;
15831rb_ruby_parser_set_parsing_thread(rb_parser_t *p, VALUE parsing_thread)
15833 p->parsing_thread = parsing_thread;
15837rb_ruby_parser_ripper_initialize(rb_parser_t *p, rb_parser_lex_gets_func *gets, rb_parser_input_data input, VALUE sourcefile_string, const char *sourcefile, int sourceline)
15839 p->lex.gets = gets;
15840 p->lex.input = input;
15842 p->ruby_sourcefile_string = sourcefile_string;
15843 p->ruby_sourcefile = sourcefile;
15844 p->ruby_sourceline = sourceline;
15848rb_ruby_parser_result(rb_parser_t *p)
15854rb_ruby_parser_enc(rb_parser_t *p)
15860rb_ruby_parser_ruby_sourcefile_string(rb_parser_t *p)
15862 return p->ruby_sourcefile_string;
15866rb_ruby_parser_ruby_sourceline(rb_parser_t *p)
15868 return p->ruby_sourceline;
15872rb_ruby_parser_lex_state(rb_parser_t *p)
15874 return p->lex.state;
15878rb_ruby_ripper_parse0(rb_parser_t *p)
15881 p->ast = rb_ast_new();
15882 ripper_yyparse((void*)p);
15883 rb_ast_free(p->ast);
15886 p->eval_tree_begin = 0;
15890rb_ruby_ripper_dedent_string(rb_parser_t *p, rb_parser_string_t *string, int width)
15892 return dedent_string(p, string, width);
15896rb_ruby_ripper_initialized_p(rb_parser_t *p)
15898 return p->lex.input != 0;
15902rb_ruby_ripper_parser_initialize(rb_parser_t *p)
15904 parser_initialize(p);
15908rb_ruby_ripper_column(rb_parser_t *p)
15910 return p->lex.ptok - p->lex.pbeg;
15914rb_ruby_ripper_token_len(rb_parser_t *p)
15916 return p->lex.pcur - p->lex.ptok;
15919rb_parser_string_t *
15920rb_ruby_ripper_lex_lastline(rb_parser_t *p)
15922 return p->lex.lastline;
15926rb_ruby_ripper_lex_state_name(struct parser_params *p, int state)
15928 return rb_parser_lex_state_name(p, (enum lex_state_e)state);
15931#ifdef UNIVERSAL_PARSER
15933rb_ripper_parser_params_allocate(const rb_parser_config_t *config)
15935 rb_parser_t *p = (rb_parser_t *)config->calloc(1, sizeof(rb_parser_t));
15936 p->config = config;
15941struct parser_params*
15942rb_ruby_ripper_parser_allocate(void)
15944 return (struct parser_params *)ruby_xcalloc(1, sizeof(struct parser_params));
15950rb_parser_printf(struct parser_params *p, const char *fmt, ...)
15953 VALUE mesg = p->debug_buffer;
15955 if (NIL_P(mesg)) p->debug_buffer = mesg = rb_str_new(0, 0);
15957 rb_str_vcatf(mesg, fmt, ap);
15959 if (char_at_end(p, mesg, 0) == '\n') {
15960 rb_io_write(p->debug_output, mesg);
15961 p->debug_buffer = Qnil;
15966parser_compile_error(struct parser_params *p, const rb_code_location_t *loc, const char *fmt, ...)
15969 int lineno, column;
15972 lineno = loc->end_pos.lineno;
15973 column = loc->end_pos.column;
15976 lineno = p->ruby_sourceline;
15977 column = rb_long2int(p->lex.pcur - p->lex.pbeg);
15980 rb_io_flush(p->debug_output);
15984 rb_syntax_error_append(p->error_buffer,
15985 p->ruby_sourcefile_string,
15992count_char(const char *str, int c)
15995 while (str[n] == c) ++n;
16000 * strip enclosing double-quotes, same as the default yytnamerr except
16001 * for that single-quotes matching back-quotes do not stop stripping.
16003 * "\"`class' keyword\"" => "`class' keyword"
16006rb_yytnamerr(struct parser_params *p, char *yyres, const char *yystr)
16008 if (*yystr == '"') {
16009 size_t yyn = 0, bquote = 0;
16010 const char *yyp = yystr;
16016 bquote = count_char(yyp+1, '\'') + 1;
16017 if (yyres) memcpy(&yyres[yyn], yyp, bquote);
16023 if (bquote && count_char(yyp+1, '\'') + 1 == bquote) {
16024 if (yyres) memcpy(yyres + yyn, yyp, bquote);
16030 if (yyp[1] && yyp[1] != '\'' && yyp[2] == '\'') {
16031 if (yyres) memcpy(yyres + yyn, yyp, 3);
16036 goto do_not_strip_quotes;
16040 goto do_not_strip_quotes;
16043 if (*++yyp != '\\')
16044 goto do_not_strip_quotes;
16045 /* Fall through. */
16059 do_not_strip_quotes: ;
16062 if (!yyres) return strlen(yystr);
16064 return (YYSIZE_T)(yystpcpy(yyres, yystr) - yyres);
16069#define validate(x) (void)(x)
16072ripper_dispatch0(struct parser_params *p, ID mid)
16074 return rb_funcall(p->value, mid, 0);
16078ripper_dispatch1(struct parser_params *p, ID mid, VALUE a)
16081 return rb_funcall(p->value, mid, 1, a);
16085ripper_dispatch2(struct parser_params *p, ID mid, VALUE a, VALUE b)
16089 return rb_funcall(p->value, mid, 2, a, b);
16093ripper_dispatch3(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c)
16098 return rb_funcall(p->value, mid, 3, a, b, c);
16102ripper_dispatch4(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d)
16108 return rb_funcall(p->value, mid, 4, a, b, c, d);
16112ripper_dispatch5(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e)
16119 return rb_funcall(p->value, mid, 5, a, b, c, d, e);
16123ripper_dispatch7(struct parser_params *p, ID mid, VALUE a, VALUE b, VALUE c, VALUE d, VALUE e, VALUE f, VALUE g)
16132 return rb_funcall(p->value, mid, 7, a, b, c, d, e, f, g);
16136ripper_error(struct parser_params *p)
16142ripper_value(struct parser_params *p)
16144 (void)yystpcpy; /* may not used in newer bison */
16153 * c-file-style: "ruby"