Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: trunk
Fetching contributors…

Cannot retrieve contributors at this time

10748 lines (9946 sloc) 234.544 kb
/**********************************************************************
parse.y -
$Author$
created at: Fri May 28 18:02:42 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
%{
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define YYSTACK_USE_ALLOCA 0
#include "ruby/ruby.h"
#include "ruby/st.h"
#include "ruby/encoding.h"
#include "node.h"
#include "parse.h"
#include "id.h"
#include "regenc.h"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#define YYMALLOC(size) rb_parser_malloc(parser, (size))
#define YYREALLOC(ptr, size) rb_parser_realloc(parser, (ptr), (size))
#define YYCALLOC(nelem, size) rb_parser_calloc(parser, (nelem), (size))
#define YYFREE(ptr) rb_parser_free(parser, (ptr))
#define malloc YYMALLOC
#define realloc YYREALLOC
#define calloc YYCALLOC
#define free YYFREE
#ifndef RIPPER
static ID register_symid(ID, const char *, long, rb_encoding *);
#define REGISTER_SYMID(id, name) register_symid((id), (name), strlen(name), enc)
#include "id.c"
#endif
#define is_notop_id(id) ((id)>tLAST_TOKEN)
#define is_local_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_LOCAL)
#define is_global_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_GLOBAL)
#define is_instance_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_INSTANCE)
#define is_attrset_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_ATTRSET)
#define is_const_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CONST)
#define is_class_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_CLASS)
#define is_junk_id(id) (is_notop_id(id)&&((id)&ID_SCOPE_MASK)==ID_JUNK)
#define is_asgn_or_id(id) ((is_notop_id(id)) && \
(((id)&ID_SCOPE_MASK) == ID_GLOBAL || \
((id)&ID_SCOPE_MASK) == ID_INSTANCE || \
((id)&ID_SCOPE_MASK) == ID_CLASS))
enum lex_state_e {
EXPR_BEG, /* ignore newline, +/- is a sign. */
EXPR_END, /* newline significant, +/- is an operator. */
EXPR_ENDARG, /* ditto, and unbound braces. */
EXPR_ENDFN, /* ditto, and unbound braces. */
EXPR_ARG, /* newline significant, +/- is an operator. */
EXPR_CMDARG, /* newline significant, +/- is an operator. */
EXPR_MID, /* newline significant, +/- is an operator. */
EXPR_FNAME, /* ignore newline, no reserved words. */
EXPR_DOT, /* right after `.' or `::', no reserved words. */
EXPR_CLASS, /* immediate after `class', no here document. */
EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */
EXPR_MAX_STATE
};
typedef VALUE stack_type;
# define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1))
# define BITSTACK_POP(stack) ((stack) = (stack) >> 1)
# define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1))
# define BITSTACK_SET_P(stack) ((stack)&1)
#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
#define COND_POP() BITSTACK_POP(cond_stack)
#define COND_LEXPOP() BITSTACK_LEXPOP(cond_stack)
#define COND_P() BITSTACK_SET_P(cond_stack)
#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
#define CMDARG_LEXPOP() BITSTACK_LEXPOP(cmdarg_stack)
#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
struct vtable {
ID *tbl;
int pos;
int capa;
struct vtable *prev;
};
struct local_vars {
struct vtable *args;
struct vtable *vars;
struct vtable *used;
struct local_vars *prev;
};
#define DVARS_INHERIT ((void*)1)
#define DVARS_TOPSCOPE NULL
#define DVARS_SPECIAL_P(tbl) (!POINTER_P(tbl))
#define POINTER_P(val) ((VALUE)(val) & ~(VALUE)3)
static int
vtable_size(const struct vtable *tbl)
{
if (POINTER_P(tbl)) {
return tbl->pos;
}
else {
return 0;
}
}
#define VTBL_DEBUG 0
static struct vtable *
vtable_alloc(struct vtable *prev)
{
struct vtable *tbl = ALLOC(struct vtable);
tbl->pos = 0;
tbl->capa = 8;
tbl->tbl = ALLOC_N(ID, tbl->capa);
tbl->prev = prev;
if (VTBL_DEBUG) printf("vtable_alloc: %p\n", (void *)tbl);
return tbl;
}
static void
vtable_free(struct vtable *tbl)
{
if (VTBL_DEBUG)printf("vtable_free: %p\n", (void *)tbl);
if (POINTER_P(tbl)) {
if (tbl->tbl) {
xfree(tbl->tbl);
}
xfree(tbl);
}
}
static void
vtable_add(struct vtable *tbl, ID id)
{
if (!POINTER_P(tbl)) {
rb_bug("vtable_add: vtable is not allocated (%p)", (void *)tbl);
}
if (VTBL_DEBUG) printf("vtable_add: %p, %s\n", (void *)tbl, rb_id2name(id));
if (tbl->pos == tbl->capa) {
tbl->capa = tbl->capa * 2;
REALLOC_N(tbl->tbl, ID, tbl->capa);
}
tbl->tbl[tbl->pos++] = id;
}
static int
vtable_included(const struct vtable * tbl, ID id)
{
int i;
if (POINTER_P(tbl)) {
for (i = 0; i < tbl->pos; i++) {
if (tbl->tbl[i] == id) {
return i+1;
}
}
}
return 0;
}
#ifndef RIPPER
typedef struct token_info {
const char *token;
int linenum;
int column;
int nonspc;
struct token_info *next;
} token_info;
#endif
/*
Structure of Lexer Buffer:
lex_pbeg tokp lex_p lex_pend
| | | |
|-----------+--------------+------------|
|<------------>|
token
*/
struct parser_params {
int is_ripper;
NODE *heap;
YYSTYPE *parser_yylval;
VALUE eofp;
NODE *parser_lex_strterm;
enum lex_state_e parser_lex_state;
stack_type parser_cond_stack;
stack_type parser_cmdarg_stack;
int parser_class_nest;
int parser_paren_nest;
int parser_lpar_beg;
int parser_in_single;
int parser_in_def;
int parser_compile_for_eval;
VALUE parser_cur_mid;
int parser_in_defined;
char *parser_tokenbuf;
int parser_tokidx;
int parser_toksiz;
VALUE parser_lex_input;
VALUE parser_lex_lastline;
VALUE parser_lex_nextline;
const char *parser_lex_pbeg;
const char *parser_lex_p;
const char *parser_lex_pend;
int parser_heredoc_end;
int parser_command_start;
NODE *parser_deferred_nodes;
long parser_lex_gets_ptr;
VALUE (*parser_lex_gets)(struct parser_params*,VALUE);
struct local_vars *parser_lvtbl;
int parser_ruby__end__seen;
int line_count;
int has_shebang;
char *parser_ruby_sourcefile; /* current source file */
int parser_ruby_sourceline; /* current line no. */
rb_encoding *enc;
rb_encoding *utf8;
int parser_yydebug;
#ifndef RIPPER
/* Ruby core only */
NODE *parser_eval_tree_begin;
NODE *parser_eval_tree;
VALUE debug_lines;
VALUE coverage;
int nerr;
int parser_token_info_enabled;
token_info *parser_token_info;
#else
/* Ripper only */
VALUE parser_ruby_sourcefile_string;
const char *tokp;
VALUE delayed;
int delayed_line;
int delayed_col;
VALUE value;
VALUE result;
VALUE parsing_thread;
int toplevel_p;
#endif
};
#define UTF8_ENC() (parser->utf8 ? parser->utf8 : \
(parser->utf8 = rb_utf8_encoding()))
#define STR_NEW(p,n) rb_enc_str_new((p),(n),parser->enc)
#define STR_NEW0() rb_enc_str_new(0,0,parser->enc)
#define STR_NEW2(p) rb_enc_str_new((p),strlen(p),parser->enc)
#define STR_NEW3(p,n,e,func) parser_str_new((p),(n),(e),(func),parser->enc)
#define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
#define TOK_INTERN(mb) rb_intern3(tok(), toklen(), parser->enc)
static int parser_yyerror(struct parser_params*, const char*);
#define yyerror(msg) parser_yyerror(parser, (msg))
#define YYLEX_PARAM parser
#define lex_strterm (parser->parser_lex_strterm)
#define lex_state (parser->parser_lex_state)
#define cond_stack (parser->parser_cond_stack)
#define cmdarg_stack (parser->parser_cmdarg_stack)
#define class_nest (parser->parser_class_nest)
#define paren_nest (parser->parser_paren_nest)
#define lpar_beg (parser->parser_lpar_beg)
#define in_single (parser->parser_in_single)
#define in_def (parser->parser_in_def)
#define compile_for_eval (parser->parser_compile_for_eval)
#define cur_mid (parser->parser_cur_mid)
#define in_defined (parser->parser_in_defined)
#define tokenbuf (parser->parser_tokenbuf)
#define tokidx (parser->parser_tokidx)
#define toksiz (parser->parser_toksiz)
#define lex_input (parser->parser_lex_input)
#define lex_lastline (parser->parser_lex_lastline)
#define lex_nextline (parser->parser_lex_nextline)
#define lex_pbeg (parser->parser_lex_pbeg)
#define lex_p (parser->parser_lex_p)
#define lex_pend (parser->parser_lex_pend)
#define heredoc_end (parser->parser_heredoc_end)
#define command_start (parser->parser_command_start)
#define deferred_nodes (parser->parser_deferred_nodes)
#define lex_gets_ptr (parser->parser_lex_gets_ptr)
#define lex_gets (parser->parser_lex_gets)
#define lvtbl (parser->parser_lvtbl)
#define ruby__end__seen (parser->parser_ruby__end__seen)
#define ruby_sourceline (parser->parser_ruby_sourceline)
#define ruby_sourcefile (parser->parser_ruby_sourcefile)
#define current_enc (parser->enc)
#define yydebug (parser->parser_yydebug)
#ifdef RIPPER
#else
#define ruby_eval_tree (parser->parser_eval_tree)
#define ruby_eval_tree_begin (parser->parser_eval_tree_begin)
#define ruby_debug_lines (parser->debug_lines)
#define ruby_coverage (parser->coverage)
#endif
static int yylex(void*, void*);
#ifndef RIPPER
#define yyparse ruby_yyparse
static NODE* node_newnode(struct parser_params *, enum node_type, VALUE, VALUE, VALUE);
#define rb_node_newnode(type, a1, a2, a3) node_newnode(parser, (type), (a1), (a2), (a3))
static NODE *cond_gen(struct parser_params*,NODE*);
#define cond(node) cond_gen(parser, (node))
static NODE *logop_gen(struct parser_params*,enum node_type,NODE*,NODE*);
#define logop(type,node1,node2) logop_gen(parser, (type), (node1), (node2))
static NODE *newline_node(NODE*);
static void fixpos(NODE*,NODE*);
static int value_expr_gen(struct parser_params*,NODE*);
static void void_expr_gen(struct parser_params*,NODE*);
static NODE *remove_begin(NODE*);
#define value_expr(node) value_expr_gen(parser, (node) = remove_begin(node))
#define void_expr0(node) void_expr_gen(parser, (node))
#define void_expr(node) void_expr0((node) = remove_begin(node))
static void void_stmts_gen(struct parser_params*,NODE*);
#define void_stmts(node) void_stmts_gen(parser, (node))
static void reduce_nodes_gen(struct parser_params*,NODE**);
#define reduce_nodes(n) reduce_nodes_gen(parser,(n))
static void block_dup_check_gen(struct parser_params*,NODE*,NODE*);
#define block_dup_check(n1,n2) block_dup_check_gen(parser,(n1),(n2))
static NODE *block_append_gen(struct parser_params*,NODE*,NODE*);
#define block_append(h,t) block_append_gen(parser,(h),(t))
static NODE *list_append_gen(struct parser_params*,NODE*,NODE*);
#define list_append(l,i) list_append_gen(parser,(l),(i))
static NODE *list_concat_gen(struct parser_params*,NODE*,NODE*);
#define list_concat(h,t) list_concat_gen(parser,(h),(t))
static NODE *arg_append_gen(struct parser_params*,NODE*,NODE*);
#define arg_append(h,t) arg_append_gen(parser,(h),(t))
static NODE *arg_concat_gen(struct parser_params*,NODE*,NODE*);
#define arg_concat(h,t) arg_concat_gen(parser,(h),(t))
static NODE *literal_concat_gen(struct parser_params*,NODE*,NODE*);
#define literal_concat(h,t) literal_concat_gen(parser,(h),(t))
static int literal_concat0(struct parser_params *, VALUE, VALUE);
static NODE *new_evstr_gen(struct parser_params*,NODE*);
#define new_evstr(n) new_evstr_gen(parser,(n))
static NODE *evstr2dstr_gen(struct parser_params*,NODE*);
#define evstr2dstr(n) evstr2dstr_gen(parser,(n))
static NODE *splat_array(NODE*);
static NODE *call_bin_op_gen(struct parser_params*,NODE*,ID,NODE*);
#define call_bin_op(recv,id,arg1) call_bin_op_gen(parser, (recv),(id),(arg1))
static NODE *call_uni_op_gen(struct parser_params*,NODE*,ID);
#define call_uni_op(recv,id) call_uni_op_gen(parser, (recv),(id))
static NODE *new_args_gen(struct parser_params*,NODE*,NODE*,ID,NODE*,ID);
#define new_args(f,o,r,p,b) new_args_gen(parser, (f),(o),(r),(p),(b))
static NODE *negate_lit(NODE*);
static NODE *ret_args_gen(struct parser_params*,NODE*);
#define ret_args(node) ret_args_gen(parser, (node))
static NODE *arg_blk_pass(NODE*,NODE*);
static NODE *new_yield_gen(struct parser_params*,NODE*);
#define new_yield(node) new_yield_gen(parser, (node))
static NODE *gettable_gen(struct parser_params*,ID);
#define gettable(id) gettable_gen(parser,(id))
static NODE *assignable_gen(struct parser_params*,ID,NODE*);
#define assignable(id,node) assignable_gen(parser, (id), (node))
static NODE *aryset_gen(struct parser_params*,NODE*,NODE*);
#define aryset(node1,node2) aryset_gen(parser, (node1), (node2))
static NODE *attrset_gen(struct parser_params*,NODE*,ID);
#define attrset(node,id) attrset_gen(parser, (node), (id))
static void rb_backref_error_gen(struct parser_params*,NODE*);
#define rb_backref_error(n) rb_backref_error_gen(parser,(n))
static NODE *node_assign_gen(struct parser_params*,NODE*,NODE*);
#define node_assign(node1, node2) node_assign_gen(parser, (node1), (node2))
static NODE *match_op_gen(struct parser_params*,NODE*,NODE*);
#define match_op(node1,node2) match_op_gen(parser, (node1), (node2))
static ID *local_tbl_gen(struct parser_params*);
#define local_tbl() local_tbl_gen(parser)
static void fixup_nodes(NODE **);
extern int rb_dvar_defined(ID);
extern int rb_local_defined(ID);
extern int rb_parse_in_eval(void);
extern int rb_parse_in_main(void);
static VALUE reg_compile_gen(struct parser_params*, VALUE, int);
#define reg_compile(str,options) reg_compile_gen(parser, (str), (options))
static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int);
#define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, (str), (options))
static int reg_fragment_check_gen(struct parser_params*, VALUE, int);
#define reg_fragment_check(str,options) reg_fragment_check_gen(parser, (str), (options))
static NODE *reg_named_capture_assign_gen(struct parser_params* parser, VALUE regexp, NODE *match);
#define reg_named_capture_assign(regexp,match) reg_named_capture_assign_gen(parser,(regexp),(match))
#define get_id(id) (id)
#define get_value(val) (val)
#else
#define remove_begin(node) (node)
#define rb_dvar_defined(id) 0
#define rb_local_defined(id) 0
static ID ripper_get_id(VALUE);
#define get_id(id) ripper_get_id(id)
static VALUE ripper_get_value(VALUE);
#define get_value(val) ripper_get_value(val)
static VALUE assignable_gen(struct parser_params*,VALUE);
#define assignable(lhs,node) assignable_gen(parser, (lhs))
#endif /* !RIPPER */
static ID formal_argument_gen(struct parser_params*, ID);
#define formal_argument(id) formal_argument_gen(parser, (id))
static ID shadowing_lvar_gen(struct parser_params*,ID);
#define shadowing_lvar(name) shadowing_lvar_gen(parser, (name))
static void new_bv_gen(struct parser_params*,ID);
#define new_bv(id) new_bv_gen(parser, (id))
static void local_push_gen(struct parser_params*,int);
#define local_push(top) local_push_gen(parser,(top))
static void local_pop_gen(struct parser_params*);
#define local_pop() local_pop_gen(parser)
static int local_var_gen(struct parser_params*, ID);
#define local_var(id) local_var_gen(parser, (id));
static int arg_var_gen(struct parser_params*, ID);
#define arg_var(id) arg_var_gen(parser, (id))
static int local_id_gen(struct parser_params*, ID);
#define local_id(id) local_id_gen(parser, (id))
static ID internal_id_gen(struct parser_params*);
#define internal_id() internal_id_gen(parser)
static const struct vtable *dyna_push_gen(struct parser_params *);
#define dyna_push() dyna_push_gen(parser)
static void dyna_pop_gen(struct parser_params*, const struct vtable *);
#define dyna_pop(node) dyna_pop_gen(parser, (node))
static int dyna_in_block_gen(struct parser_params*);
#define dyna_in_block() dyna_in_block_gen(parser)
#define dyna_var(id) local_var(id)
static int dvar_defined_gen(struct parser_params*,ID,int);
#define dvar_defined(id) dvar_defined_gen(parser, (id), 0)
#define dvar_defined_get(id) dvar_defined_gen(parser, (id), 1)
static int dvar_curr_gen(struct parser_params*,ID);
#define dvar_curr(id) dvar_curr_gen(parser, (id))
static int lvar_defined_gen(struct parser_params*, ID);
#define lvar_defined(id) lvar_defined_gen(parser, (id))
#define RE_OPTION_ONCE (1<<16)
#define RE_OPTION_ENCODING_SHIFT 8
#define RE_OPTION_ENCODING(e) (((e)&0xff)<<RE_OPTION_ENCODING_SHIFT)
#define RE_OPTION_ENCODING_IDX(o) (((o)>>RE_OPTION_ENCODING_SHIFT)&0xff)
#define RE_OPTION_ENCODING_NONE(o) ((o)&RE_OPTION_ARG_ENCODING_NONE)
#define RE_OPTION_MASK 0xff
#define RE_OPTION_ARG_ENCODING_NONE 32
#define NODE_STRTERM NODE_ZARRAY /* nothing to gc */
#define NODE_HEREDOC NODE_ARRAY /* 1, 3 to gc */
#define SIGN_EXTEND(x,n) (((1<<(n)-1)^((x)&~(~0<<(n))))-(1<<(n)-1))
#define nd_func u1.id
#if SIZEOF_SHORT == 2
#define nd_term(node) ((signed short)(node)->u2.id)
#else
#define nd_term(node) SIGN_EXTEND((node)->u2.id, CHAR_BIT*2)
#endif
#define nd_paren(node) (char)((node)->u2.id >> CHAR_BIT*2)
#define nd_nest u3.cnt
/****** Ripper *******/
#ifdef RIPPER
#define RIPPER_VERSION "0.1.0"
#include "eventids1.c"
#include "eventids2.c"
static ID ripper_id_gets;
static VALUE ripper_dispatch0(struct parser_params*,ID);
static VALUE ripper_dispatch1(struct parser_params*,ID,VALUE);
static VALUE ripper_dispatch2(struct parser_params*,ID,VALUE,VALUE);
static VALUE ripper_dispatch3(struct parser_params*,ID,VALUE,VALUE,VALUE);
static VALUE ripper_dispatch4(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE);
static VALUE ripper_dispatch5(struct parser_params*,ID,VALUE,VALUE,VALUE,VALUE,VALUE);
#define dispatch0(n) ripper_dispatch0(parser, TOKEN_PASTE(ripper_id_, n))
#define dispatch1(n,a) ripper_dispatch1(parser, TOKEN_PASTE(ripper_id_, n), (a))
#define dispatch2(n,a,b) ripper_dispatch2(parser, TOKEN_PASTE(ripper_id_, n), (a), (b))
#define dispatch3(n,a,b,c) ripper_dispatch3(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c))
#define dispatch4(n,a,b,c,d) ripper_dispatch4(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d))
#define dispatch5(n,a,b,c,d,e) ripper_dispatch5(parser, TOKEN_PASTE(ripper_id_, n), (a), (b), (c), (d), (e))
#define yyparse ripper_yyparse
#define ripper_intern(s) ID2SYM(rb_intern(s))
static VALUE ripper_id2sym(ID);
#ifdef __GNUC__
#define ripper_id2sym(id) ((id) < 256 && rb_ispunct(id) ? \
ID2SYM(id) : ripper_id2sym(id))
#endif
#define arg_new() dispatch0(args_new)
#define arg_add(l,a) dispatch2(args_add, (l), (a))
#define arg_add_star(l,a) dispatch2(args_add_star, (l), (a))
#define arg_add_block(l,b) dispatch2(args_add_block, (l), (b))
#define arg_add_optblock(l,b) ((b)==Qundef? (l) : dispatch2(args_add_block, (l), (b)))
#define bare_assoc(v) dispatch1(bare_assoc_hash, (v))
#define arg_add_assocs(l,b) arg_add((l), bare_assoc(b))
#define args2mrhs(a) dispatch1(mrhs_new_from_args, (a))
#define mrhs_new() dispatch0(mrhs_new)
#define mrhs_add(l,a) dispatch2(mrhs_add, (l), (a))
#define mrhs_add_star(l,a) dispatch2(mrhs_add_star, (l), (a))
#define mlhs_new() dispatch0(mlhs_new)
#define mlhs_add(l,a) dispatch2(mlhs_add, (l), (a))
#define mlhs_add_star(l,a) dispatch2(mlhs_add_star, (l), (a))
#define params_new(pars, opts, rest, pars2, blk) \
dispatch5(params, (pars), (opts), (rest), (pars2), (blk))
#define blockvar_new(p,v) dispatch2(block_var, (p), (v))
#define blockvar_add_star(l,a) dispatch2(block_var_add_star, (l), (a))
#define blockvar_add_block(l,a) dispatch2(block_var_add_block, (l), (a))
#define method_optarg(m,a) ((a)==Qundef ? (m) : dispatch2(method_add_arg,(m),(a)))
#define method_arg(m,a) dispatch2(method_add_arg,(m),(a))
#define method_add_block(m,b) dispatch2(method_add_block, (m), (b))
#define escape_Qundef(x) ((x)==Qundef ? Qnil : (x))
#define FIXME 0
#endif /* RIPPER */
#ifndef RIPPER
# define ifndef_ripper(x) (x)
#else
# define ifndef_ripper(x)
#endif
#ifndef RIPPER
# define rb_warn0(fmt) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt))
# define rb_warnI(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
# define rb_warnS(fmt,a) rb_compile_warn(ruby_sourcefile, ruby_sourceline, (fmt), (a))
# define rb_warning0(fmt) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt))
# define rb_warningS(fmt,a) rb_compile_warning(ruby_sourcefile, ruby_sourceline, (fmt), (a))
#else
# define rb_warn0(fmt) ripper_warn0(parser, (fmt))
# define rb_warnI(fmt,a) ripper_warnI(parser, (fmt), (a))
# define rb_warnS(fmt,a) ripper_warnS(parser, (fmt), (a))
# define rb_warning0(fmt) ripper_warning0(parser, (fmt))
# define rb_warningS(fmt,a) ripper_warningS(parser, (fmt), (a))
static void ripper_warn0(struct parser_params*, const char*);
static void ripper_warnI(struct parser_params*, const char*, int);
#if 0
static void ripper_warnS(struct parser_params*, const char*, const char*);
#endif
static void ripper_warning0(struct parser_params*, const char*);
static void ripper_warningS(struct parser_params*, const char*, const char*);
#endif
#ifdef RIPPER
static void ripper_compile_error(struct parser_params*, const char *fmt, ...);
# define rb_compile_error ripper_compile_error
# define compile_error ripper_compile_error
# define PARSER_ARG parser,
#else
# define rb_compile_error rb_compile_error_with_enc
# define compile_error parser->nerr++,rb_compile_error_with_enc
# define PARSER_ARG ruby_sourcefile, ruby_sourceline, current_enc,
#endif
/* Older versions of Yacc set YYMAXDEPTH to a very low value by default (150,
for instance). This is too low for Ruby to parse some files, such as
date/format.rb, therefore bump the value up to at least Bison's default. */
#ifdef OLD_YACC
#ifndef YYMAXDEPTH
#define YYMAXDEPTH 10000
#endif
#endif
#ifndef RIPPER
static void token_info_push(struct parser_params*, const char *token);
static void token_info_pop(struct parser_params*, const char *token);
#define token_info_push(token) (RTEST(ruby_verbose) ? token_info_push(parser, (token)) : (void)0)
#define token_info_pop(token) (RTEST(ruby_verbose) ? token_info_pop(parser, (token)) : (void)0)
#else
#define token_info_push(token) /* nothing */
#define token_info_pop(token) /* nothing */
#endif
%}
%pure_parser
%parse-param {struct parser_params *parser}
%union {
VALUE val;
NODE *node;
ID id;
int num;
const struct vtable *vars;
}
/*%%%*/
%token
/*%
%token <val>
%*/
keyword_class
keyword_module
keyword_def
keyword_undef
keyword_begin
keyword_rescue
keyword_ensure
keyword_end
keyword_if
keyword_unless
keyword_then
keyword_elsif
keyword_else
keyword_case
keyword_when
keyword_while
keyword_until
keyword_for
keyword_break
keyword_next
keyword_redo
keyword_retry
keyword_in
keyword_do
keyword_do_cond
keyword_do_block
keyword_do_LAMBDA
keyword_return
keyword_yield
keyword_super
keyword_self
keyword_nil
keyword_true
keyword_false
keyword_and
keyword_or
keyword_not
modifier_if
modifier_unless
modifier_while
modifier_until
modifier_rescue
keyword_alias
keyword_defined
keyword_BEGIN
keyword_END
keyword__LINE__
keyword__FILE__
keyword__ENCODING__
%token <id> tIDENTIFIER tFID tGVAR tIVAR tCONSTANT tCVAR tLABEL
%token <node> tINTEGER tFLOAT tSTRING_CONTENT tCHAR
%token <node> tNTH_REF tBACK_REF
%token <num> tREGEXP_END
%type <node> singleton strings string string1 xstring regexp
%type <node> string_contents xstring_contents regexp_contents string_content
%type <node> words qwords word_list qword_list word
%type <node> literal numeric dsym cpath
%type <node> top_compstmt top_stmts top_stmt
%type <node> bodystmt compstmt stmts stmt expr arg primary command command_call method_call
%type <node> expr_value arg_value primary_value
%type <node> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure
%type <node> args call_args opt_call_args
%type <node> paren_args opt_paren_args
%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
%type <node> command_asgn mrhs superclass block_call block_command
%type <node> f_block_optarg f_block_opt
%type <node> f_arglist f_args f_arg f_arg_item f_optarg f_marg f_marg_list f_margs
%type <node> assoc_list assocs assoc undef_list backref string_dvar for_var
%type <node> block_param opt_block_param block_param_def f_opt
%type <node> bv_decls opt_bv_decl bvar
%type <node> lambda f_larglist lambda_body
%type <node> brace_block cmd_brace_block do_block lhs none fitem
%type <node> mlhs mlhs_head mlhs_basic mlhs_item mlhs_node mlhs_post mlhs_inner
%type <id> fsym variable sym symbol operation operation2 operation3
%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_bad_arg
/*%%%*/
/*%
%type <val> program reswords then do dot_or_colon
%*/
%token tUPLUS /* unary+ */
%token tUMINUS /* unary- */
%token tPOW /* ** */
%token tCMP /* <=> */
%token tEQ /* == */
%token tEQQ /* === */
%token tNEQ /* != */
%token tGEQ /* >= */
%token tLEQ /* <= */
%token tANDOP tOROP /* && and || */
%token tMATCH tNMATCH /* =~ and !~ */
%token tDOT2 tDOT3 /* .. and ... */
%token tAREF tASET /* [] and []= */
%token tLSHFT tRSHFT /* << and >> */
%token tCOLON2 /* :: */
%token tCOLON3 /* :: at EXPR_BEG */
%token <id> tOP_ASGN /* +=, -= etc. */
%token tASSOC /* => */
%token tLPAREN /* ( */
%token tLPAREN_ARG /* ( */
%token tRPAREN /* ) */
%token tLBRACK /* [ */
%token tLBRACE /* { */
%token tLBRACE_ARG /* { */
%token tSTAR /* * */
%token tAMPER /* & */
%token tLAMBDA /* -> */
%token tSYMBEG tSTRING_BEG tXSTRING_BEG tREGEXP_BEG tWORDS_BEG tQWORDS_BEG
%token tSTRING_DBEG tSTRING_DVAR tSTRING_END tLAMBEG
/*
* precedence table
*/
%nonassoc tLOWEST
%nonassoc tLBRACE_ARG
%nonassoc modifier_if modifier_unless modifier_while modifier_until
%left keyword_or keyword_and
%right keyword_not
%nonassoc keyword_defined
%right '=' tOP_ASGN
%left modifier_rescue
%right '?' ':'
%nonassoc tDOT2 tDOT3
%left tOROP
%left tANDOP
%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
%left '>' tGEQ '<' tLEQ
%left '|' '^'
%left '&'
%left tLSHFT tRSHFT
%left '+' '-'
%left '*' '/' '%'
%right tUMINUS_NUM tUMINUS
%right tPOW
%right '!' '~' tUPLUS
%nonassoc idNULL
%nonassoc idRespond_to
%nonassoc idIFUNC
%nonassoc idCFUNC
%nonassoc id_core_set_method_alias
%nonassoc id_core_set_variable_alias
%nonassoc id_core_undef_method
%nonassoc id_core_define_method
%nonassoc id_core_define_singleton_method
%nonassoc id_core_set_postexe
%token tLAST_TOKEN
%%
program : {
lex_state = EXPR_BEG;
/*%%%*/
local_push(compile_for_eval || rb_parse_in_main());
/*%
local_push(0);
%*/
}
top_compstmt
{
/*%%%*/
if ($2 && !compile_for_eval) {
/* last expression should not be void */
if (nd_type($2) != NODE_BLOCK) void_expr($2);
else {
NODE *node = $2;
while (node->nd_next) {
node = node->nd_next;
}
void_expr(node->nd_head);
}
}
ruby_eval_tree = NEW_SCOPE(0, block_append(ruby_eval_tree, $2));
/*%
$$ = $2;
parser->result = dispatch1(program, $$);
%*/
local_pop();
}
;
top_compstmt : top_stmts opt_terms
{
/*%%%*/
void_stmts($1);
fixup_nodes(&deferred_nodes);
/*%
%*/
$$ = $1;
}
;
top_stmts : none
{
/*%%%*/
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch2(stmts_add, dispatch0(stmts_new),
dispatch0(void_stmt));
%*/
}
| top_stmt
{
/*%%%*/
$$ = newline_node($1);
/*%
$$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
%*/
}
| top_stmts terms top_stmt
{
/*%%%*/
$$ = block_append($1, newline_node($3));
/*%
$$ = dispatch2(stmts_add, $1, $3);
%*/
}
| error top_stmt
{
$$ = remove_begin($2);
}
;
top_stmt : stmt
| keyword_BEGIN
{
if (in_def || in_single) {
yyerror("BEGIN in method");
}
/*%%%*/
/* local_push(0); */
/*%
%*/
}
'{' top_compstmt '}'
{
/*%%%*/
ruby_eval_tree_begin = block_append(ruby_eval_tree_begin,
$4);
/* NEW_PREEXE($4)); */
/* local_pop(); */
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(BEGIN, $4);
%*/
}
;
bodystmt : compstmt
opt_rescue
opt_else
opt_ensure
{
/*%%%*/
$$ = $1;
if ($2) {
$$ = NEW_RESCUE($1, $2, $3);
}
else if ($3) {
rb_warn0("else without rescue is useless");
$$ = block_append($$, $3);
}
if ($4) {
if ($$) {
$$ = NEW_ENSURE($$, $4);
}
else {
$$ = block_append($4, NEW_NIL());
}
}
fixpos($$, $1);
/*%
$$ = dispatch4(bodystmt,
escape_Qundef($1),
escape_Qundef($2),
escape_Qundef($3),
escape_Qundef($4));
%*/
}
;
compstmt : stmts opt_terms
{
/*%%%*/
void_stmts($1);
fixup_nodes(&deferred_nodes);
/*%
%*/
$$ = $1;
}
;
stmts : none
{
/*%%%*/
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch2(stmts_add, dispatch0(stmts_new),
dispatch0(void_stmt));
%*/
}
| stmt
{
/*%%%*/
$$ = newline_node($1);
/*%
$$ = dispatch2(stmts_add, dispatch0(stmts_new), $1);
%*/
}
| stmts terms stmt
{
/*%%%*/
$$ = block_append($1, newline_node($3));
/*%
$$ = dispatch2(stmts_add, $1, $3);
%*/
}
| error stmt
{
$$ = remove_begin($2);
}
;
stmt : keyword_alias fitem {lex_state = EXPR_FNAME;} fitem
{
/*%%%*/
$$ = NEW_ALIAS($2, $4);
/*%
$$ = dispatch2(alias, $2, $4);
%*/
}
| keyword_alias tGVAR tGVAR
{
/*%%%*/
$$ = NEW_VALIAS($2, $3);
/*%
$$ = dispatch2(var_alias, $2, $3);
%*/
}
| keyword_alias tGVAR tBACK_REF
{
/*%%%*/
char buf[2];
buf[0] = '$';
buf[1] = (char)$3->nd_nth;
$$ = NEW_VALIAS($2, rb_intern2(buf, 2));
/*%
$$ = dispatch2(var_alias, $2, $3);
%*/
}
| keyword_alias tGVAR tNTH_REF
{
/*%%%*/
yyerror("can't make alias for the number variables");
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch2(var_alias, $2, $3);
$$ = dispatch1(alias_error, $$);
%*/
}
| keyword_undef undef_list
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(undef, $2);
%*/
}
| stmt modifier_if expr_value
{
/*%%%*/
$$ = NEW_IF(cond($3), remove_begin($1), 0);
fixpos($$, $3);
/*%
$$ = dispatch2(if_mod, $3, $1);
%*/
}
| stmt modifier_unless expr_value
{
/*%%%*/
$$ = NEW_UNLESS(cond($3), remove_begin($1), 0);
fixpos($$, $3);
/*%
$$ = dispatch2(unless_mod, $3, $1);
%*/
}
| stmt modifier_while expr_value
{
/*%%%*/
if ($1 && nd_type($1) == NODE_BEGIN) {
$$ = NEW_WHILE(cond($3), $1->nd_body, 0);
}
else {
$$ = NEW_WHILE(cond($3), $1, 1);
}
/*%
$$ = dispatch2(while_mod, $3, $1);
%*/
}
| stmt modifier_until expr_value
{
/*%%%*/
if ($1 && nd_type($1) == NODE_BEGIN) {
$$ = NEW_UNTIL(cond($3), $1->nd_body, 0);
}
else {
$$ = NEW_UNTIL(cond($3), $1, 1);
}
/*%
$$ = dispatch2(until_mod, $3, $1);
%*/
}
| stmt modifier_rescue stmt
{
/*%%%*/
NODE *resq = NEW_RESBODY(0, remove_begin($3), 0);
$$ = NEW_RESCUE(remove_begin($1), resq, 0);
/*%
$$ = dispatch2(rescue_mod, $3, $1);
%*/
}
| keyword_END '{' compstmt '}'
{
if (in_def || in_single) {
rb_warn0("END in method; use at_exit");
}
/*%%%*/
$$ = NEW_POSTEXE(NEW_NODE(
NODE_SCOPE, 0 /* tbl */, $3 /* body */, 0 /* args */));
/*%
$$ = dispatch1(END, $3);
%*/
}
| command_asgn
| mlhs '=' command_call
{
/*%%%*/
value_expr($3);
$1->nd_value = $3;
$$ = $1;
/*%
$$ = dispatch2(massign, $1, $3);
%*/
}
| var_lhs tOP_ASGN command_call
{
/*%%%*/
value_expr($3);
if ($1) {
ID vid = $1->nd_vid;
if ($2 == tOROP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
$$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
}
}
else {
$$ = NEW_BEGIN(0);
}
/*%
$$ = dispatch3(opassign, $1, $2, $3);
%*/
}
| primary_value '[' opt_call_args rbracket tOP_ASGN command_call
{
/*%%%*/
NODE *args;
value_expr($6);
if (!$3) $3 = NEW_ZARRAY();
args = arg_concat($3, $6);
if ($5 == tOROP) {
$5 = 0;
}
else if ($5 == tANDOP) {
$5 = 1;
}
$$ = NEW_OP_ASGN1($1, $5, args);
fixpos($$, $1);
/*%
$$ = dispatch2(aref_field, $1, escape_Qundef($3));
$$ = dispatch3(opassign, $$, $5, $6);
%*/
}
| primary_value '.' tIDENTIFIER tOP_ASGN command_call
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
$$ = dispatch3(opassign, $$, $4, $5);
%*/
}
| primary_value '.' tCONSTANT tOP_ASGN command_call
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
$$ = dispatch3(opassign, $$, $4, $5);
%*/
}
| primary_value tCOLON2 tCONSTANT tOP_ASGN command_call
{
/*%%%*/
yyerror("constant re-assignment");
$$ = 0;
/*%
$$ = dispatch2(const_path_field, $1, $3);
$$ = dispatch3(opassign, $$, $4, $5);
$$ = dispatch1(assign_error, $$);
%*/
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_call
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$$ = dispatch3(field, $1, ripper_intern("::"), $3);
$$ = dispatch3(opassign, $$, $4, $5);
%*/
}
| backref tOP_ASGN command_call
{
/*%%%*/
rb_backref_error($1);
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch2(assign, dispatch1(var_field, $1), $3);
$$ = dispatch1(assign_error, $$);
%*/
}
| lhs '=' mrhs
{
/*%%%*/
value_expr($3);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, $3);
%*/
}
| mlhs '=' arg_value
{
/*%%%*/
$1->nd_value = $3;
$$ = $1;
/*%
$$ = dispatch2(massign, $1, $3);
%*/
}
| mlhs '=' mrhs
{
/*%%%*/
$1->nd_value = $3;
$$ = $1;
/*%
$$ = dispatch2(massign, $1, $3);
%*/
}
| expr
;
command_asgn : lhs '=' command_call
{
/*%%%*/
value_expr($3);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, $3);
%*/
}
| lhs '=' command_asgn
{
/*%%%*/
value_expr($3);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, $3);
%*/
}
;
expr : command_call
| expr keyword_and expr
{
/*%%%*/
$$ = logop(NODE_AND, $1, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("and"), $3);
%*/
}
| expr keyword_or expr
{
/*%%%*/
$$ = logop(NODE_OR, $1, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("or"), $3);
%*/
}
| keyword_not opt_nl expr
{
/*%%%*/
$$ = call_uni_op(cond($3), '!');
/*%
$$ = dispatch2(unary, ripper_intern("not"), $3);
%*/
}
| '!' command_call
{
/*%%%*/
$$ = call_uni_op(cond($2), '!');
/*%
$$ = dispatch2(unary, ripper_id2sym('!'), $2);
%*/
}
| arg
;
expr_value : expr
{
/*%%%*/
value_expr($1);
$$ = $1;
if (!$$) $$ = NEW_NIL();
/*%
$$ = $1;
%*/
}
;
command_call : command
| block_command
;
block_command : block_call
| block_call '.' operation2 command_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
/*%
$$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
$$ = method_arg($$, $4);
%*/
}
| block_call tCOLON2 operation2 command_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
/*%
$$ = dispatch3(call, $1, ripper_intern("::"), $3);
$$ = method_arg($$, $4);
%*/
}
;
cmd_brace_block : tLBRACE_ARG
{
$<vars>1 = dyna_push();
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
opt_block_param
compstmt
'}'
{
/*%%%*/
$$ = NEW_ITER($3,$4);
nd_set_line($$, $<num>2);
/*%
$$ = dispatch2(brace_block, escape_Qundef($3), $4);
%*/
dyna_pop($<vars>1);
}
;
command : operation command_args %prec tLOWEST
{
/*%%%*/
$$ = NEW_FCALL($1, $2);
fixpos($$, $2);
/*%
$$ = dispatch2(command, $1, $2);
%*/
}
| operation command_args cmd_brace_block
{
/*%%%*/
block_dup_check($2,$3);
$3->nd_iter = NEW_FCALL($1, $2);
$$ = $3;
fixpos($$, $2);
/*%
$$ = dispatch2(command, $1, $2);
$$ = method_add_block($$, $3);
%*/
}
| primary_value '.' operation2 command_args %prec tLOWEST
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
fixpos($$, $1);
/*%
$$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
%*/
}
| primary_value '.' operation2 command_args cmd_brace_block
{
/*%%%*/
block_dup_check($4,$5);
$5->nd_iter = NEW_CALL($1, $3, $4);
$$ = $5;
fixpos($$, $1);
/*%
$$ = dispatch4(command_call, $1, ripper_id2sym('.'), $3, $4);
$$ = method_add_block($$, $5);
%*/
}
| primary_value tCOLON2 operation2 command_args %prec tLOWEST
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
fixpos($$, $1);
/*%
$$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4);
%*/
}
| primary_value tCOLON2 operation2 command_args cmd_brace_block
{
/*%%%*/
block_dup_check($4,$5);
$5->nd_iter = NEW_CALL($1, $3, $4);
$$ = $5;
fixpos($$, $1);
/*%
$$ = dispatch4(command_call, $1, ripper_intern("::"), $3, $4);
$$ = method_add_block($$, $5);
%*/
}
| keyword_super command_args
{
/*%%%*/
$$ = NEW_SUPER($2);
fixpos($$, $2);
/*%
$$ = dispatch1(super, $2);
%*/
}
| keyword_yield command_args
{
/*%%%*/
$$ = new_yield($2);
fixpos($$, $2);
/*%
$$ = dispatch1(yield, $2);
%*/
}
| keyword_return call_args
{
/*%%%*/
$$ = NEW_RETURN(ret_args($2));
/*%
$$ = dispatch1(return, $2);
%*/
}
| keyword_break call_args
{
/*%%%*/
$$ = NEW_BREAK(ret_args($2));
/*%
$$ = dispatch1(break, $2);
%*/
}
| keyword_next call_args
{
/*%%%*/
$$ = NEW_NEXT(ret_args($2));
/*%
$$ = dispatch1(next, $2);
%*/
}
;
mlhs : mlhs_basic
| tLPAREN mlhs_inner rparen
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(mlhs_paren, $2);
%*/
}
;
mlhs_inner : mlhs_basic
| tLPAREN mlhs_inner rparen
{
/*%%%*/
$$ = NEW_MASGN(NEW_LIST($2), 0);
/*%
$$ = dispatch1(mlhs_paren, $2);
%*/
}
;
mlhs_basic : mlhs_head
{
/*%%%*/
$$ = NEW_MASGN($1, 0);
/*%
$$ = $1;
%*/
}
| mlhs_head mlhs_item
{
/*%%%*/
$$ = NEW_MASGN(list_append($1,$2), 0);
/*%
$$ = mlhs_add($1, $2);
%*/
}
| mlhs_head tSTAR mlhs_node
{
/*%%%*/
$$ = NEW_MASGN($1, $3);
/*%
$$ = mlhs_add_star($1, $3);
%*/
}
| mlhs_head tSTAR mlhs_node ',' mlhs_post
{
/*%%%*/
$$ = NEW_MASGN($1, NEW_POSTARG($3,$5));
/*%
$1 = mlhs_add_star($1, $3);
$$ = mlhs_add($1, $5);
%*/
}
| mlhs_head tSTAR
{
/*%%%*/
$$ = NEW_MASGN($1, -1);
/*%
$$ = mlhs_add_star($1, Qnil);
%*/
}
| mlhs_head tSTAR ',' mlhs_post
{
/*%%%*/
$$ = NEW_MASGN($1, NEW_POSTARG(-1, $4));
/*%
$1 = mlhs_add_star($1, Qnil);
$$ = mlhs_add($1, $4);
%*/
}
| tSTAR mlhs_node
{
/*%%%*/
$$ = NEW_MASGN(0, $2);
/*%
$$ = mlhs_add_star(mlhs_new(), $2);
%*/
}
| tSTAR mlhs_node ',' mlhs_post
{
/*%%%*/
$$ = NEW_MASGN(0, NEW_POSTARG($2,$4));
/*%
$2 = mlhs_add_star(mlhs_new(), $2);
$$ = mlhs_add($2, $4);
%*/
}
| tSTAR
{
/*%%%*/
$$ = NEW_MASGN(0, -1);
/*%
$$ = mlhs_add_star(mlhs_new(), Qnil);
%*/
}
| tSTAR ',' mlhs_post
{
/*%%%*/
$$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
/*%
$$ = mlhs_add_star(mlhs_new(), Qnil);
$$ = mlhs_add($$, $3);
%*/
}
;
mlhs_item : mlhs_node
| tLPAREN mlhs_inner rparen
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(mlhs_paren, $2);
%*/
}
;
mlhs_head : mlhs_item ','
{
/*%%%*/
$$ = NEW_LIST($1);
/*%
$$ = mlhs_add(mlhs_new(), $1);
%*/
}
| mlhs_head mlhs_item ','
{
/*%%%*/
$$ = list_append($1, $2);
/*%
$$ = mlhs_add($1, $2);
%*/
}
;
mlhs_post : mlhs_item
{
/*%%%*/
$$ = NEW_LIST($1);
/*%
$$ = mlhs_add(mlhs_new(), $1);
%*/
}
| mlhs_post ',' mlhs_item
{
/*%%%*/
$$ = list_append($1, $3);
/*%
$$ = mlhs_add($1, $3);
%*/
}
;
mlhs_node : variable
{
$$ = assignable($1, 0);
}
| primary_value '[' opt_call_args rbracket
{
/*%%%*/
$$ = aryset($1, $3);
/*%
$$ = dispatch2(aref_field, $1, escape_Qundef($3));
%*/
}
| primary_value '.' tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
%*/
}
| primary_value tCOLON2 tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch2(const_path_field, $1, $3);
%*/
}
| primary_value '.' tCONSTANT
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
%*/
}
| primary_value tCOLON2 tCONSTANT
{
/*%%%*/
if (in_def || in_single)
yyerror("dynamic constant assignment");
$$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
/*%
if (in_def || in_single)
yyerror("dynamic constant assignment");
$$ = dispatch2(const_path_field, $1, $3);
%*/
}
| tCOLON3 tCONSTANT
{
/*%%%*/
if (in_def || in_single)
yyerror("dynamic constant assignment");
$$ = NEW_CDECL(0, 0, NEW_COLON3($2));
/*%
$$ = dispatch1(top_const_field, $2);
%*/
}
| backref
{
/*%%%*/
rb_backref_error($1);
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(var_field, $1);
$$ = dispatch1(assign_error, $$);
%*/
}
;
lhs : variable
{
$$ = assignable($1, 0);
/*%%%*/
if (!$$) $$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(var_field, $$);
%*/
}
| primary_value '[' opt_call_args rbracket
{
/*%%%*/
$$ = aryset($1, $3);
/*%
$$ = dispatch2(aref_field, $1, escape_Qundef($3));
%*/
}
| primary_value '.' tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
%*/
}
| primary_value tCOLON2 tIDENTIFIER
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch3(field, $1, ripper_intern("::"), $3);
%*/
}
| primary_value '.' tCONSTANT
{
/*%%%*/
$$ = attrset($1, $3);
/*%
$$ = dispatch3(field, $1, ripper_id2sym('.'), $3);
%*/
}
| primary_value tCOLON2 tCONSTANT
{
/*%%%*/
if (in_def || in_single)
yyerror("dynamic constant assignment");
$$ = NEW_CDECL(0, 0, NEW_COLON2($1, $3));
/*%
$$ = dispatch2(const_path_field, $1, $3);
if (in_def || in_single) {
$$ = dispatch1(assign_error, $$);
}
%*/
}
| tCOLON3 tCONSTANT
{
/*%%%*/
if (in_def || in_single)
yyerror("dynamic constant assignment");
$$ = NEW_CDECL(0, 0, NEW_COLON3($2));
/*%
$$ = dispatch1(top_const_field, $2);
if (in_def || in_single) {
$$ = dispatch1(assign_error, $$);
}
%*/
}
| backref
{
/*%%%*/
rb_backref_error($1);
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(assign_error, $1);
%*/
}
;
cname : tIDENTIFIER
{
/*%%%*/
yyerror("class/module name must be CONSTANT");
/*%
$$ = dispatch1(class_name_error, $1);
%*/
}
| tCONSTANT
;
cpath : tCOLON3 cname
{
/*%%%*/
$$ = NEW_COLON3($2);
/*%
$$ = dispatch1(top_const_ref, $2);
%*/
}
| cname
{
/*%%%*/
$$ = NEW_COLON2(0, $$);
/*%
$$ = dispatch1(const_ref, $1);
%*/
}
| primary_value tCOLON2 cname
{
/*%%%*/
$$ = NEW_COLON2($1, $3);
/*%
$$ = dispatch2(const_path_ref, $1, $3);
%*/
}
;
fname : tIDENTIFIER
| tCONSTANT
| tFID
| op
{
lex_state = EXPR_ENDFN;
$$ = $1;
}
| reswords
{
lex_state = EXPR_ENDFN;
/*%%%*/
$$ = $<id>1;
/*%
$$ = $1;
%*/
}
;
fsym : fname
| symbol
;
fitem : fsym
{
/*%%%*/
$$ = NEW_LIT(ID2SYM($1));
/*%
$$ = dispatch1(symbol_literal, $1);
%*/
}
| dsym
;
undef_list : fitem
{
/*%%%*/
$$ = NEW_UNDEF($1);
/*%
$$ = rb_ary_new3(1, $1);
%*/
}
| undef_list ',' {lex_state = EXPR_FNAME;} fitem
{
/*%%%*/
$$ = block_append($1, NEW_UNDEF($4));
/*%
rb_ary_push($1, $4);
%*/
}
;
op : '|' { ifndef_ripper($$ = '|'); }
| '^' { ifndef_ripper($$ = '^'); }
| '&' { ifndef_ripper($$ = '&'); }
| tCMP { ifndef_ripper($$ = tCMP); }
| tEQ { ifndef_ripper($$ = tEQ); }
| tEQQ { ifndef_ripper($$ = tEQQ); }
| tMATCH { ifndef_ripper($$ = tMATCH); }
| tNMATCH { ifndef_ripper($$ = tNMATCH); }
| '>' { ifndef_ripper($$ = '>'); }
| tGEQ { ifndef_ripper($$ = tGEQ); }
| '<' { ifndef_ripper($$ = '<'); }
| tLEQ { ifndef_ripper($$ = tLEQ); }
| tNEQ { ifndef_ripper($$ = tNEQ); }
| tLSHFT { ifndef_ripper($$ = tLSHFT); }
| tRSHFT { ifndef_ripper($$ = tRSHFT); }
| '+' { ifndef_ripper($$ = '+'); }
| '-' { ifndef_ripper($$ = '-'); }
| '*' { ifndef_ripper($$ = '*'); }
| tSTAR { ifndef_ripper($$ = '*'); }
| '/' { ifndef_ripper($$ = '/'); }
| '%' { ifndef_ripper($$ = '%'); }
| tPOW { ifndef_ripper($$ = tPOW); }
| '!' { ifndef_ripper($$ = '!'); }
| '~' { ifndef_ripper($$ = '~'); }
| tUPLUS { ifndef_ripper($$ = tUPLUS); }
| tUMINUS { ifndef_ripper($$ = tUMINUS); }
| tAREF { ifndef_ripper($$ = tAREF); }
| tASET { ifndef_ripper($$ = tASET); }
| '`' { ifndef_ripper($$ = '`'); }
;
reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
| keyword_BEGIN | keyword_END
| keyword_alias | keyword_and | keyword_begin
| keyword_break | keyword_case | keyword_class | keyword_def
| keyword_defined | keyword_do | keyword_else | keyword_elsif
| keyword_end | keyword_ensure | keyword_false
| keyword_for | keyword_in | keyword_module | keyword_next
| keyword_nil | keyword_not | keyword_or | keyword_redo
| keyword_rescue | keyword_retry | keyword_return | keyword_self
| keyword_super | keyword_then | keyword_true | keyword_undef
| keyword_when | keyword_yield | keyword_if | keyword_unless
| keyword_while | keyword_until
;
arg : lhs '=' arg
{
/*%%%*/
value_expr($3);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, $3);
%*/
}
| lhs '=' arg modifier_rescue arg
{
/*%%%*/
value_expr($3);
$3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, dispatch2(rescue_mod, $3, $5));
%*/
}
| var_lhs tOP_ASGN arg
{
/*%%%*/
value_expr($3);
if ($1) {
ID vid = $1->nd_vid;
if ($2 == tOROP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
$$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
}
}
else {
$$ = NEW_BEGIN(0);
}
/*%
$$ = dispatch3(opassign, $1, $2, $3);
%*/
}
| var_lhs tOP_ASGN arg modifier_rescue arg
{
/*%%%*/
value_expr($3);
$3 = NEW_RESCUE($3, NEW_RESBODY(0,$5,0), 0);
if ($1) {
ID vid = $1->nd_vid;
if ($2 == tOROP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
$1->nd_value = $3;
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
$$->nd_value = NEW_CALL(gettable(vid), $2, NEW_LIST($3));
}
}
else {
$$ = NEW_BEGIN(0);
}
/*%
$3 = dispatch2(rescue_mod, $3, $5);
$$ = dispatch3(opassign, $1, $2, $3);
%*/
}
| primary_value '[' opt_call_args rbracket tOP_ASGN arg
{
/*%%%*/
NODE *args;
value_expr($6);
if (!$3) $3 = NEW_ZARRAY();
if (nd_type($3) == NODE_BLOCK_PASS) {
args = NEW_ARGSCAT($3, $6);
}
else {
args = arg_concat($3, $6);
}
if ($5 == tOROP) {
$5 = 0;
}
else if ($5 == tANDOP) {
$5 = 1;
}
$$ = NEW_OP_ASGN1($1, $5, args);
fixpos($$, $1);
/*%
$1 = dispatch2(aref_field, $1, escape_Qundef($3));
$$ = dispatch3(opassign, $1, $5, $6);
%*/
}
| primary_value '.' tIDENTIFIER tOP_ASGN arg
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$1 = dispatch3(field, $1, ripper_id2sym('.'), $3);
$$ = dispatch3(opassign, $1, $4, $5);
%*/
}
| primary_value '.' tCONSTANT tOP_ASGN arg
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$1 = dispatch3(field, $1, ripper_id2sym('.'), $3);
$$ = dispatch3(opassign, $1, $4, $5);
%*/
}
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg
{
/*%%%*/
value_expr($5);
if ($4 == tOROP) {
$4 = 0;
}
else if ($4 == tANDOP) {
$4 = 1;
}
$$ = NEW_OP_ASGN2($1, $3, $4, $5);
fixpos($$, $1);
/*%
$1 = dispatch3(field, $1, ripper_intern("::"), $3);
$$ = dispatch3(opassign, $1, $4, $5);
%*/
}
| primary_value tCOLON2 tCONSTANT tOP_ASGN arg
{
/*%%%*/
yyerror("constant re-assignment");
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch2(const_path_field, $1, $3);
$$ = dispatch3(opassign, $$, $4, $5);
$$ = dispatch1(assign_error, $$);
%*/
}
| tCOLON3 tCONSTANT tOP_ASGN arg
{
/*%%%*/
yyerror("constant re-assignment");
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(top_const_field, $2);
$$ = dispatch3(opassign, $$, $3, $4);
$$ = dispatch1(assign_error, $$);
%*/
}
| backref tOP_ASGN arg
{
/*%%%*/
rb_backref_error($1);
$$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(var_field, $1);
$$ = dispatch3(opassign, $$, $2, $3);
$$ = dispatch1(assign_error, $$);
%*/
}
| arg tDOT2 arg
{
/*%%%*/
value_expr($1);
value_expr($3);
$$ = NEW_DOT2($1, $3);
if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) &&
nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) {
deferred_nodes = list_append(deferred_nodes, $$);
}
/*%
$$ = dispatch2(dot2, $1, $3);
%*/
}
| arg tDOT3 arg
{
/*%%%*/
value_expr($1);
value_expr($3);
$$ = NEW_DOT3($1, $3);
if (nd_type($1) == NODE_LIT && FIXNUM_P($1->nd_lit) &&
nd_type($3) == NODE_LIT && FIXNUM_P($3->nd_lit)) {
deferred_nodes = list_append(deferred_nodes, $$);
}
/*%
$$ = dispatch2(dot3, $1, $3);
%*/
}
| arg '+' arg
{
/*%%%*/
$$ = call_bin_op($1, '+', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('+'), $3);
%*/
}
| arg '-' arg
{
/*%%%*/
$$ = call_bin_op($1, '-', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('-'), $3);
%*/
}
| arg '*' arg
{
/*%%%*/
$$ = call_bin_op($1, '*', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('*'), $3);
%*/
}
| arg '/' arg
{
/*%%%*/
$$ = call_bin_op($1, '/', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('/'), $3);
%*/
}
| arg '%' arg
{
/*%%%*/
$$ = call_bin_op($1, '%', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('%'), $3);
%*/
}
| arg tPOW arg
{
/*%%%*/
$$ = call_bin_op($1, tPOW, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("**"), $3);
%*/
}
| tUMINUS_NUM tINTEGER tPOW arg
{
/*%%%*/
$$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
/*%
$$ = dispatch3(binary, $2, ripper_intern("**"), $4);
$$ = dispatch2(unary, ripper_intern("-@"), $$);
%*/
}
| tUMINUS_NUM tFLOAT tPOW arg
{
/*%%%*/
$$ = NEW_CALL(call_bin_op($2, tPOW, $4), tUMINUS, 0);
/*%
$$ = dispatch3(binary, $2, ripper_intern("**"), $4);
$$ = dispatch2(unary, ripper_intern("-@"), $$);
%*/
}
| tUPLUS arg
{
/*%%%*/
$$ = call_uni_op($2, tUPLUS);
/*%
$$ = dispatch2(unary, ripper_intern("+@"), $2);
%*/
}
| tUMINUS arg
{
/*%%%*/
$$ = call_uni_op($2, tUMINUS);
/*%
$$ = dispatch2(unary, ripper_intern("-@"), $2);
%*/
}
| arg '|' arg
{
/*%%%*/
$$ = call_bin_op($1, '|', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('|'), $3);
%*/
}
| arg '^' arg
{
/*%%%*/
$$ = call_bin_op($1, '^', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('^'), $3);
%*/
}
| arg '&' arg
{
/*%%%*/
$$ = call_bin_op($1, '&', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('&'), $3);
%*/
}
| arg tCMP arg
{
/*%%%*/
$$ = call_bin_op($1, tCMP, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("<=>"), $3);
%*/
}
| arg '>' arg
{
/*%%%*/
$$ = call_bin_op($1, '>', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('>'), $3);
%*/
}
| arg tGEQ arg
{
/*%%%*/
$$ = call_bin_op($1, tGEQ, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern(">="), $3);
%*/
}
| arg '<' arg
{
/*%%%*/
$$ = call_bin_op($1, '<', $3);
/*%
$$ = dispatch3(binary, $1, ID2SYM('<'), $3);
%*/
}
| arg tLEQ arg
{
/*%%%*/
$$ = call_bin_op($1, tLEQ, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("<="), $3);
%*/
}
| arg tEQ arg
{
/*%%%*/
$$ = call_bin_op($1, tEQ, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("=="), $3);
%*/
}
| arg tEQQ arg
{
/*%%%*/
$$ = call_bin_op($1, tEQQ, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("==="), $3);
%*/
}
| arg tNEQ arg
{
/*%%%*/
$$ = call_bin_op($1, tNEQ, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("!="), $3);
%*/
}
| arg tMATCH arg
{
/*%%%*/
$$ = match_op($1, $3);
if (nd_type($1) == NODE_LIT && TYPE($1->nd_lit) == T_REGEXP) {
$$ = reg_named_capture_assign($1->nd_lit, $$);
}
/*%
$$ = dispatch3(binary, $1, ripper_intern("=~"), $3);
%*/
}
| arg tNMATCH arg
{
/*%%%*/
$$ = call_bin_op($1, tNMATCH, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("!~"), $3);
%*/
}
| '!' arg
{
/*%%%*/
$$ = call_uni_op(cond($2), '!');
/*%
$$ = dispatch2(unary, ID2SYM('!'), $2);
%*/
}
| '~' arg
{
/*%%%*/
$$ = call_uni_op($2, '~');
/*%
$$ = dispatch2(unary, ID2SYM('~'), $2);
%*/
}
| arg tLSHFT arg
{
/*%%%*/
$$ = call_bin_op($1, tLSHFT, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("<<"), $3);
%*/
}
| arg tRSHFT arg
{
/*%%%*/
$$ = call_bin_op($1, tRSHFT, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern(">>"), $3);
%*/
}
| arg tANDOP arg
{
/*%%%*/
$$ = logop(NODE_AND, $1, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("&&"), $3);
%*/
}
| arg tOROP arg
{
/*%%%*/
$$ = logop(NODE_OR, $1, $3);
/*%
$$ = dispatch3(binary, $1, ripper_intern("||"), $3);
%*/
}
| keyword_defined opt_nl {in_defined = 1;} arg
{
/*%%%*/
in_defined = 0;
$$ = NEW_DEFINED($4);
/*%
in_defined = 0;
$$ = dispatch1(defined, $4);
%*/
}
| arg '?' arg opt_nl ':' arg
{
/*%%%*/
value_expr($1);
$$ = NEW_IF(cond($1), $3, $6);
fixpos($$, $1);
/*%
$$ = dispatch3(ifop, $1, $3, $6);
%*/
}
| primary
{
$$ = $1;
}
;
arg_value : arg
{
/*%%%*/
value_expr($1);
$$ = $1;
if (!$$) $$ = NEW_NIL();
/*%
$$ = $1;
%*/
}
;
aref_args : none
| args trailer
{
$$ = $1;
}
| args ',' assocs trailer
{
/*%%%*/
$$ = arg_append($1, NEW_HASH($3));
/*%
$$ = arg_add_assocs($1, $3);
%*/
}
| assocs trailer
{
/*%%%*/
$$ = NEW_LIST(NEW_HASH($1));
/*%
$$ = arg_add_assocs(arg_new(), $1);
%*/
}
;
paren_args : '(' opt_call_args rparen
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(arg_paren, escape_Qundef($2));
%*/
}
;
opt_paren_args : none
| paren_args
;
opt_call_args : none
| call_args
;
call_args : command
{
/*%%%*/
value_expr($1);
$$ = NEW_LIST($1);
/*%
$$ = arg_add(arg_new(), $1);
%*/
}
| args opt_block_arg
{
/*%%%*/
$$ = arg_blk_pass($1, $2);
/*%
$$ = arg_add_optblock($1, $2);
%*/
}
| assocs opt_block_arg
{
/*%%%*/
$$ = NEW_LIST(NEW_HASH($1));
$$ = arg_blk_pass($$, $2);
/*%
$$ = arg_add_assocs(arg_new(), $1);
$$ = arg_add_optblock($$, $2);
%*/
}
| args ',' assocs opt_block_arg
{
/*%%%*/
$$ = arg_append($1, NEW_HASH($3));
$$ = arg_blk_pass($$, $4);
/*%
$$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
%*/
}
| block_arg
/*%c%*/
/*%c
{
$$ = arg_add_block(arg_new(), $1);
}
%*/
;
command_args : {
$<val>$ = cmdarg_stack;
CMDARG_PUSH(1);
}
call_args
{
/* CMDARG_POP() */
cmdarg_stack = $<val>1;
$$ = $2;
}
;
block_arg : tAMPER arg_value
{
/*%%%*/
$$ = NEW_BLOCK_PASS($2);
/*%
$$ = $2;
%*/
}
;
opt_block_arg : ',' block_arg
{
$$ = $2;
}
| ','
{
$$ = 0;
}
| none
{
$$ = 0;
}
;
args : arg_value
{
/*%%%*/
$$ = NEW_LIST($1);
/*%
$$ = arg_add(arg_new(), $1);
%*/
}
| tSTAR arg_value
{
/*%%%*/
$$ = NEW_SPLAT($2);
/*%
$$ = arg_add_star(arg_new(), $2);
%*/
}
| args ',' arg_value
{
/*%%%*/
NODE *n1;
if ((n1 = splat_array($1)) != 0) {
$$ = list_append(n1, $3);
}
else {
$$ = arg_append($1, $3);
}
/*%
$$ = arg_add($1, $3);
%*/
}
| args ',' tSTAR arg_value
{
/*%%%*/
NODE *n1;
if ((nd_type($4) == NODE_ARRAY) && (n1 = splat_array($1)) != 0) {
$$ = list_concat(n1, $4);
}
else {
$$ = arg_concat($1, $4);
}
/*%
$$ = arg_add_star($1, $4);
%*/
}
;
mrhs : args ',' arg_value
{
/*%%%*/
NODE *n1;
if ((n1 = splat_array($1)) != 0) {
$$ = list_append(n1, $3);
}
else {
$$ = arg_append($1, $3);
}
/*%
$$ = mrhs_add(args2mrhs($1), $3);
%*/
}
| args ',' tSTAR arg_value
{
/*%%%*/
NODE *n1;
if (nd_type($4) == NODE_ARRAY &&
(n1 = splat_array($1)) != 0) {
$$ = list_concat(n1, $4);
}
else {
$$ = arg_concat($1, $4);
}
/*%
$$ = mrhs_add_star(args2mrhs($1), $4);
%*/
}
| tSTAR arg_value
{
/*%%%*/
$$ = NEW_SPLAT($2);
/*%
$$ = mrhs_add_star(mrhs_new(), $2);
%*/
}
;
primary : literal
| strings
| xstring
| regexp
| words
| qwords
| var_ref
| backref
| tFID
{
/*%%%*/
$$ = NEW_FCALL($1, 0);
/*%
$$ = method_arg(dispatch1(fcall, $1), arg_new());
%*/
}
| k_begin
{
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
k_end
{
/*%%%*/
if ($3 == NULL) {
$$ = NEW_NIL();
}
else {
if (nd_type($3) == NODE_RESCUE ||
nd_type($3) == NODE_ENSURE)
nd_set_line($3, $<num>2);
$$ = NEW_BEGIN($3);
}
nd_set_line($$, $<num>2);
/*%
$$ = dispatch1(begin, $3);
%*/
}
| tLPAREN_ARG expr {lex_state = EXPR_ENDARG;} rparen
{
rb_warning0("(...) interpreted as grouped expression");
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(paren, $2);
%*/
}
| tLPAREN compstmt ')'
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(paren, $2);
%*/
}
| primary_value tCOLON2 tCONSTANT
{
/*%%%*/
$$ = NEW_COLON2($1, $3);
/*%
$$ = dispatch2(const_path_ref, $1, $3);
%*/
}
| tCOLON3 tCONSTANT
{
/*%%%*/
$$ = NEW_COLON3($2);
/*%
$$ = dispatch1(top_const_ref, $2);
%*/
}
| tLBRACK aref_args ']'
{
/*%%%*/
if ($2 == 0) {
$$ = NEW_ZARRAY(); /* zero length array*/
}
else {
$$ = $2;
}
/*%
$$ = dispatch1(array, escape_Qundef($2));
%*/
}
| tLBRACE assoc_list '}'
{
/*%%%*/
$$ = NEW_HASH($2);
/*%
$$ = dispatch1(hash, escape_Qundef($2));
%*/
}
| keyword_return
{
/*%%%*/
$$ = NEW_RETURN(0);
/*%
$$ = dispatch0(return0);
%*/
}
| keyword_yield '(' call_args rparen
{
/*%%%*/
$$ = new_yield($3);
/*%
$$ = dispatch1(yield, dispatch1(paren, $3));
%*/
}
| keyword_yield '(' rparen
{
/*%%%*/
$$ = NEW_YIELD(0, Qfalse);
/*%
$$ = dispatch1(yield, dispatch1(paren, arg_new()));
%*/
}
| keyword_yield
{
/*%%%*/
$$ = NEW_YIELD(0, Qfalse);
/*%
$$ = dispatch0(yield0);
%*/
}
| keyword_defined opt_nl '(' {in_defined = 1;} expr rparen
{
/*%%%*/
in_defined = 0;
$$ = NEW_DEFINED($5);
/*%
in_defined = 0;
$$ = dispatch1(defined, $5);
%*/
}
| keyword_not '(' expr rparen
{
/*%%%*/
$$ = call_uni_op(cond($3), '!');
/*%
$$ = dispatch2(unary, ripper_intern("not"), $3);
%*/
}
| keyword_not '(' rparen
{
/*%%%*/
$$ = call_uni_op(cond(NEW_NIL()), '!');
/*%
$$ = dispatch2(unary, ripper_intern("not"), Qnil);
%*/
}
| operation brace_block
{
/*%%%*/
$2->nd_iter = NEW_FCALL($1, 0);
$$ = $2;
fixpos($2->nd_iter, $2);
/*%
$$ = method_arg(dispatch1(fcall, $1), arg_new());
$$ = method_add_block($$, $2);
%*/
}
| method_call
| method_call brace_block
{
/*%%%*/
block_dup_check($1->nd_args, $2);
$2->nd_iter = $1;
$$ = $2;
fixpos($$, $1);
/*%
$$ = method_add_block($1, $2);
%*/
}
| tLAMBDA lambda
{
$$ = $2;
}
| k_if expr_value then
compstmt
if_tail
k_end
{
/*%%%*/
$$ = NEW_IF(cond($2), $4, $5);
fixpos($$, $2);
/*%
$$ = dispatch3(if, $2, $4, escape_Qundef($5));
%*/
}
| k_unless expr_value then
compstmt
opt_else
k_end
{
/*%%%*/
$$ = NEW_UNLESS(cond($2), $4, $5);
fixpos($$, $2);
/*%
$$ = dispatch3(unless, $2, $4, escape_Qundef($5));
%*/
}
| k_while {COND_PUSH(1);} expr_value do {COND_POP();}
compstmt
k_end
{
/*%%%*/
$$ = NEW_WHILE(cond($3), $6, 1);
fixpos($$, $3);
/*%
$$ = dispatch2(while, $3, $6);
%*/
}
| k_until {COND_PUSH(1);} expr_value do {COND_POP();}
compstmt
k_end
{
/*%%%*/
$$ = NEW_UNTIL(cond($3), $6, 1);
fixpos($$, $3);
/*%
$$ = dispatch2(until, $3, $6);
%*/
}
| k_case expr_value opt_terms
case_body
k_end
{
/*%%%*/
$$ = NEW_CASE($2, $4);
fixpos($$, $2);
/*%
$$ = dispatch2(case, $2, $4);
%*/
}
| k_case opt_terms case_body k_end
{
/*%%%*/
$$ = NEW_CASE(0, $3);
/*%
$$ = dispatch2(case, Qnil, $3);
%*/
}
| k_for for_var keyword_in
{COND_PUSH(1);}
expr_value do
{COND_POP();}
compstmt
k_end
{
/*%%%*/
/*
* for a, b, c in e
* #=>
* e.each{|*x| a, b, c = x
*
* for a in e
* #=>
* e.each{|x| a, = x}
*/
ID id = internal_id();
ID *tbl = ALLOC_N(ID, 2);
NODE *m = NEW_ARGS_AUX(0, 0);
NODE *args, *scope;
if (nd_type($2) == NODE_MASGN) {
/* if args.length == 1 && args[0].kind_of?(Array)
* args = args[0]
* end
*/
NODE *one = NEW_LIST(NEW_LIT(INT2FIX(1)));
NODE *zero = NEW_LIST(NEW_LIT(INT2FIX(0)));
m->nd_next = block_append(
NEW_IF(
NEW_NODE(NODE_AND,
NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("length"), 0),
rb_intern("=="), one),
NEW_CALL(NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero),
rb_intern("kind_of?"), NEW_LIST(NEW_LIT(rb_cArray))),
0),
NEW_DASGN_CURR(id,
NEW_CALL(NEW_DVAR(id), rb_intern("[]"), zero)),
0),
node_assign($2, NEW_DVAR(id)));
args = new_args(m, 0, id, 0, 0);
}
else {
if (nd_type($2) == NODE_LASGN ||
nd_type($2) == NODE_DASGN ||
nd_type($2) == NODE_DASGN_CURR) {
$2->nd_value = NEW_DVAR(id);
m->nd_plen = 1;
m->nd_next = $2;
args = new_args(m, 0, 0, 0, 0);
}
else {
m->nd_next = node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id));
args = new_args(m, 0, id, 0, 0);
}
}
scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
tbl[0] = 1; tbl[1] = id;
$$ = NEW_FOR(0, $5, scope);
fixpos($$, $2);
/*%
$$ = dispatch3(for, $2, $5, $8);
%*/
}
| k_class cpath superclass
{
if (in_def || in_single)
yyerror("class definition in method body");
local_push(0);
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
k_end
{
/*%%%*/
$$ = NEW_CLASS($2, $5, $3);
nd_set_line($$, $<num>4);
/*%
$$ = dispatch3(class, $2, $3, $5);
%*/
local_pop();
}
| k_class tLSHFT expr
{
$<num>$ = in_def;
in_def = 0;
}
term
{
$<num>$ = in_single;
in_single = 0;
local_push(0);
}
bodystmt
k_end
{
/*%%%*/
$$ = NEW_SCLASS($3, $7);
fixpos($$, $3);
/*%
$$ = dispatch2(sclass, $3, $7);
%*/
local_pop();
in_def = $<num>4;
in_single = $<num>6;
}
| k_module cpath
{
if (in_def || in_single)
yyerror("module definition in method body");
local_push(0);
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
k_end
{
/*%%%*/
$$ = NEW_MODULE($2, $4);
nd_set_line($$, $<num>3);
/*%
$$ = dispatch2(module, $2, $4);
%*/
local_pop();
}
| k_def fname
{
$<id>$ = cur_mid;
cur_mid = $2;
in_def++;
local_push(0);
}
f_arglist
bodystmt
k_end
{
/*%%%*/
NODE *body = remove_begin($5);
reduce_nodes(&body);
$$ = NEW_DEFN($2, $4, body, NOEX_PRIVATE);
nd_set_line($$, $<num>1);
/*%
$$ = dispatch3(def, $2, $4, $5);
%*/
local_pop();
in_def--;
cur_mid = $<id>3;
}
| k_def singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
{
in_single++;
lex_state = EXPR_ENDFN; /* force for args */
local_push(0);
}
f_arglist
bodystmt
k_end
{
/*%%%*/
NODE *body = remove_begin($8);
reduce_nodes(&body);
$$ = NEW_DEFS($2, $5, $7, body);
nd_set_line($$, $<num>1);
/*%
$$ = dispatch5(defs, $2, $3, $5, $7, $8);
%*/
local_pop();
in_single--;
}
| keyword_break
{
/*%%%*/
$$ = NEW_BREAK(0);
/*%
$$ = dispatch1(break, arg_new());
%*/
}
| keyword_next
{
/*%%%*/
$$ = NEW_NEXT(0);
/*%
$$ = dispatch1(next, arg_new());
%*/
}
| keyword_redo
{
/*%%%*/
$$ = NEW_REDO();
/*%
$$ = dispatch0(redo);
%*/
}
| keyword_retry
{
/*%%%*/
$$ = NEW_RETRY();
/*%
$$ = dispatch0(retry);
%*/
}
;
primary_value : primary
{
/*%%%*/
value_expr($1);
$$ = $1;
if (!$$) $$ = NEW_NIL();
/*%
$$ = $1;
%*/
}
;
k_begin : keyword_begin
{
token_info_push("begin");
}
;
k_if : keyword_if
{
token_info_push("if");
}
;
k_unless : keyword_unless
{
token_info_push("unless");
}
;
k_while : keyword_while
{
token_info_push("while");
}
;
k_until : keyword_until
{
token_info_push("until");
}
;
k_case : keyword_case
{
token_info_push("case");
}
;
k_for : keyword_for
{
token_info_push("for");
}
;
k_class : keyword_class
{
token_info_push("class");
}
;
k_module : keyword_module
{
token_info_push("module");
}
;
k_def : keyword_def
{
token_info_push("def");
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
;
k_end : keyword_end
{
token_info_pop("end");
}
;
then : term
/*%c%*/
/*%c
{ $$ = Qnil; }
%*/
| keyword_then
| term keyword_then
/*%c%*/
/*%c
{ $$ = $2; }
%*/
;
do : term
/*%c%*/
/*%c
{ $$ = Qnil; }
%*/
| keyword_do_cond
;
if_tail : opt_else
| keyword_elsif expr_value then
compstmt
if_tail
{
/*%%%*/
$$ = NEW_IF(cond($2), $4, $5);
fixpos($$, $2);
/*%
$$ = dispatch3(elsif, $2, $4, escape_Qundef($5));
%*/
}
;
opt_else : none
| keyword_else compstmt
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(else, $2);
%*/
}
;
for_var : lhs
| mlhs
;
f_marg : f_norm_arg
{
$$ = assignable($1, 0);
/*%%%*/
/*%
$$ = dispatch1(mlhs_paren, $$);
%*/
}
| tLPAREN f_margs rparen
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(mlhs_paren, $2);
%*/
}
;
f_marg_list : f_marg
{
/*%%%*/
$$ = NEW_LIST($1);
/*%
$$ = mlhs_add(mlhs_new(), $1);
%*/
}
| f_marg_list ',' f_marg
{
/*%%%*/
$$ = list_append($1, $3);
/*%
$$ = mlhs_add($1, $3);
%*/
}
;
f_margs : f_marg_list
{
/*%%%*/
$$ = NEW_MASGN($1, 0);
/*%
$$ = $1;
%*/
}
| f_marg_list ',' tSTAR f_norm_arg
{
$$ = assignable($4, 0);
/*%%%*/
$$ = NEW_MASGN($1, $$);
/*%
$$ = mlhs_add_star($1, $$);
%*/
}
| f_marg_list ',' tSTAR f_norm_arg ',' f_marg_list
{
$$ = assignable($4, 0);
/*%%%*/
$$ = NEW_MASGN($1, NEW_POSTARG($$, $6));
/*%
$$ = mlhs_add_star($1, $$);
%*/
}
| f_marg_list ',' tSTAR
{
/*%%%*/
$$ = NEW_MASGN($1, -1);
/*%
$$ = mlhs_add_star($1, Qnil);
%*/
}
| f_marg_list ',' tSTAR ',' f_marg_list
{
/*%%%*/
$$ = NEW_MASGN($1, NEW_POSTARG(-1, $5));
/*%
$$ = mlhs_add_star($1, $5);
%*/
}
| tSTAR f_norm_arg
{
$$ = assignable($2, 0);
/*%%%*/
$$ = NEW_MASGN(0, $$);
/*%
$$ = mlhs_add_star(mlhs_new(), $$);
%*/
}
| tSTAR f_norm_arg ',' f_marg_list
{
$$ = assignable($2, 0);
/*%%%*/
$$ = NEW_MASGN(0, NEW_POSTARG($$, $4));
/*%
#if 0
TODO: Check me
#endif
$$ = mlhs_add_star($$, $4);
%*/
}
| tSTAR
{
/*%%%*/
$$ = NEW_MASGN(0, -1);
/*%
$$ = mlhs_add_star(mlhs_new(), Qnil);
%*/
}
| tSTAR ',' f_marg_list
{
/*%%%*/
$$ = NEW_MASGN(0, NEW_POSTARG(-1, $3));
/*%
$$ = mlhs_add_star(mlhs_new(), Qnil);
%*/
}
;
block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, $5, 0, $6);
/*%
$$ = params_new($1, $3, $5, Qnil, escape_Qundef($6));
%*/
}
| f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, $5, $7, $8);
/*%
$$ = params_new($1, $3, $5, $7, escape_Qundef($8));
%*/
}
| f_arg ',' f_block_optarg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, 0, 0, $4);
/*%
$$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4));
%*/
}
| f_arg ',' f_block_optarg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, 0, $5, $6);
/*%
$$ = params_new($1, $3, Qnil, $5, escape_Qundef($6));
%*/
}
| f_arg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, $3, 0, $4);
/*%
$$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4));
%*/
}
| f_arg ','
{
/*%%%*/
$$ = new_args($1, 0, 1, 0, 0);
/*%
$$ = params_new($1, Qnil, Qnil, Qnil, Qnil);
dispatch1(excessed_comma, $$);
%*/
}
| f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, $3, $5, $6);
/*%
$$ = params_new($1, Qnil, $3, $5, escape_Qundef($6));
%*/
}
| f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, 0, 0, $2);
/*%
$$ = params_new($1, Qnil,Qnil, Qnil, escape_Qundef($2));
%*/
}
| f_block_optarg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, $3, 0, $4);
/*%
$$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4));
%*/
}
| f_block_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, $3, $5, $6);
/*%
$$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6));
%*/
}
| f_block_optarg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, 0, 0, $2);
/*%
$$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2));
%*/
}
| f_block_optarg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, 0, $3, $4);
/*%
$$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4));
%*/
}
| f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, $1, 0, $2);
/*%
$$ = params_new(Qnil, Qnil, $1, Qnil, escape_Qundef($2));
%*/
}
| f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, $1, $3, $4);
/*%
$$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4));
%*/
}
| f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, 0, 0, $1);
/*%
$$ = params_new(Qnil, Qnil, Qnil, Qnil, $1);
%*/
}
;
opt_block_param : none
| block_param_def
{
command_start = TRUE;
}
;
block_param_def : '|' opt_bv_decl '|'
{
/*%%%*/
$$ = 0;
/*%
$$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil),
escape_Qundef($2));
%*/
}
| tOROP
{
/*%%%*/
$$ = 0;
/*%
$$ = blockvar_new(params_new(Qnil,Qnil,Qnil,Qnil,Qnil),
Qnil);
%*/
}
| '|' block_param opt_bv_decl '|'
{
/*%%%*/
$$ = $2;
/*%
$$ = blockvar_new(escape_Qundef($2), escape_Qundef($3));
%*/
}
;
opt_bv_decl : none
| ';' bv_decls
{
/*%%%*/
$$ = 0;
/*%
$$ = $2;
%*/
}
;
bv_decls : bvar
/*%c%*/
/*%c
{
$$ = rb_ary_new3(1, $1);
}
%*/
| bv_decls ',' bvar
/*%c%*/
/*%c
{
rb_ary_push($$, $3);
}
%*/
;
bvar : tIDENTIFIER
{
new_bv(get_id($1));
/*%%%*/
/*%
$$ = get_value($1);
%*/
}
| f_bad_arg
{
$$ = 0;
}
;
lambda : {
$<vars>$ = dyna_push();
}
{
$<num>$ = lpar_beg;
lpar_beg = ++paren_nest;
}
f_larglist
lambda_body
{
lpar_beg = $<num>2;
/*%%%*/
$$ = $3;
$$->nd_body = NEW_SCOPE($3->nd_head, $4);
/*%
$$ = dispatch2(lambda, $3, $4);
%*/
dyna_pop($<vars>1);
}
;
f_larglist : '(' f_args opt_bv_decl rparen
{
/*%%%*/
$$ = NEW_LAMBDA($2);
/*%
$$ = dispatch1(paren, $2);
%*/
}
| f_args
{
/*%%%*/
$$ = NEW_LAMBDA($1);
/*%
$$ = $1;
%*/
}
;
lambda_body : tLAMBEG compstmt '}'
{
$$ = $2;
}
| keyword_do_LAMBDA compstmt keyword_end
{
$$ = $2;
}
;
do_block : keyword_do_block
{
$<vars>1 = dyna_push();
/*%%%*/
$<num>$ = ruby_sourceline;
/*% %*/
}
opt_block_param
compstmt
keyword_end
{
/*%%%*/
$$ = NEW_ITER($3,$4);
nd_set_line($$, $<num>2);
/*%
$$ = dispatch2(do_block, escape_Qundef($3), $4);
%*/
dyna_pop($<vars>1);
}
;
block_call : command do_block
{
/*%%%*/
if (nd_type($1) == NODE_YIELD) {
compile_error(PARSER_ARG "block given to yield");
}
else {
block_dup_check($1->nd_args, $2);
}
$2->nd_iter = $1;
$$ = $2;
fixpos($$, $1);
/*%
$$ = method_add_block($1, $2);
%*/
}
| block_call '.' operation2 opt_paren_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
/*%
$$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
$$ = method_optarg($$, $4);
%*/
}
| block_call tCOLON2 operation2 opt_paren_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
/*%
$$ = dispatch3(call, $1, ripper_intern("::"), $3);
$$ = method_optarg($$, $4);
%*/
}
;
method_call : operation paren_args
{
/*%%%*/
$$ = NEW_FCALL($1, $2);
fixpos($$, $2);
/*%
$$ = method_arg(dispatch1(fcall, $1), $2);
%*/
}
| primary_value '.' operation2 opt_paren_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
fixpos($$, $1);
/*%
$$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
$$ = method_optarg($$, $4);
%*/
}
| primary_value tCOLON2 operation2 paren_args
{
/*%%%*/
$$ = NEW_CALL($1, $3, $4);
fixpos($$, $1);
/*%
$$ = dispatch3(call, $1, ripper_id2sym('.'), $3);
$$ = method_optarg($$, $4);
%*/
}
| primary_value tCOLON2 operation3
{
/*%%%*/
$$ = NEW_CALL($1, $3, 0);
/*%
$$ = dispatch3(call, $1, ripper_intern("::"), $3);
%*/
}
| primary_value '.' paren_args
{
/*%%%*/
$$ = NEW_CALL($1, rb_intern("call"), $3);
fixpos($$, $1);
/*%
$$ = dispatch3(call, $1, ripper_id2sym('.'),
ripper_intern("call"));
$$ = method_optarg($$, $3);
%*/
}
| primary_value tCOLON2 paren_args
{
/*%%%*/
$$ = NEW_CALL($1, rb_intern("call"), $3);
fixpos($$, $1);
/*%
$$ = dispatch3(call, $1, ripper_intern("::"),
ripper_intern("call"));
$$ = method_optarg($$, $3);
%*/
}
| keyword_super paren_args
{
/*%%%*/
$$ = NEW_SUPER($2);
/*%
$$ = dispatch1(super, $2);
%*/
}
| keyword_super
{
/*%%%*/
$$ = NEW_ZSUPER();
/*%
$$ = dispatch0(zsuper);
%*/
}
| primary_value '[' opt_call_args rbracket
{
/*%%%*/
if ($1 && nd_type($1) == NODE_SELF)
$$ = NEW_FCALL(tAREF, $3);
else
$$ = NEW_CALL($1, tAREF, $3);
fixpos($$, $1);
/*%
$$ = dispatch2(aref, $1, escape_Qundef($3));
%*/
}
;
brace_block : '{'
{
$<vars>1 = dyna_push();
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
opt_block_param
compstmt '}'
{
/*%%%*/
$$ = NEW_ITER($3,$4);
nd_set_line($$, $<num>2);
/*%
$$ = dispatch2(brace_block, escape_Qundef($3), $4);
%*/
dyna_pop($<vars>1);
}
| keyword_do
{
$<vars>1 = dyna_push();
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
opt_block_param
compstmt keyword_end
{
/*%%%*/
$$ = NEW_ITER($3,$4);
nd_set_line($$, $<num>2);
/*%
$$ = dispatch2(do_block, escape_Qundef($3), $4);
%*/
dyna_pop($<vars>1);
}
;
case_body : keyword_when args then
compstmt
cases
{
/*%%%*/
$$ = NEW_WHEN($2, $4, $5);
/*%
$$ = dispatch3(when, $2, $4, escape_Qundef($5));
%*/
}
;
cases : opt_else
| case_body
;
opt_rescue : keyword_rescue exc_list exc_var then
compstmt
opt_rescue
{
/*%%%*/
if ($3) {
$3 = node_assign($3, NEW_ERRINFO());
$5 = block_append($3, $5);
}
$$ = NEW_RESBODY($2, $5, $6);
fixpos($$, $2?$2:$5);
/*%
$$ = dispatch4(rescue,
escape_Qundef($2),
escape_Qundef($3),
escape_Qundef($5),
escape_Qundef($6));
%*/
}
| none
;
exc_list : arg_value
{
/*%%%*/
$$ = NEW_LIST($1);
/*%
$$ = rb_ary_new3(1, $1);
%*/
}
| mrhs
{
/*%%%*/
if (!($$ = splat_array($1))) $$ = $1;
/*%
$$ = $1;
%*/
}
| none
;
exc_var : tASSOC lhs
{
$$ = $2;
}
| none
;
opt_ensure : keyword_ensure compstmt
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(ensure, $2);
%*/
}
| none
;
literal : numeric
| symbol
{
/*%%%*/
$$ = NEW_LIT(ID2SYM($1));
/*%
$$ = dispatch1(symbol_literal, $1);
%*/
}
| dsym
;
strings : string
{
/*%%%*/
NODE *node = $1;
if (!node) {
node = NEW_STR(STR_NEW0());
}
else {
node = evstr2dstr(node);
}
$$ = node;
/*%
$$ = $1;
%*/
}
;
string : tCHAR
| string1
| string string1
{
/*%%%*/
$$ = literal_concat($1, $2);
/*%
$$ = dispatch2(string_concat, $1, $2);
%*/
}
;
string1 : tSTRING_BEG string_contents tSTRING_END
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(string_literal, $2);
%*/
}
;
xstring : tXSTRING_BEG xstring_contents tSTRING_END
{
/*%%%*/
NODE *node = $2;
if (!node) {
node = NEW_XSTR(STR_NEW0());
}
else {
switch (nd_type(node)) {
case NODE_STR:
nd_set_type(node, NODE_XSTR);
break;
case NODE_DSTR:
nd_set_type(node, NODE_DXSTR);
break;
default:
node = NEW_NODE(NODE_DXSTR, Qnil, 1, NEW_LIST(node));
break;
}
}
$$ = node;
/*%
$$ = dispatch1(xstring_literal, $2);
%*/
}
;
regexp : tREGEXP_BEG regexp_contents tREGEXP_END
{
/*%%%*/
int options = $3;
NODE *node = $2;
NODE *list, *prev;
if (!node) {
node = NEW_LIT(reg_compile(STR_NEW0(), options));
}
else switch (nd_type(node)) {
case NODE_STR:
{
VALUE src = node->nd_lit;
nd_set_type(node, NODE_LIT);
node->nd_lit = reg_compile(src, options);
}
break;
default:
node = NEW_NODE(NODE_DSTR, STR_NEW0(), 1, NEW_LIST(node));
case NODE_DSTR:
if (options & RE_OPTION_ONCE) {
nd_set_type(node, NODE_DREGX_ONCE);
}
else {
nd_set_type(node, NODE_DREGX);
}
node->nd_cflag = options & RE_OPTION_MASK;
if (!NIL_P(node->nd_lit)) reg_fragment_check(node->nd_lit, options);
for (list = (prev = node)->nd_next; list; list = list->nd_next) {
if (nd_type(list->nd_head) == NODE_STR) {
VALUE tail = list->nd_head->nd_lit;
if (reg_fragment_check(tail, options) && prev && !NIL_P(prev->nd_lit)) {
VALUE lit = prev == node ? prev->nd_lit : prev->nd_head->nd_lit;
if (!literal_concat0(parser, lit, tail)) {
node = 0;
break;
}
rb_str_resize(tail, 0);
prev->nd_next = list->nd_next;
rb_gc_force_recycle((VALUE)list->nd_head);
rb_gc_force_recycle((VALUE)list);
list = prev;
}
else {
prev = list;
}
}
else {
prev = 0;
}
}
if (!node->nd_next) {
VALUE src = node->nd_lit;
nd_set_type(node, NODE_LIT);
node->nd_lit = reg_compile(src, options);
}
break;
}
$$ = node;
/*%
$$ = dispatch2(regexp_literal, $2, $3);
%*/
}
;
words : tWORDS_BEG ' ' tSTRING_END
{
/*%%%*/
$$ = NEW_ZARRAY();
/*%
$$ = dispatch0(words_new);
$$ = dispatch1(array, $$);
%*/
}
| tWORDS_BEG word_list tSTRING_END
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(array, $2);
%*/
}
;
word_list : /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = dispatch0(words_new);
%*/
}
| word_list word ' '
{
/*%%%*/
$$ = list_append($1, evstr2dstr($2));
/*%
$$ = dispatch2(words_add, $1, $2);
%*/
}
;
word : string_content
/*%c%*/
/*%c
{
$$ = dispatch0(word_new);
$$ = dispatch2(word_add, $$, $1);
}
%*/
| word string_content
{
/*%%%*/
$$ = literal_concat($1, $2);
/*%
$$ = dispatch2(word_add, $1, $2);
%*/
}
;
qwords : tQWORDS_BEG ' ' tSTRING_END
{
/*%%%*/
$$ = NEW_ZARRAY();
/*%
$$ = dispatch0(qwords_new);
$$ = dispatch1(array, $$);
%*/
}
| tQWORDS_BEG qword_list tSTRING_END
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(array, $2);
%*/
}
;
qword_list : /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = dispatch0(qwords_new);
%*/
}
| qword_list tSTRING_CONTENT ' '
{
/*%%%*/
$$ = list_append($1, $2);
/*%
$$ = dispatch2(qwords_add, $1, $2);
%*/
}
;
string_contents : /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = dispatch0(string_content);
%*/
}
| string_contents string_content
{
/*%%%*/
$$ = literal_concat($1, $2);
/*%
$$ = dispatch2(string_add, $1, $2);
%*/
}
;
xstring_contents: /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = dispatch0(xstring_new);
%*/
}
| xstring_contents string_content
{
/*%%%*/
$$ = literal_concat($1, $2);
/*%
$$ = dispatch2(xstring_add, $1, $2);
%*/
}
;
regexp_contents: /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = dispatch0(regexp_new);
%*/
}
| regexp_contents string_content
{
/*%%%*/
NODE *head = $1, *tail = $2;
if (!head) {
$$ = tail;
}
else if (!tail) {
$$ = head;
}
else {
switch (nd_type(head)) {
case NODE_STR:
nd_set_type(head, NODE_DSTR);
break;
case NODE_DSTR:
break;
default:
head = list_append(NEW_DSTR(Qnil), head);
break;
}
$$ = list_append(head, tail);
}
/*%
$$ = dispatch2(regexp_add, $1, $2);
%*/
}
;
string_content : tSTRING_CONTENT
| tSTRING_DVAR
{
$<node>$ = lex_strterm;
lex_strterm = 0;
lex_state = EXPR_BEG;
}
string_dvar
{
/*%%%*/
lex_strterm = $<node>2;
$$ = NEW_EVSTR($3);
/*%
lex_strterm = $<node>2;
$$ = dispatch1(string_dvar, $3);
%*/
}
| tSTRING_DBEG
{
$<val>1 = cond_stack;
$<val>$ = cmdarg_stack;
cond_stack = 0;
cmdarg_stack = 0;
}
{
$<node>$ = lex_strterm;
lex_strterm = 0;
lex_state = EXPR_BEG;
}
compstmt '}'
{
cond_stack = $<val>1;
cmdarg_stack = $<val>2;
lex_strterm = $<node>3;
/*%%%*/
if ($4) $4->flags &= ~NODE_FL_NEWLINE;
$$ = new_evstr($4);
/*%
$$ = dispatch1(string_embexpr, $4);
%*/
}
;
string_dvar : tGVAR
{
/*%%%*/
$$ = NEW_GVAR($1);
/*%
$$ = dispatch1(var_ref, $1);
%*/
}
| tIVAR
{
/*%%%*/
$$ = NEW_IVAR($1);
/*%
$$ = dispatch1(var_ref, $1);
%*/
}
| tCVAR
{
/*%%%*/
$$ = NEW_CVAR($1);
/*%
$$ = dispatch1(var_ref, $1);
%*/
}
| backref
;
symbol : tSYMBEG sym
{
lex_state = EXPR_END;
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(symbol, $2);
%*/
}
;
sym : fname
| tIVAR
| tGVAR
| tCVAR
;
dsym : tSYMBEG xstring_contents tSTRING_END
{
lex_state = EXPR_END;
/*%%%*/
if (!($$ = $2)) {
$$ = NEW_LIT(ID2SYM(rb_intern("")));
}
else {
VALUE lit;
switch (nd_type($$)) {
case NODE_DSTR:
nd_set_type($$, NODE_DSYM);
break;
case NODE_STR:
lit = $$->nd_lit;
$$->nd_lit = ID2SYM(rb_intern_str(lit));
nd_set_type($$, NODE_LIT);
break;
default:
$$ = NEW_NODE(NODE_DSYM, Qnil, 1, NEW_LIST($$));
break;
}
}
/*%
$$ = dispatch1(dyna_symbol, $2);
%*/
}
;
numeric : tINTEGER
| tFLOAT
| tUMINUS_NUM tINTEGER %prec tLOWEST
{
/*%%%*/
$$ = negate_lit($2);
/*%
$$ = dispatch2(unary, ripper_intern("-@"), $2);
%*/
}
| tUMINUS_NUM tFLOAT %prec tLOWEST
{
/*%%%*/
$$ = negate_lit($2);
/*%
$$ = dispatch2(unary, ripper_intern("-@"), $2);
%*/
}
;
variable : tIDENTIFIER
| tIVAR
| tGVAR
| tCONSTANT
| tCVAR
| keyword_nil {ifndef_ripper($$ = keyword_nil);}
| keyword_self {ifndef_ripper($$ = keyword_self);}
| keyword_true {ifndef_ripper($$ = keyword_true);}
| keyword_false {ifndef_ripper($$ = keyword_false);}
| keyword__FILE__ {ifndef_ripper($$ = keyword__FILE__);}
| keyword__LINE__ {ifndef_ripper($$ = keyword__LINE__);}
| keyword__ENCODING__ {ifndef_ripper($$ = keyword__ENCODING__);}
;
var_ref : variable
{
/*%%%*/
if (!($$ = gettable($1))) $$ = NEW_BEGIN(0);
/*%
$$ = dispatch1(var_ref, $1);
%*/
}
;
var_lhs : variable
{
$$ = assignable($1, 0);
/*%%%*/
/*%
$$ = dispatch1(var_field, $$);
%*/
}
;
backref : tNTH_REF
| tBACK_REF
;
superclass : term
{
/*%%%*/
$$ = 0;
/*%
$$ = Qnil;
%*/
}
| '<'
{
lex_state = EXPR_BEG;
}
expr_value term
{
$$ = $3;
}
| error term
{
/*%%%*/
yyerrok;
$$ = 0;
/*%
yyerrok;
$$ = Qnil;
%*/
}
;
f_arglist : '(' f_args rparen
{
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(paren, $2);
%*/
lex_state = EXPR_BEG;
command_start = TRUE;
}
| f_args term
{
$$ = $1;
}
;
f_args : f_arg ',' f_optarg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, $5, 0, $6);
/*%
$$ = params_new($1, $3, $5, Qnil, escape_Qundef($6));
%*/
}
| f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, $5, $7, $8);
/*%
$$ = params_new($1, $3, $5, $7, escape_Qundef($8));
%*/
}
| f_arg ',' f_optarg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, 0, 0, $4);
/*%
$$ = params_new($1, $3, Qnil, Qnil, escape_Qundef($4));
%*/
}
| f_arg ',' f_optarg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, $3, 0, $5, $6);
/*%
$$ = params_new($1, $3, Qnil, $5, escape_Qundef($6));
%*/
}
| f_arg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, $3, 0, $4);
/*%
$$ = params_new($1, Qnil, $3, Qnil, escape_Qundef($4));
%*/
}
| f_arg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, $3, $5, $6);
/*%
$$ = params_new($1, Qnil, $3, $5, escape_Qundef($6));
%*/
}
| f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args($1, 0, 0, 0, $2);
/*%
$$ = params_new($1, Qnil, Qnil, Qnil,escape_Qundef($2));
%*/
}
| f_optarg ',' f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, $3, 0, $4);
/*%
$$ = params_new(Qnil, $1, $3, Qnil, escape_Qundef($4));
%*/
}
| f_optarg ',' f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, $3, $5, $6);
/*%
$$ = params_new(Qnil, $1, $3, $5, escape_Qundef($6));
%*/
}
| f_optarg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, 0, 0, $2);
/*%
$$ = params_new(Qnil, $1, Qnil, Qnil,escape_Qundef($2));
%*/
}
| f_optarg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, $1, 0, $3, $4);
/*%
$$ = params_new(Qnil, $1, Qnil, $3, escape_Qundef($4));
%*/
}
| f_rest_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, $1, 0, $2);
/*%
$$ = params_new(Qnil, Qnil, $1, Qnil,escape_Qundef($2));
%*/
}
| f_rest_arg ',' f_arg opt_f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, $1, $3, $4);
/*%
$$ = params_new(Qnil, Qnil, $1, $3, escape_Qundef($4));
%*/
}
| f_block_arg
{
/*%%%*/
$$ = new_args(0, 0, 0, 0, $1);
/*%
$$ = params_new(Qnil, Qnil, Qnil, Qnil, $1);
%*/
}
| /* none */
{
/*%%%*/
$$ = new_args(0, 0, 0, 0, 0);
/*%
$$ = params_new(Qnil, Qnil, Qnil, Qnil, Qnil);
%*/
}
;
f_bad_arg : tCONSTANT
{
/*%%%*/
yyerror("formal argument cannot be a constant");
$$ = 0;
/*%
$$ = dispatch1(param_error, $1);
%*/
}
| tIVAR
{
/*%%%*/
yyerror("formal argument cannot be an instance variable");
$$ = 0;
/*%
$$ = dispatch1(param_error, $1);
%*/
}
| tGVAR
{
/*%%%*/
yyerror("formal argument cannot be a global variable");
$$ = 0;
/*%
$$ = dispatch1(param_error, $1);
%*/
}
| tCVAR
{
/*%%%*/
yyerror("formal argument cannot be a class variable");
$$ = 0;
/*%
$$ = dispatch1(param_error, $1);
%*/
}
;
f_norm_arg : f_bad_arg
| tIDENTIFIER
{
formal_argument(get_id($1));
$$ = $1;
}
;
f_arg_item : f_norm_arg
{
arg_var(get_id($1));
/*%%%*/
$$ = NEW_ARGS_AUX($1, 1);
/*%
$$ = get_value($1);
%*/
}
| tLPAREN f_margs rparen
{
ID tid = internal_id();
arg_var(tid);
/*%%%*/
if (dyna_in_block()) {
$2->nd_value = NEW_DVAR(tid);
}
else {
$2->nd_value = NEW_LVAR(tid);
}
$$ = NEW_ARGS_AUX(tid, 1);
$$->nd_next = $2;
/*%
$$ = dispatch1(mlhs_paren, $2);
%*/
}
;
f_arg : f_arg_item
/*%c%*/
/*%c
{
$$ = rb_ary_new3(1, $1);
}
c%*/
| f_arg ',' f_arg_item
{
/*%%%*/
$$ = $1;
$$->nd_plen++;
$$->nd_next = block_append($$->nd_next, $3->nd_next);
rb_gc_force_recycle((VALUE)$3);
/*%
$$ = rb_ary_push($1, $3);
%*/
}
;
f_opt : tIDENTIFIER '=' arg_value
{
arg_var(formal_argument(get_id($1)));
$$ = assignable($1, $3);
/*%%%*/
$$ = NEW_OPT_ARG(0, $$);
/*%
$$ = rb_assoc_new($$, $3);
%*/
}
;
f_block_opt : tIDENTIFIER '=' primary_value
{
arg_var(formal_argument(get_id($1)));
$$ = assignable($1, $3);
/*%%%*/
$$ = NEW_OPT_ARG(0, $$);
/*%
$$ = rb_assoc_new($$, $3);
%*/
}
;
f_block_optarg : f_block_opt
{
/*%%%*/
$$ = $1;
/*%
$$ = rb_ary_new3(1, $1);
%*/
}
| f_block_optarg ',' f_block_opt
{
/*%%%*/
NODE *opts = $1;
while (opts->nd_next) {
opts = opts->nd_next;
}
opts->nd_next = $3;
$$ = $1;
/*%
$$ = rb_ary_push($1, $3);
%*/
}
;
f_optarg : f_opt
{
/*%%%*/
$$ = $1;
/*%
$$ = rb_ary_new3(1, $1);
%*/
}
| f_optarg ',' f_opt
{
/*%%%*/
NODE *opts = $1;
while (opts->nd_next) {
opts = opts->nd_next;
}
opts->nd_next = $3;
$$ = $1;
/*%
$$ = rb_ary_push($1, $3);
%*/
}
;
restarg_mark : '*'
| tSTAR
;
f_rest_arg : restarg_mark tIDENTIFIER
{
/*%%%*/
if (!is_local_id($2))
yyerror("rest argument must be local variable");
/*% %*/
arg_var(shadowing_lvar(get_id($2)));
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(rest_param, $2);
%*/
}
| restarg_mark
{
/*%%%*/
$$ = internal_id();
arg_var($$);
/*%
$$ = dispatch1(rest_param, Qnil);
%*/
}
;
blkarg_mark : '&'
| tAMPER
;
f_block_arg : blkarg_mark tIDENTIFIER
{
/*%%%*/
if (!is_local_id($2))
yyerror("block argument must be local variable");
else if (!dyna_in_block() && local_id($2))
yyerror("duplicated block argument name");
/*% %*/
arg_var(shadowing_lvar(get_id($2)));
/*%%%*/
$$ = $2;
/*%
$$ = dispatch1(blockarg, $2);
%*/
}
;
opt_f_block_arg : ',' f_block_arg
{
$$ = $2;
}
| none
{
/*%%%*/
$$ = 0;
/*%
$$ = Qundef;
%*/
}
;
singleton : var_ref
{
/*%%%*/
value_expr($1);
$$ = $1;
if (!$$) $$ = NEW_NIL();
/*%
$$ = $1;
%*/
}
| '(' {lex_state = EXPR_BEG;} expr rparen
{
/*%%%*/
if ($3 == 0) {
yyerror("can't define singleton method for ().");
}
else {
switch (nd_type($3)) {
case NODE_STR:
case NODE_DSTR:
</