Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

10382 lines (9643 sloc) 227.602 kb
/*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2007-2010, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
*/
%{
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define YYSTACK_USE_ALLOCA 0
#include "macruby_internal.h"
#include "ruby/intern.h"
#define __IN_PARSE_Y__ 1
#include "ruby/node.h"
#include "ruby/st.h"
#include "ruby/encoding.h"
#include "encoding.h"
#include "symbol.h"
#include "id.h"
#include "re.h"
#include "class.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)
static inline void *orig_malloc(size_t l) { return malloc(l); }
static inline void orig_free(void *ptr) { free(ptr); }
#define malloc YYMALLOC
#define realloc YYREALLOC
#define calloc YYCALLOC
#define free YYFREE
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. */
};
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)
/* must sync with real YYSTYPE */
union tmpyystype {
VALUE val;
NODE *node;
unsigned long id;
int num;
struct RVarmap *vars;
};
struct vtable {
ID *tbl;
int pos;
int capa;
struct vtable *prev;
};
struct local_vars {
struct vtable *args;
struct vtable *vars;
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;
GC_WB(&tbl->tbl, ALLOC_N(ID, tbl->capa));
GC_WB(&tbl->prev, prev);
if (VTBL_DEBUG) printf("vtable_alloc: %p\n", tbl);
return tbl;
}
static void
vtable_free(struct vtable *tbl)
{
if (VTBL_DEBUG)printf("vtable_free: %p\n", tbl);
if (POINTER_P(tbl)) {
if (tbl->tbl) {
xfree(tbl->tbl);
}
if (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)", tbl);
}
if (VTBL_DEBUG) printf("vtable_add: %p, %s\n", tbl, rb_id2name(id));
if (tbl->pos == tbl->capa) {
tbl->capa = tbl->capa * 2;
REALLOC_N(tbl->tbl, ID, tbl->capa);
GC_WB(&tbl->tbl, tbl->tbl);
}
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 1;
}
}
}
return 0;
}
#ifndef RIPPER
static ID* vtable_tblcpy(ID *buf, const struct vtable *src);
#endif
/*
Structure of Lexer Buffer:
lex_pbeg tokp lex_p lex_pend
| | | |
|-----------+--------------+------------|
|<------------>|
token
*/
struct parser_params {
int is_ripper;
NODE *heap;
union tmpyystype *parser_yylval; /* YYSTYPE not defined yet */
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;
#if WITH_OBJC
int parser_in_def_named_args;
char parser_named_mid[1024];
#endif
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;
int 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;
int nerr;
#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
};
#if WITH_OBJC
// TODO: we should probably mimic what 1.9 does here and use the right/given
// encoding instead of always UTF8.
# define UTF8_ENC() (NULL)
# define __new_tmp_str(p, n) (rb_str_new(p, n))
# define STR_NEW(p,n) __new_tmp_str(p, n)
# define STR_NEW0() __new_tmp_str(0, 0)
# define STR_NEW2(p) __new_tmp_str(p, strlen(p))
# define STR_NEW3(p,n,e,func) __new_tmp_str(p, n)
#else
# 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_usascii_str_new(0,0)
# 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)
#endif
#if WITH_OBJC
# define STR_ENC(m) (NULL)
# define ENC_SINGLE(cr) (1)
#else
# define STR_ENC(m) ((m)?parser->enc:rb_usascii_encoding())
# define ENC_SINGLE(cr) ((cr)==ENC_CODERANGE_7BIT)
#endif
#define TOK_INTERN(mb) rb_intern3(tok(), toklen(), STR_ENC(mb))
#ifdef YYMALLOC
void *rb_parser_malloc(struct parser_params *, size_t);
void *rb_parser_realloc(struct parser_params *, void *, size_t);
void *rb_parser_calloc(struct parser_params *, size_t, size_t);
void rb_parser_free(struct parser_params *, void *);
#endif
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)
#if WITH_OBJC
# define in_def_named_args (parser->parser_in_def_named_args)
# define named_mid (parser->parser_named_mid)
#endif
#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 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)
#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)
#if WITH_OBJC
#ifndef RIPPER
static void named_arg_gen(struct parser_params*, ID, int);
# define named_arg(id, flag) named_arg_gen(parser, id, flag)
#else
# define named_arg(id, flag)
#endif /* !RIPPER */
#ifndef RIPPER
static NODE *process_named_args_gen(struct parser_params*, NODE*);
# define process_named_args(node) process_named_args_gen(parser, node)
static NODE *unprocess_named_args_gen(struct parser_params*, NODE*);
# define unprocess_named_args(node) unprocess_named_args_gen(parser, node)
#endif /* !RIPPER */
#endif
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);
#define dvar_defined(id) dvar_defined_gen(parser, id)
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);
#define arg_new() dispatch0(args_new)
#define arg_add(l,a) dispatch2(args_add, l, a)
#define arg_prepend(l,a) dispatch2(args_prepend, 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);
//static void ripper_warnS(struct parser_params*, const char*, const char*);
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 compile_error parser->nerr++,rb_compile_error
# define PARSER_ARG ruby_sourcefile, ruby_sourceline,
#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
%}
%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 call_args2 opt_call_args
%type <node> open_args paren_args opt_paren_args
%type <node> command_args aref_args opt_block_arg block_arg var_ref var_lhs
%type <node> 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
%token tLAST_TOKEN
%%
program : {
lex_state = EXPR_BEG;
/*%%%*/
local_push(compile_for_eval);
/*%
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);
}
}
GC_WB(&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);
%*/
}
| lhs '=' command_call
{
/*%%%*/
value_expr($3);
$$ = node_assign($1, $3);
/*%
$$ = dispatch2(assign, $1, $3);
%*/
}
| mlhs '=' command_call
{
/*%%%*/
value_expr($3);
GC_WB(&$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) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
GC_WB(&$$->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($6, $3);
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
{
/*%%%*/
GC_WB(&$1->nd_value, $3);
$$ = $1;
/*%
$$ = dispatch2(massign, $1, $3);
%*/
}
| mlhs '=' mrhs
{
/*%%%*/
GC_WB(&$1->nd_value, $3);
$$ = $1;
/*%
$$ = dispatch2(massign, $1, $3);
%*/
}
| expr
;
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);
GC_WB(&$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);
GC_WB(&$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);
GC_WB(&$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) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
GC_WB(&$$->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) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_OR(gettable(vid), $1);
if (is_asgn_or_id(vid)) {
$$->nd_aid = vid;
}
}
else if ($2 == tANDOP) {
GC_WB(&$1->nd_value, $3);
$$ = NEW_OP_ASGN_AND(gettable(vid), $1);
}
else {
$$ = $1;
GC_WB(&$$->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();
args = arg_concat($6, $3);
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
{
/*%%%*/
#if WITH_OBJC
if ($1->nd_alen == 1) {
NODE *n;
unsigned all_symbol_pairs = 1;
for (n = $3; n != NULL;
n = n->nd_next->nd_next) {
if (nd_type(n->nd_head) != NODE_LIT
|| TYPE(n->nd_head->nd_head) != T_SYMBOL) {
all_symbol_pairs = 0;
break;
}
}
if (all_symbol_pairs) {
$$ = arg_append($1, $3);
$3->flags |= NODE_ARRAY_NAMED_ARGS;
}
else {
$$ = arg_append($1, NEW_HASH($3));
}
}
else
#endif
$$ = arg_append($1, NEW_HASH($3));
$$ = arg_blk_pass($$, $4);
/*%
$$ = arg_add_optblock(arg_add_assocs($1, $3), $4);
%*/
}
| args ',' assocs ',' args opt_block_arg
{
/*%%%*/
#if WITH_OBJC
NODE *n;
unsigned all_symbol_pairs = 1;
if ($1->nd_alen != 1)
yyerror("invalid use of named arguments in method call");
for (n = $3; n != NULL;
n = n->nd_next->nd_next) {
if (nd_type(n->nd_head) != NODE_LIT
|| TYPE(n->nd_head->nd_head) != T_SYMBOL) {
all_symbol_pairs = 0;
break;
}
}
if (!all_symbol_pairs)
yyerror("invalid use of named arguments in method call");
$$ = arg_append($1, $3);
$3->flags |= NODE_ARRAY_NAMED_ARGS;
$$ = arg_append($1, $5);
$$ = arg_blk_pass($$, $6);
/*%
$$ = arg_add_optblock(arg_add_star(arg_add_assocs($1, $3), $5), $6);
%*/
#endif
}
| block_arg
/*%c%*/
/*%c
{
$$ = arg_add_block(arg_new(), $1);
}
%*/
;
call_args2 : arg_value ',' args opt_block_arg
{
/*%%%*/
$$ = arg_blk_pass(list_concat(NEW_LIST($1),$3), $4);
/*%
$$ = arg_add_optblock(arg_prepend($3, $1), $4);
%*/
}
| arg_value ',' block_arg
{
/*%%%*/
$$ = arg_blk_pass(NEW_LIST($1), $3);
/*%
$$ = arg_add_block(arg_add(arg_new(), $1), $3);
%*/
}
| assocs opt_block_arg
{
/*%%%*/
$$ = NEW_LIST(NEW_HASH($1));
$$ = arg_blk_pass($$, $2);
/*%
$$ = arg_add_assocs(arg_new(), $1);
$$ = arg_add_optblock($$, $2);
%*/
}
| arg_value ',' assocs opt_block_arg
{
/*%%%*/
$$ = arg_append(NEW_LIST($1), NEW_HASH($3));
$$ = arg_blk_pass($$, $4);
/*%
$$ = arg_add_assocs(arg_add(arg_new(), $1), $3);
$$ = arg_add_optblock($$, $4);
%*/
}
| arg_value ',' args ',' assocs opt_block_arg
{
/*%%%*/
$$ = arg_append(list_concat(NEW_LIST($1),$3), NEW_HASH($5));
$$ = arg_blk_pass($$, $6);
/*%
$$ = arg_add_assocs(arg_prepend($3, $1), $5);
$$ = arg_add_optblock($$, $6);
%*/
}
| block_arg
;
command_args : {
$<val>$ = cmdarg_stack;
CMDARG_PUSH(1);
}
open_args
{
/* CMDARG_POP() */
cmdarg_stack = $<val>1;
$$ = $2;
}
;
open_args : call_args
| tLPAREN_ARG {lex_state = EXPR_ENDARG;} rparen
{
/*%%%*/
rb_warning0("don't put space before argument parentheses");
$$ = 0;
/*%
$$ = dispatch1(space, dispatch1(arg_paren, arg_new()));
%*/
}
| tLPAREN_ARG call_args2 {lex_state = EXPR_ENDARG;} rparen
{
/*%%%*/
rb_warning0("don't put space before argument parentheses");
$$ = $2;
/*%
$$ = dispatch1(space, dispatch1(arg_paren, $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());
%*/
}
| keyword_begin
{
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
keyword_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 tLPAREN expr rparen
{
/*%%%*/
$$ = call_uni_op(cond($3), '!');
/*%
$$ = dispatch2(unary, ripper_intern("not"), $3);
%*/
}
| keyword_not tLPAREN rparen
{
/*%%%*/
$$ = call_uni_op(cond(NEW_NIL()), '!');
/*%
$$ = dispatch2(unary, ripper_intern("not"), Qnil);
%*/
}
| operation brace_block
{
/*%%%*/
GC_WB(&$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);
GC_WB(&$2->nd_iter, $1);
$$ = $2;
fixpos($$, $1);
/*%
$$ = method_add_block($1, $2);
%*/
}
| tLAMBDA lambda
{
$$ = $2;
}
| keyword_if expr_value then
compstmt
if_tail
keyword_end
{
/*%%%*/
$$ = NEW_IF(cond($2), $4, $5);
fixpos($$, $2);
/*%
$$ = dispatch3(if, $2, $4, escape_Qundef($5));
%*/
}
| keyword_unless expr_value then
compstmt
opt_else
keyword_end
{
/*%%%*/
$$ = NEW_UNLESS(cond($2), $4, $5);
fixpos($$, $2);
/*%
$$ = dispatch3(unless, $2, $4, escape_Qundef($5));
%*/
}
| keyword_while {COND_PUSH(1);} expr_value do {COND_POP();}
compstmt
keyword_end
{
/*%%%*/
$$ = NEW_WHILE(cond($3), $6, 1);
fixpos($$, $3);
/*%
$$ = dispatch2(while, $3, $6);
%*/
}
| keyword_until {COND_PUSH(1);} expr_value do {COND_POP();}
compstmt
keyword_end
{
/*%%%*/
$$ = NEW_UNTIL(cond($3), $6, 1);
fixpos($$, $3);
/*%
$$ = dispatch2(until, $3, $6);
%*/
}
| keyword_case expr_value opt_terms
case_body
keyword_end
{
/*%%%*/
$$ = NEW_CASE($2, $4);
fixpos($$, $2);
/*%
$$ = dispatch2(case, $2, $4);
%*/
}
| keyword_case opt_terms case_body keyword_end
{
/*%%%*/
$$ = NEW_CASE(0, $3);
/*%
$$ = dispatch2(case, Qnil, $3);
%*/
}
| keyword_for for_var keyword_in
{COND_PUSH(1);}
expr_value do
{COND_POP();}
compstmt
keyword_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;
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)));
GC_WB(&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) {
GC_WB(&$2->nd_value, NEW_DVAR(id));
m->nd_plen = 1;
GC_WB(&m->nd_next, $2);
args = new_args(m, 0, 0, 0, 0);
}
else {
GC_WB(&m->nd_next, node_assign(NEW_MASGN(NEW_LIST($2), 0), NEW_DVAR(id)));
args = new_args(m, 0, id, 0, 0);
}
}
int args_count = vtable_size(lvtbl->args);
int vars_count = vtable_size(lvtbl->vars);
tbl = ALLOC_N(ID, args_count + vars_count + 3);
tbl[0] = 1; tbl[1] = id;
tbl[2] = args_count + vars_count;
vtable_tblcpy(tbl+3, lvtbl->args);
vtable_tblcpy(tbl+3+args_count, lvtbl->vars);
scope = NEW_NODE(NODE_SCOPE, tbl, $8, args);
$$ = NEW_FOR(0, $5, scope);
fixpos($$, $2);
/*%
$$ = dispatch3(for, $2, $5, $8);
%*/
}
| keyword_class cpath superclass
{
if (in_def || in_single)
yyerror("class definition in method body");
local_push(0);
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
keyword_end
{
/*%%%*/
$$ = NEW_CLASS($2, $5, $3);
nd_set_line($$, $<num>4);
/*%
$$ = dispatch3(class, $2, $3, $5);
%*/
local_pop();
}
| keyword_class tLSHFT expr
{
$<num>$ = in_def;
in_def = 0;
}
term
{
$<num>$ = in_single;
in_single = 0;
local_push(0);
}
bodystmt
keyword_end
{
/*%%%*/
$$ = NEW_SCLASS($3, $7);
fixpos($$, $3);
/*%
$$ = dispatch2(sclass, $3, $7);
%*/
local_pop();
in_def = $<num>4;
in_single = $<num>6;
}
| keyword_module cpath
{
if (in_def || in_single)
yyerror("module definition in method body");
local_push(0);
/*%%%*/
$<num>$ = ruby_sourceline;
/*%
%*/
}
bodystmt
keyword_end
{
/*%%%*/
$$ = NEW_MODULE($2, $4);
nd_set_line($$, $<num>3);
/*%
$$ = dispatch2(module, $2, $4);
%*/
local_pop();
}
| keyword_def fname
{
$<id>$ = cur_mid;
cur_mid = $2;
in_def++;
#if WITH_OBJC
in_def_named_args = 0;
named_arg($2, 1);
#endif
local_push(0);
}
f_arglist
bodystmt
keyword_end
{
/*%%%*/
NODE *body = remove_begin($5);
ID mid = $2;
reduce_nodes(&body);
#if WITH_OBJC
if (in_def_named_args > 0)
mid = rb_intern(named_mid);
#endif
$$ = NEW_DEFN(mid, $4, body, NOEX_PRIVATE);
fixpos($$, $4);
#if WITH_OBJC
if (in_def_named_args > 0
&& in_def_named_args
!= $$->nd_defn->nd_args->nd_frml - 1) {
yyerror("invalid use of named arguments in " \
"method definition");
}
in_def_named_args = 0;
#endif
/*%
$$ = dispatch3(def, $2, $4, $5);
%*/
local_pop();
in_def--;
cur_mid = $<id>3;
}
| keyword_def singleton dot_or_colon {lex_state = EXPR_FNAME;} fname
{
in_single++;
lex_state = EXPR_ENDFN; /* force for args */
#if WITH_OBJC
in_def_named_args = 0;
named_arg($5, 1);
#endif
local_push(0);
}
f_arglist
bodystmt
keyword_end
{
/*%%%*/
NODE *body = remove_begin($8);
ID mid = $5;
reduce_nodes(&body);
#if WITH_OBJC
if (in_def_named_args > 0)
mid = rb_intern(named_mid);
#endif
$$ = NEW_DEFS($2, mid, $7, body);
fixpos($$, $2);
#if WITH_OBJC
if (in_def_named_args > 0
&& in_def_named_args
!= $$->nd_defn->nd_args->nd_frml - 1) {
yyerror("invalid use of named arguments in " \
"method definition");
}
in_def_named_args = 0;
#endif
/*%
$$ = 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;
%*/
}
;
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);
}
GC_WB(&$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, STR_NEW0(), 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);
GC_WB(&node->nd_lit, GC_RETAIN(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);
GC_WB(&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);
GC_WB(&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
{
/*%%%*/
GC_WB(&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;
GC_WB(&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, STR_NEW0(), 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;
}
| tIDENTIFIER tASSOC tIDENTIFIER
{
#if WITH_OBJC
named_arg($1, 0);
#endif
$$ = $3;
}
| tLABEL tIDENTIFIER
{
#if WITH_OBJC
named_arg($1, 0);
#endif
$$ = $2;
}
;
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()) {
GC_WB(&$2->nd_value, NEW_DVAR(tid));
}
else {
GC_WB(&$2->nd_value, NEW_LVAR(tid));
}
$$ = NEW_ARGS_AUX(tid, 1);
GC_WB(&$$->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++;
GC_WB(&$$->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;
}
GC_WB(&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:
case NODE_XSTR:
case NODE_DXSTR:
case NODE_DREGX:
case NODE_LIT:
case NODE_ARRAY:
case NODE_ZARRAY:
yyerror("can't define singleton method for literals");
default:
value_expr($3);
break;
}
}
$$ = $3;
/*%
$$ = dispatch1(paren, $3);
%*/
}
;
assoc_list : none
| assocs trailer
{
/*%%%*/
$$ = $1;
/*%
$$ = dispatch1(assoclist_from_args, $1);
%*/
}
;
assocs : assoc
/*%c%*/
/*%c
{
$$ = rb_ary_new3(1, $1);
}
%*/
| assocs ',' assoc
{
/*%%%*/
$$ = list_concat($1, $3);
/*%
$$ = rb_ary_push($1, $3);
%*/
}
;
assoc : arg_value tASSOC arg_value
{
/*%%%*/
$$ = list_append(NEW_LIST($1), $3);
/*%
$$ = dispatch2(assoc_new, $1, $3);
%*/
}
| tLABEL arg_value
{
/*%%%*/
$$ = list_append(NEW_LIST(NEW_LIT(ID2SYM($1))), $2);
/*%
$$ = dispatch2(assoc_new, $1, $2);
%*/
}
;
operation : tIDENTIFIER
| tCONSTANT
| tFID
;
operation2 : tIDENTIFIER
| tCONSTANT
| tFID
| op
;
operation3 : tIDENTIFIER
| tFID
| op
;
dot_or_colon : '.'
/*%c%*/
/*%c
{ $$ = $<val>1; }
%*/
| tCOLON2
/*%c%*/
/*%c
{ $$ = $<val>1; }
%*/
;
opt_terms : /* none */
| terms
;
opt_nl : /* none */
| '\n'
;
rparen : opt_nl ')'
;
rbracket : opt_nl ']'
;
trailer : /* none */
| '\n'
| ','
;
term : ';' {yyerrok;}
| '\n'
;
terms : term
| terms ';' {yyerrok;}
;
none : /* none */
{
/*%%%*/
$$ = 0;
/*%
$$ = Qundef;
%*/
}
;
%%
# undef parser
# undef yylex
# undef yylval
# define yylval (*((YYSTYPE*)(parser->parser_yylval)))
static int parser_regx_options(struct parser_params*);
static int parser_tokadd_string(struct parser_params*,int,int,int,long*,rb_encoding**);
static void parser_tokaddmbc(struct parser_params *parser, int c, rb_encoding *enc);
static int parser_parse_string(struct parser_params*,NODE*);
static int parser_here_document(struct parser_params*,NODE*);
# define nextc() parser_nextc(parser)
# define pushback(c) parser_pushback(parser, c)
# define newtok() parser_newtok(parser)
# define tokspace(n) parser_tokspace(parser, n)
# define tokadd(c) parser_tokadd(parser, c)
# define tok_hex(numlen) parser_tok_hex(parser, numlen)
# define read_escape(flags,e) parser_read_escape(parser, flags, e)
# define tokadd_escape(e) parser_tokadd_escape(parser, e)
# define regx_options() parser_regx_options(parser)
# define tokadd_string(f,t,p,n,e) parser_tokadd_string(parser,f,t,p,n,e)
# define parse_string(n) parser_parse_string(parser,n)
# define tokaddmbc(c, enc) parser_tokaddmbc(parser, c, enc)
# define here_document(n) parser_here_document(parser,n)
# define heredoc_identifier() parser_heredoc_identifier(parser)
# define heredoc_restore(n) parser_heredoc_restore(parser,n)
# define whole_match_p(e,l,i) parser_whole_match_p(parser,e,l,i)
#ifndef RIPPER
# define set_yylval_str(x) yylval.node = NEW_STR(x)
# define set_yylval_num(x) yylval.num = x
# define set_yylval_id(x) yylval.id = x
# define set_yylval_name(x) yylval.id = x
# define set_yylval_literal(x) yylval.node = NEW_LIT(x)
# define set_yylval_node(x) yylval.node = x
# define yylval_id() yylval.id
#else
static inline VALUE
ripper_yylval_id(ID x)
{
return (VALUE)NEW_LASGN(x, ID2SYM(x));
}
# define set_yylval_str(x) (void)(x)
# define set_yylval_num(x) (void)(x)
# define set_yylval_id(x) (void)(x)
# define set_yylval_name(x) (void)(yylval.val = ripper_yylval_id(x))
# define set_yylval_literal(x) (void)(x)
# define set_yylval_node(x) (void)(x)
# define yylval_id() yylval.id
#endif
#ifndef RIPPER
#define ripper_flush(p) (void)(p)
#else
#define ripper_flush(p) (p->tokp = p->parser_lex_p)
#if !WITH_OBJC
#define yylval_rval *(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)
#endif
static int
ripper_has_scan_event(struct parser_params *parser)
{
if (lex_p < parser->tokp) rb_raise(rb_eRuntimeError, "lex_p < tokp");
return lex_p > parser->tokp;
}
static VALUE
ripper_scan_event_val(struct parser_params *parser, int t)
{
VALUE str = STR_NEW(parser->tokp, lex_p - parser->tokp);
VALUE rval = ripper_dispatch1(parser, ripper_token2eventid(t), str);
ripper_flush(parser);
return rval;
}
static void
ripper_dispatch_scan_event(struct parser_params *parser, int t)
{
if (!ripper_has_scan_event(parser)) return;
#if WITH_OBJC
yylval.val = ripper_scan_event_val(parser, t);
#else
yylval_rval = ripper_scan_event_val(parser, t);
#endif
}
static void
ripper_dispatch_ignored_scan_event(struct parser_params *parser, int t)
{
if (!ripper_has_scan_event(parser)) return;
(void)ripper_scan_event_val(parser, t);
}
static void
ripper_dispatch_delayed_token(struct parser_params *parser, int t)
{
int saved_line = ruby_sourceline;
const char *saved_tokp = parser->tokp;
ruby_sourceline = parser->delayed_line;
parser->tokp = lex_pbeg + parser->delayed_col;
#if WITH_OBJC
yylval.val = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
#else
yylval_rval = ripper_dispatch1(parser, ripper_token2eventid(t), parser->delayed);
#endif
parser->delayed = Qnil;
ruby_sourceline = saved_line;
parser->tokp = saved_tokp;
}
#endif /* RIPPER */
#include "ruby/util.h"
/* We remove any previous definition of `SIGN_EXTEND_CHAR',
since ours (we hope) works properly with all combinations of
machines, compilers, `char' and `unsigned char' argument types.
(Per Bothner suggested the basic approach.) */
#undef SIGN_EXTEND_CHAR
#if __STDC__
# define SIGN_EXTEND_CHAR(c) ((signed char)(c))
#else /* not __STDC__ */
/* As in Harbison and Steele. */
# define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
#endif
#define is_identchar(p,e,enc) (rb_enc_isalnum(*p,enc) || (*p) == '_' || !ISASCII(*p))
#define parser_is_identchar() (!parser->eofp && is_identchar((lex_p-1),lex_pend,parser->enc))
#define parser_isascii() ISASCII(*(lex_p-1))
static int
parser_yyerror(struct parser_params *parser, const char *msg)
{
#ifndef RIPPER
const int max_line_margin = 30;
const char *p, *pe;
char *buf;
long len;
int i;
compile_error(PARSER_ARG "%s", msg);
p = lex_p;
while (lex_pbeg <= p) {
if (*p == '\n') break;
p--;
}
p++;
pe = lex_p;
while (pe < lex_pend) {
if (*pe == '\n') break;
pe++;
}
len = pe - p;
if (len > 4) {
char *p2;
const char *pre = "", *post = "";
if (len > max_line_margin * 2 + 10) {
if (lex_p - p > max_line_margin) {
#if WITH_OBJC
p = lex_p - max_line_margin;
#else
p = rb_enc_prev_char(p, lex_p - max_line_margin, rb_enc_get(lex_lastline));
#endif
pre = "...";
}
if (pe - lex_p > max_line_margin) {
#if WITH_OBJC
pe = lex_p + max_line_margin;
#else
pe = rb_enc_prev_char(lex_p, lex_p + max_line_margin, rb_enc_get(lex_lastline));
#endif
post = "...";
}
len = pe - p;
}
buf = ALLOCA_N(char, len+2);
MEMCPY(buf, p, char, len);
buf[len] = '\0';
rb_compile_error_append("%s%s%s", pre, buf, post);
i = (int)(lex_p - p);
p2 = buf; pe = buf + len;
while (p2 < pe) {
if (*p2 != '\t') *p2 = ' ';
p2++;
}
buf[i] = '^';
buf[i+1] = '\0';
rb_compile_error_append("%s%s", pre, buf);
}
#else
dispatch1(parse_error, STR_NEW2(msg));
#endif /* !RIPPER */
return 0;
}
static void parser_prepare(struct parser_params *parser);
#ifndef RIPPER
//VALUE ruby_suppress_tracing(VALUE (*func)(VALUE, int), VALUE arg, int always);
static VALUE
debug_lines(const char *f)
{
static ID script_lines;
if (!script_lines) script_lines = rb_intern("SCRIPT_LINES__");
if (rb_const_defined_at(rb_cObject, script_lines)) {
VALUE hash = rb_const_get_at(rb_cObject, script_lines);
if (TYPE(hash) == T_HASH) {
VALUE fname = rb_str_new2(f);
VALUE lines = rb_ary_new();
rb_hash_aset(hash, fname, lines);
return lines;
}
}
return 0;
}
static VALUE
yycompile0(VALUE arg, int tracing)
{
int n;
NODE *tree;
struct parser_params *parser = (struct parser_params *)arg;
if (!compile_for_eval && rb_safe_level() == 0) {
ruby_debug_lines = debug_lines(ruby_sourcefile);
if (ruby_debug_lines && ruby_sourceline > 0) {
VALUE str = STR_NEW0();
n = ruby_sourceline;
do {
rb_ary_push(ruby_debug_lines, str);
} while (--n);
}
}
parser_prepare(parser);
deferred_nodes = 0;
n = yyparse((void*)parser);
ruby_debug_lines = 0;
compile_for_eval = 0;
lex_strterm = 0;
lex_p = lex_pbeg = lex_pend = 0;
lex_lastline = lex_nextline = 0;
if (parser->nerr) {
return 0;
}
tree = ruby_eval_tree;
if (!tree) {
tree = NEW_NIL();
}
else if (ruby_eval_tree_begin) {
GC_WB(&tree->nd_body, NEW_PRELUDE(ruby_eval_tree_begin, tree->nd_body));
}
return (VALUE)tree;
}
static NODE*
yycompile(struct parser_params *parser, const char *f, int line)
{
GC_WB(&ruby_sourcefile, ruby_strdup(f));
ruby_sourceline = line - 1;
return (NODE *)yycompile0((VALUE)parser, Qtrue);
// return (NODE *)ruby_suppress_tracing(yycompile0, (VALUE)parser, Qtrue);
}
#endif /* !RIPPER */
struct lex_get_str_context {
VALUE str;
UChar *chars;
long chars_len;
};
static VALUE
lex_get_str(struct parser_params *parser, VALUE s)
{
struct lex_get_str_context *ctx = (struct lex_get_str_context *)s;
long beg = 0;
if (lex_gets_ptr > 0) {
if (ctx->chars_len == lex_gets_ptr) {
return Qnil;
}
beg += lex_gets_ptr;
}
lex_gets_ptr = ctx->chars_len;
for (long i = beg; i < ctx->chars_len; i++) {
if (ctx->chars[i] == '\n') {
lex_gets_ptr = i + 1;
break;
}
}
return rb_unicode_str_new(&ctx->chars[beg], lex_gets_ptr - beg);
}
static VALUE
lex_getline(struct parser_params *parser)
{
VALUE line = (*parser->parser_lex_gets)(parser, parser->parser_lex_input);
#ifndef RIPPER
if (ruby_debug_lines && !NIL_P(line)) {
rb_ary_push(ruby_debug_lines, line);
}
#endif
return line;
}
#ifndef RIPPER
NODE*
rb_compile_string(const char *f, VALUE s, int line)
{
return rb_parser_compile_string(rb_parser_new(), f, s, line);
}
NODE *
rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s,
int line)
{
struct parser_params *parser;
Data_Get_Struct(vparser, struct parser_params, parser);
long chars_len = 0;
UChar *chars = rb_str_xcopy_uchars(s, &chars_len);
struct lex_get_str_context *ctx = (struct lex_get_str_context *)
xmalloc(sizeof(struct lex_get_str_context));
GC_WB(&ctx->str, s);
GC_WB(&ctx->chars, chars);
ctx->chars_len = chars_len;
lex_gets = lex_get_str;
lex_gets_ptr = 0;
GC_WB(&lex_input, ctx);
lex_pbeg = lex_p = lex_pend = 0;
compile_for_eval = rb_parse_in_eval();
return yycompile(parser, f, line);
}
NODE*