| 1 |
/* |
| 2 |
** parse.y - mruby parser |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
%{ |
| 8 |
#undef PARSER_DEBUG |
| 9 |
#ifdef PARSER_DEBUG |
| 10 |
# define YYDEBUG 1 |
| 11 |
#endif |
| 12 |
#define YYSTACK_USE_ALLOCA 1 |
| 13 |
|
| 14 |
#include <ctype.h> |
| 15 |
#include <string.h> |
| 16 |
#include <mruby.h> |
| 17 |
#include <mruby/compile.h> |
| 18 |
#include <mruby/proc.h> |
| 19 |
#include <mruby/error.h> |
| 20 |
#include <mruby/throw.h> |
| 21 |
#include <mruby/string.h> |
| 22 |
#include <mruby/dump.h> |
| 23 |
#include <mruby/internal.h> |
| 24 |
#include <mruby/presym.h> |
| 25 |
#include "node.h" |
| 26 |
|
| 27 |
#define YYLEX_PARAM p |
| 28 |
|
| 29 |
typedef mrb_ast_node node; |
| 30 |
typedef struct mrb_parser_state parser_state; |
| 31 |
typedef struct mrb_parser_heredoc_info parser_heredoc_info; |
| 32 |
|
| 33 |
static int yyparse(parser_state *p); |
| 34 |
static int yylex(void *lval, parser_state *p); |
| 35 |
static void yyerror(parser_state *p, const char *s); |
| 36 |
static void yywarning(parser_state *p, const char *s); |
| 37 |
static void backref_error(parser_state *p, node *n); |
| 38 |
static void void_expr_error(parser_state *p, node *n); |
| 39 |
static void tokadd(parser_state *p, int32_t c); |
| 40 |
|
| 41 |
#define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c)) |
| 42 |
|
| 43 |
typedef unsigned int stack_type; |
| 44 |
|
| 45 |
#define BITSTACK_PUSH(stack, n) ((stack) = ((stack)<<1)|((n)&1)) |
| 46 |
#define BITSTACK_POP(stack) ((stack) = (stack) >> 1) |
| 47 |
#define BITSTACK_LEXPOP(stack) ((stack) = ((stack) >> 1) | ((stack) & 1)) |
| 48 |
#define BITSTACK_SET_P(stack) ((stack)&1) |
| 49 |
|
| 50 |
#define COND_PUSH(n) BITSTACK_PUSH(p->cond_stack, (n)) |
| 51 |
#define COND_POP() BITSTACK_POP(p->cond_stack) |
| 52 |
#define COND_LEXPOP() BITSTACK_LEXPOP(p->cond_stack) |
| 53 |
#define COND_P() BITSTACK_SET_P(p->cond_stack) |
| 54 |
|
| 55 |
#define CMDARG_PUSH(n) BITSTACK_PUSH(p->cmdarg_stack, (n)) |
| 56 |
#define CMDARG_POP() BITSTACK_POP(p->cmdarg_stack) |
| 57 |
#define CMDARG_LEXPOP() BITSTACK_LEXPOP(p->cmdarg_stack) |
| 58 |
#define CMDARG_P() BITSTACK_SET_P(p->cmdarg_stack) |
| 59 |
|
| 60 |
#define SET_LINENO(c,n) ((c)->lineno = (n)) |
| 61 |
#define NODE_LINENO(c,n) do {\ |
| 62 |
if (n) {\ |
| 63 |
(c)->filename_index = (n)->filename_index;\ |
| 64 |
(c)->lineno = (n)->lineno;\ |
| 65 |
}\ |
| 66 |
} while (0) |
| 67 |
|
| 68 |
#define sym(x) ((mrb_sym)(intptr_t)(x)) |
| 69 |
#define nsym(x) ((node*)(intptr_t)(x)) |
| 70 |
#define nint(x) ((node*)(intptr_t)(x)) |
| 71 |
#define intn(x) ((int)(intptr_t)(x)) |
| 72 |
#define typen(x) ((enum node_type)(intptr_t)(x)) |
| 73 |
|
| 74 |
#define NUM_SUFFIX_R (1<<0) |
| 75 |
#define NUM_SUFFIX_I (1<<1) |
| 76 |
|
| 77 |
static inline mrb_sym |
| 78 |
intern_cstr_gen(parser_state *p, const char *s) |
| 79 |
{ |
| 80 |
return mrb_intern_cstr(p->mrb, s); |
| 81 |
} |
| 82 |
#define intern_cstr(s) intern_cstr_gen(p,(s)) |
| 83 |
|
| 84 |
static inline mrb_sym |
| 85 |
intern_gen(parser_state *p, const char *s, size_t len) |
| 86 |
{ |
| 87 |
return mrb_intern(p->mrb, s, len); |
| 88 |
} |
| 89 |
#define intern(s,len) intern_gen(p,(s),(len)) |
| 90 |
|
| 91 |
#define intern_op(op) MRB_OPSYM_2(p->mrb, op) |
| 92 |
|
| 93 |
static mrb_sym |
| 94 |
intern_numparam_gen(parser_state *p, int num) |
| 95 |
{ |
| 96 |
char buf[3]; |
| 97 |
buf[0] = '_'; buf[1] = '0'+num; buf[2] = '\0'; |
| 98 |
return intern(buf, 2); |
| 99 |
} |
| 100 |
#define intern_numparam(n) intern_numparam_gen(p,(n)) |
| 101 |
|
| 102 |
static void |
| 103 |
cons_free_gen(parser_state *p, node *cons) |
| 104 |
{ |
| 105 |
cons->cdr = p->cells; |
| 106 |
p->cells = cons; |
| 107 |
} |
| 108 |
#define cons_free(c) cons_free_gen(p, (c)) |
| 109 |
|
| 110 |
static void* |
| 111 |
parser_palloc(parser_state *p, size_t size) |
| 112 |
{ |
| 113 |
void *m = mrb_pool_alloc(p->pool, size); |
| 114 |
|
| 115 |
if (!m) { |
| 116 |
MRB_THROW(p->mrb->jmp); |
| 117 |
} |
| 118 |
return m; |
| 119 |
} |
| 120 |
|
| 121 |
#define parser_pfree(ptr) do { if (sizeof(node) <= sizeof(*(ptr))) cons_free((node*)ptr);} while (0) |
| 122 |
|
| 123 |
static node* |
| 124 |
cons_gen(parser_state *p, node *car, node *cdr) |
| 125 |
{ |
| 126 |
node *c; |
| 127 |
|
| 128 |
if (p->cells) { |
| 129 |
c = p->cells; |
| 130 |
p->cells = p->cells->cdr; |
| 131 |
} |
| 132 |
else { |
| 133 |
c = (node*)parser_palloc(p, sizeof(mrb_ast_node)); |
| 134 |
} |
| 135 |
|
| 136 |
c->car = car; |
| 137 |
c->cdr = cdr; |
| 138 |
c->lineno = p->lineno; |
| 139 |
c->filename_index = p->current_filename_index; |
| 140 |
/* beginning of next partial file; need to point the previous file */ |
| 141 |
if (p->lineno == 0 && p->current_filename_index > 0) { |
| 142 |
c->filename_index--; |
| 143 |
} |
| 144 |
return c; |
| 145 |
} |
| 146 |
#define cons(a,b) cons_gen(p,(a),(b)) |
| 147 |
|
| 148 |
static node* |
| 149 |
list1_gen(parser_state *p, node *a) |
| 150 |
{ |
| 151 |
return cons(a, 0); |
| 152 |
} |
| 153 |
#define list1(a) list1_gen(p, (a)) |
| 154 |
|
| 155 |
static node* |
| 156 |
list2_gen(parser_state *p, node *a, node *b) |
| 157 |
{ |
| 158 |
return cons(a, cons(b,0)); |
| 159 |
} |
| 160 |
#define list2(a,b) list2_gen(p, (a),(b)) |
| 161 |
|
| 162 |
static node* |
| 163 |
list3_gen(parser_state *p, node *a, node *b, node *c) |
| 164 |
{ |
| 165 |
return cons(a, cons(b, cons(c,0))); |
| 166 |
} |
| 167 |
#define list3(a,b,c) list3_gen(p, (a),(b),(c)) |
| 168 |
|
| 169 |
static node* |
| 170 |
list4_gen(parser_state *p, node *a, node *b, node *c, node *d) |
| 171 |
{ |
| 172 |
return cons(a, cons(b, cons(c, cons(d, 0)))); |
| 173 |
} |
| 174 |
#define list4(a,b,c,d) list4_gen(p, (a),(b),(c),(d)) |
| 175 |
|
| 176 |
static node* |
| 177 |
list5_gen(parser_state *p, node *a, node *b, node *c, node *d, node *e) |
| 178 |
{ |
| 179 |
return cons(a, cons(b, cons(c, cons(d, cons(e, 0))))); |
| 180 |
} |
| 181 |
#define list5(a,b,c,d,e) list5_gen(p, (a),(b),(c),(d),(e)) |
| 182 |
|
| 183 |
static node* |
| 184 |
list6_gen(parser_state *p, node *a, node *b, node *c, node *d, node *e, node *f) |
| 185 |
{ |
| 186 |
return cons(a, cons(b, cons(c, cons(d, cons(e, cons(f, 0)))))); |
| 187 |
} |
| 188 |
#define list6(a,b,c,d,e,f) list6_gen(p, (a),(b),(c),(d),(e),(f)) |
| 189 |
|
| 190 |
static node* |
| 191 |
append_gen(parser_state *p, node *a, node *b) |
| 192 |
{ |
| 193 |
node *c = a; |
| 194 |
|
| 195 |
if (!a) return b; |
| 196 |
if (!b) return a; |
| 197 |
while (c->cdr) { |
| 198 |
c = c->cdr; |
| 199 |
} |
| 200 |
c->cdr = b; |
| 201 |
return a; |
| 202 |
} |
| 203 |
#define append(a,b) append_gen(p,(a),(b)) |
| 204 |
#define push(a,b) append_gen(p,(a),list1(b)) |
| 205 |
|
| 206 |
static char* |
| 207 |
parser_strndup(parser_state *p, const char *s, size_t len) |
| 208 |
{ |
| 209 |
char *b = (char*)parser_palloc(p, len+1); |
| 210 |
|
| 211 |
memcpy(b, s, len); |
| 212 |
b[len] = '\0'; |
| 213 |
return b; |
| 214 |
} |
| 215 |
#undef strndup |
| 216 |
#define strndup(s,len) parser_strndup(p, s, len) |
| 217 |
|
| 218 |
static char* |
| 219 |
parser_strdup(parser_state *p, const char *s) |
| 220 |
{ |
| 221 |
return parser_strndup(p, s, strlen(s)); |
| 222 |
} |
| 223 |
#undef strdup |
| 224 |
#define strdup(s) parser_strdup(p, s) |
| 225 |
|
| 226 |
static void |
| 227 |
dump_int(uint16_t i, char *s) |
| 228 |
{ |
| 229 |
char *p = s; |
| 230 |
char *t = s; |
| 231 |
|
| 232 |
while (i > 0) { |
| 233 |
*p++ = (i % 10)+'0'; |
| 234 |
i /= 10; |
| 235 |
} |
| 236 |
if (p == s) *p++ = '0'; |
| 237 |
*p = 0; |
| 238 |
p--; /* point the last char */ |
| 239 |
while (t < p) { |
| 240 |
char c = *t; |
| 241 |
*t++ = *p; |
| 242 |
*p-- = c; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/* xxx ----------------------------- */ |
| 247 |
|
| 248 |
static node* |
| 249 |
local_switch(parser_state *p) |
| 250 |
{ |
| 251 |
node *prev = p->locals; |
| 252 |
|
| 253 |
p->locals = cons(0, 0); |
| 254 |
return prev; |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
local_resume(parser_state *p, node *prev) |
| 259 |
{ |
| 260 |
p->locals = prev; |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
local_nest(parser_state *p) |
| 265 |
{ |
| 266 |
p->locals = cons(0, p->locals); |
| 267 |
} |
| 268 |
|
| 269 |
static void |
| 270 |
local_unnest(parser_state *p) |
| 271 |
{ |
| 272 |
if (p->locals) { |
| 273 |
p->locals = p->locals->cdr; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
static mrb_bool |
| 278 |
local_var_p(parser_state *p, mrb_sym sym) |
| 279 |
{ |
| 280 |
const struct RProc *u; |
| 281 |
node *l = p->locals; |
| 282 |
|
| 283 |
while (l) { |
| 284 |
node *n = l->car; |
| 285 |
while (n) { |
| 286 |
if (sym(n->car) == sym) return TRUE; |
| 287 |
n = n->cdr; |
| 288 |
} |
| 289 |
l = l->cdr; |
| 290 |
} |
| 291 |
|
| 292 |
u = p->upper; |
| 293 |
while (u && !MRB_PROC_CFUNC_P(u)) { |
| 294 |
const struct mrb_irep *ir = u->body.irep; |
| 295 |
const mrb_sym *v = ir->lv; |
| 296 |
int i; |
| 297 |
|
| 298 |
if (v) { |
| 299 |
for (i=0; i+1 < ir->nlocals; i++) { |
| 300 |
if (v[i] == sym) return TRUE; |
| 301 |
} |
| 302 |
} |
| 303 |
if (MRB_PROC_SCOPE_P(u)) break; |
| 304 |
u = u->upper; |
| 305 |
} |
| 306 |
return FALSE; |
| 307 |
} |
| 308 |
|
| 309 |
static void |
| 310 |
local_add_f(parser_state *p, mrb_sym sym) |
| 311 |
{ |
| 312 |
if (p->locals) { |
| 313 |
node *n = p->locals->car; |
| 314 |
while (n) { |
| 315 |
if (sym(n->car) == sym) { |
| 316 |
mrb_int len; |
| 317 |
const char* name = mrb_sym_name_len(p->mrb, sym, &len); |
| 318 |
if (len > 0 && name[0] != '_') { |
| 319 |
yyerror(p, "duplicated argument name"); |
| 320 |
return; |
| 321 |
} |
| 322 |
} |
| 323 |
n = n->cdr; |
| 324 |
} |
| 325 |
p->locals->car = push(p->locals->car, nsym(sym)); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
static void |
| 330 |
local_add(parser_state *p, mrb_sym sym) |
| 331 |
{ |
| 332 |
if (!local_var_p(p, sym)) { |
| 333 |
local_add_f(p, sym); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
static void |
| 338 |
local_add_blk(parser_state *p, mrb_sym blk) |
| 339 |
{ |
| 340 |
/* allocate register for block */ |
| 341 |
local_add_f(p, blk ? blk : 0); |
| 342 |
} |
| 343 |
|
| 344 |
static void |
| 345 |
local_add_kw(parser_state *p, mrb_sym kwd) |
| 346 |
{ |
| 347 |
/* allocate register for keywords hash */ |
| 348 |
local_add_f(p, kwd ? kwd : intern_op(pow)); |
| 349 |
} |
| 350 |
|
| 351 |
static node* |
| 352 |
locals_node(parser_state *p) |
| 353 |
{ |
| 354 |
return p->locals ? p->locals->car : NULL; |
| 355 |
} |
| 356 |
|
| 357 |
static void |
| 358 |
nvars_nest(parser_state *p) |
| 359 |
{ |
| 360 |
p->nvars = cons(nint(0), p->nvars); |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
nvars_block(parser_state *p) |
| 365 |
{ |
| 366 |
p->nvars = cons(nint(-2), p->nvars); |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
nvars_unnest(parser_state *p) |
| 371 |
{ |
| 372 |
p->nvars = p->nvars->cdr; |
| 373 |
} |
| 374 |
|
| 375 |
/* (:scope (vars..) (prog...)) */ |
| 376 |
static node* |
| 377 |
new_scope(parser_state *p, node *body) |
| 378 |
{ |
| 379 |
return cons((node*)NODE_SCOPE, cons(locals_node(p), body)); |
| 380 |
} |
| 381 |
|
| 382 |
/* (:begin prog...) */ |
| 383 |
static node* |
| 384 |
new_begin(parser_state *p, node *body) |
| 385 |
{ |
| 386 |
if (body) { |
| 387 |
return list2((node*)NODE_BEGIN, body); |
| 388 |
} |
| 389 |
return cons((node*)NODE_BEGIN, 0); |
| 390 |
} |
| 391 |
|
| 392 |
#define newline_node(n) (n) |
| 393 |
|
| 394 |
/* (:rescue body rescue else) */ |
| 395 |
static node* |
| 396 |
new_rescue(parser_state *p, node *body, node *resq, node *els) |
| 397 |
{ |
| 398 |
return list4((node*)NODE_RESCUE, body, resq, els); |
| 399 |
} |
| 400 |
|
| 401 |
static node* |
| 402 |
new_mod_rescue(parser_state *p, node *body, node *resq) |
| 403 |
{ |
| 404 |
return new_rescue(p, body, list1(list3(0, 0, resq)), 0); |
| 405 |
} |
| 406 |
|
| 407 |
/* (:ensure body ensure) */ |
| 408 |
static node* |
| 409 |
new_ensure(parser_state *p, node *a, node *b) |
| 410 |
{ |
| 411 |
return cons((node*)NODE_ENSURE, cons(a, cons(0, b))); |
| 412 |
} |
| 413 |
|
| 414 |
/* (:nil) */ |
| 415 |
static node* |
| 416 |
new_nil(parser_state *p) |
| 417 |
{ |
| 418 |
return list1((node*)NODE_NIL); |
| 419 |
} |
| 420 |
|
| 421 |
/* (:true) */ |
| 422 |
static node* |
| 423 |
new_true(parser_state *p) |
| 424 |
{ |
| 425 |
return list1((node*)NODE_TRUE); |
| 426 |
} |
| 427 |
|
| 428 |
/* (:false) */ |
| 429 |
static node* |
| 430 |
new_false(parser_state *p) |
| 431 |
{ |
| 432 |
return list1((node*)NODE_FALSE); |
| 433 |
} |
| 434 |
|
| 435 |
/* (:alias new old) */ |
| 436 |
static node* |
| 437 |
new_alias(parser_state *p, mrb_sym a, mrb_sym b) |
| 438 |
{ |
| 439 |
return cons((node*)NODE_ALIAS, cons(nsym(a), nsym(b))); |
| 440 |
} |
| 441 |
|
| 442 |
/* (:if cond then else) */ |
| 443 |
static node* |
| 444 |
new_if(parser_state *p, node *a, node *b, node *c) |
| 445 |
{ |
| 446 |
void_expr_error(p, a); |
| 447 |
return list4((node*)NODE_IF, a, b, c); |
| 448 |
} |
| 449 |
|
| 450 |
/* (:unless cond then else) */ |
| 451 |
static node* |
| 452 |
new_unless(parser_state *p, node *a, node *b, node *c) |
| 453 |
{ |
| 454 |
void_expr_error(p, a); |
| 455 |
return list4((node*)NODE_IF, a, c, b); |
| 456 |
} |
| 457 |
|
| 458 |
/* (:while cond body) */ |
| 459 |
static node* |
| 460 |
new_while(parser_state *p, node *a, node *b) |
| 461 |
{ |
| 462 |
void_expr_error(p, a); |
| 463 |
return cons((node*)NODE_WHILE, cons(a, b)); |
| 464 |
} |
| 465 |
|
| 466 |
/* (:until cond body) */ |
| 467 |
static node* |
| 468 |
new_until(parser_state *p, node *a, node *b) |
| 469 |
{ |
| 470 |
void_expr_error(p, a); |
| 471 |
return cons((node*)NODE_UNTIL, cons(a, b)); |
| 472 |
} |
| 473 |
|
| 474 |
/* (:for var obj body) */ |
| 475 |
static node* |
| 476 |
new_for(parser_state *p, node *v, node *o, node *b) |
| 477 |
{ |
| 478 |
void_expr_error(p, o); |
| 479 |
return list4((node*)NODE_FOR, v, o, b); |
| 480 |
} |
| 481 |
|
| 482 |
/* (:case a ((when ...) body) ((when...) body)) */ |
| 483 |
static node* |
| 484 |
new_case(parser_state *p, node *a, node *b) |
| 485 |
{ |
| 486 |
node *n = list2((node*)NODE_CASE, a); |
| 487 |
node *n2 = n; |
| 488 |
|
| 489 |
void_expr_error(p, a); |
| 490 |
while (n2->cdr) { |
| 491 |
n2 = n2->cdr; |
| 492 |
} |
| 493 |
n2->cdr = b; |
| 494 |
return n; |
| 495 |
} |
| 496 |
|
| 497 |
/* (:postexe a) */ |
| 498 |
static node* |
| 499 |
new_postexe(parser_state *p, node *a) |
| 500 |
{ |
| 501 |
return cons((node*)NODE_POSTEXE, a); |
| 502 |
} |
| 503 |
|
| 504 |
/* (:self) */ |
| 505 |
static node* |
| 506 |
new_self(parser_state *p) |
| 507 |
{ |
| 508 |
return list1((node*)NODE_SELF); |
| 509 |
} |
| 510 |
|
| 511 |
/* (:call a b c) */ |
| 512 |
static node* |
| 513 |
new_call(parser_state *p, node *a, mrb_sym b, node *c, int pass) |
| 514 |
{ |
| 515 |
node *n = list4(nint(pass?NODE_CALL:NODE_SCALL), a, nsym(b), c); |
| 516 |
void_expr_error(p, a); |
| 517 |
NODE_LINENO(n, a); |
| 518 |
return n; |
| 519 |
} |
| 520 |
|
| 521 |
/* (:fcall self mid args) */ |
| 522 |
static node* |
| 523 |
new_fcall(parser_state *p, mrb_sym b, node *c) |
| 524 |
{ |
| 525 |
node *n = list4((node*)NODE_FCALL, 0, nsym(b), c); |
| 526 |
NODE_LINENO(n, c); |
| 527 |
return n; |
| 528 |
} |
| 529 |
|
| 530 |
/* (a b . c) */ |
| 531 |
static node* |
| 532 |
new_callargs(parser_state *p, node *a, node *b, node *c) |
| 533 |
{ |
| 534 |
return cons(a, cons(b, c)); |
| 535 |
} |
| 536 |
|
| 537 |
/* (:super . c) */ |
| 538 |
static node* |
| 539 |
new_super(parser_state *p, node *c) |
| 540 |
{ |
| 541 |
return cons((node*)NODE_SUPER, c); |
| 542 |
} |
| 543 |
|
| 544 |
/* (:zsuper) */ |
| 545 |
static node* |
| 546 |
new_zsuper(parser_state *p) |
| 547 |
{ |
| 548 |
return cons((node*)NODE_ZSUPER, 0); |
| 549 |
} |
| 550 |
|
| 551 |
/* (:yield . c) */ |
| 552 |
static node* |
| 553 |
new_yield(parser_state *p, node *c) |
| 554 |
{ |
| 555 |
if (c) { |
| 556 |
if (c->cdr) { |
| 557 |
if (c->cdr->cdr) { |
| 558 |
yyerror(p, "both block arg and actual block given"); |
| 559 |
} |
| 560 |
if (c->cdr->car) { |
| 561 |
return cons((node*)NODE_YIELD, push(c->car, c->cdr->car)); |
| 562 |
} |
| 563 |
} |
| 564 |
return cons((node*)NODE_YIELD, c->car); |
| 565 |
} |
| 566 |
return cons((node*)NODE_YIELD, 0); |
| 567 |
} |
| 568 |
|
| 569 |
/* (:return . c) */ |
| 570 |
static node* |
| 571 |
new_return(parser_state *p, node *c) |
| 572 |
{ |
| 573 |
return cons((node*)NODE_RETURN, c); |
| 574 |
} |
| 575 |
|
| 576 |
/* (:break . c) */ |
| 577 |
static node* |
| 578 |
new_break(parser_state *p, node *c) |
| 579 |
{ |
| 580 |
return cons((node*)NODE_BREAK, c); |
| 581 |
} |
| 582 |
|
| 583 |
/* (:next . c) */ |
| 584 |
static node* |
| 585 |
new_next(parser_state *p, node *c) |
| 586 |
{ |
| 587 |
return cons((node*)NODE_NEXT, c); |
| 588 |
} |
| 589 |
|
| 590 |
/* (:redo) */ |
| 591 |
static node* |
| 592 |
new_redo(parser_state *p) |
| 593 |
{ |
| 594 |
return list1((node*)NODE_REDO); |
| 595 |
} |
| 596 |
|
| 597 |
/* (:retry) */ |
| 598 |
static node* |
| 599 |
new_retry(parser_state *p) |
| 600 |
{ |
| 601 |
return list1((node*)NODE_RETRY); |
| 602 |
} |
| 603 |
|
| 604 |
/* (:dot2 a b) */ |
| 605 |
static node* |
| 606 |
new_dot2(parser_state *p, node *a, node *b) |
| 607 |
{ |
| 608 |
return cons((node*)NODE_DOT2, cons(a, b)); |
| 609 |
} |
| 610 |
|
| 611 |
/* (:dot3 a b) */ |
| 612 |
static node* |
| 613 |
new_dot3(parser_state *p, node *a, node *b) |
| 614 |
{ |
| 615 |
return cons((node*)NODE_DOT3, cons(a, b)); |
| 616 |
} |
| 617 |
|
| 618 |
/* (:colon2 b c) */ |
| 619 |
static node* |
| 620 |
new_colon2(parser_state *p, node *b, mrb_sym c) |
| 621 |
{ |
| 622 |
void_expr_error(p, b); |
| 623 |
return cons((node*)NODE_COLON2, cons(b, nsym(c))); |
| 624 |
} |
| 625 |
|
| 626 |
/* (:colon3 . c) */ |
| 627 |
static node* |
| 628 |
new_colon3(parser_state *p, mrb_sym c) |
| 629 |
{ |
| 630 |
return cons((node*)NODE_COLON3, nsym(c)); |
| 631 |
} |
| 632 |
|
| 633 |
/* (:and a b) */ |
| 634 |
static node* |
| 635 |
new_and(parser_state *p, node *a, node *b) |
| 636 |
{ |
| 637 |
void_expr_error(p, a); |
| 638 |
return cons((node*)NODE_AND, cons(a, b)); |
| 639 |
} |
| 640 |
|
| 641 |
/* (:or a b) */ |
| 642 |
static node* |
| 643 |
new_or(parser_state *p, node *a, node *b) |
| 644 |
{ |
| 645 |
void_expr_error(p, a); |
| 646 |
return cons((node*)NODE_OR, cons(a, b)); |
| 647 |
} |
| 648 |
|
| 649 |
/* (:array a...) */ |
| 650 |
static node* |
| 651 |
new_array(parser_state *p, node *a) |
| 652 |
{ |
| 653 |
return cons((node*)NODE_ARRAY, a); |
| 654 |
} |
| 655 |
|
| 656 |
/* (:splat . a) */ |
| 657 |
static node* |
| 658 |
new_splat(parser_state *p, node *a) |
| 659 |
{ |
| 660 |
void_expr_error(p, a); |
| 661 |
return cons((node*)NODE_SPLAT, a); |
| 662 |
} |
| 663 |
|
| 664 |
/* (:hash (k . v) (k . v)...) */ |
| 665 |
static node* |
| 666 |
new_hash(parser_state *p, node *a) |
| 667 |
{ |
| 668 |
return cons((node*)NODE_HASH, a); |
| 669 |
} |
| 670 |
|
| 671 |
/* (:kw_hash (k . v) (k . v)...) */ |
| 672 |
static node* |
| 673 |
new_kw_hash(parser_state *p, node *a) |
| 674 |
{ |
| 675 |
return cons((node*)NODE_KW_HASH, a); |
| 676 |
} |
| 677 |
|
| 678 |
/* (:sym . a) */ |
| 679 |
static node* |
| 680 |
new_sym(parser_state *p, mrb_sym sym) |
| 681 |
{ |
| 682 |
return cons((node*)NODE_SYM, nsym(sym)); |
| 683 |
} |
| 684 |
|
| 685 |
static mrb_sym |
| 686 |
new_strsym(parser_state *p, node* str) |
| 687 |
{ |
| 688 |
const char *s = (const char*)str->cdr->car; |
| 689 |
size_t len = (size_t)str->cdr->cdr; |
| 690 |
|
| 691 |
return mrb_intern(p->mrb, s, len); |
| 692 |
} |
| 693 |
|
| 694 |
/* (:lvar . a) */ |
| 695 |
static node* |
| 696 |
new_lvar(parser_state *p, mrb_sym sym) |
| 697 |
{ |
| 698 |
return cons((node*)NODE_LVAR, nsym(sym)); |
| 699 |
} |
| 700 |
|
| 701 |
/* (:gvar . a) */ |
| 702 |
static node* |
| 703 |
new_gvar(parser_state *p, mrb_sym sym) |
| 704 |
{ |
| 705 |
return cons((node*)NODE_GVAR, nsym(sym)); |
| 706 |
} |
| 707 |
|
| 708 |
/* (:ivar . a) */ |
| 709 |
static node* |
| 710 |
new_ivar(parser_state *p, mrb_sym sym) |
| 711 |
{ |
| 712 |
return cons((node*)NODE_IVAR, nsym(sym)); |
| 713 |
} |
| 714 |
|
| 715 |
/* (:cvar . a) */ |
| 716 |
static node* |
| 717 |
new_cvar(parser_state *p, mrb_sym sym) |
| 718 |
{ |
| 719 |
return cons((node*)NODE_CVAR, nsym(sym)); |
| 720 |
} |
| 721 |
|
| 722 |
/* (:nvar . a) */ |
| 723 |
static node* |
| 724 |
new_nvar(parser_state *p, int num) |
| 725 |
{ |
| 726 |
return cons((node*)NODE_NVAR, nint(num)); |
| 727 |
} |
| 728 |
|
| 729 |
/* (:const . a) */ |
| 730 |
static node* |
| 731 |
new_const(parser_state *p, mrb_sym sym) |
| 732 |
{ |
| 733 |
return cons((node*)NODE_CONST, nsym(sym)); |
| 734 |
} |
| 735 |
|
| 736 |
/* (:undef a...) */ |
| 737 |
static node* |
| 738 |
new_undef(parser_state *p, mrb_sym sym) |
| 739 |
{ |
| 740 |
return list2((node*)NODE_UNDEF, nsym(sym)); |
| 741 |
} |
| 742 |
|
| 743 |
/* (:class class super body) */ |
| 744 |
static node* |
| 745 |
new_class(parser_state *p, node *c, node *s, node *b) |
| 746 |
{ |
| 747 |
void_expr_error(p, s); |
| 748 |
return list4((node*)NODE_CLASS, c, s, cons(locals_node(p), b)); |
| 749 |
} |
| 750 |
|
| 751 |
/* (:sclass obj body) */ |
| 752 |
static node* |
| 753 |
new_sclass(parser_state *p, node *o, node *b) |
| 754 |
{ |
| 755 |
void_expr_error(p, o); |
| 756 |
return list3((node*)NODE_SCLASS, o, cons(locals_node(p), b)); |
| 757 |
} |
| 758 |
|
| 759 |
/* (:module module body) */ |
| 760 |
static node* |
| 761 |
new_module(parser_state *p, node *m, node *b) |
| 762 |
{ |
| 763 |
return list3((node*)NODE_MODULE, m, cons(locals_node(p), b)); |
| 764 |
} |
| 765 |
|
| 766 |
/* (:def m lv (arg . body)) */ |
| 767 |
static node* |
| 768 |
new_def(parser_state *p, mrb_sym m, node *a, node *b) |
| 769 |
{ |
| 770 |
return list5((node*)NODE_DEF, nsym(m), 0, a, b); |
| 771 |
} |
| 772 |
|
| 773 |
static void |
| 774 |
defn_setup(parser_state *p, node *d, node *a, node *b) |
| 775 |
{ |
| 776 |
node *n = d->cdr->cdr; |
| 777 |
|
| 778 |
n->car = locals_node(p); |
| 779 |
p->cmdarg_stack = intn(n->cdr->car); |
| 780 |
n->cdr->car = a; |
| 781 |
local_resume(p, n->cdr->cdr->car); |
| 782 |
n->cdr->cdr->car = b; |
| 783 |
} |
| 784 |
|
| 785 |
/* (:sdef obj m lv (arg . body)) */ |
| 786 |
static node* |
| 787 |
new_sdef(parser_state *p, node *o, mrb_sym m, node *a, node *b) |
| 788 |
{ |
| 789 |
void_expr_error(p, o); |
| 790 |
return list6((node*)NODE_SDEF, o, nsym(m), 0, a, b); |
| 791 |
} |
| 792 |
|
| 793 |
static void |
| 794 |
defs_setup(parser_state *p, node *d, node *a, node *b) |
| 795 |
{ |
| 796 |
node *n = d->cdr->cdr->cdr; |
| 797 |
|
| 798 |
n->car = locals_node(p); |
| 799 |
p->cmdarg_stack = intn(n->cdr->car); |
| 800 |
n->cdr->car = a; |
| 801 |
local_resume(p, n->cdr->cdr->car); |
| 802 |
n->cdr->cdr->car = b; |
| 803 |
} |
| 804 |
|
| 805 |
/* (:arg . sym) */ |
| 806 |
static node* |
| 807 |
new_arg(parser_state *p, mrb_sym sym) |
| 808 |
{ |
| 809 |
return cons((node*)NODE_ARG, nsym(sym)); |
| 810 |
} |
| 811 |
|
| 812 |
static void |
| 813 |
local_add_margs(parser_state *p, node *n) |
| 814 |
{ |
| 815 |
while (n) { |
| 816 |
if (typen(n->car->car) == NODE_MASGN) { |
| 817 |
node *t = n->car->cdr->cdr; |
| 818 |
|
| 819 |
n->car->cdr->cdr = NULL; |
| 820 |
while (t) { |
| 821 |
local_add_f(p, sym(t->car)); |
| 822 |
t = t->cdr; |
| 823 |
} |
| 824 |
local_add_margs(p, n->car->cdr->car->car); |
| 825 |
local_add_margs(p, n->car->cdr->car->cdr->cdr->car); |
| 826 |
} |
| 827 |
n = n->cdr; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
static void |
| 832 |
local_add_lv(parser_state *p, node *lv) |
| 833 |
{ |
| 834 |
while (lv) { |
| 835 |
local_add_f(p, sym(lv->car)); |
| 836 |
lv = lv->cdr; |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
/* (m o r m2 tail) */ |
| 841 |
/* m: (a b c) */ |
| 842 |
/* o: ((a . e1) (b . e2)) */ |
| 843 |
/* r: a */ |
| 844 |
/* m2: (a b c) */ |
| 845 |
/* b: a */ |
| 846 |
static node* |
| 847 |
new_args(parser_state *p, node *m, node *opt, mrb_sym rest, node *m2, node *tail) |
| 848 |
{ |
| 849 |
node *n; |
| 850 |
|
| 851 |
local_add_margs(p, m); |
| 852 |
local_add_margs(p, m2); |
| 853 |
n = cons(m2, tail); |
| 854 |
n = cons(nsym(rest), n); |
| 855 |
n = cons(opt, n); |
| 856 |
while (opt) { |
| 857 |
/* opt: (sym . (opt . lv)) -> (sym . opt) */ |
| 858 |
local_add_lv(p, opt->car->cdr->cdr); |
| 859 |
opt->car->cdr = opt->car->cdr->car; |
| 860 |
opt = opt->cdr; |
| 861 |
} |
| 862 |
return cons(m, n); |
| 863 |
} |
| 864 |
|
| 865 |
/* (:args_tail keywords rest_keywords_sym block_sym) */ |
| 866 |
static node* |
| 867 |
new_args_tail(parser_state *p, node *kws, node *kwrest, mrb_sym blk) |
| 868 |
{ |
| 869 |
node *k; |
| 870 |
|
| 871 |
if (kws || kwrest) { |
| 872 |
local_add_kw(p, (kwrest && kwrest->cdr)? sym(kwrest->cdr) : 0); |
| 873 |
} |
| 874 |
|
| 875 |
local_add_blk(p, blk); |
| 876 |
|
| 877 |
/* allocate register for keywords arguments */ |
| 878 |
/* order is for Proc#parameters */ |
| 879 |
for (k = kws; k; k = k->cdr) { |
| 880 |
if (!k->car->cdr->cdr->car) { /* allocate required keywords */ |
| 881 |
local_add_f(p, sym(k->car->cdr->car)); |
| 882 |
} |
| 883 |
} |
| 884 |
for (k = kws; k; k = k->cdr) { |
| 885 |
if (k->car->cdr->cdr->car) { /* allocate keywords with default */ |
| 886 |
local_add_lv(p, k->car->cdr->cdr->car->cdr); |
| 887 |
k->car->cdr->cdr->car = k->car->cdr->cdr->car->car; |
| 888 |
local_add_f(p, sym(k->car->cdr->car)); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
return list4((node*)NODE_ARGS_TAIL, kws, kwrest, nsym(blk)); |
| 893 |
} |
| 894 |
|
| 895 |
/* (:kw_arg kw_sym def_arg) */ |
| 896 |
static node* |
| 897 |
new_kw_arg(parser_state *p, mrb_sym kw, node *def_arg) |
| 898 |
{ |
| 899 |
mrb_assert(kw); |
| 900 |
return list3((node*)NODE_KW_ARG, nsym(kw), def_arg); |
| 901 |
} |
| 902 |
|
| 903 |
/* (:kw_rest_args . a) */ |
| 904 |
static node* |
| 905 |
new_kw_rest_args(parser_state *p, mrb_sym sym) |
| 906 |
{ |
| 907 |
return cons((node*)NODE_KW_REST_ARGS, nsym(sym)); |
| 908 |
} |
| 909 |
|
| 910 |
static node* |
| 911 |
new_args_dots(parser_state *p, node *m) |
| 912 |
{ |
| 913 |
mrb_sym r = intern_op(mul); |
| 914 |
mrb_sym k = intern_op(pow); |
| 915 |
mrb_sym b = intern_op(and); |
| 916 |
local_add_f(p, r); |
| 917 |
return new_args(p, m, 0, r, 0, |
| 918 |
new_args_tail(p, 0, new_kw_rest_args(p, k), b)); |
| 919 |
} |
| 920 |
|
| 921 |
/* (:block_arg . a) */ |
| 922 |
static node* |
| 923 |
new_block_arg(parser_state *p, node *a) |
| 924 |
{ |
| 925 |
return cons((node*)NODE_BLOCK_ARG, a); |
| 926 |
} |
| 927 |
|
| 928 |
static node* |
| 929 |
setup_numparams(parser_state *p, node *a) |
| 930 |
{ |
| 931 |
int nvars = intn(p->nvars->car); |
| 932 |
if (nvars > 0) { |
| 933 |
int i; |
| 934 |
mrb_sym sym; |
| 935 |
// m || opt || rest || tail |
| 936 |
if (a && (a->car || (a->cdr && a->cdr->car) || (a->cdr->cdr && a->cdr->cdr->car) || (a->cdr->cdr->cdr->cdr && a->cdr->cdr->cdr->cdr->car))) { |
| 937 |
yyerror(p, "ordinary parameter is defined"); |
| 938 |
} |
| 939 |
else if (p->locals) { |
| 940 |
/* p->locals should not be NULL unless error happens before the point */ |
| 941 |
node* args = 0; |
| 942 |
for (i = nvars; i > 0; i--) { |
| 943 |
char buf[3]; |
| 944 |
|
| 945 |
buf[0] = '_'; |
| 946 |
buf[1] = i+'0'; |
| 947 |
buf[2] = '\0'; |
| 948 |
sym = intern_cstr(buf); |
| 949 |
args = cons(new_arg(p, sym), args); |
| 950 |
p->locals->car = cons(nsym(sym), p->locals->car); |
| 951 |
} |
| 952 |
a = new_args(p, args, 0, 0, 0, 0); |
| 953 |
} |
| 954 |
} |
| 955 |
return a; |
| 956 |
} |
| 957 |
|
| 958 |
/* (:block arg body) */ |
| 959 |
static node* |
| 960 |
new_block(parser_state *p, node *a, node *b) |
| 961 |
{ |
| 962 |
a = setup_numparams(p, a); |
| 963 |
return list4((node*)NODE_BLOCK, locals_node(p), a, b); |
| 964 |
} |
| 965 |
|
| 966 |
/* (:lambda arg body) */ |
| 967 |
static node* |
| 968 |
new_lambda(parser_state *p, node *a, node *b) |
| 969 |
{ |
| 970 |
return list4((node*)NODE_LAMBDA, locals_node(p), a, b); |
| 971 |
} |
| 972 |
|
| 973 |
/* (:asgn lhs rhs) */ |
| 974 |
static node* |
| 975 |
new_asgn(parser_state *p, node *a, node *b) |
| 976 |
{ |
| 977 |
void_expr_error(p, b); |
| 978 |
return cons((node*)NODE_ASGN, cons(a, b)); |
| 979 |
} |
| 980 |
|
| 981 |
/* (:masgn mlhs=(pre rest post) mrhs) */ |
| 982 |
static node* |
| 983 |
new_masgn(parser_state *p, node *a, node *b) |
| 984 |
{ |
| 985 |
void_expr_error(p, b); |
| 986 |
return cons((node*)NODE_MASGN, cons(a, b)); |
| 987 |
} |
| 988 |
|
| 989 |
/* (:masgn mlhs mrhs) no check */ |
| 990 |
static node* |
| 991 |
new_masgn_param(parser_state *p, node *a, node *b) |
| 992 |
{ |
| 993 |
return cons((node*)NODE_MASGN, cons(a, b)); |
| 994 |
} |
| 995 |
|
| 996 |
/* (:asgn lhs rhs) */ |
| 997 |
static node* |
| 998 |
new_op_asgn(parser_state *p, node *a, mrb_sym op, node *b) |
| 999 |
{ |
| 1000 |
void_expr_error(p, b); |
| 1001 |
return list4((node*)NODE_OP_ASGN, a, nsym(op), b); |
| 1002 |
} |
| 1003 |
|
| 1004 |
static node* |
| 1005 |
new_imaginary(parser_state *p, node *imaginary) |
| 1006 |
{ |
| 1007 |
return new_call(p, new_const(p, MRB_SYM_2(p->mrb, Kernel)), MRB_SYM_2(p->mrb, Complex), |
| 1008 |
new_callargs(p, list2(list3((node*)NODE_INT, (node*)strdup("0"), nint(10)), imaginary), 0, 0), '.'); |
| 1009 |
} |
| 1010 |
|
| 1011 |
static node* |
| 1012 |
new_rational(parser_state *p, node *rational) |
| 1013 |
{ |
| 1014 |
return new_call(p, new_const(p, MRB_SYM_2(p->mrb, Kernel)), MRB_SYM_2(p->mrb, Rational), new_callargs(p, list1(rational), 0, 0), '.'); |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* (:int . i) */ |
| 1018 |
static node* |
| 1019 |
new_int(parser_state *p, const char *s, int base, int suffix) |
| 1020 |
{ |
| 1021 |
node* result = list3((node*)NODE_INT, (node*)strdup(s), nint(base)); |
| 1022 |
if (suffix & NUM_SUFFIX_R) { |
| 1023 |
result = new_rational(p, result); |
| 1024 |
} |
| 1025 |
if (suffix & NUM_SUFFIX_I) { |
| 1026 |
result = new_imaginary(p, result); |
| 1027 |
} |
| 1028 |
return result; |
| 1029 |
} |
| 1030 |
|
| 1031 |
#ifndef MRB_NO_FLOAT |
| 1032 |
/* (:float . i) */ |
| 1033 |
static node* |
| 1034 |
new_float(parser_state *p, const char *s, int suffix) |
| 1035 |
{ |
| 1036 |
node* result = cons((node*)NODE_FLOAT, (node*)strdup(s)); |
| 1037 |
if (suffix & NUM_SUFFIX_R) { |
| 1038 |
result = new_rational(p, result); |
| 1039 |
} |
| 1040 |
if (suffix & NUM_SUFFIX_I) { |
| 1041 |
result = new_imaginary(p, result); |
| 1042 |
} |
| 1043 |
return result; |
| 1044 |
} |
| 1045 |
#endif |
| 1046 |
|
| 1047 |
/* (:str . (s . len)) */ |
| 1048 |
static node* |
| 1049 |
new_str(parser_state *p, const char *s, size_t len) |
| 1050 |
{ |
| 1051 |
return cons((node*)NODE_STR, cons((node*)strndup(s, len), nint(len))); |
| 1052 |
} |
| 1053 |
|
| 1054 |
/* (:dstr . a) */ |
| 1055 |
static node* |
| 1056 |
new_dstr(parser_state *p, node *a) |
| 1057 |
{ |
| 1058 |
return cons((node*)NODE_DSTR, a); |
| 1059 |
} |
| 1060 |
|
| 1061 |
static int |
| 1062 |
string_node_p(node *n) |
| 1063 |
{ |
| 1064 |
return (int)(typen(n->car) == NODE_STR); |
| 1065 |
} |
| 1066 |
|
| 1067 |
static node* |
| 1068 |
composite_string_node(parser_state *p, node *a, node *b) |
| 1069 |
{ |
| 1070 |
size_t newlen = (size_t)a->cdr + (size_t)b->cdr; |
| 1071 |
char *str = (char*)mrb_pool_realloc(p->pool, a->car, (size_t)a->cdr + 1, newlen + 1); |
| 1072 |
memcpy(str + (size_t)a->cdr, b->car, (size_t)b->cdr); |
| 1073 |
str[newlen] = '\0'; |
| 1074 |
a->car = (node*)str; |
| 1075 |
a->cdr = (node*)newlen; |
| 1076 |
cons_free(b); |
| 1077 |
return a; |
| 1078 |
} |
| 1079 |
|
| 1080 |
static node* |
| 1081 |
concat_string(parser_state *p, node *a, node *b) |
| 1082 |
{ |
| 1083 |
if (string_node_p(a)) { |
| 1084 |
if (string_node_p(b)) { |
| 1085 |
/* a == NODE_STR && b == NODE_STR */ |
| 1086 |
composite_string_node(p, a->cdr, b->cdr); |
| 1087 |
cons_free(b); |
| 1088 |
return a; |
| 1089 |
} |
| 1090 |
else { |
| 1091 |
/* a == NODE_STR && b == NODE_DSTR */ |
| 1092 |
|
| 1093 |
if (string_node_p(b->cdr->car)) { |
| 1094 |
/* a == NODE_STR && b->[NODE_STR, ...] */ |
| 1095 |
composite_string_node(p, a->cdr, b->cdr->car->cdr); |
| 1096 |
cons_free(b->cdr->car); |
| 1097 |
b->cdr->car = a; |
| 1098 |
return b; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
else { |
| 1103 |
node *c; /* last node of a */ |
| 1104 |
for (c = a; c->cdr != NULL; c = c->cdr) |
| 1105 |
; |
| 1106 |
if (string_node_p(b)) { |
| 1107 |
/* a == NODE_DSTR && b == NODE_STR */ |
| 1108 |
if (string_node_p(c->car)) { |
| 1109 |
/* a->[..., NODE_STR] && b == NODE_STR */ |
| 1110 |
composite_string_node(p, c->car->cdr, b->cdr); |
| 1111 |
cons_free(b); |
| 1112 |
return a; |
| 1113 |
} |
| 1114 |
|
| 1115 |
push(a, b); |
| 1116 |
return a; |
| 1117 |
} |
| 1118 |
else { |
| 1119 |
/* a == NODE_DSTR && b == NODE_DSTR */ |
| 1120 |
if (string_node_p(c->car) && string_node_p(b->cdr->car)) { |
| 1121 |
/* a->[..., NODE_STR] && b->[NODE_STR, ...] */ |
| 1122 |
node *d = b->cdr; |
| 1123 |
cons_free(b); |
| 1124 |
composite_string_node(p, c->car->cdr, d->car->cdr); |
| 1125 |
cons_free(d->car); |
| 1126 |
c->cdr = d->cdr; |
| 1127 |
cons_free(d); |
| 1128 |
return a; |
| 1129 |
} |
| 1130 |
else { |
| 1131 |
c->cdr = b->cdr; |
| 1132 |
cons_free(b); |
| 1133 |
return a; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
return new_dstr(p, list2(a, b)); |
| 1139 |
} |
| 1140 |
|
| 1141 |
/* (:str . (s . len)) */ |
| 1142 |
static node* |
| 1143 |
new_xstr(parser_state *p, const char *s, int len) |
| 1144 |
{ |
| 1145 |
return cons((node*)NODE_XSTR, cons((node*)strndup(s, len), nint(len))); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* (:xstr . a) */ |
| 1149 |
static node* |
| 1150 |
new_dxstr(parser_state *p, node *a) |
| 1151 |
{ |
| 1152 |
return cons((node*)NODE_DXSTR, a); |
| 1153 |
} |
| 1154 |
|
| 1155 |
/* (:dsym . a) */ |
| 1156 |
static node* |
| 1157 |
new_dsym(parser_state *p, node *a) |
| 1158 |
{ |
| 1159 |
return cons((node*)NODE_DSYM, a); |
| 1160 |
} |
| 1161 |
|
| 1162 |
/* (:regx . (s . (opt . enc))) */ |
| 1163 |
static node* |
| 1164 |
new_regx(parser_state *p, const char *p1, const char* p2, const char* p3) |
| 1165 |
{ |
| 1166 |
return cons((node*)NODE_REGX, cons((node*)p1, cons((node*)p2, (node*)p3))); |
| 1167 |
} |
| 1168 |
|
| 1169 |
/* (:dregx . (a . b)) */ |
| 1170 |
static node* |
| 1171 |
new_dregx(parser_state *p, node *a, node *b) |
| 1172 |
{ |
| 1173 |
return cons((node*)NODE_DREGX, cons(a, b)); |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* (:backref . n) */ |
| 1177 |
static node* |
| 1178 |
new_back_ref(parser_state *p, int n) |
| 1179 |
{ |
| 1180 |
return cons((node*)NODE_BACK_REF, nint(n)); |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* (:nthref . n) */ |
| 1184 |
static node* |
| 1185 |
new_nth_ref(parser_state *p, int n) |
| 1186 |
{ |
| 1187 |
return cons((node*)NODE_NTH_REF, nint(n)); |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* (:heredoc . a) */ |
| 1191 |
static node* |
| 1192 |
new_heredoc(parser_state *p) |
| 1193 |
{ |
| 1194 |
parser_heredoc_info *inf = (parser_heredoc_info*)parser_palloc(p, sizeof(parser_heredoc_info)); |
| 1195 |
return cons((node*)NODE_HEREDOC, (node*)inf); |
| 1196 |
} |
| 1197 |
|
| 1198 |
static void |
| 1199 |
new_bv(parser_state *p, mrb_sym id) |
| 1200 |
{ |
| 1201 |
} |
| 1202 |
|
| 1203 |
static node* |
| 1204 |
new_literal_delim(parser_state *p) |
| 1205 |
{ |
| 1206 |
return cons((node*)NODE_LITERAL_DELIM, 0); |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* (:words . a) */ |
| 1210 |
static node* |
| 1211 |
new_words(parser_state *p, node *a) |
| 1212 |
{ |
| 1213 |
return cons((node*)NODE_WORDS, a); |
| 1214 |
} |
| 1215 |
|
| 1216 |
/* (:symbols . a) */ |
| 1217 |
static node* |
| 1218 |
new_symbols(parser_state *p, node *a) |
| 1219 |
{ |
| 1220 |
return cons((node*)NODE_SYMBOLS, a); |
| 1221 |
} |
| 1222 |
|
| 1223 |
/* xxx ----------------------------- */ |
| 1224 |
|
| 1225 |
/* (:call a op) */ |
| 1226 |
static node* |
| 1227 |
call_uni_op(parser_state *p, node *recv, const char *m) |
| 1228 |
{ |
| 1229 |
void_expr_error(p, recv); |
| 1230 |
return new_call(p, recv, intern_cstr(m), 0, '.'); |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* (:call a op b) */ |
| 1234 |
static node* |
| 1235 |
call_bin_op(parser_state *p, node *recv, const char *m, node *arg1) |
| 1236 |
{ |
| 1237 |
return new_call(p, recv, intern_cstr(m), new_callargs(p, list1(arg1), 0, 0), '.'); |
| 1238 |
} |
| 1239 |
|
| 1240 |
static void |
| 1241 |
args_with_block(parser_state *p, node *a, node *b) |
| 1242 |
{ |
| 1243 |
if (b) { |
| 1244 |
if (a->cdr && a->cdr->cdr) { |
| 1245 |
yyerror(p, "both block arg and actual block given"); |
| 1246 |
} |
| 1247 |
a->cdr->cdr = b; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
static void |
| 1252 |
endless_method_name(parser_state *p, node *defn) |
| 1253 |
{ |
| 1254 |
mrb_sym sym = sym(defn->cdr->car); |
| 1255 |
mrb_int len; |
| 1256 |
const char *name = mrb_sym_name_len(p->mrb, sym, &len); |
| 1257 |
|
| 1258 |
if (len > 1 && name[len-1] == '=') { |
| 1259 |
for (int i=0; i<len-1; i++) { |
| 1260 |
if (!identchar(name[i])) return; |
| 1261 |
} |
| 1262 |
yyerror(p, "setter method cannot be defined by endless method definition"); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
static void |
| 1267 |
call_with_block(parser_state *p, node *a, node *b) |
| 1268 |
{ |
| 1269 |
node *n; |
| 1270 |
|
| 1271 |
switch (typen(a->car)) { |
| 1272 |
case NODE_SUPER: |
| 1273 |
case NODE_ZSUPER: |
| 1274 |
if (!a->cdr) a->cdr = new_callargs(p, 0, 0, b); |
| 1275 |
else args_with_block(p, a->cdr, b); |
| 1276 |
break; |
| 1277 |
case NODE_CALL: |
| 1278 |
case NODE_FCALL: |
| 1279 |
case NODE_SCALL: |
| 1280 |
/* (NODE_CALL recv mid (args kw . blk)) */ |
| 1281 |
n = a->cdr->cdr->cdr; /* (args kw . blk) */ |
| 1282 |
if (!n->car) n->car = new_callargs(p, 0, 0, b); |
| 1283 |
else args_with_block(p, n->car, b); |
| 1284 |
break; |
| 1285 |
case NODE_RETURN: |
| 1286 |
case NODE_BREAK: |
| 1287 |
case NODE_NEXT: |
| 1288 |
if (a->cdr == NULL) return; |
| 1289 |
call_with_block(p, a->cdr, b); |
| 1290 |
break; |
| 1291 |
default: |
| 1292 |
break; |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
static node* |
| 1297 |
new_negate(parser_state *p, node *n) |
| 1298 |
{ |
| 1299 |
return cons((node*)NODE_NEGATE, n); |
| 1300 |
} |
| 1301 |
|
| 1302 |
static node* |
| 1303 |
cond(node *n) |
| 1304 |
{ |
| 1305 |
return n; |
| 1306 |
} |
| 1307 |
|
| 1308 |
static node* |
| 1309 |
ret_args(parser_state *p, node *n) |
| 1310 |
{ |
| 1311 |
if (n->cdr->cdr) { |
| 1312 |
yyerror(p, "block argument should not be given"); |
| 1313 |
return NULL; |
| 1314 |
} |
| 1315 |
if (!n->car) return NULL; |
| 1316 |
if (!n->car->cdr) return n->car->car; |
| 1317 |
return new_array(p, n->car); |
| 1318 |
} |
| 1319 |
|
| 1320 |
static void |
| 1321 |
assignable(parser_state *p, node *lhs) |
| 1322 |
{ |
| 1323 |
if (intn(lhs->car) == NODE_LVAR) { |
| 1324 |
local_add(p, sym(lhs->cdr)); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
static node* |
| 1329 |
var_reference(parser_state *p, node *lhs) |
| 1330 |
{ |
| 1331 |
node *n; |
| 1332 |
|
| 1333 |
if (intn(lhs->car) == NODE_LVAR) { |
| 1334 |
if (!local_var_p(p, sym(lhs->cdr))) { |
| 1335 |
n = new_fcall(p, sym(lhs->cdr), 0); |
| 1336 |
cons_free(lhs); |
| 1337 |
return n; |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
return lhs; |
| 1342 |
} |
| 1343 |
|
| 1344 |
static node* |
| 1345 |
label_reference(parser_state *p, mrb_sym sym) |
| 1346 |
{ |
| 1347 |
const char *name = mrb_sym_name(p->mrb, sym); |
| 1348 |
node *n; |
| 1349 |
|
| 1350 |
if (local_var_p(p, sym)) { |
| 1351 |
n = new_lvar(p, sym); |
| 1352 |
} |
| 1353 |
else if (ISUPPER(name[0])) { |
| 1354 |
n = new_const(p, sym); |
| 1355 |
} |
| 1356 |
else { |
| 1357 |
n = new_fcall(p, sym, 0); |
| 1358 |
} |
| 1359 |
return n; |
| 1360 |
} |
| 1361 |
|
| 1362 |
typedef enum mrb_string_type string_type; |
| 1363 |
|
| 1364 |
typedef struct parser_lex_strterm { |
| 1365 |
int type; |
| 1366 |
int level; |
| 1367 |
int term; |
| 1368 |
int paren; |
| 1369 |
struct parser_lex_strterm *prev; |
| 1370 |
} parser_lex_strterm; |
| 1371 |
|
| 1372 |
static parser_lex_strterm* |
| 1373 |
new_strterm(parser_state *p, string_type type, int term, int paren) |
| 1374 |
{ |
| 1375 |
parser_lex_strterm *lex = (parser_lex_strterm*)parser_palloc(p, sizeof(parser_lex_strterm)); |
| 1376 |
lex->type = type; |
| 1377 |
lex->level = 0; |
| 1378 |
lex->term = term; |
| 1379 |
lex->paren = paren; |
| 1380 |
lex->prev = p->lex_strterm; |
| 1381 |
return lex; |
| 1382 |
} |
| 1383 |
|
| 1384 |
static void |
| 1385 |
end_strterm(parser_state *p) |
| 1386 |
{ |
| 1387 |
parser_lex_strterm *term = p->lex_strterm->prev; |
| 1388 |
parser_pfree(p->lex_strterm); |
| 1389 |
p->lex_strterm = term; |
| 1390 |
} |
| 1391 |
|
| 1392 |
static node* |
| 1393 |
push_strterm(parser_state *p) |
| 1394 |
{ |
| 1395 |
node *n = cons((node*)p->lex_strterm, p->parsing_heredoc); |
| 1396 |
p->lex_strterm = NULL; |
| 1397 |
return n; |
| 1398 |
} |
| 1399 |
|
| 1400 |
static void |
| 1401 |
pop_strterm(parser_state *p, node *n) |
| 1402 |
{ |
| 1403 |
p->lex_strterm = (parser_lex_strterm*)n->car; |
| 1404 |
p->parsing_heredoc = n->cdr; |
| 1405 |
cons_free(n); |
| 1406 |
} |
| 1407 |
|
| 1408 |
static parser_heredoc_info * |
| 1409 |
parsing_heredoc_info(parser_state *p) |
| 1410 |
{ |
| 1411 |
node *nd = p->parsing_heredoc; |
| 1412 |
if (nd == NULL) |
| 1413 |
return NULL; |
| 1414 |
/* mrb_assert(nd->car->car == NODE_HEREDOC); */ |
| 1415 |
return (parser_heredoc_info*)nd->car->cdr; |
| 1416 |
} |
| 1417 |
|
| 1418 |
static void |
| 1419 |
heredoc_treat_nextline(parser_state *p) |
| 1420 |
{ |
| 1421 |
if (p->heredocs_from_nextline == NULL) return; |
| 1422 |
if (p->parsing_heredoc && p->lex_strterm) { |
| 1423 |
append(p->heredocs_from_nextline, p->parsing_heredoc); |
| 1424 |
} |
| 1425 |
p->parsing_heredoc = p->heredocs_from_nextline; |
| 1426 |
p->lex_strterm = new_strterm(p, parsing_heredoc_info(p)->type, 0, 0); |
| 1427 |
p->heredocs_from_nextline = NULL; |
| 1428 |
} |
| 1429 |
|
| 1430 |
static void |
| 1431 |
heredoc_end(parser_state *p) |
| 1432 |
{ |
| 1433 |
p->parsing_heredoc = p->parsing_heredoc->cdr; |
| 1434 |
if (p->parsing_heredoc == NULL) { |
| 1435 |
p->lstate = EXPR_BEG; |
| 1436 |
end_strterm(p); |
| 1437 |
} |
| 1438 |
else { |
| 1439 |
/* next heredoc */ |
| 1440 |
p->lex_strterm->type = parsing_heredoc_info(p)->type; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
#define is_strterm_type(p,str_func) ((p)->lex_strterm->type & (str_func)) |
| 1444 |
|
| 1445 |
/* xxx ----------------------------- */ |
| 1446 |
|
| 1447 |
%} |
| 1448 |
|
| 1449 |
%define parse.error verbose |
| 1450 |
%define api.pure |
| 1451 |
%parse-param {parser_state *p} |
| 1452 |
%lex-param {parser_state *p} |
| 1453 |
|
| 1454 |
%union { |
| 1455 |
node *nd; |
| 1456 |
mrb_sym id; |
| 1457 |
int num; |
| 1458 |
stack_type stack; |
| 1459 |
const struct vtable *vars; |
| 1460 |
} |
| 1461 |
|
| 1462 |
%token <num> |
| 1463 |
keyword_class "'class'" |
| 1464 |
keyword_module "'module'" |
| 1465 |
keyword_def "'def'" |
| 1466 |
keyword_begin "'begin'" |
| 1467 |
keyword_if "'if'" |
| 1468 |
keyword_unless "'unless'" |
| 1469 |
keyword_while "'while'" |
| 1470 |
keyword_until "'until'" |
| 1471 |
keyword_for "'for'" |
| 1472 |
|
| 1473 |
%token |
| 1474 |
keyword_undef "'undef'" |
| 1475 |
keyword_rescue "'rescue'" |
| 1476 |
keyword_ensure "'ensure'" |
| 1477 |
keyword_end "'end'" |
| 1478 |
keyword_then "'then'" |
| 1479 |
keyword_elsif "'elsif'" |
| 1480 |
keyword_else "'else'" |
| 1481 |
keyword_case "'case'" |
| 1482 |
keyword_when "'when'" |
| 1483 |
keyword_break "'break'" |
| 1484 |
keyword_next "'next'" |
| 1485 |
keyword_redo "'redo'" |
| 1486 |
keyword_retry "'retry'" |
| 1487 |
keyword_in "'in'" |
| 1488 |
keyword_do "'do'" |
| 1489 |
keyword_do_cond "'do' for condition" |
| 1490 |
keyword_do_block "'do' for block" |
| 1491 |
keyword_do_LAMBDA "'do' for lambda" |
| 1492 |
keyword_return "'return'" |
| 1493 |
keyword_yield "'yield'" |
| 1494 |
keyword_super "'super'" |
| 1495 |
keyword_self "'self'" |
| 1496 |
keyword_nil "'nil'" |
| 1497 |
keyword_true "'true'" |
| 1498 |
keyword_false "'false'" |
| 1499 |
keyword_and "'and'" |
| 1500 |
keyword_or "'or'" |
| 1501 |
keyword_not "'not'" |
| 1502 |
modifier_if "'if' modifier" |
| 1503 |
modifier_unless "'unless' modifier" |
| 1504 |
modifier_while "'while' modifier" |
| 1505 |
modifier_until "'until' modifier" |
| 1506 |
modifier_rescue "'rescue' modifier" |
| 1507 |
keyword_alias "'alis'" |
| 1508 |
keyword_BEGIN "'BEGIN'" |
| 1509 |
keyword_END "'END'" |
| 1510 |
keyword__LINE__ "'__LINE__'" |
| 1511 |
keyword__FILE__ "'__FILE__'" |
| 1512 |
keyword__ENCODING__ "'__ENCODING__'" |
| 1513 |
|
| 1514 |
%token <id> tIDENTIFIER "local variable or method" |
| 1515 |
%token <id> tFID "method" |
| 1516 |
%token <id> tGVAR "global variable" |
| 1517 |
%token <id> tIVAR "instance variable" |
| 1518 |
%token <id> tCONSTANT "constant" |
| 1519 |
%token <id> tCVAR "class variable" |
| 1520 |
%token <id> tLABEL_TAG "label" |
| 1521 |
%token <nd> tINTEGER "integer literal" |
| 1522 |
%token <nd> tFLOAT "float literal" |
| 1523 |
%token <nd> tCHAR "character literal" |
| 1524 |
%token <nd> tXSTRING tREGEXP |
| 1525 |
%token <nd> tSTRING tSTRING_PART tSTRING_MID |
| 1526 |
%token <nd> tNTH_REF tBACK_REF |
| 1527 |
%token <num> tREGEXP_END |
| 1528 |
%token <num> tNUMPARAM "numbered parameter" |
| 1529 |
|
| 1530 |
%type <nd> singleton string string_fragment string_rep string_interp xstring regexp |
| 1531 |
%type <nd> literal numeric cpath symbol defn_head defs_head |
| 1532 |
%type <nd> top_compstmt top_stmts top_stmt |
| 1533 |
%type <nd> bodystmt compstmt stmts stmt expr arg primary command command_call method_call |
| 1534 |
%type <nd> expr_value arg_rhs primary_value |
| 1535 |
%type <nd> if_tail opt_else case_body cases opt_rescue exc_list exc_var opt_ensure |
| 1536 |
%type <nd> args call_args opt_call_args |
| 1537 |
%type <nd> paren_args opt_paren_args variable |
| 1538 |
%type <nd> command_args aref_args opt_block_arg block_arg var_ref var_lhs |
| 1539 |
%type <nd> command_asgn command_rhs mrhs superclass block_call block_command |
| 1540 |
%type <nd> f_block_optarg f_block_opt |
| 1541 |
%type <nd> f_opt_arglist_paren f_arglist_paren f_arglist |
| 1542 |
%type <nd> f_args f_arg f_arg_item f_optarg f_margs |
| 1543 |
%type <nd> assoc_list assocs assoc undef_list backref for_var |
| 1544 |
%type <nd> block_param opt_block_param block_param_def f_opt |
| 1545 |
%type <nd> bv_decls opt_bv_decl bvar f_larglist lambda_body |
| 1546 |
%type <nd> brace_block cmd_brace_block do_block lhs none f_bad_arg |
| 1547 |
%type <nd> mlhs mlhs_list mlhs_post mlhs_basic mlhs_item mlhs_node mlhs_inner |
| 1548 |
%type <id> fsym sym basic_symbol operation operation2 operation3 |
| 1549 |
%type <id> cname fname op f_rest_arg f_block_arg opt_f_block_arg f_norm_arg f_opt_asgn |
| 1550 |
%type <nd> heredoc words symbols |
| 1551 |
%type <num> call_op call_op2 /* 0:'&.', 1:'.', 2:'::' */ |
| 1552 |
|
| 1553 |
%type <nd> args_tail opt_args_tail f_kwarg f_kw f_kwrest |
| 1554 |
%type <nd> f_block_kwarg f_block_kw block_args_tail opt_block_args_tail |
| 1555 |
%type <id> f_label |
| 1556 |
|
| 1557 |
%token tUPLUS "unary plus" |
| 1558 |
%token tUMINUS "unary minus" |
| 1559 |
%token tCMP "<=>" |
| 1560 |
%token tEQ "==" |
| 1561 |
%token tEQQ "===" |
| 1562 |
%token tNEQ "!=" |
| 1563 |
%token tGEQ ">=" |
| 1564 |
%token tLEQ "<=" |
| 1565 |
%token tANDOP "&&" |
| 1566 |
%token tOROP "||" |
| 1567 |
%token tMATCH "=~" |
| 1568 |
%token tNMATCH "!~" |
| 1569 |
%token tDOT2 ".." |
| 1570 |
%token tDOT3 "..." |
| 1571 |
%token tBDOT2 tBDOT3 /* (.. and (... */ |
| 1572 |
%token tAREF tASET /* [] and []= */ |
| 1573 |
%token tLSHFT "<<" |
| 1574 |
%token tRSHFT ">>" |
| 1575 |
%token tCOLON2 "::" |
| 1576 |
%token tCOLON3 /* :: at EXPR_BEG */ |
| 1577 |
%token <id> tOP_ASGN /* +=, -= etc. */ |
| 1578 |
%token tASSOC "=>" |
| 1579 |
%token tLPAREN tLPAREN_ARG "(" |
| 1580 |
%token tRPAREN ")" |
| 1581 |
%token tLBRACK "[" |
| 1582 |
%token tLBRACE tLBRACE_ARG "{" |
| 1583 |
%token tSTAR "*" |
| 1584 |
%token tPOW tDSTAR "**" |
| 1585 |
%token tAMPER "&" |
| 1586 |
%token tLAMBDA "->" |
| 1587 |
%token tANDDOT "&." |
| 1588 |
%token tSYMBEG "symbol" |
| 1589 |
%token tSTRING_BEG "string literal" |
| 1590 |
%token tXSTRING_BEG tSTRING_DVAR tREGEXP_BEG tWORDS_BEG tSYMBOLS_BEG tLAMBEG |
| 1591 |
%token <nd> tHEREDOC_BEG "here document" |
| 1592 |
%token tHEREDOC_END tLITERAL_DELIM tHD_LITERAL_DELIM |
| 1593 |
%token <nd> tHD_STRING_PART tHD_STRING_MID |
| 1594 |
|
| 1595 |
/* |
| 1596 |
* precedence table |
| 1597 |
*/ |
| 1598 |
|
| 1599 |
%nonassoc tLOWEST |
| 1600 |
%nonassoc tLBRACE_ARG |
| 1601 |
|
| 1602 |
%nonassoc modifier_if modifier_unless modifier_while modifier_until |
| 1603 |
%left keyword_or keyword_and |
| 1604 |
%right keyword_not |
| 1605 |
%right '=' tOP_ASGN |
| 1606 |
%left modifier_rescue |
| 1607 |
%right '?' ':' tLABEL_TAG |
| 1608 |
%nonassoc tDOT2 tDOT3 tBDOT2 tBDOT3 |
| 1609 |
%left tOROP |
| 1610 |
%left tANDOP |
| 1611 |
%nonassoc tCMP tEQ tEQQ tNEQ tMATCH tNMATCH |
| 1612 |
%left '>' tGEQ '<' tLEQ |
| 1613 |
%left '|' '^' |
| 1614 |
%left '&' |
| 1615 |
%left tLSHFT tRSHFT |
| 1616 |
%left '+' '-' |
| 1617 |
%left '*' '/' '%' |
| 1618 |
%right tUMINUS_NUM tUMINUS |
| 1619 |
%right tPOW |
| 1620 |
%right '!' '~' tUPLUS |
| 1621 |
|
| 1622 |
%token tLAST_TOKEN |
| 1623 |
|
| 1624 |
%% |
| 1625 |
program : { |
| 1626 |
p->lstate = EXPR_BEG; |
| 1627 |
if (!p->locals) p->locals = cons(0,0); |
| 1628 |
} |
| 1629 |
top_compstmt |
| 1630 |
{ |
| 1631 |
p->tree = new_scope(p, $2); |
| 1632 |
NODE_LINENO(p->tree, $2); |
| 1633 |
} |
| 1634 |
; |
| 1635 |
|
| 1636 |
top_compstmt : top_stmts opt_terms |
| 1637 |
{ |
| 1638 |
$$ = $1; |
| 1639 |
} |
| 1640 |
; |
| 1641 |
|
| 1642 |
top_stmts : none |
| 1643 |
{ |
| 1644 |
$$ = new_begin(p, 0); |
| 1645 |
} |
| 1646 |
| top_stmt |
| 1647 |
{ |
| 1648 |
$$ = new_begin(p, $1); |
| 1649 |
NODE_LINENO($$, $1); |
| 1650 |
} |
| 1651 |
| top_stmts terms top_stmt |
| 1652 |
{ |
| 1653 |
$$ = push($1, newline_node($3)); |
| 1654 |
} |
| 1655 |
| error top_stmt |
| 1656 |
{ |
| 1657 |
$$ = new_begin(p, 0); |
| 1658 |
} |
| 1659 |
; |
| 1660 |
|
| 1661 |
top_stmt : stmt |
| 1662 |
| keyword_BEGIN |
| 1663 |
{ |
| 1664 |
$<nd>$ = local_switch(p); |
| 1665 |
nvars_block(p); |
| 1666 |
} |
| 1667 |
'{' top_compstmt '}' |
| 1668 |
{ |
| 1669 |
yyerror(p, "BEGIN not supported"); |
| 1670 |
local_resume(p, $<nd>2); |
| 1671 |
nvars_unnest(p); |
| 1672 |
$$ = 0; |
| 1673 |
} |
| 1674 |
; |
| 1675 |
|
| 1676 |
bodystmt : compstmt |
| 1677 |
opt_rescue |
| 1678 |
opt_else |
| 1679 |
opt_ensure |
| 1680 |
{ |
| 1681 |
if ($2) { |
| 1682 |
$$ = new_rescue(p, $1, $2, $3); |
| 1683 |
NODE_LINENO($$, $1); |
| 1684 |
} |
| 1685 |
else if ($3) { |
| 1686 |
yywarning(p, "else without rescue is useless"); |
| 1687 |
$$ = push($1, $3); |
| 1688 |
} |
| 1689 |
else { |
| 1690 |
$$ = $1; |
| 1691 |
} |
| 1692 |
if ($4) { |
| 1693 |
if ($$) { |
| 1694 |
$$ = new_ensure(p, $$, $4); |
| 1695 |
} |
| 1696 |
else { |
| 1697 |
$$ = push($4, new_nil(p)); |
| 1698 |
} |
| 1699 |
} |
| 1700 |
} |
| 1701 |
; |
| 1702 |
|
| 1703 |
compstmt : stmts opt_terms |
| 1704 |
{ |
| 1705 |
$$ = $1; |
| 1706 |
} |
| 1707 |
; |
| 1708 |
|
| 1709 |
stmts : none |
| 1710 |
{ |
| 1711 |
$$ = new_begin(p, 0); |
| 1712 |
} |
| 1713 |
| stmt |
| 1714 |
{ |
| 1715 |
$$ = new_begin(p, $1); |
| 1716 |
NODE_LINENO($$, $1); |
| 1717 |
} |
| 1718 |
| stmts terms stmt |
| 1719 |
{ |
| 1720 |
$$ = push($1, newline_node($3)); |
| 1721 |
} |
| 1722 |
| error stmt |
| 1723 |
{ |
| 1724 |
$$ = new_begin(p, $2); |
| 1725 |
} |
| 1726 |
; |
| 1727 |
|
| 1728 |
stmt : keyword_alias fsym {p->lstate = EXPR_FNAME;} fsym |
| 1729 |
{ |
| 1730 |
$$ = new_alias(p, $2, $4); |
| 1731 |
} |
| 1732 |
| keyword_undef undef_list |
| 1733 |
{ |
| 1734 |
$$ = $2; |
| 1735 |
} |
| 1736 |
| stmt modifier_if expr_value |
| 1737 |
{ |
| 1738 |
$$ = new_if(p, cond($3), $1, 0); |
| 1739 |
} |
| 1740 |
| stmt modifier_unless expr_value |
| 1741 |
{ |
| 1742 |
$$ = new_unless(p, cond($3), $1, 0); |
| 1743 |
} |
| 1744 |
| stmt modifier_while expr_value |
| 1745 |
{ |
| 1746 |
$$ = new_while(p, cond($3), $1); |
| 1747 |
} |
| 1748 |
| stmt modifier_until expr_value |
| 1749 |
{ |
| 1750 |
$$ = new_until(p, cond($3), $1); |
| 1751 |
} |
| 1752 |
| stmt modifier_rescue stmt |
| 1753 |
{ |
| 1754 |
$$ = new_mod_rescue(p, $1, $3); |
| 1755 |
} |
| 1756 |
| keyword_END '{' compstmt '}' |
| 1757 |
{ |
| 1758 |
yyerror(p, "END not supported"); |
| 1759 |
$$ = new_postexe(p, $3); |
| 1760 |
} |
| 1761 |
| command_asgn |
| 1762 |
| mlhs '=' command_call |
| 1763 |
{ |
| 1764 |
$$ = new_masgn(p, $1, $3); |
| 1765 |
} |
| 1766 |
| lhs '=' mrhs |
| 1767 |
{ |
| 1768 |
$$ = new_asgn(p, $1, new_array(p, $3)); |
| 1769 |
} |
| 1770 |
| mlhs '=' arg |
| 1771 |
{ |
| 1772 |
$$ = new_masgn(p, $1, $3); |
| 1773 |
} |
| 1774 |
| mlhs '=' mrhs |
| 1775 |
{ |
| 1776 |
$$ = new_masgn(p, $1, new_array(p, $3)); |
| 1777 |
} |
| 1778 |
| arg tASSOC tIDENTIFIER |
| 1779 |
{ |
| 1780 |
node *lhs = new_lvar(p, $3); |
| 1781 |
assignable(p, lhs); |
| 1782 |
$$ = new_asgn(p, lhs, $1); |
| 1783 |
} |
| 1784 |
| expr |
| 1785 |
; |
| 1786 |
|
| 1787 |
command_asgn : lhs '=' command_rhs |
| 1788 |
{ |
| 1789 |
$$ = new_asgn(p, $1, $3); |
| 1790 |
} |
| 1791 |
| var_lhs tOP_ASGN command_rhs |
| 1792 |
{ |
| 1793 |
$$ = new_op_asgn(p, $1, $2, $3); |
| 1794 |
} |
| 1795 |
| primary_value '[' opt_call_args ']' tOP_ASGN command_rhs |
| 1796 |
{ |
| 1797 |
$$ = new_op_asgn(p, new_call(p, $1, intern_op(aref), $3, '.'), $5, $6); |
| 1798 |
} |
| 1799 |
| primary_value call_op tIDENTIFIER tOP_ASGN command_rhs |
| 1800 |
{ |
| 1801 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, $2), $4, $5); |
| 1802 |
} |
| 1803 |
| primary_value call_op tCONSTANT tOP_ASGN command_rhs |
| 1804 |
{ |
| 1805 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, $2), $4, $5); |
| 1806 |
} |
| 1807 |
| primary_value tCOLON2 tCONSTANT tOP_ASGN command_call |
| 1808 |
{ |
| 1809 |
yyerror(p, "constant re-assignment"); |
| 1810 |
$$ = 0; |
| 1811 |
} |
| 1812 |
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN command_rhs |
| 1813 |
{ |
| 1814 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, tCOLON2), $4, $5); |
| 1815 |
} |
| 1816 |
| defn_head f_opt_arglist_paren '=' command |
| 1817 |
{ |
| 1818 |
$$ = $1; |
| 1819 |
endless_method_name(p, $1); |
| 1820 |
void_expr_error(p, $4); |
| 1821 |
defn_setup(p, $$, $2, $4); |
| 1822 |
nvars_unnest(p); |
| 1823 |
p->in_def--; |
| 1824 |
} |
| 1825 |
| defn_head f_opt_arglist_paren '=' command modifier_rescue arg |
| 1826 |
{ |
| 1827 |
$$ = $1; |
| 1828 |
endless_method_name(p, $1); |
| 1829 |
void_expr_error(p, $4); |
| 1830 |
defn_setup(p, $$, $2, new_mod_rescue(p, $4, $6)); |
| 1831 |
nvars_unnest(p); |
| 1832 |
p->in_def--; |
| 1833 |
} |
| 1834 |
| defs_head f_opt_arglist_paren '=' command |
| 1835 |
{ |
| 1836 |
$$ = $1; |
| 1837 |
void_expr_error(p, $4); |
| 1838 |
defs_setup(p, $$, $2, $4); |
| 1839 |
nvars_unnest(p); |
| 1840 |
p->in_def--; |
| 1841 |
p->in_single--; |
| 1842 |
} |
| 1843 |
| defs_head f_opt_arglist_paren '=' command modifier_rescue arg |
| 1844 |
{ |
| 1845 |
$$ = $1; |
| 1846 |
void_expr_error(p, $4); |
| 1847 |
defs_setup(p, $$, $2, new_mod_rescue(p, $4, $6)); |
| 1848 |
nvars_unnest(p); |
| 1849 |
p->in_def--; |
| 1850 |
p->in_single--; |
| 1851 |
} |
| 1852 |
| backref tOP_ASGN command_rhs |
| 1853 |
{ |
| 1854 |
backref_error(p, $1); |
| 1855 |
$$ = new_begin(p, 0); |
| 1856 |
} |
| 1857 |
; |
| 1858 |
|
| 1859 |
command_rhs : command_call %prec tOP_ASGN |
| 1860 |
| command_call modifier_rescue stmt |
| 1861 |
{ |
| 1862 |
$$ = new_mod_rescue(p, $1, $3); |
| 1863 |
} |
| 1864 |
| command_asgn |
| 1865 |
; |
| 1866 |
|
| 1867 |
|
| 1868 |
expr : command_call |
| 1869 |
| expr keyword_and expr |
| 1870 |
{ |
| 1871 |
$$ = new_and(p, $1, $3); |
| 1872 |
} |
| 1873 |
| expr keyword_or expr |
| 1874 |
{ |
| 1875 |
$$ = new_or(p, $1, $3); |
| 1876 |
} |
| 1877 |
| keyword_not opt_nl expr |
| 1878 |
{ |
| 1879 |
$$ = call_uni_op(p, cond($3), "!"); |
| 1880 |
} |
| 1881 |
| '!' command_call |
| 1882 |
{ |
| 1883 |
$$ = call_uni_op(p, cond($2), "!"); |
| 1884 |
} |
| 1885 |
| arg |
| 1886 |
; |
| 1887 |
|
| 1888 |
|
| 1889 |
defn_head : keyword_def fname |
| 1890 |
{ |
| 1891 |
$$ = new_def(p, $2, nint(p->cmdarg_stack), local_switch(p)); |
| 1892 |
p->cmdarg_stack = 0; |
| 1893 |
p->in_def++; |
| 1894 |
nvars_block(p); |
| 1895 |
} |
| 1896 |
; |
| 1897 |
|
| 1898 |
defs_head : keyword_def singleton dot_or_colon |
| 1899 |
{ |
| 1900 |
p->lstate = EXPR_FNAME; |
| 1901 |
} |
| 1902 |
fname |
| 1903 |
{ |
| 1904 |
$$ = new_sdef(p, $2, $5, nint(p->cmdarg_stack), local_switch(p)); |
| 1905 |
p->cmdarg_stack = 0; |
| 1906 |
p->in_def++; |
| 1907 |
p->in_single++; |
| 1908 |
nvars_block(p); |
| 1909 |
p->lstate = EXPR_ENDFN; /* force for args */ |
| 1910 |
} |
| 1911 |
; |
| 1912 |
|
| 1913 |
expr_value : expr |
| 1914 |
{ |
| 1915 |
if (!$1) $$ = new_nil(p); |
| 1916 |
else { |
| 1917 |
$$ = $1; |
| 1918 |
} |
| 1919 |
} |
| 1920 |
; |
| 1921 |
|
| 1922 |
command_call : command |
| 1923 |
| block_command |
| 1924 |
; |
| 1925 |
|
| 1926 |
block_command : block_call |
| 1927 |
| block_call call_op2 operation2 command_args |
| 1928 |
{ |
| 1929 |
$$ = new_call(p, $1, $3, $4, $2); |
| 1930 |
} |
| 1931 |
; |
| 1932 |
|
| 1933 |
cmd_brace_block : tLBRACE_ARG |
| 1934 |
{ |
| 1935 |
local_nest(p); |
| 1936 |
nvars_nest(p); |
| 1937 |
} |
| 1938 |
opt_block_param |
| 1939 |
compstmt |
| 1940 |
'}' |
| 1941 |
{ |
| 1942 |
$$ = new_block(p, $3, $4); |
| 1943 |
local_unnest(p); |
| 1944 |
nvars_unnest(p); |
| 1945 |
} |
| 1946 |
; |
| 1947 |
|
| 1948 |
command : operation command_args %prec tLOWEST |
| 1949 |
{ |
| 1950 |
$$ = new_fcall(p, $1, $2); |
| 1951 |
} |
| 1952 |
| operation command_args cmd_brace_block |
| 1953 |
{ |
| 1954 |
args_with_block(p, $2, $3); |
| 1955 |
$$ = new_fcall(p, $1, $2); |
| 1956 |
} |
| 1957 |
| primary_value call_op operation2 command_args %prec tLOWEST |
| 1958 |
{ |
| 1959 |
$$ = new_call(p, $1, $3, $4, $2); |
| 1960 |
} |
| 1961 |
| primary_value call_op operation2 command_args cmd_brace_block |
| 1962 |
{ |
| 1963 |
args_with_block(p, $4, $5); |
| 1964 |
$$ = new_call(p, $1, $3, $4, $2); |
| 1965 |
} |
| 1966 |
| primary_value tCOLON2 operation2 command_args %prec tLOWEST |
| 1967 |
{ |
| 1968 |
$$ = new_call(p, $1, $3, $4, tCOLON2); |
| 1969 |
} |
| 1970 |
| primary_value tCOLON2 operation2 command_args cmd_brace_block |
| 1971 |
{ |
| 1972 |
args_with_block(p, $4, $5); |
| 1973 |
$$ = new_call(p, $1, $3, $4, tCOLON2); |
| 1974 |
} |
| 1975 |
| keyword_super command_args |
| 1976 |
{ |
| 1977 |
$$ = new_super(p, $2); |
| 1978 |
} |
| 1979 |
| keyword_yield command_args |
| 1980 |
{ |
| 1981 |
$$ = new_yield(p, $2); |
| 1982 |
} |
| 1983 |
| keyword_return call_args |
| 1984 |
{ |
| 1985 |
$$ = new_return(p, ret_args(p, $2)); |
| 1986 |
} |
| 1987 |
| keyword_break call_args |
| 1988 |
{ |
| 1989 |
$$ = new_break(p, ret_args(p, $2)); |
| 1990 |
} |
| 1991 |
| keyword_next call_args |
| 1992 |
{ |
| 1993 |
$$ = new_next(p, ret_args(p, $2)); |
| 1994 |
} |
| 1995 |
; |
| 1996 |
|
| 1997 |
mlhs : mlhs_basic |
| 1998 |
{ |
| 1999 |
$$ = $1; |
| 2000 |
} |
| 2001 |
| tLPAREN mlhs_inner rparen |
| 2002 |
{ |
| 2003 |
$$ = $2; |
| 2004 |
} |
| 2005 |
; |
| 2006 |
|
| 2007 |
mlhs_inner : mlhs_basic |
| 2008 |
| tLPAREN mlhs_inner rparen |
| 2009 |
{ |
| 2010 |
$$ = $2; |
| 2011 |
} |
| 2012 |
; |
| 2013 |
|
| 2014 |
mlhs_basic : mlhs_list |
| 2015 |
{ |
| 2016 |
$$ = list1($1); |
| 2017 |
} |
| 2018 |
| mlhs_list mlhs_item |
| 2019 |
{ |
| 2020 |
$$ = list1(push($1,$2)); |
| 2021 |
} |
| 2022 |
| mlhs_list tSTAR mlhs_node |
| 2023 |
{ |
| 2024 |
$$ = list2($1, $3); |
| 2025 |
} |
| 2026 |
| mlhs_list tSTAR mlhs_node ',' mlhs_post |
| 2027 |
{ |
| 2028 |
$$ = list3($1, $3, $5); |
| 2029 |
} |
| 2030 |
| mlhs_list tSTAR |
| 2031 |
{ |
| 2032 |
$$ = list2($1, new_nil(p)); |
| 2033 |
} |
| 2034 |
| mlhs_list tSTAR ',' mlhs_post |
| 2035 |
{ |
| 2036 |
$$ = list3($1, new_nil(p), $4); |
| 2037 |
} |
| 2038 |
| tSTAR mlhs_node |
| 2039 |
{ |
| 2040 |
$$ = list2(0, $2); |
| 2041 |
} |
| 2042 |
| tSTAR mlhs_node ',' mlhs_post |
| 2043 |
{ |
| 2044 |
$$ = list3(0, $2, $4); |
| 2045 |
} |
| 2046 |
| tSTAR |
| 2047 |
{ |
| 2048 |
$$ = list2(0, new_nil(p)); |
| 2049 |
} |
| 2050 |
| tSTAR ',' mlhs_post |
| 2051 |
{ |
| 2052 |
$$ = list3(0, new_nil(p), $3); |
| 2053 |
} |
| 2054 |
; |
| 2055 |
|
| 2056 |
mlhs_item : mlhs_node |
| 2057 |
| tLPAREN mlhs_inner rparen |
| 2058 |
{ |
| 2059 |
$$ = new_masgn(p, $2, NULL); |
| 2060 |
} |
| 2061 |
; |
| 2062 |
|
| 2063 |
mlhs_list : mlhs_item ',' |
| 2064 |
{ |
| 2065 |
$$ = list1($1); |
| 2066 |
} |
| 2067 |
| mlhs_list mlhs_item ',' |
| 2068 |
{ |
| 2069 |
$$ = push($1, $2); |
| 2070 |
} |
| 2071 |
; |
| 2072 |
|
| 2073 |
mlhs_post : mlhs_item |
| 2074 |
{ |
| 2075 |
$$ = list1($1); |
| 2076 |
} |
| 2077 |
| mlhs_list mlhs_item |
| 2078 |
{ |
| 2079 |
$$ = push($1, $2); |
| 2080 |
} |
| 2081 |
; |
| 2082 |
|
| 2083 |
mlhs_node : variable |
| 2084 |
{ |
| 2085 |
assignable(p, $1); |
| 2086 |
} |
| 2087 |
| primary_value '[' opt_call_args ']' |
| 2088 |
{ |
| 2089 |
$$ = new_call(p, $1, intern_op(aref), $3, '.'); |
| 2090 |
} |
| 2091 |
| primary_value call_op tIDENTIFIER |
| 2092 |
{ |
| 2093 |
$$ = new_call(p, $1, $3, 0, $2); |
| 2094 |
} |
| 2095 |
| primary_value tCOLON2 tIDENTIFIER |
| 2096 |
{ |
| 2097 |
$$ = new_call(p, $1, $3, 0, tCOLON2); |
| 2098 |
} |
| 2099 |
| primary_value call_op tCONSTANT |
| 2100 |
{ |
| 2101 |
$$ = new_call(p, $1, $3, 0, $2); |
| 2102 |
} |
| 2103 |
| primary_value tCOLON2 tCONSTANT |
| 2104 |
{ |
| 2105 |
if (p->in_def || p->in_single) |
| 2106 |
yyerror(p, "dynamic constant assignment"); |
| 2107 |
$$ = new_colon2(p, $1, $3); |
| 2108 |
} |
| 2109 |
| tCOLON3 tCONSTANT |
| 2110 |
{ |
| 2111 |
if (p->in_def || p->in_single) |
| 2112 |
yyerror(p, "dynamic constant assignment"); |
| 2113 |
$$ = new_colon3(p, $2); |
| 2114 |
} |
| 2115 |
| backref |
| 2116 |
{ |
| 2117 |
backref_error(p, $1); |
| 2118 |
$$ = 0; |
| 2119 |
} |
| 2120 |
; |
| 2121 |
|
| 2122 |
lhs : variable |
| 2123 |
{ |
| 2124 |
assignable(p, $1); |
| 2125 |
} |
| 2126 |
| primary_value '[' opt_call_args ']' |
| 2127 |
{ |
| 2128 |
$$ = new_call(p, $1, intern_op(aref), $3, '.'); |
| 2129 |
} |
| 2130 |
| primary_value call_op tIDENTIFIER |
| 2131 |
{ |
| 2132 |
$$ = new_call(p, $1, $3, 0, $2); |
| 2133 |
} |
| 2134 |
| primary_value tCOLON2 tIDENTIFIER |
| 2135 |
{ |
| 2136 |
$$ = new_call(p, $1, $3, 0, tCOLON2); |
| 2137 |
} |
| 2138 |
| primary_value call_op tCONSTANT |
| 2139 |
{ |
| 2140 |
$$ = new_call(p, $1, $3, 0, $2); |
| 2141 |
} |
| 2142 |
| primary_value tCOLON2 tCONSTANT |
| 2143 |
{ |
| 2144 |
if (p->in_def || p->in_single) |
| 2145 |
yyerror(p, "dynamic constant assignment"); |
| 2146 |
$$ = new_colon2(p, $1, $3); |
| 2147 |
} |
| 2148 |
| tCOLON3 tCONSTANT |
| 2149 |
{ |
| 2150 |
if (p->in_def || p->in_single) |
| 2151 |
yyerror(p, "dynamic constant assignment"); |
| 2152 |
$$ = new_colon3(p, $2); |
| 2153 |
} |
| 2154 |
| backref |
| 2155 |
{ |
| 2156 |
backref_error(p, $1); |
| 2157 |
$$ = 0; |
| 2158 |
} |
| 2159 |
| tNUMPARAM |
| 2160 |
{ |
| 2161 |
yyerror(p, "can't assign to numbered parameter"); |
| 2162 |
} |
| 2163 |
; |
| 2164 |
|
| 2165 |
cname : tIDENTIFIER |
| 2166 |
{ |
| 2167 |
yyerror(p, "class/module name must be CONSTANT"); |
| 2168 |
} |
| 2169 |
| tCONSTANT |
| 2170 |
; |
| 2171 |
|
| 2172 |
cpath : tCOLON3 cname |
| 2173 |
{ |
| 2174 |
$$ = cons(nint(1), nsym($2)); |
| 2175 |
} |
| 2176 |
| cname |
| 2177 |
{ |
| 2178 |
$$ = cons(nint(0), nsym($1)); |
| 2179 |
} |
| 2180 |
| primary_value tCOLON2 cname |
| 2181 |
{ |
| 2182 |
void_expr_error(p, $1); |
| 2183 |
$$ = cons($1, nsym($3)); |
| 2184 |
} |
| 2185 |
; |
| 2186 |
|
| 2187 |
fname : tIDENTIFIER |
| 2188 |
| tCONSTANT |
| 2189 |
| tFID |
| 2190 |
| op |
| 2191 |
{ |
| 2192 |
p->lstate = EXPR_ENDFN; |
| 2193 |
$$ = $1; |
| 2194 |
} |
| 2195 |
| reswords |
| 2196 |
{ |
| 2197 |
p->lstate = EXPR_ENDFN; |
| 2198 |
$$ = $<id>1; |
| 2199 |
} |
| 2200 |
; |
| 2201 |
|
| 2202 |
fsym : fname |
| 2203 |
| basic_symbol |
| 2204 |
; |
| 2205 |
|
| 2206 |
undef_list : fsym |
| 2207 |
{ |
| 2208 |
$$ = new_undef(p, $1); |
| 2209 |
} |
| 2210 |
| undef_list ',' {p->lstate = EXPR_FNAME;} fsym |
| 2211 |
{ |
| 2212 |
$$ = push($1, nsym($4)); |
| 2213 |
} |
| 2214 |
; |
| 2215 |
|
| 2216 |
op : '|' { $$ = intern_op(or); } |
| 2217 |
| '^' { $$ = intern_op(xor); } |
| 2218 |
| '&' { $$ = intern_op(and); } |
| 2219 |
| tCMP { $$ = intern_op(cmp); } |
| 2220 |
| tEQ { $$ = intern_op(eq); } |
| 2221 |
| tEQQ { $$ = intern_op(eqq); } |
| 2222 |
| tMATCH { $$ = intern_op(match); } |
| 2223 |
| tNMATCH { $$ = intern_op(nmatch); } |
| 2224 |
| '>' { $$ = intern_op(gt); } |
| 2225 |
| tGEQ { $$ = intern_op(ge); } |
| 2226 |
| '<' { $$ = intern_op(lt); } |
| 2227 |
| tLEQ { $$ = intern_op(le); } |
| 2228 |
| tNEQ { $$ = intern_op(neq); } |
| 2229 |
| tLSHFT { $$ = intern_op(lshift); } |
| 2230 |
| tRSHFT { $$ = intern_op(rshift); } |
| 2231 |
| '+' { $$ = intern_op(add); } |
| 2232 |
| '-' { $$ = intern_op(sub); } |
| 2233 |
| '*' { $$ = intern_op(mul); } |
| 2234 |
| tSTAR { $$ = intern_op(mul); } |
| 2235 |
| '/' { $$ = intern_op(div); } |
| 2236 |
| '%' { $$ = intern_op(mod); } |
| 2237 |
| tPOW { $$ = intern_op(pow); } |
| 2238 |
| tDSTAR { $$ = intern_op(pow); } |
| 2239 |
| '!' { $$ = intern_op(not); } |
| 2240 |
| '~' { $$ = intern_op(neg); } |
| 2241 |
| tUPLUS { $$ = intern_op(plus); } |
| 2242 |
| tUMINUS { $$ = intern_op(minus); } |
| 2243 |
| tAREF { $$ = intern_op(aref); } |
| 2244 |
| tASET { $$ = intern_op(aset); } |
| 2245 |
| '`' { $$ = intern_op(tick); } |
| 2246 |
; |
| 2247 |
|
| 2248 |
reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ |
| 2249 |
| keyword_BEGIN | keyword_END |
| 2250 |
| keyword_alias | keyword_and | keyword_begin |
| 2251 |
| keyword_break | keyword_case | keyword_class | keyword_def |
| 2252 |
| keyword_do | keyword_else | keyword_elsif |
| 2253 |
| keyword_end | keyword_ensure | keyword_false |
| 2254 |
| keyword_for | keyword_in | keyword_module | keyword_next |
| 2255 |
| keyword_nil | keyword_not | keyword_or | keyword_redo |
| 2256 |
| keyword_rescue | keyword_retry | keyword_return | keyword_self |
| 2257 |
| keyword_super | keyword_then | keyword_true | keyword_undef |
| 2258 |
| keyword_when | keyword_yield | keyword_if | keyword_unless |
| 2259 |
| keyword_while | keyword_until |
| 2260 |
; |
| 2261 |
|
| 2262 |
arg : lhs '=' arg_rhs |
| 2263 |
{ |
| 2264 |
$$ = new_asgn(p, $1, $3); |
| 2265 |
} |
| 2266 |
| var_lhs tOP_ASGN arg_rhs |
| 2267 |
{ |
| 2268 |
$$ = new_op_asgn(p, $1, $2, $3); |
| 2269 |
} |
| 2270 |
| primary_value '[' opt_call_args ']' tOP_ASGN arg_rhs |
| 2271 |
{ |
| 2272 |
$$ = new_op_asgn(p, new_call(p, $1, intern_op(aref), $3, '.'), $5, $6); |
| 2273 |
} |
| 2274 |
| primary_value call_op tIDENTIFIER tOP_ASGN arg_rhs |
| 2275 |
{ |
| 2276 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, $2), $4, $5); |
| 2277 |
} |
| 2278 |
| primary_value call_op tCONSTANT tOP_ASGN arg_rhs |
| 2279 |
{ |
| 2280 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, $2), $4, $5); |
| 2281 |
} |
| 2282 |
| primary_value tCOLON2 tIDENTIFIER tOP_ASGN arg_rhs |
| 2283 |
{ |
| 2284 |
$$ = new_op_asgn(p, new_call(p, $1, $3, 0, tCOLON2), $4, $5); |
| 2285 |
} |
| 2286 |
| primary_value tCOLON2 tCONSTANT tOP_ASGN arg_rhs |
| 2287 |
{ |
| 2288 |
yyerror(p, "constant re-assignment"); |
| 2289 |
$$ = new_begin(p, 0); |
| 2290 |
} |
| 2291 |
| tCOLON3 tCONSTANT tOP_ASGN arg_rhs |
| 2292 |
{ |
| 2293 |
yyerror(p, "constant re-assignment"); |
| 2294 |
$$ = new_begin(p, 0); |
| 2295 |
} |
| 2296 |
| backref tOP_ASGN arg_rhs |
| 2297 |
{ |
| 2298 |
backref_error(p, $1); |
| 2299 |
$$ = new_begin(p, 0); |
| 2300 |
} |
| 2301 |
| arg tDOT2 arg |
| 2302 |
{ |
| 2303 |
$$ = new_dot2(p, $1, $3); |
| 2304 |
} |
| 2305 |
| arg tDOT2 |
| 2306 |
{ |
| 2307 |
$$ = new_dot2(p, $1, new_nil(p)); |
| 2308 |
} |
| 2309 |
| tBDOT2 arg |
| 2310 |
{ |
| 2311 |
$$ = new_dot2(p, new_nil(p), $2); |
| 2312 |
} |
| 2313 |
| arg tDOT3 arg |
| 2314 |
{ |
| 2315 |
$$ = new_dot3(p, $1, $3); |
| 2316 |
} |
| 2317 |
| arg tDOT3 |
| 2318 |
{ |
| 2319 |
$$ = new_dot3(p, $1, new_nil(p)); |
| 2320 |
} |
| 2321 |
| tBDOT3 arg |
| 2322 |
{ |
| 2323 |
$$ = new_dot3(p, new_nil(p), $2); |
| 2324 |
} |
| 2325 |
| arg '+' arg |
| 2326 |
{ |
| 2327 |
$$ = call_bin_op(p, $1, "+", $3); |
| 2328 |
} |
| 2329 |
| arg '-' arg |
| 2330 |
{ |
| 2331 |
$$ = call_bin_op(p, $1, "-", $3); |
| 2332 |
} |
| 2333 |
| arg '*' arg |
| 2334 |
{ |
| 2335 |
$$ = call_bin_op(p, $1, "*", $3); |
| 2336 |
} |
| 2337 |
| arg '/' arg |
| 2338 |
{ |
| 2339 |
$$ = call_bin_op(p, $1, "/", $3); |
| 2340 |
} |
| 2341 |
| arg '%' arg |
| 2342 |
{ |
| 2343 |
$$ = call_bin_op(p, $1, "%", $3); |
| 2344 |
} |
| 2345 |
| arg tPOW arg |
| 2346 |
{ |
| 2347 |
$$ = call_bin_op(p, $1, "**", $3); |
| 2348 |
} |
| 2349 |
| tUMINUS_NUM tINTEGER tPOW arg |
| 2350 |
{ |
| 2351 |
$$ = new_negate(p, call_bin_op(p, $2, "**", $4)); |
| 2352 |
} |
| 2353 |
| tUMINUS_NUM tFLOAT tPOW arg |
| 2354 |
{ |
| 2355 |
$$ = new_negate(p, call_bin_op(p, $2, "**", $4)); |
| 2356 |
} |
| 2357 |
| tUPLUS arg |
| 2358 |
{ |
| 2359 |
$$ = call_uni_op(p, $2, "+@"); |
| 2360 |
} |
| 2361 |
| tUMINUS arg |
| 2362 |
{ |
| 2363 |
$$ = new_negate(p, $2); |
| 2364 |
} |
| 2365 |
| arg '|' arg |
| 2366 |
{ |
| 2367 |
$$ = call_bin_op(p, $1, "|", $3); |
| 2368 |
} |
| 2369 |
| arg '^' arg |
| 2370 |
{ |
| 2371 |
$$ = call_bin_op(p, $1, "^", $3); |
| 2372 |
} |
| 2373 |
| arg '&' arg |
| 2374 |
{ |
| 2375 |
$$ = call_bin_op(p, $1, "&", $3); |
| 2376 |
} |
| 2377 |
| arg tCMP arg |
| 2378 |
{ |
| 2379 |
$$ = call_bin_op(p, $1, "<=>", $3); |
| 2380 |
} |
| 2381 |
| arg '>' arg |
| 2382 |
{ |
| 2383 |
$$ = call_bin_op(p, $1, ">", $3); |
| 2384 |
} |
| 2385 |
| arg tGEQ arg |
| 2386 |
{ |
| 2387 |
$$ = call_bin_op(p, $1, ">=", $3); |
| 2388 |
} |
| 2389 |
| arg '<' arg |
| 2390 |
{ |
| 2391 |
$$ = call_bin_op(p, $1, "<", $3); |
| 2392 |
} |
| 2393 |
| arg tLEQ arg |
| 2394 |
{ |
| 2395 |
$$ = call_bin_op(p, $1, "<=", $3); |
| 2396 |
} |
| 2397 |
| arg tEQ arg |
| 2398 |
{ |
| 2399 |
$$ = call_bin_op(p, $1, "==", $3); |
| 2400 |
} |
| 2401 |
| arg tEQQ arg |
| 2402 |
{ |
| 2403 |
$$ = call_bin_op(p, $1, "===", $3); |
| 2404 |
} |
| 2405 |
| arg tNEQ arg |
| 2406 |
{ |
| 2407 |
$$ = call_bin_op(p, $1, "!=", $3); |
| 2408 |
} |
| 2409 |
| arg tMATCH arg |
| 2410 |
{ |
| 2411 |
$$ = call_bin_op(p, $1, "=~", $3); |
| 2412 |
} |
| 2413 |
| arg tNMATCH arg |
| 2414 |
{ |
| 2415 |
$$ = call_bin_op(p, $1, "!~", $3); |
| 2416 |
} |
| 2417 |
| '!' arg |
| 2418 |
{ |
| 2419 |
$$ = call_uni_op(p, cond($2), "!"); |
| 2420 |
} |
| 2421 |
| '~' arg |
| 2422 |
{ |
| 2423 |
$$ = call_uni_op(p, cond($2), "~"); |
| 2424 |
} |
| 2425 |
| arg tLSHFT arg |
| 2426 |
{ |
| 2427 |
$$ = call_bin_op(p, $1, "<<", $3); |
| 2428 |
} |
| 2429 |
| arg tRSHFT arg |
| 2430 |
{ |
| 2431 |
$$ = call_bin_op(p, $1, ">>", $3); |
| 2432 |
} |
| 2433 |
| arg tANDOP arg |
| 2434 |
{ |
| 2435 |
$$ = new_and(p, $1, $3); |
| 2436 |
} |
| 2437 |
| arg tOROP arg |
| 2438 |
{ |
| 2439 |
$$ = new_or(p, $1, $3); |
| 2440 |
} |
| 2441 |
| arg '?' arg opt_nl ':' arg |
| 2442 |
{ |
| 2443 |
$$ = new_if(p, cond($1), $3, $6); |
| 2444 |
} |
| 2445 |
| arg '?' arg opt_nl tLABEL_TAG arg |
| 2446 |
{ |
| 2447 |
$$ = new_if(p, cond($1), $3, $6); |
| 2448 |
} |
| 2449 |
| defn_head f_opt_arglist_paren '=' arg |
| 2450 |
{ |
| 2451 |
$$ = $1; |
| 2452 |
endless_method_name(p, $1); |
| 2453 |
void_expr_error(p, $4); |
| 2454 |
defn_setup(p, $$, $2, $4); |
| 2455 |
nvars_unnest(p); |
| 2456 |
p->in_def--; |
| 2457 |
} |
| 2458 |
| defn_head f_opt_arglist_paren '=' arg modifier_rescue arg |
| 2459 |
{ |
| 2460 |
$$ = $1; |
| 2461 |
endless_method_name(p, $1); |
| 2462 |
void_expr_error(p, $4); |
| 2463 |
defn_setup(p, $$, $2, new_mod_rescue(p, $4, $6)); |
| 2464 |
nvars_unnest(p); |
| 2465 |
p->in_def--; |
| 2466 |
} |
| 2467 |
| defs_head f_opt_arglist_paren '=' arg |
| 2468 |
{ |
| 2469 |
$$ = $1; |
| 2470 |
void_expr_error(p, $4); |
| 2471 |
defs_setup(p, $$, $2, $4); |
| 2472 |
nvars_unnest(p); |
| 2473 |
p->in_def--; |
| 2474 |
p->in_single--; |
| 2475 |
} |
| 2476 |
| defs_head f_opt_arglist_paren '=' arg modifier_rescue arg |
| 2477 |
{ |
| 2478 |
$$ = $1; |
| 2479 |
void_expr_error(p, $4); |
| 2480 |
defs_setup(p, $$, $2, new_mod_rescue(p, $4, $6)); |
| 2481 |
nvars_unnest(p); |
| 2482 |
p->in_def--; |
| 2483 |
p->in_single--; |
| 2484 |
} |
| 2485 |
| primary |
| 2486 |
{ |
| 2487 |
$$ = $1; |
| 2488 |
} |
| 2489 |
; |
| 2490 |
|
| 2491 |
aref_args : none |
| 2492 |
| args trailer |
| 2493 |
{ |
| 2494 |
$$ = $1; |
| 2495 |
NODE_LINENO($$, $1); |
| 2496 |
} |
| 2497 |
| args comma assocs trailer |
| 2498 |
{ |
| 2499 |
$$ = push($1, new_hash(p, $3)); |
| 2500 |
} |
| 2501 |
| assocs trailer |
| 2502 |
{ |
| 2503 |
$$ = cons(new_kw_hash(p, $1), 0); |
| 2504 |
NODE_LINENO($$, $1); |
| 2505 |
} |
| 2506 |
; |
| 2507 |
|
| 2508 |
arg_rhs : arg %prec tOP_ASGN |
| 2509 |
{ |
| 2510 |
$$ = $1; |
| 2511 |
} |
| 2512 |
| arg modifier_rescue arg |
| 2513 |
{ |
| 2514 |
void_expr_error(p, $1); |
| 2515 |
$$ = new_mod_rescue(p, $1, $3); |
| 2516 |
} |
| 2517 |
; |
| 2518 |
|
| 2519 |
paren_args : '(' opt_call_args ')' |
| 2520 |
{ |
| 2521 |
$$ = $2; |
| 2522 |
} |
| 2523 |
| '(' args comma tBDOT3 rparen |
| 2524 |
{ |
| 2525 |
mrb_sym r = intern_op(mul); |
| 2526 |
mrb_sym k = intern_op(pow); |
| 2527 |
mrb_sym b = intern_op(and); |
| 2528 |
$$ = new_callargs(p, push($2, new_splat(p, new_lvar(p, r))), |
| 2529 |
new_kw_hash(p, list1(cons(new_kw_rest_args(p, 0), new_lvar(p, k)))), |
| 2530 |
new_block_arg(p, new_lvar(p, b))); |
| 2531 |
} |
| 2532 |
| '(' tBDOT3 rparen |
| 2533 |
{ |
| 2534 |
mrb_sym r = intern_op(mul); |
| 2535 |
mrb_sym k = intern_op(pow); |
| 2536 |
mrb_sym b = intern_op(and); |
| 2537 |
if (local_var_p(p, r) && local_var_p(p, k) && local_var_p(p, b)) { |
| 2538 |
$$ = new_callargs(p, list1(new_splat(p, new_lvar(p, r))), |
| 2539 |
new_kw_hash(p, list1(cons(new_kw_rest_args(p, 0), new_lvar(p, k)))), |
| 2540 |
new_block_arg(p, new_lvar(p, b))); |
| 2541 |
} |
| 2542 |
else { |
| 2543 |
yyerror(p, "unexpected argument forwarding ..."); |
| 2544 |
$$ = 0; |
| 2545 |
} |
| 2546 |
} |
| 2547 |
; |
| 2548 |
|
| 2549 |
opt_paren_args : none |
| 2550 |
| paren_args |
| 2551 |
; |
| 2552 |
|
| 2553 |
opt_call_args : none |
| 2554 |
| call_args opt_terms |
| 2555 |
| args comma |
| 2556 |
{ |
| 2557 |
$$ = new_callargs(p,$1,0,0); |
| 2558 |
NODE_LINENO($$, $1); |
| 2559 |
} |
| 2560 |
| args comma assocs comma |
| 2561 |
{ |
| 2562 |
$$ = new_callargs(p,$1,new_kw_hash(p,$3),0); |
| 2563 |
NODE_LINENO($$, $1); |
| 2564 |
} |
| 2565 |
| assocs comma |
| 2566 |
{ |
| 2567 |
$$ = new_callargs(p,0,new_kw_hash(p,$1),0); |
| 2568 |
NODE_LINENO($$, $1); |
| 2569 |
} |
| 2570 |
; |
| 2571 |
|
| 2572 |
call_args : command |
| 2573 |
{ |
| 2574 |
void_expr_error(p, $1); |
| 2575 |
$$ = new_callargs(p, list1($1), 0, 0); |
| 2576 |
NODE_LINENO($$, $1); |
| 2577 |
} |
| 2578 |
| args opt_block_arg |
| 2579 |
{ |
| 2580 |
$$ = new_callargs(p, $1, 0, $2); |
| 2581 |
NODE_LINENO($$, $1); |
| 2582 |
} |
| 2583 |
| assocs opt_block_arg |
| 2584 |
{ |
| 2585 |
$$ = new_callargs(p, 0, new_kw_hash(p, $1), $2); |
| 2586 |
NODE_LINENO($$, $1); |
| 2587 |
} |
| 2588 |
| args comma assocs opt_block_arg |
| 2589 |
{ |
| 2590 |
$$ = new_callargs(p, $1, new_kw_hash(p, $3), $4); |
| 2591 |
NODE_LINENO($$, $1); |
| 2592 |
} |
| 2593 |
| block_arg |
| 2594 |
{ |
| 2595 |
$$ = new_callargs(p, 0, 0, $1); |
| 2596 |
NODE_LINENO($$, $1); |
| 2597 |
} |
| 2598 |
; |
| 2599 |
|
| 2600 |
command_args : { |
| 2601 |
$<stack>$ = p->cmdarg_stack; |
| 2602 |
CMDARG_PUSH(1); |
| 2603 |
} |
| 2604 |
call_args |
| 2605 |
{ |
| 2606 |
p->cmdarg_stack = $<stack>1; |
| 2607 |
$$ = $2; |
| 2608 |
} |
| 2609 |
; |
| 2610 |
|
| 2611 |
block_arg : tAMPER arg |
| 2612 |
{ |
| 2613 |
$$ = new_block_arg(p, $2); |
| 2614 |
} |
| 2615 |
| tAMPER |
| 2616 |
{ |
| 2617 |
$$ = new_block_arg(p, 0); |
| 2618 |
} |
| 2619 |
; |
| 2620 |
|
| 2621 |
opt_block_arg : comma block_arg |
| 2622 |
{ |
| 2623 |
$$ = $2; |
| 2624 |
} |
| 2625 |
| none |
| 2626 |
{ |
| 2627 |
$$ = 0; |
| 2628 |
} |
| 2629 |
; |
| 2630 |
|
| 2631 |
comma : ',' opt_nl |
| 2632 |
; |
| 2633 |
|
| 2634 |
args : arg |
| 2635 |
{ |
| 2636 |
void_expr_error(p, $1); |
| 2637 |
$$ = list1($1); |
| 2638 |
NODE_LINENO($$, $1); |
| 2639 |
} |
| 2640 |
| tSTAR |
| 2641 |
{ |
| 2642 |
$$ = list1(new_splat(p, new_lvar(p, intern_op(mul)))); |
| 2643 |
} |
| 2644 |
| tSTAR arg |
| 2645 |
{ |
| 2646 |
$$ = list1(new_splat(p, $2)); |
| 2647 |
NODE_LINENO($$, $2); |
| 2648 |
} |
| 2649 |
| args comma arg |
| 2650 |
{ |
| 2651 |
void_expr_error(p, $3); |
| 2652 |
$$ = push($1, $3); |
| 2653 |
} |
| 2654 |
| args comma tSTAR |
| 2655 |
{ |
| 2656 |
$$ = push($1, new_splat(p, new_lvar(p, intern_op(mul)))); |
| 2657 |
} |
| 2658 |
| args comma tSTAR arg |
| 2659 |
{ |
| 2660 |
$$ = push($1, new_splat(p, $4)); |
| 2661 |
} |
| 2662 |
; |
| 2663 |
|
| 2664 |
mrhs : args comma arg |
| 2665 |
{ |
| 2666 |
void_expr_error(p, $3); |
| 2667 |
$$ = push($1, $3); |
| 2668 |
} |
| 2669 |
| args comma tSTAR arg |
| 2670 |
{ |
| 2671 |
$$ = push($1, new_splat(p, $4)); |
| 2672 |
} |
| 2673 |
| tSTAR arg |
| 2674 |
{ |
| 2675 |
$$ = list1(new_splat(p, $2)); |
| 2676 |
} |
| 2677 |
; |
| 2678 |
|
| 2679 |
primary : literal |
| 2680 |
| string |
| 2681 |
| xstring |
| 2682 |
| regexp |
| 2683 |
| heredoc |
| 2684 |
| var_ref |
| 2685 |
| backref |
| 2686 |
| tNUMPARAM |
| 2687 |
{ |
| 2688 |
$$ = new_nvar(p, $1); |
| 2689 |
} |
| 2690 |
| tFID |
| 2691 |
{ |
| 2692 |
$$ = new_fcall(p, $1, 0); |
| 2693 |
} |
| 2694 |
| keyword_begin |
| 2695 |
{ |
| 2696 |
$<stack>$ = p->cmdarg_stack; |
| 2697 |
p->cmdarg_stack = 0; |
| 2698 |
} |
| 2699 |
bodystmt |
| 2700 |
keyword_end |
| 2701 |
{ |
| 2702 |
p->cmdarg_stack = $<stack>2; |
| 2703 |
$$ = $3; |
| 2704 |
} |
| 2705 |
| tLPAREN_ARG |
| 2706 |
{ |
| 2707 |
$<stack>$ = p->cmdarg_stack; |
| 2708 |
p->cmdarg_stack = 0; |
| 2709 |
} |
| 2710 |
stmt {p->lstate = EXPR_ENDARG;} rparen |
| 2711 |
{ |
| 2712 |
p->cmdarg_stack = $<stack>2; |
| 2713 |
$$ = $3; |
| 2714 |
} |
| 2715 |
| tLPAREN_ARG {p->lstate = EXPR_ENDARG;} rparen |
| 2716 |
{ |
| 2717 |
$$ = new_nil(p); |
| 2718 |
} |
| 2719 |
| tLPAREN compstmt ')' |
| 2720 |
{ |
| 2721 |
$$ = $2; |
| 2722 |
} |
| 2723 |
| primary_value tCOLON2 tCONSTANT |
| 2724 |
{ |
| 2725 |
$$ = new_colon2(p, $1, $3); |
| 2726 |
} |
| 2727 |
| tCOLON3 tCONSTANT |
| 2728 |
{ |
| 2729 |
$$ = new_colon3(p, $2); |
| 2730 |
} |
| 2731 |
| tLBRACK aref_args ']' |
| 2732 |
{ |
| 2733 |
$$ = new_array(p, $2); |
| 2734 |
NODE_LINENO($$, $2); |
| 2735 |
} |
| 2736 |
| tLBRACE assoc_list '}' |
| 2737 |
{ |
| 2738 |
$$ = new_hash(p, $2); |
| 2739 |
NODE_LINENO($$, $2); |
| 2740 |
} |
| 2741 |
| keyword_return |
| 2742 |
{ |
| 2743 |
$$ = new_return(p, 0); |
| 2744 |
} |
| 2745 |
| keyword_yield opt_paren_args |
| 2746 |
{ |
| 2747 |
$$ = new_yield(p, $2); |
| 2748 |
} |
| 2749 |
| keyword_not '(' expr rparen |
| 2750 |
{ |
| 2751 |
$$ = call_uni_op(p, cond($3), "!"); |
| 2752 |
} |
| 2753 |
| keyword_not '(' rparen |
| 2754 |
{ |
| 2755 |
$$ = call_uni_op(p, new_nil(p), "!"); |
| 2756 |
} |
| 2757 |
| operation brace_block |
| 2758 |
{ |
| 2759 |
$$ = new_fcall(p, $1, new_callargs(p, 0, 0, $2)); |
| 2760 |
} |
| 2761 |
| method_call |
| 2762 |
| method_call brace_block |
| 2763 |
{ |
| 2764 |
call_with_block(p, $1, $2); |
| 2765 |
$$ = $1; |
| 2766 |
} |
| 2767 |
| tLAMBDA |
| 2768 |
{ |
| 2769 |
local_nest(p); |
| 2770 |
nvars_nest(p); |
| 2771 |
$<num>$ = p->lpar_beg; |
| 2772 |
p->lpar_beg = ++p->paren_nest; |
| 2773 |
} |
| 2774 |
f_larglist |
| 2775 |
{ |
| 2776 |
$<stack>$ = p->cmdarg_stack; |
| 2777 |
p->cmdarg_stack = 0; |
| 2778 |
} |
| 2779 |
lambda_body |
| 2780 |
{ |
| 2781 |
p->lpar_beg = $<num>2; |
| 2782 |
$$ = new_lambda(p, $3, $5); |
| 2783 |
local_unnest(p); |
| 2784 |
nvars_unnest(p); |
| 2785 |
p->cmdarg_stack = $<stack>4; |
| 2786 |
CMDARG_LEXPOP(); |
| 2787 |
} |
| 2788 |
| keyword_if expr_value then |
| 2789 |
compstmt |
| 2790 |
if_tail |
| 2791 |
keyword_end |
| 2792 |
{ |
| 2793 |
$$ = new_if(p, cond($2), $4, $5); |
| 2794 |
SET_LINENO($$, $1); |
| 2795 |
} |
| 2796 |
| keyword_unless expr_value then |
| 2797 |
compstmt |
| 2798 |
opt_else |
| 2799 |
keyword_end |
| 2800 |
{ |
| 2801 |
$$ = new_unless(p, cond($2), $4, $5); |
| 2802 |
SET_LINENO($$, $1); |
| 2803 |
} |
| 2804 |
| keyword_while {COND_PUSH(1);} expr_value do {COND_POP();} |
| 2805 |
compstmt |
| 2806 |
keyword_end |
| 2807 |
{ |
| 2808 |
$$ = new_while(p, cond($3), $6); |
| 2809 |
SET_LINENO($$, $1); |
| 2810 |
} |
| 2811 |
| keyword_until {COND_PUSH(1);} expr_value do {COND_POP();} |
| 2812 |
compstmt |
| 2813 |
keyword_end |
| 2814 |
{ |
| 2815 |
$$ = new_until(p, cond($3), $6); |
| 2816 |
SET_LINENO($$, $1); |
| 2817 |
} |
| 2818 |
| keyword_case expr_value opt_terms |
| 2819 |
case_body |
| 2820 |
keyword_end |
| 2821 |
{ |
| 2822 |
$$ = new_case(p, $2, $4); |
| 2823 |
} |
| 2824 |
| keyword_case opt_terms case_body keyword_end |
| 2825 |
{ |
| 2826 |
$$ = new_case(p, 0, $3); |
| 2827 |
} |
| 2828 |
| keyword_for for_var keyword_in |
| 2829 |
{COND_PUSH(1);} |
| 2830 |
expr_value do |
| 2831 |
{COND_POP();} |
| 2832 |
compstmt |
| 2833 |
keyword_end |
| 2834 |
{ |
| 2835 |
$$ = new_for(p, $2, $5, $8); |
| 2836 |
SET_LINENO($$, $1); |
| 2837 |
} |
| 2838 |
| keyword_class |
| 2839 |
cpath superclass |
| 2840 |
{ |
| 2841 |
if (p->in_def || p->in_single) |
| 2842 |
yyerror(p, "class definition in method body"); |
| 2843 |
$<nd>$ = local_switch(p); |
| 2844 |
nvars_block(p); |
| 2845 |
} |
| 2846 |
bodystmt |
| 2847 |
keyword_end |
| 2848 |
{ |
| 2849 |
$$ = new_class(p, $2, $3, $5); |
| 2850 |
SET_LINENO($$, $1); |
| 2851 |
local_resume(p, $<nd>4); |
| 2852 |
nvars_unnest(p); |
| 2853 |
} |
| 2854 |
| keyword_class |
| 2855 |
tLSHFT expr |
| 2856 |
{ |
| 2857 |
$<num>$ = p->in_def; |
| 2858 |
p->in_def = 0; |
| 2859 |
} |
| 2860 |
term |
| 2861 |
{ |
| 2862 |
$<nd>$ = cons(local_switch(p), nint(p->in_single)); |
| 2863 |
nvars_block(p); |
| 2864 |
p->in_single = 0; |
| 2865 |
} |
| 2866 |
bodystmt |
| 2867 |
keyword_end |
| 2868 |
{ |
| 2869 |
$$ = new_sclass(p, $3, $7); |
| 2870 |
SET_LINENO($$, $1); |
| 2871 |
local_resume(p, $<nd>6->car); |
| 2872 |
nvars_unnest(p); |
| 2873 |
p->in_def = $<num>4; |
| 2874 |
p->in_single = intn($<nd>6->cdr); |
| 2875 |
} |
| 2876 |
| keyword_module |
| 2877 |
cpath |
| 2878 |
{ |
| 2879 |
if (p->in_def || p->in_single) |
| 2880 |
yyerror(p, "module definition in method body"); |
| 2881 |
$<nd>$ = local_switch(p); |
| 2882 |
nvars_block(p); |
| 2883 |
} |
| 2884 |
bodystmt |
| 2885 |
keyword_end |
| 2886 |
{ |
| 2887 |
$$ = new_module(p, $2, $4); |
| 2888 |
SET_LINENO($$, $1); |
| 2889 |
local_resume(p, $<nd>3); |
| 2890 |
nvars_unnest(p); |
| 2891 |
} |
| 2892 |
| defn_head |
| 2893 |
f_arglist |
| 2894 |
bodystmt |
| 2895 |
keyword_end |
| 2896 |
{ |
| 2897 |
$$ = $1; |
| 2898 |
defn_setup(p, $$, $2, $3); |
| 2899 |
nvars_unnest(p); |
| 2900 |
p->in_def--; |
| 2901 |
} |
| 2902 |
| defs_head |
| 2903 |
f_arglist |
| 2904 |
bodystmt |
| 2905 |
keyword_end |
| 2906 |
{ |
| 2907 |
$$ = $1; |
| 2908 |
defs_setup(p, $$, $2, $3); |
| 2909 |
nvars_unnest(p); |
| 2910 |
p->in_def--; |
| 2911 |
p->in_single--; |
| 2912 |
} |
| 2913 |
| keyword_break |
| 2914 |
{ |
| 2915 |
$$ = new_break(p, 0); |
| 2916 |
} |
| 2917 |
| keyword_next |
| 2918 |
{ |
| 2919 |
$$ = new_next(p, 0); |
| 2920 |
} |
| 2921 |
| keyword_redo |
| 2922 |
{ |
| 2923 |
$$ = new_redo(p); |
| 2924 |
} |
| 2925 |
| keyword_retry |
| 2926 |
{ |
| 2927 |
$$ = new_retry(p); |
| 2928 |
} |
| 2929 |
; |
| 2930 |
|
| 2931 |
primary_value : primary |
| 2932 |
{ |
| 2933 |
$$ = $1; |
| 2934 |
if (!$$) $$ = new_nil(p); |
| 2935 |
} |
| 2936 |
; |
| 2937 |
|
| 2938 |
then : term |
| 2939 |
| keyword_then |
| 2940 |
| term keyword_then |
| 2941 |
; |
| 2942 |
|
| 2943 |
do : term |
| 2944 |
| keyword_do_cond |
| 2945 |
; |
| 2946 |
|
| 2947 |
if_tail : opt_else |
| 2948 |
| keyword_elsif expr_value then |
| 2949 |
compstmt |
| 2950 |
if_tail |
| 2951 |
{ |
| 2952 |
$$ = new_if(p, cond($2), $4, $5); |
| 2953 |
} |
| 2954 |
; |
| 2955 |
|
| 2956 |
opt_else : none |
| 2957 |
| keyword_else compstmt |
| 2958 |
{ |
| 2959 |
$$ = $2; |
| 2960 |
} |
| 2961 |
; |
| 2962 |
|
| 2963 |
for_var : lhs |
| 2964 |
{ |
| 2965 |
$$ = list1(list1($1)); |
| 2966 |
} |
| 2967 |
| mlhs |
| 2968 |
; |
| 2969 |
|
| 2970 |
f_margs : f_arg |
| 2971 |
{ |
| 2972 |
$$ = list3($1,0,0); |
| 2973 |
} |
| 2974 |
| f_arg ',' tSTAR f_norm_arg |
| 2975 |
{ |
| 2976 |
$$ = list3($1, new_arg(p, $4), 0); |
| 2977 |
} |
| 2978 |
| f_arg ',' tSTAR f_norm_arg ',' f_arg |
| 2979 |
{ |
| 2980 |
$$ = list3($1, new_arg(p, $4), $6); |
| 2981 |
} |
| 2982 |
| f_arg ',' tSTAR |
| 2983 |
{ |
| 2984 |
local_add_f(p, intern_op(mul)); |
| 2985 |
$$ = list3($1, nint(-1), 0); |
| 2986 |
} |
| 2987 |
| f_arg ',' tSTAR ',' f_arg |
| 2988 |
{ |
| 2989 |
$$ = list3($1, nint(-1), $5); |
| 2990 |
} |
| 2991 |
| tSTAR f_norm_arg |
| 2992 |
{ |
| 2993 |
$$ = list3(0, new_arg(p, $2), 0); |
| 2994 |
} |
| 2995 |
| tSTAR f_norm_arg ',' f_arg |
| 2996 |
{ |
| 2997 |
$$ = list3(0, new_arg(p, $2), $4); |
| 2998 |
} |
| 2999 |
| tSTAR |
| 3000 |
{ |
| 3001 |
local_add_f(p, intern_op(mul)); |
| 3002 |
$$ = list3(0, nint(-1), 0); |
| 3003 |
} |
| 3004 |
| tSTAR ',' |
| 3005 |
{ |
| 3006 |
local_add_f(p, intern_op(mul)); |
| 3007 |
} |
| 3008 |
f_arg |
| 3009 |
{ |
| 3010 |
$$ = list3(0, nint(-1), $4); |
| 3011 |
} |
| 3012 |
; |
| 3013 |
|
| 3014 |
block_args_tail : f_block_kwarg ',' f_kwrest opt_f_block_arg |
| 3015 |
{ |
| 3016 |
$$ = new_args_tail(p, $1, $3, $4); |
| 3017 |
} |
| 3018 |
| f_block_kwarg opt_f_block_arg |
| 3019 |
{ |
| 3020 |
$$ = new_args_tail(p, $1, 0, $2); |
| 3021 |
} |
| 3022 |
| f_kwrest opt_f_block_arg |
| 3023 |
{ |
| 3024 |
$$ = new_args_tail(p, 0, $1, $2); |
| 3025 |
} |
| 3026 |
| f_block_arg |
| 3027 |
{ |
| 3028 |
$$ = new_args_tail(p, 0, 0, $1); |
| 3029 |
} |
| 3030 |
; |
| 3031 |
|
| 3032 |
opt_block_args_tail : ',' block_args_tail |
| 3033 |
{ |
| 3034 |
$$ = $2; |
| 3035 |
} |
| 3036 |
| /* none */ |
| 3037 |
{ |
| 3038 |
$$ = new_args_tail(p, 0, 0, 0); |
| 3039 |
} |
| 3040 |
; |
| 3041 |
|
| 3042 |
block_param : f_arg ',' f_block_optarg ',' f_rest_arg opt_block_args_tail |
| 3043 |
{ |
| 3044 |
$$ = new_args(p, $1, $3, $5, 0, $6); |
| 3045 |
} |
| 3046 |
| f_arg ',' f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail |
| 3047 |
{ |
| 3048 |
$$ = new_args(p, $1, $3, $5, $7, $8); |
| 3049 |
} |
| 3050 |
| f_arg ',' f_block_optarg opt_block_args_tail |
| 3051 |
{ |
| 3052 |
$$ = new_args(p, $1, $3, 0, 0, $4); |
| 3053 |
} |
| 3054 |
| f_arg ',' f_block_optarg ',' f_arg opt_block_args_tail |
| 3055 |
{ |
| 3056 |
$$ = new_args(p, $1, $3, 0, $5, $6); |
| 3057 |
} |
| 3058 |
| f_arg ',' f_rest_arg opt_block_args_tail |
| 3059 |
{ |
| 3060 |
$$ = new_args(p, $1, 0, $3, 0, $4); |
| 3061 |
} |
| 3062 |
| f_arg ',' opt_block_args_tail |
| 3063 |
{ |
| 3064 |
$$ = new_args(p, $1, 0, 0, 0, $3); |
| 3065 |
} |
| 3066 |
| f_arg ',' f_rest_arg ',' f_arg opt_block_args_tail |
| 3067 |
{ |
| 3068 |
$$ = new_args(p, $1, 0, $3, $5, $6); |
| 3069 |
} |
| 3070 |
| f_arg opt_block_args_tail |
| 3071 |
{ |
| 3072 |
$$ = new_args(p, $1, 0, 0, 0, $2); |
| 3073 |
} |
| 3074 |
| f_block_optarg ',' f_rest_arg opt_block_args_tail |
| 3075 |
{ |
| 3076 |
$$ = new_args(p, 0, $1, $3, 0, $4); |
| 3077 |
} |
| 3078 |
| f_block_optarg ',' f_rest_arg ',' f_arg opt_block_args_tail |
| 3079 |
{ |
| 3080 |
$$ = new_args(p, 0, $1, $3, $5, $6); |
| 3081 |
} |
| 3082 |
| f_block_optarg opt_block_args_tail |
| 3083 |
{ |
| 3084 |
$$ = new_args(p, 0, $1, 0, 0, $2); |
| 3085 |
} |
| 3086 |
| f_block_optarg ',' f_arg opt_block_args_tail |
| 3087 |
{ |
| 3088 |
$$ = new_args(p, 0, $1, 0, $3, $4); |
| 3089 |
} |
| 3090 |
| f_rest_arg opt_block_args_tail |
| 3091 |
{ |
| 3092 |
$$ = new_args(p, 0, 0, $1, 0, $2); |
| 3093 |
} |
| 3094 |
| f_rest_arg ',' f_arg opt_block_args_tail |
| 3095 |
{ |
| 3096 |
$$ = new_args(p, 0, 0, $1, $3, $4); |
| 3097 |
} |
| 3098 |
| block_args_tail |
| 3099 |
{ |
| 3100 |
$$ = new_args(p, 0, 0, 0, 0, $1); |
| 3101 |
} |
| 3102 |
; |
| 3103 |
|
| 3104 |
opt_block_param : none |
| 3105 |
{ |
| 3106 |
local_add_blk(p, 0); |
| 3107 |
$$ = 0; |
| 3108 |
} |
| 3109 |
| block_param_def |
| 3110 |
{ |
| 3111 |
p->cmd_start = TRUE; |
| 3112 |
$$ = $1; |
| 3113 |
} |
| 3114 |
; |
| 3115 |
|
| 3116 |
block_param_def : '|' {local_add_blk(p, 0);} opt_bv_decl '|' |
| 3117 |
{ |
| 3118 |
$$ = 0; |
| 3119 |
} |
| 3120 |
| tOROP |
| 3121 |
{ |
| 3122 |
local_add_blk(p, 0); |
| 3123 |
$$ = 0; |
| 3124 |
} |
| 3125 |
| '|' block_param opt_bv_decl '|' |
| 3126 |
{ |
| 3127 |
$$ = $2; |
| 3128 |
} |
| 3129 |
; |
| 3130 |
|
| 3131 |
|
| 3132 |
opt_bv_decl : opt_nl |
| 3133 |
{ |
| 3134 |
$$ = 0; |
| 3135 |
} |
| 3136 |
| opt_nl ';' bv_decls opt_nl |
| 3137 |
{ |
| 3138 |
$$ = 0; |
| 3139 |
} |
| 3140 |
; |
| 3141 |
|
| 3142 |
bv_decls : bvar |
| 3143 |
| bv_decls ',' bvar |
| 3144 |
; |
| 3145 |
|
| 3146 |
bvar : tIDENTIFIER |
| 3147 |
{ |
| 3148 |
local_add_f(p, $1); |
| 3149 |
new_bv(p, $1); |
| 3150 |
} |
| 3151 |
| f_bad_arg |
| 3152 |
; |
| 3153 |
|
| 3154 |
f_larglist : '(' f_args opt_bv_decl ')' |
| 3155 |
{ |
| 3156 |
$$ = $2; |
| 3157 |
} |
| 3158 |
| f_args |
| 3159 |
{ |
| 3160 |
$$ = $1; |
| 3161 |
} |
| 3162 |
; |
| 3163 |
|
| 3164 |
lambda_body : tLAMBEG compstmt '}' |
| 3165 |
{ |
| 3166 |
$$ = $2; |
| 3167 |
} |
| 3168 |
| keyword_do_LAMBDA bodystmt keyword_end |
| 3169 |
{ |
| 3170 |
$$ = $2; |
| 3171 |
} |
| 3172 |
; |
| 3173 |
|
| 3174 |
do_block : keyword_do_block |
| 3175 |
{ |
| 3176 |
local_nest(p); |
| 3177 |
nvars_nest(p); |
| 3178 |
$<num>$ = p->lineno; |
| 3179 |
} |
| 3180 |
opt_block_param |
| 3181 |
bodystmt |
| 3182 |
keyword_end |
| 3183 |
{ |
| 3184 |
$$ = new_block(p,$3,$4); |
| 3185 |
SET_LINENO($$, $<num>2); |
| 3186 |
local_unnest(p); |
| 3187 |
nvars_unnest(p); |
| 3188 |
} |
| 3189 |
; |
| 3190 |
|
| 3191 |
block_call : command do_block |
| 3192 |
{ |
| 3193 |
if (typen($1->car) == NODE_YIELD) { |
| 3194 |
yyerror(p, "block given to yield"); |
| 3195 |
} |
| 3196 |
else { |
| 3197 |
call_with_block(p, $1, $2); |
| 3198 |
} |
| 3199 |
$$ = $1; |
| 3200 |
} |
| 3201 |
| block_call call_op2 operation2 opt_paren_args |
| 3202 |
{ |
| 3203 |
$$ = new_call(p, $1, $3, $4, $2); |
| 3204 |
} |
| 3205 |
| block_call call_op2 operation2 opt_paren_args brace_block |
| 3206 |
{ |
| 3207 |
$$ = new_call(p, $1, $3, $4, $2); |
| 3208 |
call_with_block(p, $$, $5); |
| 3209 |
} |
| 3210 |
| block_call call_op2 operation2 command_args do_block |
| 3211 |
{ |
| 3212 |
$$ = new_call(p, $1, $3, $4, $2); |
| 3213 |
call_with_block(p, $$, $5); |
| 3214 |
} |
| 3215 |
; |
| 3216 |
|
| 3217 |
method_call : operation paren_args |
| 3218 |
{ |
| 3219 |
$$ = new_fcall(p, $1, $2); |
| 3220 |
} |
| 3221 |
| primary_value call_op operation2 opt_paren_args |
| 3222 |
{ |
| 3223 |
$$ = new_call(p, $1, $3, $4, $2); |
| 3224 |
} |
| 3225 |
| primary_value tCOLON2 operation2 paren_args |
| 3226 |
{ |
| 3227 |
$$ = new_call(p, $1, $3, $4, tCOLON2); |
| 3228 |
} |
| 3229 |
| primary_value tCOLON2 operation3 |
| 3230 |
{ |
| 3231 |
$$ = new_call(p, $1, $3, 0, tCOLON2); |
| 3232 |
} |
| 3233 |
| primary_value call_op paren_args |
| 3234 |
{ |
| 3235 |
$$ = new_call(p, $1, MRB_SYM_2(p->mrb, call), $3, $2); |
| 3236 |
} |
| 3237 |
| primary_value tCOLON2 paren_args |
| 3238 |
{ |
| 3239 |
$$ = new_call(p, $1, MRB_SYM_2(p->mrb, call), $3, tCOLON2); |
| 3240 |
} |
| 3241 |
| keyword_super paren_args |
| 3242 |
{ |
| 3243 |
$$ = new_super(p, $2); |
| 3244 |
} |
| 3245 |
| keyword_super |
| 3246 |
{ |
| 3247 |
$$ = new_zsuper(p); |
| 3248 |
} |
| 3249 |
| primary_value '[' opt_call_args ']' |
| 3250 |
{ |
| 3251 |
$$ = new_call(p, $1, intern_op(aref), $3, '.'); |
| 3252 |
} |
| 3253 |
; |
| 3254 |
|
| 3255 |
brace_block : '{' |
| 3256 |
{ |
| 3257 |
local_nest(p); |
| 3258 |
nvars_nest(p); |
| 3259 |
$<num>$ = p->lineno; |
| 3260 |
} |
| 3261 |
opt_block_param |
| 3262 |
compstmt '}' |
| 3263 |
{ |
| 3264 |
$$ = new_block(p,$3,$4); |
| 3265 |
SET_LINENO($$, $<num>2); |
| 3266 |
local_unnest(p); |
| 3267 |
nvars_unnest(p); |
| 3268 |
} |
| 3269 |
| keyword_do |
| 3270 |
{ |
| 3271 |
local_nest(p); |
| 3272 |
nvars_nest(p); |
| 3273 |
$<num>$ = p->lineno; |
| 3274 |
} |
| 3275 |
opt_block_param |
| 3276 |
bodystmt keyword_end |
| 3277 |
{ |
| 3278 |
$$ = new_block(p,$3,$4); |
| 3279 |
SET_LINENO($$, $<num>2); |
| 3280 |
local_unnest(p); |
| 3281 |
nvars_unnest(p); |
| 3282 |
} |
| 3283 |
; |
| 3284 |
|
| 3285 |
case_body : keyword_when args then |
| 3286 |
compstmt |
| 3287 |
cases |
| 3288 |
{ |
| 3289 |
$$ = cons(cons($2, $4), $5); |
| 3290 |
} |
| 3291 |
; |
| 3292 |
|
| 3293 |
cases : opt_else |
| 3294 |
{ |
| 3295 |
if ($1) { |
| 3296 |
$$ = cons(cons(0, $1), 0); |
| 3297 |
} |
| 3298 |
else { |
| 3299 |
$$ = 0; |
| 3300 |
} |
| 3301 |
} |
| 3302 |
| case_body |
| 3303 |
; |
| 3304 |
|
| 3305 |
opt_rescue : keyword_rescue exc_list exc_var then |
| 3306 |
compstmt |
| 3307 |
opt_rescue |
| 3308 |
{ |
| 3309 |
$$ = list1(list3($2, $3, $5)); |
| 3310 |
if ($6) $$ = append($$, $6); |
| 3311 |
} |
| 3312 |
| none |
| 3313 |
; |
| 3314 |
|
| 3315 |
exc_list : arg |
| 3316 |
{ |
| 3317 |
$$ = list1($1); |
| 3318 |
} |
| 3319 |
| mrhs |
| 3320 |
| none |
| 3321 |
; |
| 3322 |
|
| 3323 |
exc_var : tASSOC lhs |
| 3324 |
{ |
| 3325 |
$$ = $2; |
| 3326 |
} |
| 3327 |
| none |
| 3328 |
; |
| 3329 |
|
| 3330 |
opt_ensure : keyword_ensure compstmt |
| 3331 |
{ |
| 3332 |
$$ = $2; |
| 3333 |
} |
| 3334 |
| none |
| 3335 |
; |
| 3336 |
|
| 3337 |
literal : numeric |
| 3338 |
| symbol |
| 3339 |
| words |
| 3340 |
| symbols |
| 3341 |
; |
| 3342 |
|
| 3343 |
string : string_fragment |
| 3344 |
| string string_fragment |
| 3345 |
{ |
| 3346 |
$$ = concat_string(p, $1, $2); |
| 3347 |
} |
| 3348 |
; |
| 3349 |
|
| 3350 |
string_fragment : tCHAR |
| 3351 |
| tSTRING |
| 3352 |
| tSTRING_BEG tSTRING |
| 3353 |
{ |
| 3354 |
$$ = $2; |
| 3355 |
} |
| 3356 |
| tSTRING_BEG string_rep tSTRING |
| 3357 |
{ |
| 3358 |
node *n = $2; |
| 3359 |
if (intn($3->cdr->cdr) > 0) { |
| 3360 |
n = push(n, $3); |
| 3361 |
} |
| 3362 |
else { |
| 3363 |
cons_free($3); |
| 3364 |
} |
| 3365 |
$$ = new_dstr(p, n); |
| 3366 |
} |
| 3367 |
; |
| 3368 |
|
| 3369 |
string_rep : string_interp |
| 3370 |
| string_rep string_interp |
| 3371 |
{ |
| 3372 |
$$ = append($1, $2); |
| 3373 |
} |
| 3374 |
; |
| 3375 |
|
| 3376 |
string_interp : tSTRING_MID |
| 3377 |
{ |
| 3378 |
$$ = list1($1); |
| 3379 |
} |
| 3380 |
| tSTRING_PART |
| 3381 |
{ |
| 3382 |
$<nd>$ = push_strterm(p); |
| 3383 |
} |
| 3384 |
compstmt |
| 3385 |
'}' |
| 3386 |
{ |
| 3387 |
pop_strterm(p,$<nd>2); |
| 3388 |
$$ = list2($1, $3); |
| 3389 |
} |
| 3390 |
| tLITERAL_DELIM |
| 3391 |
{ |
| 3392 |
$$ = list1(new_literal_delim(p)); |
| 3393 |
} |
| 3394 |
| tHD_LITERAL_DELIM heredoc_bodies |
| 3395 |
{ |
| 3396 |
$$ = list1(new_literal_delim(p)); |
| 3397 |
} |
| 3398 |
; |
| 3399 |
|
| 3400 |
xstring : tXSTRING_BEG tXSTRING |
| 3401 |
{ |
| 3402 |
$$ = $2; |
| 3403 |
} |
| 3404 |
| tXSTRING_BEG string_rep tXSTRING |
| 3405 |
{ |
| 3406 |
node *n = $2; |
| 3407 |
if (intn($3->cdr->cdr) > 0) { |
| 3408 |
n = push(n, $3); |
| 3409 |
} |
| 3410 |
else { |
| 3411 |
cons_free($3); |
| 3412 |
} |
| 3413 |
$$ = new_dxstr(p, n); |
| 3414 |
} |
| 3415 |
; |
| 3416 |
|
| 3417 |
regexp : tREGEXP_BEG tREGEXP |
| 3418 |
{ |
| 3419 |
$$ = $2; |
| 3420 |
} |
| 3421 |
| tREGEXP_BEG string_rep tREGEXP |
| 3422 |
{ |
| 3423 |
$$ = new_dregx(p, $2, $3); |
| 3424 |
} |
| 3425 |
; |
| 3426 |
|
| 3427 |
heredoc : tHEREDOC_BEG |
| 3428 |
; |
| 3429 |
|
| 3430 |
heredoc_bodies : heredoc_body |
| 3431 |
| heredoc_bodies heredoc_body |
| 3432 |
; |
| 3433 |
|
| 3434 |
heredoc_body : tHEREDOC_END |
| 3435 |
{ |
| 3436 |
parser_heredoc_info *info = parsing_heredoc_info(p); |
| 3437 |
info->doc = push(info->doc, new_str(p, "", 0)); |
| 3438 |
heredoc_end(p); |
| 3439 |
} |
| 3440 |
| heredoc_string_rep tHEREDOC_END |
| 3441 |
{ |
| 3442 |
heredoc_end(p); |
| 3443 |
} |
| 3444 |
; |
| 3445 |
|
| 3446 |
heredoc_string_rep : heredoc_string_interp |
| 3447 |
| heredoc_string_rep heredoc_string_interp |
| 3448 |
; |
| 3449 |
|
| 3450 |
heredoc_string_interp : tHD_STRING_MID |
| 3451 |
{ |
| 3452 |
parser_heredoc_info *info = parsing_heredoc_info(p); |
| 3453 |
info->doc = push(info->doc, $1); |
| 3454 |
heredoc_treat_nextline(p); |
| 3455 |
} |
| 3456 |
| tHD_STRING_PART |
| 3457 |
{ |
| 3458 |
$<nd>$ = push_strterm(p); |
| 3459 |
} |
| 3460 |
compstmt |
| 3461 |
'}' |
| 3462 |
{ |
| 3463 |
pop_strterm(p, $<nd>2); |
| 3464 |
parser_heredoc_info *info = parsing_heredoc_info(p); |
| 3465 |
info->doc = push(push(info->doc, $1), $3); |
| 3466 |
} |
| 3467 |
; |
| 3468 |
|
| 3469 |
words : tWORDS_BEG tSTRING |
| 3470 |
{ |
| 3471 |
$$ = new_words(p, list1($2)); |
| 3472 |
} |
| 3473 |
| tWORDS_BEG string_rep tSTRING |
| 3474 |
{ |
| 3475 |
node *n = $2; |
| 3476 |
if (intn($3->cdr->cdr) > 0) { |
| 3477 |
n = push(n, $3); |
| 3478 |
} |
| 3479 |
else { |
| 3480 |
cons_free($3); |
| 3481 |
} |
| 3482 |
$$ = new_words(p, n); |
| 3483 |
} |
| 3484 |
; |
| 3485 |
|
| 3486 |
|
| 3487 |
symbol : basic_symbol |
| 3488 |
{ |
| 3489 |
p->lstate = EXPR_ENDARG; |
| 3490 |
$$ = new_sym(p, $1); |
| 3491 |
} |
| 3492 |
| tSYMBEG tSTRING_BEG string_rep tSTRING |
| 3493 |
{ |
| 3494 |
node *n = $3; |
| 3495 |
p->lstate = EXPR_ENDARG; |
| 3496 |
if (intn($4->cdr->cdr) > 0) { |
| 3497 |
n = push(n, $4); |
| 3498 |
} |
| 3499 |
else { |
| 3500 |
cons_free($4); |
| 3501 |
} |
| 3502 |
$$ = new_dsym(p, new_dstr(p, n)); |
| 3503 |
} |
| 3504 |
; |
| 3505 |
|
| 3506 |
basic_symbol : tSYMBEG sym |
| 3507 |
{ |
| 3508 |
$$ = $2; |
| 3509 |
} |
| 3510 |
; |
| 3511 |
|
| 3512 |
sym : fname |
| 3513 |
| tIVAR |
| 3514 |
| tGVAR |
| 3515 |
| tCVAR |
| 3516 |
| tSTRING |
| 3517 |
{ |
| 3518 |
$$ = new_strsym(p, $1); |
| 3519 |
} |
| 3520 |
| tSTRING_BEG tSTRING |
| 3521 |
{ |
| 3522 |
$$ = new_strsym(p, $2); |
| 3523 |
} |
| 3524 |
; |
| 3525 |
|
| 3526 |
symbols : tSYMBOLS_BEG tSTRING |
| 3527 |
{ |
| 3528 |
$$ = new_symbols(p, list1($2)); |
| 3529 |
} |
| 3530 |
| tSYMBOLS_BEG string_rep tSTRING |
| 3531 |
{ |
| 3532 |
node *n = $2; |
| 3533 |
if (intn($3->cdr->cdr) > 0) { |
| 3534 |
n = push(n, $3); |
| 3535 |
} |
| 3536 |
$$ = new_symbols(p, n); |
| 3537 |
} |
| 3538 |
; |
| 3539 |
|
| 3540 |
numeric : tINTEGER |
| 3541 |
| tFLOAT |
| 3542 |
| tUMINUS_NUM tINTEGER %prec tLOWEST |
| 3543 |
{ |
| 3544 |
$$ = new_negate(p, $2); |
| 3545 |
} |
| 3546 |
| tUMINUS_NUM tFLOAT %prec tLOWEST |
| 3547 |
{ |
| 3548 |
$$ = new_negate(p, $2); |
| 3549 |
} |
| 3550 |
; |
| 3551 |
|
| 3552 |
variable : tIDENTIFIER |
| 3553 |
{ |
| 3554 |
$$ = new_lvar(p, $1); |
| 3555 |
} |
| 3556 |
| tIVAR |
| 3557 |
{ |
| 3558 |
$$ = new_ivar(p, $1); |
| 3559 |
} |
| 3560 |
| tGVAR |
| 3561 |
{ |
| 3562 |
$$ = new_gvar(p, $1); |
| 3563 |
} |
| 3564 |
| tCVAR |
| 3565 |
{ |
| 3566 |
$$ = new_cvar(p, $1); |
| 3567 |
} |
| 3568 |
| tCONSTANT |
| 3569 |
{ |
| 3570 |
$$ = new_const(p, $1); |
| 3571 |
} |
| 3572 |
; |
| 3573 |
|
| 3574 |
var_lhs : variable |
| 3575 |
{ |
| 3576 |
assignable(p, $1); |
| 3577 |
} |
| 3578 |
| tNUMPARAM |
| 3579 |
{ |
| 3580 |
yyerror(p, "can't assign to numbered parameter"); |
| 3581 |
} |
| 3582 |
; |
| 3583 |
|
| 3584 |
var_ref : variable |
| 3585 |
{ |
| 3586 |
$$ = var_reference(p, $1); |
| 3587 |
} |
| 3588 |
| keyword_nil |
| 3589 |
{ |
| 3590 |
$$ = new_nil(p); |
| 3591 |
} |
| 3592 |
| keyword_self |
| 3593 |
{ |
| 3594 |
$$ = new_self(p); |
| 3595 |
} |
| 3596 |
| keyword_true |
| 3597 |
{ |
| 3598 |
$$ = new_true(p); |
| 3599 |
} |
| 3600 |
| keyword_false |
| 3601 |
{ |
| 3602 |
$$ = new_false(p); |
| 3603 |
} |
| 3604 |
| keyword__FILE__ |
| 3605 |
{ |
| 3606 |
const char *fn = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); |
| 3607 |
if (!fn) { |
| 3608 |
fn = "(null)"; |
| 3609 |
} |
| 3610 |
$$ = new_str(p, fn, strlen(fn)); |
| 3611 |
} |
| 3612 |
| keyword__LINE__ |
| 3613 |
{ |
| 3614 |
char buf[16]; |
| 3615 |
|
| 3616 |
dump_int(p->lineno, buf); |
| 3617 |
$$ = new_int(p, buf, 10, 0); |
| 3618 |
} |
| 3619 |
| keyword__ENCODING__ |
| 3620 |
{ |
| 3621 |
$$ = new_fcall(p, MRB_SYM_2(p->mrb, __ENCODING__), 0); |
| 3622 |
} |
| 3623 |
; |
| 3624 |
|
| 3625 |
backref : tNTH_REF |
| 3626 |
| tBACK_REF |
| 3627 |
; |
| 3628 |
|
| 3629 |
superclass : /* term */ |
| 3630 |
{ |
| 3631 |
$$ = 0; |
| 3632 |
} |
| 3633 |
| '<' |
| 3634 |
{ |
| 3635 |
p->lstate = EXPR_BEG; |
| 3636 |
p->cmd_start = TRUE; |
| 3637 |
} |
| 3638 |
expr_value term |
| 3639 |
{ |
| 3640 |
$$ = $3; |
| 3641 |
} /* |
| 3642 |
| error term |
| 3643 |
{ |
| 3644 |
yyerrok; |
| 3645 |
$$ = 0; |
| 3646 |
} */ |
| 3647 |
; |
| 3648 |
|
| 3649 |
f_opt_arglist_paren |
| 3650 |
: f_arglist_paren |
| 3651 |
| none |
| 3652 |
; |
| 3653 |
|
| 3654 |
f_arglist_paren : '(' f_args rparen |
| 3655 |
{ |
| 3656 |
$$ = $2; |
| 3657 |
p->lstate = EXPR_BEG; |
| 3658 |
p->cmd_start = TRUE; |
| 3659 |
} |
| 3660 |
| '(' f_arg ',' tBDOT3 rparen |
| 3661 |
{ |
| 3662 |
$$ = new_args_dots(p, $2); |
| 3663 |
} |
| 3664 |
| '(' tBDOT3 rparen |
| 3665 |
{ |
| 3666 |
$$ = new_args_dots(p, 0); |
| 3667 |
} |
| 3668 |
; |
| 3669 |
|
| 3670 |
f_arglist : f_arglist_paren |
| 3671 |
| f_args term |
| 3672 |
{ |
| 3673 |
$$ = $1; |
| 3674 |
} |
| 3675 |
| f_arg ',' tBDOT3 term |
| 3676 |
{ |
| 3677 |
$$ = new_args_dots(p, $1); |
| 3678 |
} |
| 3679 |
| tDOT3 term |
| 3680 |
{ |
| 3681 |
$$ = new_args_dots(p, 0); |
| 3682 |
} |
| 3683 |
; |
| 3684 |
|
| 3685 |
f_label : tIDENTIFIER tLABEL_TAG |
| 3686 |
{ |
| 3687 |
local_nest(p); |
| 3688 |
} |
| 3689 |
| tNUMPARAM tLABEL_TAG |
| 3690 |
{ |
| 3691 |
local_nest(p); |
| 3692 |
} |
| 3693 |
; |
| 3694 |
|
| 3695 |
f_kw : f_label arg |
| 3696 |
{ |
| 3697 |
void_expr_error(p, $2); |
| 3698 |
$$ = new_kw_arg(p, $1, cons($2, locals_node(p))); |
| 3699 |
local_unnest(p); |
| 3700 |
} |
| 3701 |
| f_label |
| 3702 |
{ |
| 3703 |
$$ = new_kw_arg(p, $1, 0); |
| 3704 |
local_unnest(p); |
| 3705 |
} |
| 3706 |
; |
| 3707 |
|
| 3708 |
f_block_kw : f_label primary_value |
| 3709 |
{ |
| 3710 |
void_expr_error(p, $2); |
| 3711 |
$$ = new_kw_arg(p, $1, cons($2, locals_node(p))); |
| 3712 |
local_unnest(p); |
| 3713 |
} |
| 3714 |
| f_label |
| 3715 |
{ |
| 3716 |
$$ = new_kw_arg(p, $1, 0); |
| 3717 |
local_unnest(p); |
| 3718 |
} |
| 3719 |
; |
| 3720 |
|
| 3721 |
f_block_kwarg : f_block_kw |
| 3722 |
{ |
| 3723 |
$$ = list1($1); |
| 3724 |
} |
| 3725 |
| f_block_kwarg ',' f_block_kw |
| 3726 |
{ |
| 3727 |
$$ = push($1, $3); |
| 3728 |
} |
| 3729 |
; |
| 3730 |
|
| 3731 |
f_kwarg : f_kw |
| 3732 |
{ |
| 3733 |
$$ = list1($1); |
| 3734 |
} |
| 3735 |
| f_kwarg ',' f_kw |
| 3736 |
{ |
| 3737 |
$$ = push($1, $3); |
| 3738 |
} |
| 3739 |
; |
| 3740 |
|
| 3741 |
kwrest_mark : tPOW |
| 3742 |
| tDSTAR |
| 3743 |
; |
| 3744 |
|
| 3745 |
f_kwrest : kwrest_mark tIDENTIFIER |
| 3746 |
{ |
| 3747 |
$$ = new_kw_rest_args(p, $2); |
| 3748 |
} |
| 3749 |
| kwrest_mark |
| 3750 |
{ |
| 3751 |
$$ = new_kw_rest_args(p, 0); |
| 3752 |
} |
| 3753 |
; |
| 3754 |
|
| 3755 |
args_tail : f_kwarg ',' f_kwrest opt_f_block_arg |
| 3756 |
{ |
| 3757 |
$$ = new_args_tail(p, $1, $3, $4); |
| 3758 |
} |
| 3759 |
| f_kwarg opt_f_block_arg |
| 3760 |
{ |
| 3761 |
$$ = new_args_tail(p, $1, 0, $2); |
| 3762 |
} |
| 3763 |
| f_kwrest opt_f_block_arg |
| 3764 |
{ |
| 3765 |
$$ = new_args_tail(p, 0, $1, $2); |
| 3766 |
} |
| 3767 |
| f_block_arg |
| 3768 |
{ |
| 3769 |
$$ = new_args_tail(p, 0, 0, $1); |
| 3770 |
} |
| 3771 |
; |
| 3772 |
|
| 3773 |
opt_args_tail : ',' args_tail |
| 3774 |
{ |
| 3775 |
$$ = $2; |
| 3776 |
} |
| 3777 |
| /* none */ |
| 3778 |
{ |
| 3779 |
$$ = new_args_tail(p, 0, 0, 0); |
| 3780 |
} |
| 3781 |
; |
| 3782 |
|
| 3783 |
f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail |
| 3784 |
{ |
| 3785 |
$$ = new_args(p, $1, $3, $5, 0, $6); |
| 3786 |
} |
| 3787 |
| f_arg ',' f_optarg ',' f_rest_arg ',' f_arg opt_args_tail |
| 3788 |
{ |
| 3789 |
$$ = new_args(p, $1, $3, $5, $7, $8); |
| 3790 |
} |
| 3791 |
| f_arg ',' f_optarg opt_args_tail |
| 3792 |
{ |
| 3793 |
$$ = new_args(p, $1, $3, 0, 0, $4); |
| 3794 |
} |
| 3795 |
| f_arg ',' f_optarg ',' f_arg opt_args_tail |
| 3796 |
{ |
| 3797 |
$$ = new_args(p, $1, $3, 0, $5, $6); |
| 3798 |
} |
| 3799 |
| f_arg ',' f_rest_arg opt_args_tail |
| 3800 |
{ |
| 3801 |
$$ = new_args(p, $1, 0, $3, 0, $4); |
| 3802 |
} |
| 3803 |
| f_arg ',' f_rest_arg ',' f_arg opt_args_tail |
| 3804 |
{ |
| 3805 |
$$ = new_args(p, $1, 0, $3, $5, $6); |
| 3806 |
} |
| 3807 |
| f_arg opt_args_tail |
| 3808 |
{ |
| 3809 |
$$ = new_args(p, $1, 0, 0, 0, $2); |
| 3810 |
} |
| 3811 |
| f_optarg ',' f_rest_arg opt_args_tail |
| 3812 |
{ |
| 3813 |
$$ = new_args(p, 0, $1, $3, 0, $4); |
| 3814 |
} |
| 3815 |
| f_optarg ',' f_rest_arg ',' f_arg opt_args_tail |
| 3816 |
{ |
| 3817 |
$$ = new_args(p, 0, $1, $3, $5, $6); |
| 3818 |
} |
| 3819 |
| f_optarg opt_args_tail |
| 3820 |
{ |
| 3821 |
$$ = new_args(p, 0, $1, 0, 0, $2); |
| 3822 |
} |
| 3823 |
| f_optarg ',' f_arg opt_args_tail |
| 3824 |
{ |
| 3825 |
$$ = new_args(p, 0, $1, 0, $3, $4); |
| 3826 |
} |
| 3827 |
| f_rest_arg opt_args_tail |
| 3828 |
{ |
| 3829 |
$$ = new_args(p, 0, 0, $1, 0, $2); |
| 3830 |
} |
| 3831 |
| f_rest_arg ',' f_arg opt_args_tail |
| 3832 |
{ |
| 3833 |
$$ = new_args(p, 0, 0, $1, $3, $4); |
| 3834 |
} |
| 3835 |
| args_tail |
| 3836 |
{ |
| 3837 |
$$ = new_args(p, 0, 0, 0, 0, $1); |
| 3838 |
} |
| 3839 |
| /* none */ |
| 3840 |
{ |
| 3841 |
local_add_f(p, 0); |
| 3842 |
$$ = new_args(p, 0, 0, 0, 0, 0); |
| 3843 |
} |
| 3844 |
; |
| 3845 |
|
| 3846 |
f_bad_arg : tCONSTANT |
| 3847 |
{ |
| 3848 |
yyerror(p, "formal argument cannot be a constant"); |
| 3849 |
$$ = 0; |
| 3850 |
} |
| 3851 |
| tIVAR |
| 3852 |
{ |
| 3853 |
yyerror(p, "formal argument cannot be an instance variable"); |
| 3854 |
$$ = 0; |
| 3855 |
} |
| 3856 |
| tGVAR |
| 3857 |
{ |
| 3858 |
yyerror(p, "formal argument cannot be a global variable"); |
| 3859 |
$$ = 0; |
| 3860 |
} |
| 3861 |
| tCVAR |
| 3862 |
{ |
| 3863 |
yyerror(p, "formal argument cannot be a class variable"); |
| 3864 |
$$ = 0; |
| 3865 |
} |
| 3866 |
| tNUMPARAM |
| 3867 |
{ |
| 3868 |
yyerror(p, "formal argument cannot be a numbered parameter"); |
| 3869 |
$$ = 0; |
| 3870 |
} |
| 3871 |
; |
| 3872 |
|
| 3873 |
f_norm_arg : f_bad_arg |
| 3874 |
{ |
| 3875 |
$$ = 0; |
| 3876 |
} |
| 3877 |
| tIDENTIFIER |
| 3878 |
{ |
| 3879 |
local_add_f(p, $1); |
| 3880 |
$$ = $1; |
| 3881 |
} |
| 3882 |
; |
| 3883 |
|
| 3884 |
f_arg_item : f_norm_arg |
| 3885 |
{ |
| 3886 |
$$ = new_arg(p, $1); |
| 3887 |
} |
| 3888 |
| tLPAREN |
| 3889 |
{ |
| 3890 |
$<nd>$ = local_switch(p); |
| 3891 |
} |
| 3892 |
f_margs rparen |
| 3893 |
{ |
| 3894 |
$$ = new_masgn_param(p, $3, p->locals->car); |
| 3895 |
local_resume(p, $<nd>2); |
| 3896 |
local_add_f(p, 0); |
| 3897 |
} |
| 3898 |
; |
| 3899 |
|
| 3900 |
f_arg : f_arg_item |
| 3901 |
{ |
| 3902 |
$$ = list1($1); |
| 3903 |
} |
| 3904 |
| f_arg ',' f_arg_item |
| 3905 |
{ |
| 3906 |
$$ = push($1, $3); |
| 3907 |
} |
| 3908 |
; |
| 3909 |
|
| 3910 |
f_opt_asgn : tIDENTIFIER '=' |
| 3911 |
{ |
| 3912 |
local_add_f(p, $1); |
| 3913 |
local_nest(p); |
| 3914 |
$$ = $1; |
| 3915 |
} |
| 3916 |
; |
| 3917 |
|
| 3918 |
f_opt : f_opt_asgn arg |
| 3919 |
{ |
| 3920 |
void_expr_error(p, $2); |
| 3921 |
$$ = cons(nsym($1), cons($2, locals_node(p))); |
| 3922 |
local_unnest(p); |
| 3923 |
} |
| 3924 |
; |
| 3925 |
|
| 3926 |
f_block_opt : f_opt_asgn primary_value |
| 3927 |
{ |
| 3928 |
void_expr_error(p, $2); |
| 3929 |
$$ = cons(nsym($1), cons($2, locals_node(p))); |
| 3930 |
local_unnest(p); |
| 3931 |
} |
| 3932 |
; |
| 3933 |
|
| 3934 |
f_block_optarg : f_block_opt |
| 3935 |
{ |
| 3936 |
$$ = list1($1); |
| 3937 |
} |
| 3938 |
| f_block_optarg ',' f_block_opt |
| 3939 |
{ |
| 3940 |
$$ = push($1, $3); |
| 3941 |
} |
| 3942 |
; |
| 3943 |
|
| 3944 |
f_optarg : f_opt |
| 3945 |
{ |
| 3946 |
$$ = list1($1); |
| 3947 |
} |
| 3948 |
| f_optarg ',' f_opt |
| 3949 |
{ |
| 3950 |
$$ = push($1, $3); |
| 3951 |
} |
| 3952 |
; |
| 3953 |
|
| 3954 |
restarg_mark : '*' |
| 3955 |
| tSTAR |
| 3956 |
; |
| 3957 |
|
| 3958 |
f_rest_arg : restarg_mark tIDENTIFIER |
| 3959 |
{ |
| 3960 |
local_add_f(p, $2); |
| 3961 |
$$ = $2; |
| 3962 |
} |
| 3963 |
| restarg_mark |
| 3964 |
{ |
| 3965 |
$$ = intern_op(mul); |
| 3966 |
local_add_f(p, $$); |
| 3967 |
} |
| 3968 |
; |
| 3969 |
|
| 3970 |
blkarg_mark : '&' |
| 3971 |
| tAMPER |
| 3972 |
; |
| 3973 |
|
| 3974 |
f_block_arg : blkarg_mark tIDENTIFIER |
| 3975 |
{ |
| 3976 |
$$ = $2; |
| 3977 |
} |
| 3978 |
| blkarg_mark |
| 3979 |
{ |
| 3980 |
$$ = intern_op(and); |
| 3981 |
} |
| 3982 |
; |
| 3983 |
|
| 3984 |
opt_f_block_arg : ',' f_block_arg |
| 3985 |
{ |
| 3986 |
$$ = $2; |
| 3987 |
} |
| 3988 |
| none |
| 3989 |
{ |
| 3990 |
$$ = 0; |
| 3991 |
} |
| 3992 |
; |
| 3993 |
|
| 3994 |
singleton : var_ref |
| 3995 |
{ |
| 3996 |
$$ = $1; |
| 3997 |
if (!$$) $$ = new_nil(p); |
| 3998 |
} |
| 3999 |
| '(' {p->lstate = EXPR_BEG;} expr rparen |
| 4000 |
{ |
| 4001 |
if ($3 == 0) { |
| 4002 |
yyerror(p, "can't define singleton method for ()."); |
| 4003 |
} |
| 4004 |
else { |
| 4005 |
switch (typen($3->car)) { |
| 4006 |
case NODE_STR: |
| 4007 |
case NODE_DSTR: |
| 4008 |
case NODE_XSTR: |
| 4009 |
case NODE_DXSTR: |
| 4010 |
case NODE_DREGX: |
| 4011 |
case NODE_MATCH: |
| 4012 |
case NODE_FLOAT: |
| 4013 |
case NODE_ARRAY: |
| 4014 |
case NODE_HEREDOC: |
| 4015 |
yyerror(p, "can't define singleton method for literals"); |
| 4016 |
default: |
| 4017 |
break; |
| 4018 |
} |
| 4019 |
} |
| 4020 |
$$ = $3; |
| 4021 |
} |
| 4022 |
; |
| 4023 |
|
| 4024 |
assoc_list : none |
| 4025 |
| assocs trailer |
| 4026 |
{ |
| 4027 |
$$ = $1; |
| 4028 |
} |
| 4029 |
; |
| 4030 |
|
| 4031 |
assocs : assoc |
| 4032 |
{ |
| 4033 |
$$ = list1($1); |
| 4034 |
NODE_LINENO($$, $1); |
| 4035 |
} |
| 4036 |
| assocs comma assoc |
| 4037 |
{ |
| 4038 |
$$ = push($1, $3); |
| 4039 |
} |
| 4040 |
; |
| 4041 |
|
| 4042 |
assoc : arg tASSOC arg |
| 4043 |
{ |
| 4044 |
void_expr_error(p, $1); |
| 4045 |
void_expr_error(p, $3); |
| 4046 |
$$ = cons($1, $3); |
| 4047 |
} |
| 4048 |
| tIDENTIFIER tLABEL_TAG arg |
| 4049 |
{ |
| 4050 |
void_expr_error(p, $3); |
| 4051 |
$$ = cons(new_sym(p, $1), $3); |
| 4052 |
} |
| 4053 |
| tIDENTIFIER tLABEL_TAG |
| 4054 |
{ |
| 4055 |
$$ = cons(new_sym(p, $1), label_reference(p, $1)); |
| 4056 |
} |
| 4057 |
| tNUMPARAM tLABEL_TAG |
| 4058 |
{ |
| 4059 |
mrb_sym sym = intern_numparam($1); |
| 4060 |
$$ = cons(new_sym(p, sym), label_reference(p, sym)); |
| 4061 |
} |
| 4062 |
| tNUMPARAM tLABEL_TAG arg |
| 4063 |
{ |
| 4064 |
void_expr_error(p, $3); |
| 4065 |
$$ = cons(new_sym(p, intern_numparam($1)), $3); |
| 4066 |
} |
| 4067 |
| string_fragment tLABEL_TAG arg |
| 4068 |
{ |
| 4069 |
void_expr_error(p, $3); |
| 4070 |
if (typen($1->car) == NODE_DSTR) { |
| 4071 |
$$ = cons(new_dsym(p, $1), $3); |
| 4072 |
} |
| 4073 |
else { |
| 4074 |
$$ = cons(new_sym(p, new_strsym(p, $1)), $3); |
| 4075 |
} |
| 4076 |
} |
| 4077 |
| tDSTAR arg |
| 4078 |
{ |
| 4079 |
void_expr_error(p, $2); |
| 4080 |
$$ = cons(new_kw_rest_args(p, 0), $2); |
| 4081 |
} |
| 4082 |
| tDSTAR |
| 4083 |
{ |
| 4084 |
$$ = cons(new_kw_rest_args(p, 0), new_lvar(p, intern_op(pow))); |
| 4085 |
} |
| 4086 |
; |
| 4087 |
|
| 4088 |
operation : tIDENTIFIER |
| 4089 |
| tCONSTANT |
| 4090 |
| tFID |
| 4091 |
; |
| 4092 |
|
| 4093 |
operation2 : tIDENTIFIER |
| 4094 |
| tCONSTANT |
| 4095 |
| tFID |
| 4096 |
| op |
| 4097 |
; |
| 4098 |
|
| 4099 |
operation3 : tIDENTIFIER |
| 4100 |
| tFID |
| 4101 |
| op |
| 4102 |
; |
| 4103 |
|
| 4104 |
dot_or_colon : '.' |
| 4105 |
| tCOLON2 |
| 4106 |
; |
| 4107 |
|
| 4108 |
call_op : '.' |
| 4109 |
{ |
| 4110 |
$$ = '.'; |
| 4111 |
} |
| 4112 |
| tANDDOT |
| 4113 |
{ |
| 4114 |
$$ = 0; |
| 4115 |
} |
| 4116 |
; |
| 4117 |
|
| 4118 |
call_op2 : call_op |
| 4119 |
| tCOLON2 |
| 4120 |
{ |
| 4121 |
$$ = tCOLON2; |
| 4122 |
} |
| 4123 |
; |
| 4124 |
|
| 4125 |
opt_terms : /* none */ |
| 4126 |
| terms |
| 4127 |
; |
| 4128 |
|
| 4129 |
opt_nl : /* none */ |
| 4130 |
| opt_nl nl |
| 4131 |
; |
| 4132 |
|
| 4133 |
rparen : opt_terms ')' |
| 4134 |
; |
| 4135 |
|
| 4136 |
trailer : /* none */ |
| 4137 |
| terms |
| 4138 |
| comma |
| 4139 |
; |
| 4140 |
|
| 4141 |
term : ';' {yyerrok;} |
| 4142 |
| nl |
| 4143 |
; |
| 4144 |
|
| 4145 |
nl : '\n' |
| 4146 |
{ |
| 4147 |
p->lineno += $<num>1; |
| 4148 |
p->column = 0; |
| 4149 |
} |
| 4150 |
| heredoc_body |
| 4151 |
; |
| 4152 |
|
| 4153 |
terms : term |
| 4154 |
| terms term |
| 4155 |
; |
| 4156 |
|
| 4157 |
none : /* none */ |
| 4158 |
{ |
| 4159 |
$$ = 0; |
| 4160 |
} |
| 4161 |
; |
| 4162 |
%% |
| 4163 |
#define pylval (*((YYSTYPE*)(p->ylval))) |
| 4164 |
|
| 4165 |
static void |
| 4166 |
yyerror(parser_state *p, const char *s) |
| 4167 |
{ |
| 4168 |
char* c; |
| 4169 |
size_t n; |
| 4170 |
|
| 4171 |
if (! p->capture_errors) { |
| 4172 |
#ifndef MRB_NO_STDIO |
| 4173 |
if (p->filename_sym) { |
| 4174 |
const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); |
| 4175 |
fprintf(stderr, "%s:%d:%d: %s\n", filename, p->lineno, p->column, s); |
| 4176 |
} |
| 4177 |
else { |
| 4178 |
fprintf(stderr, "line %d:%d: %s\n", p->lineno, p->column, s); |
| 4179 |
} |
| 4180 |
#endif |
| 4181 |
} |
| 4182 |
else if (p->nerr < sizeof(p->error_buffer) / sizeof(p->error_buffer[0])) { |
| 4183 |
n = strlen(s); |
| 4184 |
c = (char*)parser_palloc(p, n + 1); |
| 4185 |
memcpy(c, s, n + 1); |
| 4186 |
p->error_buffer[p->nerr].message = c; |
| 4187 |
p->error_buffer[p->nerr].lineno = p->lineno; |
| 4188 |
p->error_buffer[p->nerr].column = p->column; |
| 4189 |
} |
| 4190 |
p->nerr++; |
| 4191 |
} |
| 4192 |
|
| 4193 |
static void |
| 4194 |
yyerror_c(parser_state *p, const char *msg, char c) |
| 4195 |
{ |
| 4196 |
char buf[256]; |
| 4197 |
|
| 4198 |
strncpy(buf, msg, sizeof(buf) - 2); |
| 4199 |
buf[sizeof(buf) - 2] = '\0'; |
| 4200 |
strncat(buf, &c, 1); |
| 4201 |
yyerror(p, buf); |
| 4202 |
} |
| 4203 |
|
| 4204 |
static void |
| 4205 |
yywarning(parser_state *p, const char *s) |
| 4206 |
{ |
| 4207 |
char* c; |
| 4208 |
size_t n; |
| 4209 |
|
| 4210 |
if (! p->capture_errors) { |
| 4211 |
#ifndef MRB_NO_STDIO |
| 4212 |
if (p->filename_sym) { |
| 4213 |
const char *filename = mrb_sym_name_len(p->mrb, p->filename_sym, NULL); |
| 4214 |
fprintf(stderr, "%s:%d:%d: warning: %s\n", filename, p->lineno, p->column, s); |
| 4215 |
} |
| 4216 |
else { |
| 4217 |
fprintf(stderr, "line %d:%d: warning: %s\n", p->lineno, p->column, s); |
| 4218 |
} |
| 4219 |
#endif |
| 4220 |
} |
| 4221 |
else if (p->nwarn < sizeof(p->warn_buffer) / sizeof(p->warn_buffer[0])) { |
| 4222 |
n = strlen(s); |
| 4223 |
c = (char*)parser_palloc(p, n + 1); |
| 4224 |
memcpy(c, s, n + 1); |
| 4225 |
p->warn_buffer[p->nwarn].message = c; |
| 4226 |
p->warn_buffer[p->nwarn].lineno = p->lineno; |
| 4227 |
p->warn_buffer[p->nwarn].column = p->column; |
| 4228 |
} |
| 4229 |
p->nwarn++; |
| 4230 |
} |
| 4231 |
|
| 4232 |
static void |
| 4233 |
yywarning_s(parser_state *p, const char *msg, const char *s) |
| 4234 |
{ |
| 4235 |
char buf[256]; |
| 4236 |
|
| 4237 |
strncpy(buf, msg, sizeof(buf) - 1); |
| 4238 |
buf[sizeof(buf) - 1] = '\0'; |
| 4239 |
strncat(buf, ": ", sizeof(buf) - strlen(buf) - 1); |
| 4240 |
strncat(buf, s, sizeof(buf) - strlen(buf) - 1); |
| 4241 |
yywarning(p, buf); |
| 4242 |
} |
| 4243 |
|
| 4244 |
static void |
| 4245 |
backref_error(parser_state *p, node *n) |
| 4246 |
{ |
| 4247 |
int c; |
| 4248 |
|
| 4249 |
c = intn(n->car); |
| 4250 |
|
| 4251 |
if (c == NODE_NTH_REF) { |
| 4252 |
yyerror_c(p, "can't set variable $", (char)intn(n->cdr)+'0'); |
| 4253 |
} |
| 4254 |
else if (c == NODE_BACK_REF) { |
| 4255 |
yyerror_c(p, "can't set variable $", (char)intn(n->cdr)); |
| 4256 |
} |
| 4257 |
else { |
| 4258 |
yyerror(p, "Internal error in backref_error()"); |
| 4259 |
} |
| 4260 |
} |
| 4261 |
|
| 4262 |
static void |
| 4263 |
void_expr_error(parser_state *p, node *n) |
| 4264 |
{ |
| 4265 |
int c; |
| 4266 |
|
| 4267 |
if (n == NULL) return; |
| 4268 |
c = intn(n->car); |
| 4269 |
switch (c) { |
| 4270 |
case NODE_BREAK: |
| 4271 |
case NODE_RETURN: |
| 4272 |
case NODE_NEXT: |
| 4273 |
case NODE_REDO: |
| 4274 |
case NODE_RETRY: |
| 4275 |
yyerror(p, "void value expression"); |
| 4276 |
break; |
| 4277 |
case NODE_AND: |
| 4278 |
case NODE_OR: |
| 4279 |
if (n->cdr) { |
| 4280 |
void_expr_error(p, n->cdr->car); |
| 4281 |
void_expr_error(p, n->cdr->cdr); |
| 4282 |
} |
| 4283 |
break; |
| 4284 |
case NODE_BEGIN: |
| 4285 |
if (n->cdr) { |
| 4286 |
while (n->cdr) { |
| 4287 |
n = n->cdr; |
| 4288 |
} |
| 4289 |
void_expr_error(p, n->car); |
| 4290 |
} |
| 4291 |
break; |
| 4292 |
default: |
| 4293 |
break; |
| 4294 |
} |
| 4295 |
} |
| 4296 |
|
| 4297 |
static void pushback(parser_state *p, int c); |
| 4298 |
static mrb_bool peeks(parser_state *p, const char *s); |
| 4299 |
static mrb_bool skips(parser_state *p, const char *s); |
| 4300 |
|
| 4301 |
static inline int |
| 4302 |
nextc0(parser_state *p) |
| 4303 |
{ |
| 4304 |
if (p->s && p->s < p->send) { |
| 4305 |
return (unsigned char)*p->s++; |
| 4306 |
} |
| 4307 |
else { |
| 4308 |
#ifndef MRB_NO_STDIO |
| 4309 |
int c; |
| 4310 |
|
| 4311 |
if (p->f) { |
| 4312 |
c = fgetc(p->f); |
| 4313 |
if (!feof(p->f)) return c; |
| 4314 |
} |
| 4315 |
#endif |
| 4316 |
return -1; |
| 4317 |
} |
| 4318 |
} |
| 4319 |
|
| 4320 |
static inline int |
| 4321 |
nextc(parser_state *p) |
| 4322 |
{ |
| 4323 |
int c; |
| 4324 |
|
| 4325 |
if (p->pb) { |
| 4326 |
node *tmp; |
| 4327 |
|
| 4328 |
c = intn(p->pb->car); |
| 4329 |
tmp = p->pb; |
| 4330 |
p->pb = p->pb->cdr; |
| 4331 |
cons_free(tmp); |
| 4332 |
} |
| 4333 |
else { |
| 4334 |
c = nextc0(p); |
| 4335 |
if (c < 0) goto eof; |
| 4336 |
} |
| 4337 |
if (c >= 0) { |
| 4338 |
p->column++; |
| 4339 |
} |
| 4340 |
if (c == '\r') { |
| 4341 |
const int lf = nextc0(p); |
| 4342 |
if (lf == '\n') { |
| 4343 |
return '\n'; |
| 4344 |
} |
| 4345 |
if (lf > 0) pushback(p, lf); |
| 4346 |
} |
| 4347 |
return c; |
| 4348 |
|
| 4349 |
eof: |
| 4350 |
if (!p->cxt) return -1; |
| 4351 |
else { |
| 4352 |
if (p->cxt->partial_hook(p) < 0) |
| 4353 |
return -1; /* end of program(s) */ |
| 4354 |
return -2; /* end of a file in the program files */ |
| 4355 |
} |
| 4356 |
} |
| 4357 |
|
| 4358 |
static void |
| 4359 |
pushback(parser_state *p, int c) |
| 4360 |
{ |
| 4361 |
if (c >= 0) { |
| 4362 |
p->column--; |
| 4363 |
} |
| 4364 |
p->pb = cons(nint(c), p->pb); |
| 4365 |
} |
| 4366 |
|
| 4367 |
static void |
| 4368 |
skip(parser_state *p, char term) |
| 4369 |
{ |
| 4370 |
int c; |
| 4371 |
|
| 4372 |
for (;;) { |
| 4373 |
c = nextc(p); |
| 4374 |
if (c < 0) break; |
| 4375 |
if (c == term) break; |
| 4376 |
} |
| 4377 |
} |
| 4378 |
|
| 4379 |
static int |
| 4380 |
peekc_n(parser_state *p, int n) |
| 4381 |
{ |
| 4382 |
node *list = 0; |
| 4383 |
int c0; |
| 4384 |
|
| 4385 |
do { |
| 4386 |
c0 = nextc(p); |
| 4387 |
if (c0 == -1) return c0; /* do not skip partial EOF */ |
| 4388 |
if (c0 >= 0) --p->column; |
| 4389 |
list = push(list, nint(c0)); |
| 4390 |
} while(n--); |
| 4391 |
if (p->pb) { |
| 4392 |
p->pb = append(list, p->pb); |
| 4393 |
} |
| 4394 |
else { |
| 4395 |
p->pb = list; |
| 4396 |
} |
| 4397 |
return c0; |
| 4398 |
} |
| 4399 |
|
| 4400 |
static mrb_bool |
| 4401 |
peek_n(parser_state *p, int c, int n) |
| 4402 |
{ |
| 4403 |
return peekc_n(p, n) == c && c >= 0; |
| 4404 |
} |
| 4405 |
#define peek(p,c) peek_n((p), (c), 0) |
| 4406 |
|
| 4407 |
static mrb_bool |
| 4408 |
peeks(parser_state *p, const char *s) |
| 4409 |
{ |
| 4410 |
size_t len = strlen(s); |
| 4411 |
|
| 4412 |
#ifndef MRB_NO_STDIO |
| 4413 |
if (p->f) { |
| 4414 |
int n = 0; |
| 4415 |
while (*s) { |
| 4416 |
if (!peek_n(p, *s++, n++)) return FALSE; |
| 4417 |
} |
| 4418 |
return TRUE; |
| 4419 |
} |
| 4420 |
else |
| 4421 |
#endif |
| 4422 |
if (p->s && p->s + len <= p->send) { |
| 4423 |
if (memcmp(p->s, s, len) == 0) return TRUE; |
| 4424 |
} |
| 4425 |
return FALSE; |
| 4426 |
} |
| 4427 |
|
| 4428 |
static mrb_bool |
| 4429 |
skips(parser_state *p, const char *s) |
| 4430 |
{ |
| 4431 |
int c; |
| 4432 |
|
| 4433 |
for (;;) { |
| 4434 |
/* skip until first char */ |
| 4435 |
for (;;) { |
| 4436 |
c = nextc(p); |
| 4437 |
if (c < 0) return FALSE; |
| 4438 |
if (c == '\n') { |
| 4439 |
p->lineno++; |
| 4440 |
p->column = 0; |
| 4441 |
} |
| 4442 |
if (c == *s) break; |
| 4443 |
} |
| 4444 |
s++; |
| 4445 |
if (peeks(p, s)) { |
| 4446 |
size_t len = strlen(s); |
| 4447 |
|
| 4448 |
while (len--) { |
| 4449 |
if (nextc(p) == '\n') { |
| 4450 |
p->lineno++; |
| 4451 |
p->column = 0; |
| 4452 |
} |
| 4453 |
} |
| 4454 |
return TRUE; |
| 4455 |
} |
| 4456 |
else{ |
| 4457 |
s--; |
| 4458 |
} |
| 4459 |
} |
| 4460 |
return FALSE; |
| 4461 |
} |
| 4462 |
|
| 4463 |
|
| 4464 |
static int |
| 4465 |
newtok(parser_state *p) |
| 4466 |
{ |
| 4467 |
if (p->tokbuf != p->buf) { |
| 4468 |
mrb_free(p->mrb, p->tokbuf); |
| 4469 |
p->tokbuf = p->buf; |
| 4470 |
p->tsiz = MRB_PARSER_TOKBUF_SIZE; |
| 4471 |
} |
| 4472 |
p->tidx = 0; |
| 4473 |
return p->column - 1; |
| 4474 |
} |
| 4475 |
|
| 4476 |
static void |
| 4477 |
tokadd(parser_state *p, int32_t c) |
| 4478 |
{ |
| 4479 |
char utf8[4]; |
| 4480 |
int i, len; |
| 4481 |
|
| 4482 |
/* mrb_assert(-0x10FFFF <= c && c <= 0xFF); */ |
| 4483 |
if (c >= 0) { |
| 4484 |
/* Single byte from source or non-Unicode escape */ |
| 4485 |
utf8[0] = (char)c; |
| 4486 |
len = 1; |
| 4487 |
} |
| 4488 |
else { |
| 4489 |
/* Unicode character */ |
| 4490 |
c = -c; |
| 4491 |
if (c < 0x80) { |
| 4492 |
utf8[0] = (char)c; |
| 4493 |
len = 1; |
| 4494 |
} |
| 4495 |
else if (c < 0x800) { |
| 4496 |
utf8[0] = (char)(0xC0 | (c >> 6)); |
| 4497 |
utf8[1] = (char)(0x80 | (c & 0x3F)); |
| 4498 |
len = 2; |
| 4499 |
} |
| 4500 |
else if (c < 0x10000) { |
| 4501 |
utf8[0] = (char)(0xE0 | (c >> 12) ); |
| 4502 |
utf8[1] = (char)(0x80 | ((c >> 6) & 0x3F)); |
| 4503 |
utf8[2] = (char)(0x80 | ( c & 0x3F)); |
| 4504 |
len = 3; |
| 4505 |
} |
| 4506 |
else { |
| 4507 |
utf8[0] = (char)(0xF0 | (c >> 18) ); |
| 4508 |
utf8[1] = (char)(0x80 | ((c >> 12) & 0x3F)); |
| 4509 |
utf8[2] = (char)(0x80 | ((c >> 6) & 0x3F)); |
| 4510 |
utf8[3] = (char)(0x80 | ( c & 0x3F)); |
| 4511 |
len = 4; |
| 4512 |
} |
| 4513 |
} |
| 4514 |
if (p->tidx+len >= p->tsiz) { |
| 4515 |
if (p->tsiz >= MRB_PARSER_TOKBUF_MAX) { |
| 4516 |
p->tidx += len; |
| 4517 |
return; |
| 4518 |
} |
| 4519 |
p->tsiz *= 2; |
| 4520 |
if (p->tokbuf == p->buf) { |
| 4521 |
p->tokbuf = (char*)mrb_malloc(p->mrb, p->tsiz); |
| 4522 |
memcpy(p->tokbuf, p->buf, MRB_PARSER_TOKBUF_SIZE); |
| 4523 |
} |
| 4524 |
else { |
| 4525 |
p->tokbuf = (char*)mrb_realloc(p->mrb, p->tokbuf, p->tsiz); |
| 4526 |
} |
| 4527 |
} |
| 4528 |
for (i = 0; i < len; i++) { |
| 4529 |
p->tokbuf[p->tidx++] = utf8[i]; |
| 4530 |
} |
| 4531 |
} |
| 4532 |
|
| 4533 |
static int |
| 4534 |
toklast(parser_state *p) |
| 4535 |
{ |
| 4536 |
return p->tokbuf[p->tidx-1]; |
| 4537 |
} |
| 4538 |
|
| 4539 |
static void |
| 4540 |
tokfix(parser_state *p) |
| 4541 |
{ |
| 4542 |
if (p->tidx >= MRB_PARSER_TOKBUF_MAX) { |
| 4543 |
p->tidx = MRB_PARSER_TOKBUF_MAX-1; |
| 4544 |
yyerror(p, "string too long (truncated)"); |
| 4545 |
} |
| 4546 |
p->tokbuf[p->tidx] = '\0'; |
| 4547 |
} |
| 4548 |
|
| 4549 |
static const char* |
| 4550 |
tok(parser_state *p) |
| 4551 |
{ |
| 4552 |
return p->tokbuf; |
| 4553 |
} |
| 4554 |
|
| 4555 |
static int |
| 4556 |
toklen(parser_state *p) |
| 4557 |
{ |
| 4558 |
return p->tidx; |
| 4559 |
} |
| 4560 |
|
| 4561 |
#define IS_ARG() (p->lstate == EXPR_ARG || p->lstate == EXPR_CMDARG) |
| 4562 |
#define IS_END() (p->lstate == EXPR_END || p->lstate == EXPR_ENDARG || p->lstate == EXPR_ENDFN) |
| 4563 |
#define IS_BEG() (p->lstate == EXPR_BEG || p->lstate == EXPR_MID || p->lstate == EXPR_VALUE || p->lstate == EXPR_CLASS) |
| 4564 |
#define IS_SPCARG(c) (IS_ARG() && space_seen && !ISSPACE(c)) |
| 4565 |
#define IS_LABEL_POSSIBLE() ((p->lstate == EXPR_BEG && !cmd_state) || IS_ARG()) |
| 4566 |
#define IS_LABEL_SUFFIX(n) (peek_n(p, ':',(n)) && !peek_n(p, ':', (n)+1)) |
| 4567 |
|
| 4568 |
static int32_t |
| 4569 |
scan_oct(const int *start, int len, int *retlen) |
| 4570 |
{ |
| 4571 |
const int *s = start; |
| 4572 |
int32_t retval = 0; |
| 4573 |
|
| 4574 |
/* mrb_assert(len <= 3) */ |
| 4575 |
while (len-- && *s >= '0' && *s <= '7') { |
| 4576 |
retval <<= 3; |
| 4577 |
retval |= *s++ - '0'; |
| 4578 |
} |
| 4579 |
*retlen = (int)(s - start); |
| 4580 |
|
| 4581 |
return retval; |
| 4582 |
} |
| 4583 |
|
| 4584 |
static int32_t |
| 4585 |
scan_hex(parser_state *p, const int *start, int len, int *retlen) |
| 4586 |
{ |
| 4587 |
static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF"; |
| 4588 |
const int *s = start; |
| 4589 |
uint32_t retval = 0; |
| 4590 |
char *tmp; |
| 4591 |
|
| 4592 |
/* mrb_assert(len <= 8) */ |
| 4593 |
while (len-- && *s && (tmp = (char*)strchr(hexdigit, *s))) { |
| 4594 |
retval <<= 4; |
| 4595 |
retval |= (tmp - hexdigit) & 15; |
| 4596 |
s++; |
| 4597 |
} |
| 4598 |
*retlen = (int)(s - start); |
| 4599 |
|
| 4600 |
return (int32_t)retval; |
| 4601 |
} |
| 4602 |
|
| 4603 |
static int32_t |
| 4604 |
read_escape_unicode(parser_state *p, int limit) |
| 4605 |
{ |
| 4606 |
int buf[9]; |
| 4607 |
int i; |
| 4608 |
int32_t hex; |
| 4609 |
|
| 4610 |
/* Look for opening brace */ |
| 4611 |
i = 0; |
| 4612 |
buf[0] = nextc(p); |
| 4613 |
if (buf[0] < 0) { |
| 4614 |
eof: |
| 4615 |
yyerror(p, "invalid escape character syntax"); |
| 4616 |
return -1; |
| 4617 |
} |
| 4618 |
if (ISXDIGIT(buf[0])) { |
| 4619 |
/* \uxxxx form */ |
| 4620 |
for (i=1; i<limit; i++) { |
| 4621 |
buf[i] = nextc(p); |
| 4622 |
if (buf[i] < 0) goto eof; |
| 4623 |
if (!ISXDIGIT(buf[i])) { |
| 4624 |
pushback(p, buf[i]); |
| 4625 |
break; |
| 4626 |
} |
| 4627 |
} |
| 4628 |
} |
| 4629 |
else { |
| 4630 |
pushback(p, buf[0]); |
| 4631 |
} |
| 4632 |
hex = scan_hex(p, buf, i, &i); |
| 4633 |
if (i == 0 || hex > 0x10FFFF || (hex & 0xFFFFF800) == 0xD800) { |
| 4634 |
yyerror(p, "invalid Unicode code point"); |
| 4635 |
return -1; |
| 4636 |
} |
| 4637 |
return hex; |
| 4638 |
} |
| 4639 |
|
| 4640 |
/* Return negative to indicate Unicode code point */ |
| 4641 |
static int32_t |
| 4642 |
read_escape(parser_state *p) |
| 4643 |
{ |
| 4644 |
int32_t c; |
| 4645 |
|
| 4646 |
switch (c = nextc(p)) { |
| 4647 |
case '\\':/* Backslash */ |
| 4648 |
return c; |
| 4649 |
|
| 4650 |
case 'n':/* newline */ |
| 4651 |
return '\n'; |
| 4652 |
|
| 4653 |
case 't':/* horizontal tab */ |
| 4654 |
return '\t'; |
| 4655 |
|
| 4656 |
case 'r':/* carriage-return */ |
| 4657 |
return '\r'; |
| 4658 |
|
| 4659 |
case 'f':/* form-feed */ |
| 4660 |
return '\f'; |
| 4661 |
|
| 4662 |
case 'v':/* vertical tab */ |
| 4663 |
return '\13'; |
| 4664 |
|
| 4665 |
case 'a':/* alarm(bell) */ |
| 4666 |
return '\007'; |
| 4667 |
|
| 4668 |
case 'e':/* escape */ |
| 4669 |
return 033; |
| 4670 |
|
| 4671 |
case '0': case '1': case '2': case '3': /* octal constant */ |
| 4672 |
case '4': case '5': case '6': case '7': |
| 4673 |
{ |
| 4674 |
int buf[3]; |
| 4675 |
int i; |
| 4676 |
|
| 4677 |
buf[0] = c; |
| 4678 |
for (i=1; i<3; i++) { |
| 4679 |
buf[i] = nextc(p); |
| 4680 |
if (buf[i] < 0) goto eof; |
| 4681 |
if (buf[i] < '0' || '7' < buf[i]) { |
| 4682 |
pushback(p, buf[i]); |
| 4683 |
break; |
| 4684 |
} |
| 4685 |
} |
| 4686 |
c = scan_oct(buf, i, &i); |
| 4687 |
} |
| 4688 |
return c; |
| 4689 |
|
| 4690 |
case 'x': /* hex constant */ |
| 4691 |
{ |
| 4692 |
int buf[2]; |
| 4693 |
int i; |
| 4694 |
|
| 4695 |
for (i=0; i<2; i++) { |
| 4696 |
buf[i] = nextc(p); |
| 4697 |
if (buf[i] < 0) goto eof; |
| 4698 |
if (!ISXDIGIT(buf[i])) { |
| 4699 |
pushback(p, buf[i]); |
| 4700 |
break; |
| 4701 |
} |
| 4702 |
} |
| 4703 |
if (i == 0) { |
| 4704 |
yyerror(p, "invalid hex escape"); |
| 4705 |
return -1; |
| 4706 |
} |
| 4707 |
return scan_hex(p, buf, i, &i); |
| 4708 |
} |
| 4709 |
|
| 4710 |
case 'u': /* Unicode */ |
| 4711 |
if (peek(p, '{')) { |
| 4712 |
/* \u{xxxxxxxx} form */ |
| 4713 |
nextc(p); |
| 4714 |
c = read_escape_unicode(p, 8); |
| 4715 |
if (c < 0) return 0; |
| 4716 |
if (nextc(p) != '}') goto eof; |
| 4717 |
} |
| 4718 |
else { |
| 4719 |
c = read_escape_unicode(p, 4); |
| 4720 |
if (c < 0) return 0; |
| 4721 |
} |
| 4722 |
return -c; |
| 4723 |
|
| 4724 |
case 'b':/* backspace */ |
| 4725 |
return '\010'; |
| 4726 |
|
| 4727 |
case 's':/* space */ |
| 4728 |
return ' '; |
| 4729 |
|
| 4730 |
case 'M': |
| 4731 |
if ((c = nextc(p)) != '-') { |
| 4732 |
yyerror(p, "Invalid escape character syntax"); |
| 4733 |
pushback(p, c); |
| 4734 |
return '\0'; |
| 4735 |
} |
| 4736 |
if ((c = nextc(p)) == '\\') { |
| 4737 |
return read_escape(p) | 0x80; |
| 4738 |
} |
| 4739 |
else if (c < 0) goto eof; |
| 4740 |
else { |
| 4741 |
return ((c & 0xff) | 0x80); |
| 4742 |
} |
| 4743 |
|
| 4744 |
case 'C': |
| 4745 |
if ((c = nextc(p)) != '-') { |
| 4746 |
yyerror(p, "Invalid escape character syntax"); |
| 4747 |
pushback(p, c); |
| 4748 |
return '\0'; |
| 4749 |
} |
| 4750 |
case 'c': |
| 4751 |
if ((c = nextc(p))== '\\') { |
| 4752 |
c = read_escape(p); |
| 4753 |
} |
| 4754 |
else if (c == '?') |
| 4755 |
return 0177; |
| 4756 |
else if (c < 0) goto eof; |
| 4757 |
return c & 0x9f; |
| 4758 |
|
| 4759 |
eof: |
| 4760 |
case -1: |
| 4761 |
case -2: /* end of a file */ |
| 4762 |
yyerror(p, "Invalid escape character syntax"); |
| 4763 |
return '\0'; |
| 4764 |
|
| 4765 |
default: |
| 4766 |
return c; |
| 4767 |
} |
| 4768 |
} |
| 4769 |
|
| 4770 |
static void |
| 4771 |
heredoc_count_indent(parser_heredoc_info *hinfo, const char *str, size_t len, size_t spaces, size_t *offset) |
| 4772 |
{ |
| 4773 |
size_t indent = 0; |
| 4774 |
*offset = 0; |
| 4775 |
for (size_t i = 0; i < len; i++) { |
| 4776 |
size_t size; |
| 4777 |
if (str[i] == '\n') |
| 4778 |
break; |
| 4779 |
else if (str[i] == '\t') |
| 4780 |
size = 8; |
| 4781 |
else if (ISSPACE(str[i])) |
| 4782 |
size = 1; |
| 4783 |
else |
| 4784 |
break; |
| 4785 |
size_t nindent = indent + size; |
| 4786 |
if (nindent > spaces || nindent > hinfo->indent) |
| 4787 |
break; |
| 4788 |
indent = nindent; |
| 4789 |
*offset += 1; |
| 4790 |
} |
| 4791 |
} |
| 4792 |
|
| 4793 |
static void |
| 4794 |
heredoc_remove_indent(parser_state *p, parser_heredoc_info *hinfo) |
| 4795 |
{ |
| 4796 |
if (!hinfo->remove_indent || hinfo->indent == 0) |
| 4797 |
return; |
| 4798 |
node *indented, *n, *pair, *escaped, *nspaces; |
| 4799 |
const char *str; |
| 4800 |
size_t len, spaces, offset, start, end; |
| 4801 |
indented = hinfo->indented; |
| 4802 |
while (indented) { |
| 4803 |
n = indented->car; |
| 4804 |
pair = n->car; |
| 4805 |
str = (char*)pair->car; |
| 4806 |
len = (size_t)pair->cdr; |
| 4807 |
escaped = n->cdr->car; |
| 4808 |
nspaces = n->cdr->cdr; |
| 4809 |
if (escaped) { |
| 4810 |
char *newstr = strndup(str, len); |
| 4811 |
size_t newlen = 0; |
| 4812 |
start = 0; |
| 4813 |
while (start < len) { |
| 4814 |
end = escaped ? (size_t)escaped->car : len; |
| 4815 |
if (end > len) end = len; |
| 4816 |
spaces = (size_t)nspaces->car; |
| 4817 |
size_t esclen = end - start; |
| 4818 |
heredoc_count_indent(hinfo, str + start, esclen, spaces, &offset); |
| 4819 |
esclen -= offset; |
| 4820 |
memcpy(newstr + newlen, str + start + offset, esclen); |
| 4821 |
newlen += esclen; |
| 4822 |
start = end; |
| 4823 |
if (escaped) |
| 4824 |
escaped = escaped->cdr; |
| 4825 |
nspaces = nspaces->cdr; |
| 4826 |
} |
| 4827 |
if (newlen < len) |
| 4828 |
newstr[newlen] = '\0'; |
| 4829 |
pair->car = (node*)newstr; |
| 4830 |
pair->cdr = (node*)newlen; |
| 4831 |
} |
| 4832 |
else { |
| 4833 |
spaces = (size_t)nspaces->car; |
| 4834 |
heredoc_count_indent(hinfo, str, len, spaces, &offset); |
| 4835 |
pair->car = (node*)(str + offset); |
| 4836 |
pair->cdr = (node*)(len - offset); |
| 4837 |
} |
| 4838 |
indented = indented->cdr; |
| 4839 |
} |
| 4840 |
} |
| 4841 |
|
| 4842 |
static void |
| 4843 |
heredoc_push_indented(parser_state *p, parser_heredoc_info *hinfo, node *pair, node *escaped, node *nspaces, mrb_bool empty_line) |
| 4844 |
{ |
| 4845 |
hinfo->indented = push(hinfo->indented, cons(pair, cons(escaped, nspaces))); |
| 4846 |
while (nspaces) { |
| 4847 |
size_t tspaces = (size_t)nspaces->car; |
| 4848 |
if ((hinfo->indent == ~0U || tspaces < hinfo->indent) && !empty_line) |
| 4849 |
hinfo->indent = tspaces; |
| 4850 |
nspaces = nspaces->cdr; |
| 4851 |
} |
| 4852 |
} |
| 4853 |
|
| 4854 |
static int |
| 4855 |
parse_string(parser_state *p) |
| 4856 |
{ |
| 4857 |
int c; |
| 4858 |
string_type type = (string_type)p->lex_strterm->type; |
| 4859 |
int nest_level = p->lex_strterm->level; |
| 4860 |
int beg = p->lex_strterm->paren; |
| 4861 |
int end = p->lex_strterm->term; |
| 4862 |
parser_heredoc_info *hinfo = (type & STR_FUNC_HEREDOC) ? parsing_heredoc_info(p) : NULL; |
| 4863 |
|
| 4864 |
mrb_bool unindent = hinfo && hinfo->remove_indent; |
| 4865 |
mrb_bool head = hinfo && hinfo->line_head; |
| 4866 |
mrb_bool empty = TRUE; |
| 4867 |
size_t spaces = 0; |
| 4868 |
size_t pos = -1; |
| 4869 |
node *escaped = NULL; |
| 4870 |
node *nspaces = NULL; |
| 4871 |
|
| 4872 |
if (beg == 0) beg = -3; /* should never happen */ |
| 4873 |
if (end == 0) end = -3; |
| 4874 |
newtok(p); |
| 4875 |
while ((c = nextc(p)) != end || nest_level != 0) { |
| 4876 |
pos++; |
| 4877 |
if (hinfo && (c == '\n' || c < 0)) { |
| 4878 |
mrb_bool line_head; |
| 4879 |
tokadd(p, '\n'); |
| 4880 |
tokfix(p); |
| 4881 |
p->lineno++; |
| 4882 |
p->column = 0; |
| 4883 |
line_head = hinfo->line_head; |
| 4884 |
hinfo->line_head = TRUE; |
| 4885 |
if (line_head) { |
| 4886 |
/* check whether end of heredoc */ |
| 4887 |
const char *s = tok(p); |
| 4888 |
int len = toklen(p); |
| 4889 |
if (hinfo->allow_indent) { |
| 4890 |
while (ISSPACE(*s) && len > 0) { |
| 4891 |
s++; |
| 4892 |
len--; |
| 4893 |
} |
| 4894 |
} |
| 4895 |
if (hinfo->term_len > 0 && len-1 == hinfo->term_len && strncmp(s, hinfo->term, len-1) == 0) { |
| 4896 |
heredoc_remove_indent(p, hinfo); |
| 4897 |
return tHEREDOC_END; |
| 4898 |
} |
| 4899 |
} |
| 4900 |
if (c < 0) { |
| 4901 |
char buf[256]; |
| 4902 |
const char s1[] = "can't find heredoc delimiter \""; |
| 4903 |
const char s2[] = "\" anywhere before EOF"; |
| 4904 |
|
| 4905 |
if (sizeof(s1)+sizeof(s2)+strlen(hinfo->term)+1 >= sizeof(buf)) { |
| 4906 |
yyerror(p, "can't find heredoc delimiter anywhere before EOF"); |
| 4907 |
} |
| 4908 |
else { |
| 4909 |
strcpy(buf, s1); |
| 4910 |
strcat(buf, hinfo->term); |
| 4911 |
strcat(buf, s2); |
| 4912 |
yyerror(p, buf); |
| 4913 |
} |
| 4914 |
return 0; |
| 4915 |
} |
| 4916 |
node *nd = new_str(p, tok(p), toklen(p)); |
| 4917 |
pylval.nd = nd; |
| 4918 |
if (unindent && head) { |
| 4919 |
nspaces = push(nspaces, nint(spaces)); |
| 4920 |
heredoc_push_indented(p, hinfo, nd->cdr, escaped, nspaces, empty && line_head); |
| 4921 |
} |
| 4922 |
return tHD_STRING_MID; |
| 4923 |
} |
| 4924 |
if (unindent && empty) { |
| 4925 |
if (c == '\t') |
| 4926 |
spaces += 8; |
| 4927 |
else if (ISSPACE(c)) |
| 4928 |
spaces++; |
| 4929 |
else |
| 4930 |
empty = FALSE; |
| 4931 |
} |
| 4932 |
if (c < 0) { |
| 4933 |
yyerror(p, "unterminated string meets end of file"); |
| 4934 |
return 0; |
| 4935 |
} |
| 4936 |
else if (c == beg) { |
| 4937 |
nest_level++; |
| 4938 |
p->lex_strterm->level = nest_level; |
| 4939 |
} |
| 4940 |
else if (c == end) { |
| 4941 |
nest_level--; |
| 4942 |
p->lex_strterm->level = nest_level; |
| 4943 |
} |
| 4944 |
else if (c == '\\') { |
| 4945 |
c = nextc(p); |
| 4946 |
if (type & STR_FUNC_EXPAND) { |
| 4947 |
if (c == end || c == beg) { |
| 4948 |
tokadd(p, c); |
| 4949 |
} |
| 4950 |
else if (c == '\n') { |
| 4951 |
p->lineno++; |
| 4952 |
p->column = 0; |
| 4953 |
if (unindent) { |
| 4954 |
nspaces = push(nspaces, nint(spaces)); |
| 4955 |
escaped = push(escaped, nint(pos)); |
| 4956 |
pos--; |
| 4957 |
empty = TRUE; |
| 4958 |
spaces = 0; |
| 4959 |
} |
| 4960 |
if (type & STR_FUNC_ARRAY) { |
| 4961 |
tokadd(p, '\n'); |
| 4962 |
} |
| 4963 |
} |
| 4964 |
else if (type & STR_FUNC_REGEXP) { |
| 4965 |
tokadd(p, '\\'); |
| 4966 |
tokadd(p, c); |
| 4967 |
} |
| 4968 |
else if (c == 'u' && peek(p, '{')) { |
| 4969 |
/* \u{xxxx xxxx xxxx} form */ |
| 4970 |
nextc(p); |
| 4971 |
while (1) { |
| 4972 |
do c = nextc(p); while (ISSPACE(c)); |
| 4973 |
if (c == '}') break; |
| 4974 |
pushback(p, c); |
| 4975 |
c = read_escape_unicode(p, 8); |
| 4976 |
if (c < 0) break; |
| 4977 |
tokadd(p, -c); |
| 4978 |
} |
| 4979 |
if (hinfo) |
| 4980 |
hinfo->line_head = FALSE; |
| 4981 |
} |
| 4982 |
else { |
| 4983 |
pushback(p, c); |
| 4984 |
tokadd(p, read_escape(p)); |
| 4985 |
if (hinfo) |
| 4986 |
hinfo->line_head = FALSE; |
| 4987 |
} |
| 4988 |
} |
| 4989 |
else { |
| 4990 |
if (c != beg && c != end) { |
| 4991 |
if (c == '\n') { |
| 4992 |
p->lineno++; |
| 4993 |
p->column = 0; |
| 4994 |
} |
| 4995 |
if (!(c == '\\' || ((type & STR_FUNC_ARRAY) && ISSPACE(c)))) { |
| 4996 |
tokadd(p, '\\'); |
| 4997 |
} |
| 4998 |
} |
| 4999 |
tokadd(p, c); |
| 5000 |
} |
| 5001 |
continue; |
| 5002 |
} |
| 5003 |
else if ((c == '#') && (type & STR_FUNC_EXPAND)) { |
| 5004 |
c = nextc(p); |
| 5005 |
if (c == '{') { |
| 5006 |
tokfix(p); |
| 5007 |
p->lstate = EXPR_BEG; |
| 5008 |
p->cmd_start = TRUE; |
| 5009 |
node *nd = new_str(p, tok(p), toklen(p)); |
| 5010 |
pylval.nd = nd; |
| 5011 |
if (hinfo) { |
| 5012 |
if (unindent && head) { |
| 5013 |
nspaces = push(nspaces, nint(spaces)); |
| 5014 |
heredoc_push_indented(p, hinfo, nd->cdr, escaped, nspaces, FALSE); |
| 5015 |
} |
| 5016 |
hinfo->line_head = FALSE; |
| 5017 |
return tHD_STRING_PART; |
| 5018 |
} |
| 5019 |
return tSTRING_PART; |
| 5020 |
} |
| 5021 |
tokadd(p, '#'); |
| 5022 |
pushback(p, c); |
| 5023 |
continue; |
| 5024 |
} |
| 5025 |
if ((type & STR_FUNC_ARRAY) && ISSPACE(c)) { |
| 5026 |
if (toklen(p) == 0) { |
| 5027 |
do { |
| 5028 |
if (c == '\n') { |
| 5029 |
p->lineno++; |
| 5030 |
p->column = 0; |
| 5031 |
heredoc_treat_nextline(p); |
| 5032 |
if (p->parsing_heredoc != NULL) { |
| 5033 |
return tHD_LITERAL_DELIM; |
| 5034 |
} |
| 5035 |
} |
| 5036 |
c = nextc(p); |
| 5037 |
} while (ISSPACE(c)); |
| 5038 |
pushback(p, c); |
| 5039 |
return tLITERAL_DELIM; |
| 5040 |
} |
| 5041 |
else { |
| 5042 |
pushback(p, c); |
| 5043 |
tokfix(p); |
| 5044 |
pylval.nd = new_str(p, tok(p), toklen(p)); |
| 5045 |
return tSTRING_MID; |
| 5046 |
} |
| 5047 |
} |
| 5048 |
if (c == '\n') { |
| 5049 |
p->lineno++; |
| 5050 |
p->column = 0; |
| 5051 |
} |
| 5052 |
tokadd(p, c); |
| 5053 |
} |
| 5054 |
|
| 5055 |
tokfix(p); |
| 5056 |
p->lstate = EXPR_ENDARG; |
| 5057 |
end_strterm(p); |
| 5058 |
|
| 5059 |
if (type & STR_FUNC_XQUOTE) { |
| 5060 |
pylval.nd = new_xstr(p, tok(p), toklen(p)); |
| 5061 |
return tXSTRING; |
| 5062 |
} |
| 5063 |
|
| 5064 |
if (type & STR_FUNC_REGEXP) { |
| 5065 |
int f = 0; |
| 5066 |
int re_opt; |
| 5067 |
char *s = strndup(tok(p), toklen(p)); |
| 5068 |
char flags[3]; |
| 5069 |
char *flag = flags; |
| 5070 |
char enc = '\0'; |
| 5071 |
char *encp; |
| 5072 |
char *dup; |
| 5073 |
|
| 5074 |
newtok(p); |
| 5075 |
while (re_opt = nextc(p), re_opt >= 0 && ISALPHA(re_opt)) { |
| 5076 |
switch (re_opt) { |
| 5077 |
case 'i': f |= 1; break; |
| 5078 |
case 'x': f |= 2; break; |
| 5079 |
case 'm': f |= 4; break; |
| 5080 |
case 'u': f |= 16; break; |
| 5081 |
case 'n': f |= 32; break; |
| 5082 |
case 'o': break; |
| 5083 |
default: tokadd(p, re_opt); break; |
| 5084 |
} |
| 5085 |
} |
| 5086 |
pushback(p, re_opt); |
| 5087 |
if (toklen(p)) { |
| 5088 |
char msg[128]; |
| 5089 |
|
| 5090 |
strcpy(msg, "unknown regexp option"); |
| 5091 |
tokfix(p); |
| 5092 |
if (toklen(p) > 1) { |
| 5093 |
strcat(msg, "s"); |
| 5094 |
} |
| 5095 |
strcat(msg, " - "); |
| 5096 |
strncat(msg, tok(p), sizeof(msg) - strlen(msg) - 1); |
| 5097 |
yyerror(p, msg); |
| 5098 |
} |
| 5099 |
if (f != 0) { |
| 5100 |
if (f & 1) *flag++ = 'i'; |
| 5101 |
if (f & 2) *flag++ = 'x'; |
| 5102 |
if (f & 4) *flag++ = 'm'; |
| 5103 |
if (f & 16) enc = 'u'; |
| 5104 |
if (f & 32) enc = 'n'; |
| 5105 |
} |
| 5106 |
if (flag > flags) { |
| 5107 |
dup = strndup(flags, (size_t)(flag - flags)); |
| 5108 |
} |
| 5109 |
else { |
| 5110 |
dup = NULL; |
| 5111 |
} |
| 5112 |
if (enc) { |
| 5113 |
encp = strndup(&enc, 1); |
| 5114 |
} |
| 5115 |
else { |
| 5116 |
encp = NULL; |
| 5117 |
} |
| 5118 |
pylval.nd = new_regx(p, s, dup, encp); |
| 5119 |
|
| 5120 |
return tREGEXP; |
| 5121 |
} |
| 5122 |
pylval.nd = new_str(p, tok(p), toklen(p)); |
| 5123 |
|
| 5124 |
return tSTRING; |
| 5125 |
} |
| 5126 |
|
| 5127 |
static int |
| 5128 |
number_literal_suffix(parser_state *p) |
| 5129 |
{ |
| 5130 |
int c, result = 0; |
| 5131 |
node *list = 0; |
| 5132 |
int column = p->column; |
| 5133 |
int mask = NUM_SUFFIX_R|NUM_SUFFIX_I; |
| 5134 |
|
| 5135 |
while ((c = nextc(p)) != -1) { |
| 5136 |
list = push(list, nint(c)); |
| 5137 |
|
| 5138 |
if ((mask & NUM_SUFFIX_I) && c == 'i') { |
| 5139 |
result |= (mask & NUM_SUFFIX_I); |
| 5140 |
mask &= ~NUM_SUFFIX_I; |
| 5141 |
/* r after i, rational of complex is disallowed */ |
| 5142 |
mask &= ~NUM_SUFFIX_R; |
| 5143 |
continue; |
| 5144 |
} |
| 5145 |
if ((mask & NUM_SUFFIX_R) && c == 'r') { |
| 5146 |
result |= (mask & NUM_SUFFIX_R); |
| 5147 |
mask &= ~NUM_SUFFIX_R; |
| 5148 |
continue; |
| 5149 |
} |
| 5150 |
if (!ISASCII(c) || ISALPHA(c) || c == '_') { |
| 5151 |
p->column = column; |
| 5152 |
if (p->pb) { |
| 5153 |
p->pb = append(list, p->pb); |
| 5154 |
} |
| 5155 |
else { |
| 5156 |
p->pb = list; |
| 5157 |
} |
| 5158 |
return 0; |
| 5159 |
} |
| 5160 |
pushback(p, c); |
| 5161 |
break; |
| 5162 |
} |
| 5163 |
return result; |
| 5164 |
} |
| 5165 |
|
| 5166 |
static int |
| 5167 |
heredoc_identifier(parser_state *p) |
| 5168 |
{ |
| 5169 |
int c; |
| 5170 |
int type = str_heredoc; |
| 5171 |
mrb_bool indent = FALSE; |
| 5172 |
mrb_bool squiggly = FALSE; |
| 5173 |
mrb_bool quote = FALSE; |
| 5174 |
node *newnode; |
| 5175 |
parser_heredoc_info *info; |
| 5176 |
|
| 5177 |
c = nextc(p); |
| 5178 |
if (ISSPACE(c) || c == '=') { |
| 5179 |
pushback(p, c); |
| 5180 |
return 0; |
| 5181 |
} |
| 5182 |
if (c == '-' || c == '~') { |
| 5183 |
if (c == '-') |
| 5184 |
indent = TRUE; |
| 5185 |
if (c == '~') |
| 5186 |
squiggly = TRUE; |
| 5187 |
c = nextc(p); |
| 5188 |
} |
| 5189 |
if (c == '\'' || c == '"') { |
| 5190 |
int term = c; |
| 5191 |
if (c == '\'') |
| 5192 |
quote = TRUE; |
| 5193 |
newtok(p); |
| 5194 |
while ((c = nextc(p)) >= 0 && c != term) { |
| 5195 |
if (c == '\n') { |
| 5196 |
c = -1; |
| 5197 |
break; |
| 5198 |
} |
| 5199 |
tokadd(p, c); |
| 5200 |
} |
| 5201 |
if (c < 0) { |
| 5202 |
yyerror(p, "unterminated here document identifier"); |
| 5203 |
return 0; |
| 5204 |
} |
| 5205 |
} |
| 5206 |
else { |
| 5207 |
if (c < 0) { |
| 5208 |
return 0; /* missing here document identifier */ |
| 5209 |
} |
| 5210 |
if (! identchar(c)) { |
| 5211 |
pushback(p, c); |
| 5212 |
if (indent) pushback(p, '-'); |
| 5213 |
if (squiggly) pushback(p, '~'); |
| 5214 |
return 0; |
| 5215 |
} |
| 5216 |
newtok(p); |
| 5217 |
do { |
| 5218 |
tokadd(p, c); |
| 5219 |
} while ((c = nextc(p)) >= 0 && identchar(c)); |
| 5220 |
pushback(p, c); |
| 5221 |
} |
| 5222 |
tokfix(p); |
| 5223 |
newnode = new_heredoc(p); |
| 5224 |
info = (parser_heredoc_info*)newnode->cdr; |
| 5225 |
info->term = strndup(tok(p), toklen(p)); |
| 5226 |
info->term_len = toklen(p); |
| 5227 |
if (! quote) |
| 5228 |
type |= STR_FUNC_EXPAND; |
| 5229 |
info->type = (string_type)type; |
| 5230 |
info->allow_indent = indent || squiggly; |
| 5231 |
info->remove_indent = squiggly; |
| 5232 |
info->indent = ~0U; |
| 5233 |
info->indented = NULL; |
| 5234 |
info->line_head = TRUE; |
| 5235 |
info->doc = NULL; |
| 5236 |
p->heredocs_from_nextline = push(p->heredocs_from_nextline, newnode); |
| 5237 |
p->lstate = EXPR_END; |
| 5238 |
|
| 5239 |
pylval.nd = newnode; |
| 5240 |
return tHEREDOC_BEG; |
| 5241 |
} |
| 5242 |
|
| 5243 |
static int |
| 5244 |
arg_ambiguous(parser_state *p) |
| 5245 |
{ |
| 5246 |
yywarning(p, "ambiguous first argument; put parentheses or even spaces"); |
| 5247 |
return 1; |
| 5248 |
} |
| 5249 |
|
| 5250 |
#include "lex.def" |
| 5251 |
|
| 5252 |
static int |
| 5253 |
parser_yylex(parser_state *p) |
| 5254 |
{ |
| 5255 |
int32_t c; |
| 5256 |
int nlines = 1; |
| 5257 |
int space_seen = 0; |
| 5258 |
int cmd_state; |
| 5259 |
enum mrb_lex_state_enum last_state; |
| 5260 |
int token_column; |
| 5261 |
|
| 5262 |
if (p->lex_strterm) { |
| 5263 |
if (is_strterm_type(p, STR_FUNC_HEREDOC)) { |
| 5264 |
if (p->parsing_heredoc != NULL) |
| 5265 |
return parse_string(p); |
| 5266 |
} |
| 5267 |
else |
| 5268 |
return parse_string(p); |
| 5269 |
} |
| 5270 |
cmd_state = p->cmd_start; |
| 5271 |
p->cmd_start = FALSE; |
| 5272 |
retry: |
| 5273 |
last_state = p->lstate; |
| 5274 |
switch (c = nextc(p)) { |
| 5275 |
case '\004': /* ^D */ |
| 5276 |
case '\032': /* ^Z */ |
| 5277 |
case '\0': /* NUL */ |
| 5278 |
case -1: /* end of script. */ |
| 5279 |
if (p->heredocs_from_nextline) |
| 5280 |
goto maybe_heredoc; |
| 5281 |
return 0; |
| 5282 |
|
| 5283 |
/* white spaces */ |
| 5284 |
case ' ': case '\t': case '\f': case '\r': |
| 5285 |
case '\13': /* '\v' */ |
| 5286 |
space_seen = 1; |
| 5287 |
goto retry; |
| 5288 |
|
| 5289 |
case '#': /* it's a comment */ |
| 5290 |
skip(p, '\n'); |
| 5291 |
/* fall through */ |
| 5292 |
case -2: /* end of a file */ |
| 5293 |
case '\n': |
| 5294 |
maybe_heredoc: |
| 5295 |
heredoc_treat_nextline(p); |
| 5296 |
p->column = 0; |
| 5297 |
switch (p->lstate) { |
| 5298 |
case EXPR_BEG: |
| 5299 |
case EXPR_FNAME: |
| 5300 |
case EXPR_DOT: |
| 5301 |
case EXPR_CLASS: |
| 5302 |
case EXPR_VALUE: |
| 5303 |
p->lineno++; |
| 5304 |
if (p->parsing_heredoc != NULL) { |
| 5305 |
if (p->lex_strterm) { |
| 5306 |
return parse_string(p); |
| 5307 |
} |
| 5308 |
} |
| 5309 |
goto retry; |
| 5310 |
default: |
| 5311 |
break; |
| 5312 |
} |
| 5313 |
if (p->parsing_heredoc != NULL) { |
| 5314 |
pylval.num = nlines; |
| 5315 |
return '\n'; |
| 5316 |
} |
| 5317 |
while ((c = nextc(p))) { |
| 5318 |
switch (c) { |
| 5319 |
case ' ': case '\t': case '\f': case '\r': |
| 5320 |
case '\13': /* '\v' */ |
| 5321 |
space_seen = 1; |
| 5322 |
break; |
| 5323 |
case '#': /* comment as a whitespace */ |
| 5324 |
skip(p, '\n'); |
| 5325 |
nlines++; |
| 5326 |
break; |
| 5327 |
case '.': |
| 5328 |
if (!peek(p, '.')) { |
| 5329 |
pushback(p, '.'); |
| 5330 |
p->lineno+=nlines; nlines=1; |
| 5331 |
goto retry; |
| 5332 |
} |
| 5333 |
pushback(p, c); |
| 5334 |
goto normal_newline; |
| 5335 |
case '&': |
| 5336 |
if (peek(p, '.')) { |
| 5337 |
pushback(p, '&'); |
| 5338 |
p->lineno+=nlines; nlines=1; |
| 5339 |
goto retry; |
| 5340 |
} |
| 5341 |
pushback(p, c); |
| 5342 |
goto normal_newline; |
| 5343 |
case -1: /* EOF */ |
| 5344 |
case -2: /* end of a file */ |
| 5345 |
goto normal_newline; |
| 5346 |
default: |
| 5347 |
pushback(p, c); |
| 5348 |
goto normal_newline; |
| 5349 |
} |
| 5350 |
} |
| 5351 |
normal_newline: |
| 5352 |
p->cmd_start = TRUE; |
| 5353 |
p->lstate = EXPR_BEG; |
| 5354 |
pylval.num = nlines; |
| 5355 |
return '\n'; |
| 5356 |
|
| 5357 |
case '*': |
| 5358 |
if ((c = nextc(p)) == '*') { |
| 5359 |
if ((c = nextc(p)) == '=') { |
| 5360 |
pylval.id = intern_op(pow); |
| 5361 |
p->lstate = EXPR_BEG; |
| 5362 |
return tOP_ASGN; |
| 5363 |
} |
| 5364 |
pushback(p, c); |
| 5365 |
if (IS_SPCARG(c)) { |
| 5366 |
yywarning(p, "'**' interpreted as argument prefix"); |
| 5367 |
c = tDSTAR; |
| 5368 |
} |
| 5369 |
else if (IS_BEG()) { |
| 5370 |
c = tDSTAR; |
| 5371 |
} |
| 5372 |
else { |
| 5373 |
c = tPOW; /* "**", "argument prefix" */ |
| 5374 |
} |
| 5375 |
} |
| 5376 |
else { |
| 5377 |
if (c == '=') { |
| 5378 |
pylval.id = intern_op(mul); |
| 5379 |
p->lstate = EXPR_BEG; |
| 5380 |
return tOP_ASGN; |
| 5381 |
} |
| 5382 |
pushback(p, c); |
| 5383 |
if (IS_SPCARG(c)) { |
| 5384 |
yywarning(p, "'*' interpreted as argument prefix"); |
| 5385 |
c = tSTAR; |
| 5386 |
} |
| 5387 |
else if (IS_BEG()) { |
| 5388 |
c = tSTAR; |
| 5389 |
} |
| 5390 |
else { |
| 5391 |
c = '*'; |
| 5392 |
} |
| 5393 |
} |
| 5394 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5395 |
p->lstate = EXPR_ARG; |
| 5396 |
} |
| 5397 |
else { |
| 5398 |
p->lstate = EXPR_BEG; |
| 5399 |
} |
| 5400 |
return c; |
| 5401 |
|
| 5402 |
case '!': |
| 5403 |
c = nextc(p); |
| 5404 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5405 |
p->lstate = EXPR_ARG; |
| 5406 |
if (c == '@') { |
| 5407 |
return '!'; |
| 5408 |
} |
| 5409 |
} |
| 5410 |
else { |
| 5411 |
p->lstate = EXPR_BEG; |
| 5412 |
} |
| 5413 |
if (c == '=') { |
| 5414 |
return tNEQ; |
| 5415 |
} |
| 5416 |
if (c == '~') { |
| 5417 |
return tNMATCH; |
| 5418 |
} |
| 5419 |
pushback(p, c); |
| 5420 |
return '!'; |
| 5421 |
|
| 5422 |
case '=': |
| 5423 |
if (p->column == 1) { |
| 5424 |
static const char begin[] = "begin"; |
| 5425 |
static const char end[] = "\n=end"; |
| 5426 |
if (peeks(p, begin)) { |
| 5427 |
c = peekc_n(p, sizeof(begin)-1); |
| 5428 |
if (c < 0 || ISSPACE(c)) { |
| 5429 |
do { |
| 5430 |
if (!skips(p, end)) { |
| 5431 |
yyerror(p, "embedded document meets end of file"); |
| 5432 |
return 0; |
| 5433 |
} |
| 5434 |
c = nextc(p); |
| 5435 |
} while (!(c < 0 || ISSPACE(c))); |
| 5436 |
if (c != '\n') skip(p, '\n'); |
| 5437 |
p->lineno+=nlines; nlines=1; |
| 5438 |
p->column = 0; |
| 5439 |
goto retry; |
| 5440 |
} |
| 5441 |
} |
| 5442 |
} |
| 5443 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5444 |
p->lstate = EXPR_ARG; |
| 5445 |
} |
| 5446 |
else { |
| 5447 |
p->lstate = EXPR_BEG; |
| 5448 |
} |
| 5449 |
if ((c = nextc(p)) == '=') { |
| 5450 |
if ((c = nextc(p)) == '=') { |
| 5451 |
return tEQQ; |
| 5452 |
} |
| 5453 |
pushback(p, c); |
| 5454 |
return tEQ; |
| 5455 |
} |
| 5456 |
if (c == '~') { |
| 5457 |
return tMATCH; |
| 5458 |
} |
| 5459 |
else if (c == '>') { |
| 5460 |
return tASSOC; |
| 5461 |
} |
| 5462 |
pushback(p, c); |
| 5463 |
return '='; |
| 5464 |
|
| 5465 |
case '<': |
| 5466 |
c = nextc(p); |
| 5467 |
if (c == '<' && |
| 5468 |
p->lstate != EXPR_DOT && |
| 5469 |
p->lstate != EXPR_CLASS && |
| 5470 |
!IS_END() && |
| 5471 |
(!IS_ARG() || space_seen)) { |
| 5472 |
int token = heredoc_identifier(p); |
| 5473 |
if (token) |
| 5474 |
return token; |
| 5475 |
} |
| 5476 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5477 |
p->lstate = EXPR_ARG; |
| 5478 |
} |
| 5479 |
else { |
| 5480 |
p->lstate = EXPR_BEG; |
| 5481 |
if (p->lstate == EXPR_CLASS) { |
| 5482 |
p->cmd_start = TRUE; |
| 5483 |
} |
| 5484 |
} |
| 5485 |
if (c == '=') { |
| 5486 |
if ((c = nextc(p)) == '>') { |
| 5487 |
return tCMP; |
| 5488 |
} |
| 5489 |
pushback(p, c); |
| 5490 |
return tLEQ; |
| 5491 |
} |
| 5492 |
if (c == '<') { |
| 5493 |
if ((c = nextc(p)) == '=') { |
| 5494 |
pylval.id = intern_op(lshift); |
| 5495 |
p->lstate = EXPR_BEG; |
| 5496 |
return tOP_ASGN; |
| 5497 |
} |
| 5498 |
pushback(p, c); |
| 5499 |
return tLSHFT; |
| 5500 |
} |
| 5501 |
pushback(p, c); |
| 5502 |
return '<'; |
| 5503 |
|
| 5504 |
case '>': |
| 5505 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5506 |
p->lstate = EXPR_ARG; |
| 5507 |
} |
| 5508 |
else { |
| 5509 |
p->lstate = EXPR_BEG; |
| 5510 |
} |
| 5511 |
if ((c = nextc(p)) == '=') { |
| 5512 |
return tGEQ; |
| 5513 |
} |
| 5514 |
if (c == '>') { |
| 5515 |
if ((c = nextc(p)) == '=') { |
| 5516 |
pylval.id = intern_op(rshift); |
| 5517 |
p->lstate = EXPR_BEG; |
| 5518 |
return tOP_ASGN; |
| 5519 |
} |
| 5520 |
pushback(p, c); |
| 5521 |
return tRSHFT; |
| 5522 |
} |
| 5523 |
pushback(p, c); |
| 5524 |
return '>'; |
| 5525 |
|
| 5526 |
case '"': |
| 5527 |
p->lex_strterm = new_strterm(p, str_dquote, '"', 0); |
| 5528 |
return tSTRING_BEG; |
| 5529 |
|
| 5530 |
case '\'': |
| 5531 |
p->lex_strterm = new_strterm(p, str_squote, '\'', 0); |
| 5532 |
return parse_string(p); |
| 5533 |
|
| 5534 |
case '`': |
| 5535 |
if (p->lstate == EXPR_FNAME) { |
| 5536 |
p->lstate = EXPR_ENDFN; |
| 5537 |
return '`'; |
| 5538 |
} |
| 5539 |
if (p->lstate == EXPR_DOT) { |
| 5540 |
if (cmd_state) |
| 5541 |
p->lstate = EXPR_CMDARG; |
| 5542 |
else |
| 5543 |
p->lstate = EXPR_ARG; |
| 5544 |
return '`'; |
| 5545 |
} |
| 5546 |
p->lex_strterm = new_strterm(p, str_xquote, '`', 0); |
| 5547 |
return tXSTRING_BEG; |
| 5548 |
|
| 5549 |
case '?': |
| 5550 |
if (IS_END()) { |
| 5551 |
p->lstate = EXPR_VALUE; |
| 5552 |
return '?'; |
| 5553 |
} |
| 5554 |
c = nextc(p); |
| 5555 |
if (c < 0) { |
| 5556 |
yyerror(p, "incomplete character syntax"); |
| 5557 |
return 0; |
| 5558 |
} |
| 5559 |
if (ISSPACE(c)) { |
| 5560 |
if (!IS_ARG()) { |
| 5561 |
int c2; |
| 5562 |
switch (c) { |
| 5563 |
case ' ': |
| 5564 |
c2 = 's'; |
| 5565 |
break; |
| 5566 |
case '\n': |
| 5567 |
c2 = 'n'; |
| 5568 |
break; |
| 5569 |
case '\t': |
| 5570 |
c2 = 't'; |
| 5571 |
break; |
| 5572 |
case '\v': |
| 5573 |
c2 = 'v'; |
| 5574 |
break; |
| 5575 |
case '\r': |
| 5576 |
c2 = 'r'; |
| 5577 |
break; |
| 5578 |
case '\f': |
| 5579 |
c2 = 'f'; |
| 5580 |
break; |
| 5581 |
default: |
| 5582 |
c2 = 0; |
| 5583 |
break; |
| 5584 |
} |
| 5585 |
if (c2) { |
| 5586 |
char buf[256]; |
| 5587 |
char cc[] = { (char)c2, '\0' }; |
| 5588 |
|
| 5589 |
strcpy(buf, "invalid character syntax; use ?\\"); |
| 5590 |
strncat(buf, cc, 2); |
| 5591 |
yyerror(p, buf); |
| 5592 |
} |
| 5593 |
} |
| 5594 |
ternary: |
| 5595 |
pushback(p, c); |
| 5596 |
p->lstate = EXPR_VALUE; |
| 5597 |
return '?'; |
| 5598 |
} |
| 5599 |
newtok(p); |
| 5600 |
/* need support UTF-8 if configured */ |
| 5601 |
if ((ISALNUM(c) || c == '_')) { |
| 5602 |
int c2 = nextc(p); |
| 5603 |
pushback(p, c2); |
| 5604 |
if ((ISALNUM(c2) || c2 == '_')) { |
| 5605 |
goto ternary; |
| 5606 |
} |
| 5607 |
} |
| 5608 |
if (c == '\\') { |
| 5609 |
c = read_escape(p); |
| 5610 |
tokadd(p, c); |
| 5611 |
} |
| 5612 |
else { |
| 5613 |
tokadd(p, c); |
| 5614 |
} |
| 5615 |
tokfix(p); |
| 5616 |
pylval.nd = new_str(p, tok(p), toklen(p)); |
| 5617 |
p->lstate = EXPR_ENDARG; |
| 5618 |
return tCHAR; |
| 5619 |
|
| 5620 |
case '&': |
| 5621 |
if ((c = nextc(p)) == '&') { |
| 5622 |
p->lstate = EXPR_BEG; |
| 5623 |
if ((c = nextc(p)) == '=') { |
| 5624 |
pylval.id = intern_op(andand); |
| 5625 |
p->lstate = EXPR_BEG; |
| 5626 |
return tOP_ASGN; |
| 5627 |
} |
| 5628 |
pushback(p, c); |
| 5629 |
return tANDOP; |
| 5630 |
} |
| 5631 |
else if (c == '.') { |
| 5632 |
p->lstate = EXPR_DOT; |
| 5633 |
return tANDDOT; |
| 5634 |
} |
| 5635 |
else if (c == '=') { |
| 5636 |
pylval.id = intern_op(and); |
| 5637 |
p->lstate = EXPR_BEG; |
| 5638 |
return tOP_ASGN; |
| 5639 |
} |
| 5640 |
pushback(p, c); |
| 5641 |
if (IS_SPCARG(c)) { |
| 5642 |
yywarning(p, "'&' interpreted as argument prefix"); |
| 5643 |
c = tAMPER; |
| 5644 |
} |
| 5645 |
else if (IS_BEG()) { |
| 5646 |
c = tAMPER; |
| 5647 |
} |
| 5648 |
else { |
| 5649 |
c = '&'; |
| 5650 |
} |
| 5651 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5652 |
p->lstate = EXPR_ARG; |
| 5653 |
} |
| 5654 |
else { |
| 5655 |
p->lstate = EXPR_BEG; |
| 5656 |
} |
| 5657 |
return c; |
| 5658 |
|
| 5659 |
case '|': |
| 5660 |
if ((c = nextc(p)) == '|') { |
| 5661 |
p->lstate = EXPR_BEG; |
| 5662 |
if ((c = nextc(p)) == '=') { |
| 5663 |
pylval.id = intern_op(oror); |
| 5664 |
p->lstate = EXPR_BEG; |
| 5665 |
return tOP_ASGN; |
| 5666 |
} |
| 5667 |
pushback(p, c); |
| 5668 |
return tOROP; |
| 5669 |
} |
| 5670 |
if (c == '=') { |
| 5671 |
pylval.id = intern_op(or); |
| 5672 |
p->lstate = EXPR_BEG; |
| 5673 |
return tOP_ASGN; |
| 5674 |
} |
| 5675 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5676 |
p->lstate = EXPR_ARG; |
| 5677 |
} |
| 5678 |
else { |
| 5679 |
p->lstate = EXPR_BEG; |
| 5680 |
} |
| 5681 |
pushback(p, c); |
| 5682 |
return '|'; |
| 5683 |
|
| 5684 |
case '+': |
| 5685 |
c = nextc(p); |
| 5686 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5687 |
p->lstate = EXPR_ARG; |
| 5688 |
if (c == '@') { |
| 5689 |
return tUPLUS; |
| 5690 |
} |
| 5691 |
pushback(p, c); |
| 5692 |
return '+'; |
| 5693 |
} |
| 5694 |
if (c == '=') { |
| 5695 |
pylval.id = intern_op(add); |
| 5696 |
p->lstate = EXPR_BEG; |
| 5697 |
return tOP_ASGN; |
| 5698 |
} |
| 5699 |
if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p))) { |
| 5700 |
p->lstate = EXPR_BEG; |
| 5701 |
pushback(p, c); |
| 5702 |
if (c >= 0 && ISDIGIT(c)) { |
| 5703 |
c = '+'; |
| 5704 |
goto start_num; |
| 5705 |
} |
| 5706 |
return tUPLUS; |
| 5707 |
} |
| 5708 |
p->lstate = EXPR_BEG; |
| 5709 |
pushback(p, c); |
| 5710 |
return '+'; |
| 5711 |
|
| 5712 |
case '-': |
| 5713 |
c = nextc(p); |
| 5714 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 5715 |
p->lstate = EXPR_ARG; |
| 5716 |
if (c == '@') { |
| 5717 |
return tUMINUS; |
| 5718 |
} |
| 5719 |
pushback(p, c); |
| 5720 |
return '-'; |
| 5721 |
} |
| 5722 |
if (c == '=') { |
| 5723 |
pylval.id = intern_op(sub); |
| 5724 |
p->lstate = EXPR_BEG; |
| 5725 |
return tOP_ASGN; |
| 5726 |
} |
| 5727 |
if (c == '>') { |
| 5728 |
p->lstate = EXPR_ENDFN; |
| 5729 |
return tLAMBDA; |
| 5730 |
} |
| 5731 |
if (IS_BEG() || (IS_SPCARG(c) && arg_ambiguous(p))) { |
| 5732 |
p->lstate = EXPR_BEG; |
| 5733 |
pushback(p, c); |
| 5734 |
if (c >= 0 && ISDIGIT(c)) { |
| 5735 |
return tUMINUS_NUM; |
| 5736 |
} |
| 5737 |
return tUMINUS; |
| 5738 |
} |
| 5739 |
p->lstate = EXPR_BEG; |
| 5740 |
pushback(p, c); |
| 5741 |
return '-'; |
| 5742 |
|
| 5743 |
case '.': |
| 5744 |
{ |
| 5745 |
int is_beg = IS_BEG(); |
| 5746 |
p->lstate = EXPR_MID; |
| 5747 |
if ((c = nextc(p)) == '.') { |
| 5748 |
if ((c = nextc(p)) == '.') { |
| 5749 |
return is_beg ? tBDOT3 : tDOT3; |
| 5750 |
} |
| 5751 |
pushback(p, c); |
| 5752 |
return is_beg ? tBDOT2 : tDOT2; |
| 5753 |
} |
| 5754 |
pushback(p, c); |
| 5755 |
p->lstate = EXPR_BEG; |
| 5756 |
if (c >= 0 && ISDIGIT(c)) { |
| 5757 |
yyerror(p, "no .<digit> floating literal anymore; put 0 before dot"); |
| 5758 |
} |
| 5759 |
p->lstate = EXPR_DOT; |
| 5760 |
return '.'; |
| 5761 |
} |
| 5762 |
|
| 5763 |
start_num: |
| 5764 |
case '0': case '1': case '2': case '3': case '4': |
| 5765 |
case '5': case '6': case '7': case '8': case '9': |
| 5766 |
{ |
| 5767 |
int is_float, seen_point, seen_e, nondigit; |
| 5768 |
int suffix = 0; |
| 5769 |
|
| 5770 |
is_float = seen_point = seen_e = nondigit = 0; |
| 5771 |
p->lstate = EXPR_ENDARG; |
| 5772 |
newtok(p); |
| 5773 |
if (c == '-' || c == '+') { |
| 5774 |
tokadd(p, c); |
| 5775 |
c = nextc(p); |
| 5776 |
} |
| 5777 |
if (c == '0') { |
| 5778 |
#define no_digits() do {yyerror(p,"numeric literal without digits"); return 0;} while (0) |
| 5779 |
int start = toklen(p); |
| 5780 |
c = nextc(p); |
| 5781 |
if (c == 'x' || c == 'X') { |
| 5782 |
/* hexadecimal */ |
| 5783 |
c = nextc(p); |
| 5784 |
if (c >= 0 && ISXDIGIT(c)) { |
| 5785 |
do { |
| 5786 |
if (c == '_') { |
| 5787 |
if (nondigit) break; |
| 5788 |
nondigit = c; |
| 5789 |
continue; |
| 5790 |
} |
| 5791 |
if (!ISXDIGIT(c)) break; |
| 5792 |
nondigit = 0; |
| 5793 |
tokadd(p, tolower(c)); |
| 5794 |
} while ((c = nextc(p)) >= 0); |
| 5795 |
} |
| 5796 |
pushback(p, c); |
| 5797 |
tokfix(p); |
| 5798 |
if (toklen(p) == start) { |
| 5799 |
no_digits(); |
| 5800 |
} |
| 5801 |
else if (nondigit) goto trailing_uc; |
| 5802 |
suffix = number_literal_suffix(p); |
| 5803 |
pylval.nd = new_int(p, tok(p), 16, suffix); |
| 5804 |
return tINTEGER; |
| 5805 |
} |
| 5806 |
if (c == 'b' || c == 'B') { |
| 5807 |
/* binary */ |
| 5808 |
c = nextc(p); |
| 5809 |
if (c == '0' || c == '1') { |
| 5810 |
do { |
| 5811 |
if (c == '_') { |
| 5812 |
if (nondigit) break; |
| 5813 |
nondigit = c; |
| 5814 |
continue; |
| 5815 |
} |
| 5816 |
if (c != '0' && c != '1') break; |
| 5817 |
nondigit = 0; |
| 5818 |
tokadd(p, c); |
| 5819 |
} while ((c = nextc(p)) >= 0); |
| 5820 |
} |
| 5821 |
pushback(p, c); |
| 5822 |
tokfix(p); |
| 5823 |
if (toklen(p) == start) { |
| 5824 |
no_digits(); |
| 5825 |
} |
| 5826 |
else if (nondigit) goto trailing_uc; |
| 5827 |
suffix = number_literal_suffix(p); |
| 5828 |
pylval.nd = new_int(p, tok(p), 2, suffix); |
| 5829 |
return tINTEGER; |
| 5830 |
} |
| 5831 |
if (c == 'd' || c == 'D') { |
| 5832 |
/* decimal */ |
| 5833 |
c = nextc(p); |
| 5834 |
if (c >= 0 && ISDIGIT(c)) { |
| 5835 |
do { |
| 5836 |
if (c == '_') { |
| 5837 |
if (nondigit) break; |
| 5838 |
nondigit = c; |
| 5839 |
continue; |
| 5840 |
} |
| 5841 |
if (!ISDIGIT(c)) break; |
| 5842 |
nondigit = 0; |
| 5843 |
tokadd(p, c); |
| 5844 |
} while ((c = nextc(p)) >= 0); |
| 5845 |
} |
| 5846 |
pushback(p, c); |
| 5847 |
tokfix(p); |
| 5848 |
if (toklen(p) == start) { |
| 5849 |
no_digits(); |
| 5850 |
} |
| 5851 |
else if (nondigit) goto trailing_uc; |
| 5852 |
suffix = number_literal_suffix(p); |
| 5853 |
pylval.nd = new_int(p, tok(p), 10, suffix); |
| 5854 |
return tINTEGER; |
| 5855 |
} |
| 5856 |
if (c == '_') { |
| 5857 |
/* 0_0 */ |
| 5858 |
goto octal_number; |
| 5859 |
} |
| 5860 |
if (c == 'o' || c == 'O') { |
| 5861 |
/* prefixed octal */ |
| 5862 |
c = nextc(p); |
| 5863 |
if (c < 0 || c == '_' || !ISDIGIT(c)) { |
| 5864 |
no_digits(); |
| 5865 |
} |
| 5866 |
} |
| 5867 |
if (c >= '0' && c <= '7') { |
| 5868 |
/* octal */ |
| 5869 |
octal_number: |
| 5870 |
do { |
| 5871 |
if (c == '_') { |
| 5872 |
if (nondigit) break; |
| 5873 |
nondigit = c; |
| 5874 |
continue; |
| 5875 |
} |
| 5876 |
if (c < '0' || c > '9') break; |
| 5877 |
if (c > '7') goto invalid_octal; |
| 5878 |
nondigit = 0; |
| 5879 |
tokadd(p, c); |
| 5880 |
} while ((c = nextc(p)) >= 0); |
| 5881 |
|
| 5882 |
if (toklen(p) > start) { |
| 5883 |
pushback(p, c); |
| 5884 |
tokfix(p); |
| 5885 |
if (nondigit) goto trailing_uc; |
| 5886 |
suffix = number_literal_suffix(p); |
| 5887 |
pylval.nd = new_int(p, tok(p), 8, suffix); |
| 5888 |
return tINTEGER; |
| 5889 |
} |
| 5890 |
if (nondigit) { |
| 5891 |
pushback(p, c); |
| 5892 |
goto trailing_uc; |
| 5893 |
} |
| 5894 |
} |
| 5895 |
if (c > '7' && c <= '9') { |
| 5896 |
invalid_octal: |
| 5897 |
yyerror(p, "Invalid octal digit"); |
| 5898 |
} |
| 5899 |
else if (c == '.' || c == 'e' || c == 'E') { |
| 5900 |
tokadd(p, '0'); |
| 5901 |
} |
| 5902 |
else { |
| 5903 |
pushback(p, c); |
| 5904 |
suffix = number_literal_suffix(p); |
| 5905 |
pylval.nd = new_int(p, "0", 10, suffix); |
| 5906 |
return tINTEGER; |
| 5907 |
} |
| 5908 |
} |
| 5909 |
|
| 5910 |
for (;;) { |
| 5911 |
switch (c) { |
| 5912 |
case '0': case '1': case '2': case '3': case '4': |
| 5913 |
case '5': case '6': case '7': case '8': case '9': |
| 5914 |
nondigit = 0; |
| 5915 |
tokadd(p, c); |
| 5916 |
break; |
| 5917 |
|
| 5918 |
case '.': |
| 5919 |
if (nondigit) goto trailing_uc; |
| 5920 |
if (seen_point || seen_e) { |
| 5921 |
goto decode_num; |
| 5922 |
} |
| 5923 |
else { |
| 5924 |
int c0 = nextc(p); |
| 5925 |
if (c0 < 0 || !ISDIGIT(c0)) { |
| 5926 |
pushback(p, c0); |
| 5927 |
goto decode_num; |
| 5928 |
} |
| 5929 |
c = c0; |
| 5930 |
} |
| 5931 |
tokadd(p, '.'); |
| 5932 |
tokadd(p, c); |
| 5933 |
is_float++; |
| 5934 |
seen_point++; |
| 5935 |
nondigit = 0; |
| 5936 |
break; |
| 5937 |
|
| 5938 |
case 'e': |
| 5939 |
case 'E': |
| 5940 |
if (nondigit) { |
| 5941 |
pushback(p, c); |
| 5942 |
c = nondigit; |
| 5943 |
goto decode_num; |
| 5944 |
} |
| 5945 |
if (seen_e) { |
| 5946 |
goto decode_num; |
| 5947 |
} |
| 5948 |
tokadd(p, c); |
| 5949 |
seen_e++; |
| 5950 |
is_float++; |
| 5951 |
nondigit = c; |
| 5952 |
c = nextc(p); |
| 5953 |
if (c != '-' && c != '+') continue; |
| 5954 |
tokadd(p, c); |
| 5955 |
nondigit = c; |
| 5956 |
break; |
| 5957 |
|
| 5958 |
case '_': /* '_' in number just ignored */ |
| 5959 |
if (nondigit) goto decode_num; |
| 5960 |
nondigit = c; |
| 5961 |
break; |
| 5962 |
|
| 5963 |
default: |
| 5964 |
goto decode_num; |
| 5965 |
} |
| 5966 |
c = nextc(p); |
| 5967 |
} |
| 5968 |
|
| 5969 |
decode_num: |
| 5970 |
pushback(p, c); |
| 5971 |
if (nondigit) { |
| 5972 |
trailing_uc: |
| 5973 |
yyerror_c(p, "trailing non digit in number: ", (char)nondigit); |
| 5974 |
} |
| 5975 |
tokfix(p); |
| 5976 |
if (is_float) { |
| 5977 |
#ifdef MRB_NO_FLOAT |
| 5978 |
yywarning_s(p, "floating-point numbers are not supported", tok(p)); |
| 5979 |
pylval.nd = new_int(p, "0", 10, 0); |
| 5980 |
return tINTEGER; |
| 5981 |
#else |
| 5982 |
double d; |
| 5983 |
|
| 5984 |
if (!mrb_read_float(tok(p), NULL, &d)) { |
| 5985 |
yywarning_s(p, "corrupted float value", tok(p)); |
| 5986 |
} |
| 5987 |
suffix = number_literal_suffix(p); |
| 5988 |
if (seen_e && (suffix & NUM_SUFFIX_R)) { |
| 5989 |
pushback(p, 'r'); |
| 5990 |
suffix &= ~NUM_SUFFIX_R; |
| 5991 |
} |
| 5992 |
pylval.nd = new_float(p, tok(p), suffix); |
| 5993 |
return tFLOAT; |
| 5994 |
#endif |
| 5995 |
} |
| 5996 |
suffix = number_literal_suffix(p); |
| 5997 |
pylval.nd = new_int(p, tok(p), 10, suffix); |
| 5998 |
return tINTEGER; |
| 5999 |
} |
| 6000 |
|
| 6001 |
case ')': |
| 6002 |
case ']': |
| 6003 |
p->paren_nest--; |
| 6004 |
/* fall through */ |
| 6005 |
case '}': |
| 6006 |
COND_LEXPOP(); |
| 6007 |
CMDARG_LEXPOP(); |
| 6008 |
if (c == ')') |
| 6009 |
p->lstate = EXPR_ENDFN; |
| 6010 |
else |
| 6011 |
p->lstate = EXPR_END; |
| 6012 |
return c; |
| 6013 |
|
| 6014 |
case ':': |
| 6015 |
c = nextc(p); |
| 6016 |
if (c == ':') { |
| 6017 |
if (IS_BEG() || p->lstate == EXPR_CLASS || IS_SPCARG(-1)) { |
| 6018 |
p->lstate = EXPR_BEG; |
| 6019 |
return tCOLON3; |
| 6020 |
} |
| 6021 |
p->lstate = EXPR_DOT; |
| 6022 |
return tCOLON2; |
| 6023 |
} |
| 6024 |
if (!space_seen && IS_END()) { |
| 6025 |
pushback(p, c); |
| 6026 |
p->lstate = EXPR_BEG; |
| 6027 |
return tLABEL_TAG; |
| 6028 |
} |
| 6029 |
if (IS_END() || ISSPACE(c) || c == '#') { |
| 6030 |
pushback(p, c); |
| 6031 |
p->lstate = EXPR_BEG; |
| 6032 |
return ':'; |
| 6033 |
} |
| 6034 |
pushback(p, c); |
| 6035 |
p->lstate = EXPR_FNAME; |
| 6036 |
return tSYMBEG; |
| 6037 |
|
| 6038 |
case '/': |
| 6039 |
if (IS_BEG()) { |
| 6040 |
p->lex_strterm = new_strterm(p, str_regexp, '/', 0); |
| 6041 |
return tREGEXP_BEG; |
| 6042 |
} |
| 6043 |
if ((c = nextc(p)) == '=') { |
| 6044 |
pylval.id = intern_op(div); |
| 6045 |
p->lstate = EXPR_BEG; |
| 6046 |
return tOP_ASGN; |
| 6047 |
} |
| 6048 |
pushback(p, c); |
| 6049 |
if (IS_SPCARG(c)) { |
| 6050 |
p->lex_strterm = new_strterm(p, str_regexp, '/', 0); |
| 6051 |
return tREGEXP_BEG; |
| 6052 |
} |
| 6053 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 6054 |
p->lstate = EXPR_ARG; |
| 6055 |
} |
| 6056 |
else { |
| 6057 |
p->lstate = EXPR_BEG; |
| 6058 |
} |
| 6059 |
return '/'; |
| 6060 |
|
| 6061 |
case '^': |
| 6062 |
if ((c = nextc(p)) == '=') { |
| 6063 |
pylval.id = intern_op(xor); |
| 6064 |
p->lstate = EXPR_BEG; |
| 6065 |
return tOP_ASGN; |
| 6066 |
} |
| 6067 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 6068 |
p->lstate = EXPR_ARG; |
| 6069 |
} |
| 6070 |
else { |
| 6071 |
p->lstate = EXPR_BEG; |
| 6072 |
} |
| 6073 |
pushback(p, c); |
| 6074 |
return '^'; |
| 6075 |
|
| 6076 |
case ';': |
| 6077 |
p->lstate = EXPR_BEG; |
| 6078 |
return ';'; |
| 6079 |
|
| 6080 |
case ',': |
| 6081 |
p->lstate = EXPR_BEG; |
| 6082 |
return ','; |
| 6083 |
|
| 6084 |
case '~': |
| 6085 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 6086 |
if ((c = nextc(p)) != '@') { |
| 6087 |
pushback(p, c); |
| 6088 |
} |
| 6089 |
p->lstate = EXPR_ARG; |
| 6090 |
} |
| 6091 |
else { |
| 6092 |
p->lstate = EXPR_BEG; |
| 6093 |
} |
| 6094 |
return '~'; |
| 6095 |
|
| 6096 |
case '(': |
| 6097 |
if (IS_BEG()) { |
| 6098 |
c = tLPAREN; |
| 6099 |
} |
| 6100 |
else if (IS_SPCARG(-1)) { |
| 6101 |
c = tLPAREN_ARG; |
| 6102 |
} |
| 6103 |
else if (p->lstate == EXPR_END && space_seen) { |
| 6104 |
c = tLPAREN_ARG; |
| 6105 |
} |
| 6106 |
p->paren_nest++; |
| 6107 |
COND_PUSH(0); |
| 6108 |
CMDARG_PUSH(0); |
| 6109 |
p->lstate = EXPR_BEG; |
| 6110 |
return c; |
| 6111 |
|
| 6112 |
case '[': |
| 6113 |
p->paren_nest++; |
| 6114 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 6115 |
p->lstate = EXPR_ARG; |
| 6116 |
p->paren_nest--; |
| 6117 |
if ((c = nextc(p)) == ']') { |
| 6118 |
if ((c = nextc(p)) == '=') { |
| 6119 |
return tASET; |
| 6120 |
} |
| 6121 |
pushback(p, c); |
| 6122 |
return tAREF; |
| 6123 |
} |
| 6124 |
pushback(p, c); |
| 6125 |
return '['; |
| 6126 |
} |
| 6127 |
else if (IS_BEG()) { |
| 6128 |
c = tLBRACK; |
| 6129 |
} |
| 6130 |
else if (IS_ARG() && space_seen) { |
| 6131 |
c = tLBRACK; |
| 6132 |
} |
| 6133 |
p->lstate = EXPR_BEG; |
| 6134 |
COND_PUSH(0); |
| 6135 |
CMDARG_PUSH(0); |
| 6136 |
return c; |
| 6137 |
|
| 6138 |
case '{': |
| 6139 |
if (p->lpar_beg && p->lpar_beg == p->paren_nest) { |
| 6140 |
p->lstate = EXPR_BEG; |
| 6141 |
p->lpar_beg = 0; |
| 6142 |
p->paren_nest--; |
| 6143 |
COND_PUSH(0); |
| 6144 |
CMDARG_PUSH(0); |
| 6145 |
return tLAMBEG; |
| 6146 |
} |
| 6147 |
if (IS_ARG() || p->lstate == EXPR_END || p->lstate == EXPR_ENDFN) |
| 6148 |
c = '{'; /* block (primary) */ |
| 6149 |
else if (p->lstate == EXPR_ENDARG) |
| 6150 |
c = tLBRACE_ARG; /* block (expr) */ |
| 6151 |
else |
| 6152 |
c = tLBRACE; /* hash */ |
| 6153 |
COND_PUSH(0); |
| 6154 |
CMDARG_PUSH(0); |
| 6155 |
p->lstate = EXPR_BEG; |
| 6156 |
return c; |
| 6157 |
|
| 6158 |
case '\\': |
| 6159 |
c = nextc(p); |
| 6160 |
if (c == '\n') { |
| 6161 |
p->lineno+=nlines; nlines=1; |
| 6162 |
p->column = 0; |
| 6163 |
space_seen = 1; |
| 6164 |
goto retry; /* skip \\n */ |
| 6165 |
} |
| 6166 |
pushback(p, c); |
| 6167 |
return '\\'; |
| 6168 |
|
| 6169 |
case '%': |
| 6170 |
if (IS_BEG()) { |
| 6171 |
int term; |
| 6172 |
int paren; |
| 6173 |
|
| 6174 |
c = nextc(p); |
| 6175 |
quotation: |
| 6176 |
if (c < 0 || !ISALNUM(c)) { |
| 6177 |
term = c; |
| 6178 |
c = 'Q'; |
| 6179 |
} |
| 6180 |
else { |
| 6181 |
term = nextc(p); |
| 6182 |
if (ISALNUM(term)) { |
| 6183 |
yyerror(p, "unknown type of %string"); |
| 6184 |
return 0; |
| 6185 |
} |
| 6186 |
} |
| 6187 |
if (c < 0 || term < 0) { |
| 6188 |
yyerror(p, "unterminated quoted string meets end of file"); |
| 6189 |
return 0; |
| 6190 |
} |
| 6191 |
paren = term; |
| 6192 |
if (term == '(') term = ')'; |
| 6193 |
else if (term == '[') term = ']'; |
| 6194 |
else if (term == '{') term = '}'; |
| 6195 |
else if (term == '<') term = '>'; |
| 6196 |
else paren = 0; |
| 6197 |
|
| 6198 |
switch (c) { |
| 6199 |
case 'Q': |
| 6200 |
p->lex_strterm = new_strterm(p, str_dquote, term, paren); |
| 6201 |
return tSTRING_BEG; |
| 6202 |
|
| 6203 |
case 'q': |
| 6204 |
p->lex_strterm = new_strterm(p, str_squote, term, paren); |
| 6205 |
return parse_string(p); |
| 6206 |
|
| 6207 |
case 'W': |
| 6208 |
p->lex_strterm = new_strterm(p, str_dword, term, paren); |
| 6209 |
return tWORDS_BEG; |
| 6210 |
|
| 6211 |
case 'w': |
| 6212 |
p->lex_strterm = new_strterm(p, str_sword, term, paren); |
| 6213 |
return tWORDS_BEG; |
| 6214 |
|
| 6215 |
case 'x': |
| 6216 |
p->lex_strterm = new_strterm(p, str_xquote, term, paren); |
| 6217 |
return tXSTRING_BEG; |
| 6218 |
|
| 6219 |
case 'r': |
| 6220 |
p->lex_strterm = new_strterm(p, str_regexp, term, paren); |
| 6221 |
return tREGEXP_BEG; |
| 6222 |
|
| 6223 |
case 's': |
| 6224 |
p->lex_strterm = new_strterm(p, str_ssym, term, paren); |
| 6225 |
return tSYMBEG; |
| 6226 |
|
| 6227 |
case 'I': |
| 6228 |
p->lex_strterm = new_strterm(p, str_dsymbols, term, paren); |
| 6229 |
return tSYMBOLS_BEG; |
| 6230 |
|
| 6231 |
case 'i': |
| 6232 |
p->lex_strterm = new_strterm(p, str_ssymbols, term, paren); |
| 6233 |
return tSYMBOLS_BEG; |
| 6234 |
|
| 6235 |
default: |
| 6236 |
yyerror(p, "unknown type of %string"); |
| 6237 |
return 0; |
| 6238 |
} |
| 6239 |
} |
| 6240 |
if ((c = nextc(p)) == '=') { |
| 6241 |
pylval.id = intern_op(mod); |
| 6242 |
p->lstate = EXPR_BEG; |
| 6243 |
return tOP_ASGN; |
| 6244 |
} |
| 6245 |
if (IS_SPCARG(c)) { |
| 6246 |
goto quotation; |
| 6247 |
} |
| 6248 |
if (p->lstate == EXPR_FNAME || p->lstate == EXPR_DOT) { |
| 6249 |
p->lstate = EXPR_ARG; |
| 6250 |
} |
| 6251 |
else { |
| 6252 |
p->lstate = EXPR_BEG; |
| 6253 |
} |
| 6254 |
pushback(p, c); |
| 6255 |
return '%'; |
| 6256 |
|
| 6257 |
case '$': |
| 6258 |
p->lstate = EXPR_END; |
| 6259 |
token_column = newtok(p); |
| 6260 |
c = nextc(p); |
| 6261 |
if (c < 0) { |
| 6262 |
yyerror(p, "incomplete global variable syntax"); |
| 6263 |
return 0; |
| 6264 |
} |
| 6265 |
switch (c) { |
| 6266 |
case '_': /* $_: last read line string */ |
| 6267 |
c = nextc(p); |
| 6268 |
if (c >= 0 && identchar(c)) { /* if there is more after _ it is a variable */ |
| 6269 |
tokadd(p, '$'); |
| 6270 |
tokadd(p, c); |
| 6271 |
break; |
| 6272 |
} |
| 6273 |
pushback(p, c); |
| 6274 |
c = '_'; |
| 6275 |
/* fall through */ |
| 6276 |
case '~': /* $~: match-data */ |
| 6277 |
case '*': /* $*: argv */ |
| 6278 |
case '$': /* $$: pid */ |
| 6279 |
case '?': /* $?: last status */ |
| 6280 |
case '!': /* $!: error string */ |
| 6281 |
case '@': /* $@: error position */ |
| 6282 |
case '/': /* $/: input record separator */ |
| 6283 |
case '\\': /* $\: output record separator */ |
| 6284 |
case ';': /* $;: field separator */ |
| 6285 |
case ',': /* $,: output field separator */ |
| 6286 |
case '.': /* $.: last read line number */ |
| 6287 |
case '=': /* $=: ignorecase */ |
| 6288 |
case ':': /* $:: load path */ |
| 6289 |
case '<': /* $<: reading filename */ |
| 6290 |
case '>': /* $>: default output handle */ |
| 6291 |
case '\"': /* $": already loaded files */ |
| 6292 |
tokadd(p, '$'); |
| 6293 |
tokadd(p, c); |
| 6294 |
tokfix(p); |
| 6295 |
pylval.id = intern(tok(p), toklen(p)); |
| 6296 |
return tGVAR; |
| 6297 |
|
| 6298 |
case '-': |
| 6299 |
tokadd(p, '$'); |
| 6300 |
tokadd(p, c); |
| 6301 |
c = nextc(p); |
| 6302 |
pushback(p, c); |
| 6303 |
gvar: |
| 6304 |
tokfix(p); |
| 6305 |
pylval.id = intern(tok(p), toklen(p)); |
| 6306 |
return tGVAR; |
| 6307 |
|
| 6308 |
case '&': /* $&: last match */ |
| 6309 |
case '`': /* $`: string before last match */ |
| 6310 |
case '\'': /* $': string after last match */ |
| 6311 |
case '+': /* $+: string matches last pattern */ |
| 6312 |
if (last_state == EXPR_FNAME) { |
| 6313 |
tokadd(p, '$'); |
| 6314 |
tokadd(p, c); |
| 6315 |
goto gvar; |
| 6316 |
} |
| 6317 |
pylval.nd = new_back_ref(p, c); |
| 6318 |
return tBACK_REF; |
| 6319 |
|
| 6320 |
case '1': case '2': case '3': |
| 6321 |
case '4': case '5': case '6': |
| 6322 |
case '7': case '8': case '9': |
| 6323 |
do { |
| 6324 |
tokadd(p, c); |
| 6325 |
c = nextc(p); |
| 6326 |
} while (c >= 0 && ISDIGIT(c)); |
| 6327 |
pushback(p, c); |
| 6328 |
if (last_state == EXPR_FNAME) goto gvar; |
| 6329 |
tokfix(p); |
| 6330 |
{ |
| 6331 |
mrb_int n; |
| 6332 |
if (!mrb_read_int(tok(p), NULL, NULL, &n)) { |
| 6333 |
yywarning(p, "capture group index too big; always nil"); |
| 6334 |
return keyword_nil; |
| 6335 |
} |
| 6336 |
pylval.nd = new_nth_ref(p, (int)n); |
| 6337 |
} |
| 6338 |
return tNTH_REF; |
| 6339 |
|
| 6340 |
default: |
| 6341 |
if (!identchar(c)) { |
| 6342 |
pushback(p, c); |
| 6343 |
return '$'; |
| 6344 |
} |
| 6345 |
/* fall through */ |
| 6346 |
case '0': |
| 6347 |
tokadd(p, '$'); |
| 6348 |
} |
| 6349 |
break; |
| 6350 |
|
| 6351 |
case '@': |
| 6352 |
c = nextc(p); |
| 6353 |
token_column = newtok(p); |
| 6354 |
tokadd(p, '@'); |
| 6355 |
if (c == '@') { |
| 6356 |
tokadd(p, '@'); |
| 6357 |
c = nextc(p); |
| 6358 |
} |
| 6359 |
if (c < 0) { |
| 6360 |
if (p->tidx == 1) { |
| 6361 |
yyerror(p, "incomplete instance variable syntax"); |
| 6362 |
} |
| 6363 |
else { |
| 6364 |
yyerror(p, "incomplete class variable syntax"); |
| 6365 |
} |
| 6366 |
return 0; |
| 6367 |
} |
| 6368 |
else if (ISDIGIT(c)) { |
| 6369 |
if (p->tidx == 1) { |
| 6370 |
yyerror_c(p, "wrong instance variable name: @", c); |
| 6371 |
} |
| 6372 |
else { |
| 6373 |
yyerror_c(p, "wrong class variable name: @@", c); |
| 6374 |
} |
| 6375 |
return 0; |
| 6376 |
} |
| 6377 |
if (!identchar(c)) { |
| 6378 |
pushback(p, c); |
| 6379 |
return '@'; |
| 6380 |
} |
| 6381 |
break; |
| 6382 |
|
| 6383 |
case '_': |
| 6384 |
token_column = newtok(p); |
| 6385 |
break; |
| 6386 |
|
| 6387 |
default: |
| 6388 |
if (!identchar(c)) { |
| 6389 |
char buf[36]; |
| 6390 |
const char s[] = "Invalid char in expression: 0x"; |
| 6391 |
const char hexdigits[] = "0123456789ABCDEF"; |
| 6392 |
|
| 6393 |
strcpy(buf, s); |
| 6394 |
buf[sizeof(s)-1] = hexdigits[(c & 0xf0) >> 4]; |
| 6395 |
buf[sizeof(s)] = hexdigits[(c & 0x0f)]; |
| 6396 |
buf[sizeof(s)+1] = 0; |
| 6397 |
yyerror(p, buf); |
| 6398 |
goto retry; |
| 6399 |
} |
| 6400 |
|
| 6401 |
token_column = newtok(p); |
| 6402 |
break; |
| 6403 |
} |
| 6404 |
|
| 6405 |
do { |
| 6406 |
tokadd(p, c); |
| 6407 |
c = nextc(p); |
| 6408 |
if (c < 0) break; |
| 6409 |
} while (identchar(c)); |
| 6410 |
if (token_column == 0 && toklen(p) == 7 && (c < 0 || c == '\n') && |
| 6411 |
strncmp(tok(p), "__END__", toklen(p)) == 0) |
| 6412 |
return -1; |
| 6413 |
|
| 6414 |
switch (tok(p)[0]) { |
| 6415 |
case '@': case '$': |
| 6416 |
pushback(p, c); |
| 6417 |
break; |
| 6418 |
default: |
| 6419 |
if ((c == '!' || c == '?') && !peek(p, '=')) { |
| 6420 |
tokadd(p, c); |
| 6421 |
} |
| 6422 |
else { |
| 6423 |
pushback(p, c); |
| 6424 |
} |
| 6425 |
} |
| 6426 |
tokfix(p); |
| 6427 |
{ |
| 6428 |
int result = 0; |
| 6429 |
|
| 6430 |
switch (tok(p)[0]) { |
| 6431 |
case '$': |
| 6432 |
p->lstate = EXPR_END; |
| 6433 |
result = tGVAR; |
| 6434 |
break; |
| 6435 |
case '@': |
| 6436 |
p->lstate = EXPR_END; |
| 6437 |
if (tok(p)[1] == '@') |
| 6438 |
result = tCVAR; |
| 6439 |
else |
| 6440 |
result = tIVAR; |
| 6441 |
break; |
| 6442 |
|
| 6443 |
case '_': |
| 6444 |
if (p->lstate != EXPR_FNAME && toklen(p) == 2 && ISDIGIT(tok(p)[1]) && p->nvars) { |
| 6445 |
int n = tok(p)[1] - '0'; |
| 6446 |
int nvar; |
| 6447 |
|
| 6448 |
if (n > 0) { |
| 6449 |
node *nvars = p->nvars->cdr; |
| 6450 |
|
| 6451 |
while (nvars) { |
| 6452 |
nvar = intn(nvars->car); |
| 6453 |
if (nvar == -2) break; /* top of the scope */ |
| 6454 |
if (nvar > 0) { |
| 6455 |
yywarning(p, "numbered parameter used in outer block"); |
| 6456 |
break; |
| 6457 |
} |
| 6458 |
nvars->car = nint(-1); |
| 6459 |
nvars = nvars->cdr; |
| 6460 |
} |
| 6461 |
nvar = intn(p->nvars->car); |
| 6462 |
if (nvar != -2) { /* numbered parameters never appear on toplevel */ |
| 6463 |
if (nvar == -1) { |
| 6464 |
yywarning(p, "numbered parameter used in inner block"); |
| 6465 |
} |
| 6466 |
else { |
| 6467 |
p->nvars->car = nint(nvar > n ? nvar : n); |
| 6468 |
} |
| 6469 |
pylval.num = n; |
| 6470 |
p->lstate = EXPR_END; |
| 6471 |
return tNUMPARAM; |
| 6472 |
} |
| 6473 |
} |
| 6474 |
} |
| 6475 |
/* fall through */ |
| 6476 |
default: |
| 6477 |
if (toklast(p) == '!' || toklast(p) == '?') { |
| 6478 |
result = tFID; |
| 6479 |
} |
| 6480 |
else { |
| 6481 |
if (p->lstate == EXPR_FNAME) { |
| 6482 |
if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') && |
| 6483 |
(!peek(p, '=') || (peek_n(p, '>', 1)))) { |
| 6484 |
result = tIDENTIFIER; |
| 6485 |
tokadd(p, c); |
| 6486 |
tokfix(p); |
| 6487 |
} |
| 6488 |
else { |
| 6489 |
pushback(p, c); |
| 6490 |
} |
| 6491 |
if ((c = nextc(p)) == '=' && !peek(p, '~') && !peek(p, '>') && |
| 6492 |
(!peek(p, '=') || (peek_n(p, '>', 1)))) { |
| 6493 |
result = tIDENTIFIER; |
| 6494 |
tokadd(p, c); |
| 6495 |
tokfix(p); |
| 6496 |
} |
| 6497 |
else { |
| 6498 |
pushback(p, c); |
| 6499 |
} |
| 6500 |
} |
| 6501 |
if (result == 0 && ISUPPER(tok(p)[0])) { |
| 6502 |
result = tCONSTANT; |
| 6503 |
} |
| 6504 |
else { |
| 6505 |
result = tIDENTIFIER; |
| 6506 |
} |
| 6507 |
} |
| 6508 |
|
| 6509 |
if (IS_LABEL_POSSIBLE()) { |
| 6510 |
if (IS_LABEL_SUFFIX(0)) { |
| 6511 |
p->lstate = EXPR_END; |
| 6512 |
tokfix(p); |
| 6513 |
pylval.id = intern(tok(p), toklen(p)); |
| 6514 |
return tIDENTIFIER; |
| 6515 |
} |
| 6516 |
} |
| 6517 |
if (p->lstate != EXPR_DOT) { |
| 6518 |
const struct kwtable *kw; |
| 6519 |
|
| 6520 |
/* See if it is a reserved word. */ |
| 6521 |
kw = mrb_reserved_word(tok(p), toklen(p)); |
| 6522 |
if (kw) { |
| 6523 |
enum mrb_lex_state_enum state = p->lstate; |
| 6524 |
pylval.num = p->lineno; |
| 6525 |
p->lstate = kw->state; |
| 6526 |
if (state == EXPR_FNAME) { |
| 6527 |
pylval.id = intern_cstr(kw->name); |
| 6528 |
return kw->id[0]; |
| 6529 |
} |
| 6530 |
if (p->lstate == EXPR_BEG) { |
| 6531 |
p->cmd_start = TRUE; |
| 6532 |
} |
| 6533 |
if (kw->id[0] == keyword_do) { |
| 6534 |
if (p->lpar_beg && p->lpar_beg == p->paren_nest) { |
| 6535 |
p->lpar_beg = 0; |
| 6536 |
p->paren_nest--; |
| 6537 |
return keyword_do_LAMBDA; |
| 6538 |
} |
| 6539 |
if (COND_P()) return keyword_do_cond; |
| 6540 |
if (CMDARG_P() && state != EXPR_CMDARG) |
| 6541 |
return keyword_do_block; |
| 6542 |
if (state == EXPR_ENDARG || state == EXPR_BEG) |
| 6543 |
return keyword_do_block; |
| 6544 |
return keyword_do; |
| 6545 |
} |
| 6546 |
if (state == EXPR_BEG || state == EXPR_VALUE || state == EXPR_CLASS) |
| 6547 |
return kw->id[0]; |
| 6548 |
else { |
| 6549 |
if (kw->id[0] != kw->id[1]) |
| 6550 |
p->lstate = EXPR_BEG; |
| 6551 |
return kw->id[1]; |
| 6552 |
} |
| 6553 |
} |
| 6554 |
} |
| 6555 |
|
| 6556 |
if (IS_BEG() || p->lstate == EXPR_DOT || IS_ARG()) { |
| 6557 |
if (cmd_state) { |
| 6558 |
p->lstate = EXPR_CMDARG; |
| 6559 |
} |
| 6560 |
else { |
| 6561 |
p->lstate = EXPR_ARG; |
| 6562 |
} |
| 6563 |
} |
| 6564 |
else if (p->lstate == EXPR_FNAME) { |
| 6565 |
p->lstate = EXPR_ENDFN; |
| 6566 |
} |
| 6567 |
else { |
| 6568 |
p->lstate = EXPR_END; |
| 6569 |
} |
| 6570 |
} |
| 6571 |
{ |
| 6572 |
mrb_sym ident = intern(tok(p), toklen(p)); |
| 6573 |
|
| 6574 |
pylval.id = ident; |
| 6575 |
if (last_state != EXPR_DOT && ISLOWER(tok(p)[0]) && local_var_p(p, ident)) { |
| 6576 |
p->lstate = EXPR_END; |
| 6577 |
} |
| 6578 |
} |
| 6579 |
return result; |
| 6580 |
} |
| 6581 |
} |
| 6582 |
|
| 6583 |
static int |
| 6584 |
yylex(void *lval, parser_state *p) |
| 6585 |
{ |
| 6586 |
p->ylval = lval; |
| 6587 |
return parser_yylex(p); |
| 6588 |
} |
| 6589 |
|
| 6590 |
static void |
| 6591 |
parser_init_cxt(parser_state *p, mrbc_context *cxt) |
| 6592 |
{ |
| 6593 |
if (!cxt) return; |
| 6594 |
if (cxt->filename) mrb_parser_set_filename(p, cxt->filename); |
| 6595 |
if (cxt->lineno) p->lineno = cxt->lineno; |
| 6596 |
if (cxt->syms) { |
| 6597 |
int i; |
| 6598 |
|
| 6599 |
p->locals = cons(0,0); |
| 6600 |
for (i=0; i<cxt->slen; i++) { |
| 6601 |
local_add_f(p, cxt->syms[i]); |
| 6602 |
} |
| 6603 |
} |
| 6604 |
p->capture_errors = cxt->capture_errors; |
| 6605 |
p->no_optimize = cxt->no_optimize; |
| 6606 |
p->no_ext_ops = cxt->no_ext_ops; |
| 6607 |
p->upper = cxt->upper; |
| 6608 |
if (cxt->partial_hook) { |
| 6609 |
p->cxt = cxt; |
| 6610 |
} |
| 6611 |
} |
| 6612 |
|
| 6613 |
static void |
| 6614 |
parser_update_cxt(parser_state *p, mrbc_context *cxt) |
| 6615 |
{ |
| 6616 |
node *n, *n0; |
| 6617 |
int i = 0; |
| 6618 |
|
| 6619 |
if (!cxt) return; |
| 6620 |
if (!p->tree) return; |
| 6621 |
if (intn(p->tree->car) != NODE_SCOPE) return; |
| 6622 |
n0 = n = p->tree->cdr->car; |
| 6623 |
while (n) { |
| 6624 |
i++; |
| 6625 |
n = n->cdr; |
| 6626 |
} |
| 6627 |
cxt->syms = (mrb_sym*)mrb_realloc(p->mrb, cxt->syms, i*sizeof(mrb_sym)); |
| 6628 |
cxt->slen = i; |
| 6629 |
for (i=0, n=n0; n; i++,n=n->cdr) { |
| 6630 |
cxt->syms[i] = sym(n->car); |
| 6631 |
} |
| 6632 |
} |
| 6633 |
|
| 6634 |
void mrb_parser_dump(mrb_state *mrb, node *tree, int offset); |
| 6635 |
|
| 6636 |
MRB_API void |
| 6637 |
mrb_parser_parse(parser_state *p, mrbc_context *c) |
| 6638 |
{ |
| 6639 |
struct mrb_jmpbuf buf1; |
| 6640 |
struct mrb_jmpbuf *prev = p->mrb->jmp; |
| 6641 |
p->mrb->jmp = &buf1; |
| 6642 |
|
| 6643 |
MRB_TRY(p->mrb->jmp) { |
| 6644 |
int n = 1; |
| 6645 |
|
| 6646 |
p->cmd_start = TRUE; |
| 6647 |
p->in_def = p->in_single = 0; |
| 6648 |
p->nerr = p->nwarn = 0; |
| 6649 |
p->lex_strterm = NULL; |
| 6650 |
parser_init_cxt(p, c); |
| 6651 |
|
| 6652 |
n = yyparse(p); |
| 6653 |
if (n != 0 || p->nerr > 0) { |
| 6654 |
p->tree = 0; |
| 6655 |
p->mrb->jmp = prev; |
| 6656 |
return; |
| 6657 |
} |
| 6658 |
parser_update_cxt(p, c); |
| 6659 |
if (c && c->dump_result) { |
| 6660 |
mrb_parser_dump(p->mrb, p->tree, 0); |
| 6661 |
} |
| 6662 |
} |
| 6663 |
MRB_CATCH(p->mrb->jmp) { |
| 6664 |
p->nerr++; |
| 6665 |
if (p->mrb->exc == NULL) { |
| 6666 |
yyerror(p, "memory allocation error"); |
| 6667 |
p->nerr++; |
| 6668 |
p->tree = 0; |
| 6669 |
} |
| 6670 |
} |
| 6671 |
MRB_END_EXC(p->jmp); |
| 6672 |
p->mrb->jmp = prev; |
| 6673 |
} |
| 6674 |
|
| 6675 |
MRB_API parser_state* |
| 6676 |
mrb_parser_new(mrb_state *mrb) |
| 6677 |
{ |
| 6678 |
mrb_pool *pool; |
| 6679 |
parser_state *p; |
| 6680 |
static const parser_state parser_state_zero = { 0 }; |
| 6681 |
|
| 6682 |
pool = mrb_pool_open(mrb); |
| 6683 |
if (!pool) return NULL; |
| 6684 |
p = (parser_state*)mrb_pool_alloc(pool, sizeof(parser_state)); |
| 6685 |
if (!p) return NULL; |
| 6686 |
|
| 6687 |
*p = parser_state_zero; |
| 6688 |
p->mrb = mrb; |
| 6689 |
p->pool = pool; |
| 6690 |
|
| 6691 |
p->s = p->send = NULL; |
| 6692 |
#ifndef MRB_NO_STDIO |
| 6693 |
p->f = NULL; |
| 6694 |
#endif |
| 6695 |
|
| 6696 |
p->cmd_start = TRUE; |
| 6697 |
p->in_def = p->in_single = 0; |
| 6698 |
|
| 6699 |
p->capture_errors = FALSE; |
| 6700 |
p->lineno = 1; |
| 6701 |
p->column = 0; |
| 6702 |
#if defined(PARSER_TEST) || defined(PARSER_DEBUG) |
| 6703 |
yydebug = 1; |
| 6704 |
#endif |
| 6705 |
p->tsiz = MRB_PARSER_TOKBUF_SIZE; |
| 6706 |
p->tokbuf = p->buf; |
| 6707 |
|
| 6708 |
p->lex_strterm = NULL; |
| 6709 |
|
| 6710 |
p->current_filename_index = -1; |
| 6711 |
p->filename_table = NULL; |
| 6712 |
p->filename_table_length = 0; |
| 6713 |
|
| 6714 |
return p; |
| 6715 |
} |
| 6716 |
|
| 6717 |
MRB_API void |
| 6718 |
mrb_parser_free(parser_state *p) { |
| 6719 |
if (p->tokbuf != p->buf) { |
| 6720 |
mrb_free(p->mrb, p->tokbuf); |
| 6721 |
} |
| 6722 |
mrb_pool_close(p->pool); |
| 6723 |
} |
| 6724 |
|
| 6725 |
MRB_API mrbc_context* |
| 6726 |
mrbc_context_new(mrb_state *mrb) |
| 6727 |
{ |
| 6728 |
return (mrbc_context*)mrb_calloc(mrb, 1, sizeof(mrbc_context)); |
| 6729 |
} |
| 6730 |
|
| 6731 |
MRB_API void |
| 6732 |
mrbc_context_free(mrb_state *mrb, mrbc_context *cxt) |
| 6733 |
{ |
| 6734 |
mrb_free(mrb, cxt->filename); |
| 6735 |
mrb_free(mrb, cxt->syms); |
| 6736 |
mrb_free(mrb, cxt); |
| 6737 |
} |
| 6738 |
|
| 6739 |
MRB_API const char* |
| 6740 |
mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s) |
| 6741 |
{ |
| 6742 |
if (s) { |
| 6743 |
size_t len = strlen(s); |
| 6744 |
char *p = (char*)mrb_malloc_simple(mrb, len + 1); |
| 6745 |
|
| 6746 |
if (p == NULL) return NULL; |
| 6747 |
memcpy(p, s, len + 1); |
| 6748 |
if (c->filename) { |
| 6749 |
mrb_free(mrb, c->filename); |
| 6750 |
} |
| 6751 |
c->filename = p; |
| 6752 |
} |
| 6753 |
return c->filename; |
| 6754 |
} |
| 6755 |
|
| 6756 |
MRB_API void |
| 6757 |
mrbc_partial_hook(mrb_state *mrb, mrbc_context *c, int (*func)(struct mrb_parser_state*), void *data) |
| 6758 |
{ |
| 6759 |
c->partial_hook = func; |
| 6760 |
c->partial_data = data; |
| 6761 |
} |
| 6762 |
|
| 6763 |
MRB_API void |
| 6764 |
mrbc_cleanup_local_variables(mrb_state *mrb, mrbc_context *c) |
| 6765 |
{ |
| 6766 |
if (c->syms) { |
| 6767 |
mrb_free(mrb, c->syms); |
| 6768 |
c->syms = NULL; |
| 6769 |
c->slen = 0; |
| 6770 |
} |
| 6771 |
c->keep_lv = FALSE; |
| 6772 |
} |
| 6773 |
|
| 6774 |
MRB_API void |
| 6775 |
mrb_parser_set_filename(struct mrb_parser_state *p, const char *f) |
| 6776 |
{ |
| 6777 |
mrb_sym sym; |
| 6778 |
uint16_t i; |
| 6779 |
mrb_sym* new_table; |
| 6780 |
|
| 6781 |
sym = mrb_intern_cstr(p->mrb, f); |
| 6782 |
p->filename_sym = sym; |
| 6783 |
p->lineno = (p->filename_table_length > 0)? 0 : 1; |
| 6784 |
|
| 6785 |
for (i = 0; i < p->filename_table_length; i++) { |
| 6786 |
if (p->filename_table[i] == sym) { |
| 6787 |
p->current_filename_index = i; |
| 6788 |
return; |
| 6789 |
} |
| 6790 |
} |
| 6791 |
|
| 6792 |
if (p->filename_table_length == UINT16_MAX) { |
| 6793 |
yyerror(p, "too many files to compile"); |
| 6794 |
return; |
| 6795 |
} |
| 6796 |
p->current_filename_index = p->filename_table_length++; |
| 6797 |
|
| 6798 |
new_table = (mrb_sym*)parser_palloc(p, sizeof(mrb_sym) * p->filename_table_length); |
| 6799 |
if (p->filename_table) { |
| 6800 |
memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->current_filename_index); |
| 6801 |
} |
| 6802 |
p->filename_table = new_table; |
| 6803 |
p->filename_table[p->filename_table_length - 1] = sym; |
| 6804 |
} |
| 6805 |
|
| 6806 |
MRB_API mrb_sym |
| 6807 |
mrb_parser_get_filename(struct mrb_parser_state* p, uint16_t idx) { |
| 6808 |
if (idx >= p->filename_table_length) return 0; |
| 6809 |
else { |
| 6810 |
return p->filename_table[idx]; |
| 6811 |
} |
| 6812 |
} |
| 6813 |
|
| 6814 |
#ifndef MRB_NO_STDIO |
| 6815 |
static struct mrb_parser_state * |
| 6816 |
mrb_parse_file_continue(mrb_state *mrb, FILE *f, const void *prebuf, size_t prebufsize, mrbc_context *c) |
| 6817 |
{ |
| 6818 |
parser_state *p; |
| 6819 |
|
| 6820 |
p = mrb_parser_new(mrb); |
| 6821 |
if (!p) return NULL; |
| 6822 |
if (prebuf) { |
| 6823 |
p->s = (const char*)prebuf; |
| 6824 |
p->send = (const char*)prebuf + prebufsize; |
| 6825 |
} |
| 6826 |
else { |
| 6827 |
p->s = p->send = NULL; |
| 6828 |
} |
| 6829 |
p->f = f; |
| 6830 |
|
| 6831 |
mrb_parser_parse(p, c); |
| 6832 |
return p; |
| 6833 |
} |
| 6834 |
|
| 6835 |
MRB_API parser_state* |
| 6836 |
mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c) |
| 6837 |
{ |
| 6838 |
return mrb_parse_file_continue(mrb, f, NULL, 0, c); |
| 6839 |
} |
| 6840 |
#endif |
| 6841 |
|
| 6842 |
MRB_API parser_state* |
| 6843 |
mrb_parse_nstring(mrb_state *mrb, const char *s, size_t len, mrbc_context *c) |
| 6844 |
{ |
| 6845 |
parser_state *p; |
| 6846 |
|
| 6847 |
p = mrb_parser_new(mrb); |
| 6848 |
if (!p) return NULL; |
| 6849 |
p->s = s; |
| 6850 |
p->send = s + len; |
| 6851 |
|
| 6852 |
mrb_parser_parse(p, c); |
| 6853 |
return p; |
| 6854 |
} |
| 6855 |
|
| 6856 |
MRB_API parser_state* |
| 6857 |
mrb_parse_string(mrb_state *mrb, const char *s, mrbc_context *c) |
| 6858 |
{ |
| 6859 |
return mrb_parse_nstring(mrb, s, strlen(s), c); |
| 6860 |
} |
| 6861 |
|
| 6862 |
MRB_API mrb_value |
| 6863 |
mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c) |
| 6864 |
{ |
| 6865 |
struct RClass *target = mrb->object_class; |
| 6866 |
struct RProc *proc; |
| 6867 |
mrb_value v; |
| 6868 |
mrb_int keep = 0; |
| 6869 |
|
| 6870 |
if (!p) { |
| 6871 |
return mrb_undef_value(); |
| 6872 |
} |
| 6873 |
if (!p->tree || p->nerr) { |
| 6874 |
if (c) c->parser_nerr = p->nerr; |
| 6875 |
if (p->capture_errors) { |
| 6876 |
char buf[256]; |
| 6877 |
|
| 6878 |
strcpy(buf, "line "); |
| 6879 |
dump_int(p->error_buffer[0].lineno, buf+5); |
| 6880 |
strcat(buf, ": "); |
| 6881 |
strncat(buf, p->error_buffer[0].message, sizeof(buf) - strlen(buf) - 1); |
| 6882 |
mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, strlen(buf))); |
| 6883 |
mrb_parser_free(p); |
| 6884 |
return mrb_undef_value(); |
| 6885 |
} |
| 6886 |
else { |
| 6887 |
if (mrb->exc == NULL) { |
| 6888 |
mrb->exc = mrb_obj_ptr(mrb_exc_new_lit(mrb, E_SYNTAX_ERROR, "syntax error")); |
| 6889 |
} |
| 6890 |
mrb_parser_free(p); |
| 6891 |
return mrb_undef_value(); |
| 6892 |
} |
| 6893 |
} |
| 6894 |
proc = mrb_generate_code(mrb, p); |
| 6895 |
mrb_parser_free(p); |
| 6896 |
if (proc == NULL) { |
| 6897 |
if (mrb->exc == NULL) { |
| 6898 |
mrb->exc = mrb_obj_ptr(mrb_exc_new_lit(mrb, E_SCRIPT_ERROR, "codegen error")); |
| 6899 |
} |
| 6900 |
return mrb_undef_value(); |
| 6901 |
} |
| 6902 |
if (c) { |
| 6903 |
if (c->dump_result) mrb_codedump_all(mrb, proc); |
| 6904 |
if (c->no_exec) return mrb_obj_value(proc); |
| 6905 |
if (c->target_class) { |
| 6906 |
target = c->target_class; |
| 6907 |
} |
| 6908 |
if (c->keep_lv) { |
| 6909 |
keep = c->slen + 1; |
| 6910 |
} |
| 6911 |
else { |
| 6912 |
c->keep_lv = TRUE; |
| 6913 |
} |
| 6914 |
} |
| 6915 |
MRB_PROC_SET_TARGET_CLASS(proc, target); |
| 6916 |
if (mrb->c->ci) { |
| 6917 |
mrb_vm_ci_target_class_set(mrb->c->ci, target); |
| 6918 |
} |
| 6919 |
v = mrb_top_run(mrb, proc, mrb_top_self(mrb), keep); |
| 6920 |
if (mrb->exc) return mrb_nil_value(); |
| 6921 |
return v; |
| 6922 |
} |
| 6923 |
|
| 6924 |
#ifndef MRB_NO_STDIO |
| 6925 |
MRB_API mrb_value |
| 6926 |
mrb_load_file_cxt(mrb_state *mrb, FILE *f, mrbc_context *c) |
| 6927 |
{ |
| 6928 |
return mrb_load_exec(mrb, mrb_parse_file(mrb, f, c), c); |
| 6929 |
} |
| 6930 |
|
| 6931 |
MRB_API mrb_value |
| 6932 |
mrb_load_file(mrb_state *mrb, FILE *f) |
| 6933 |
{ |
| 6934 |
return mrb_load_file_cxt(mrb, f, NULL); |
| 6935 |
} |
| 6936 |
|
| 6937 |
#define DETECT_SIZE 64 |
| 6938 |
|
| 6939 |
/* |
| 6940 |
* In order to be recognized as a `.mrb` file, the following three points must be satisfied: |
| 6941 |
* - File starts with "RITE" |
| 6942 |
* - At least `sizeof(struct rite_binary_header)` bytes can be read |
| 6943 |
* - `NUL` is included in the first 64 bytes of the file |
| 6944 |
*/ |
| 6945 |
MRB_API mrb_value |
| 6946 |
mrb_load_detect_file_cxt(mrb_state *mrb, FILE *fp, mrbc_context *c) |
| 6947 |
{ |
| 6948 |
union { |
| 6949 |
char b[DETECT_SIZE]; |
| 6950 |
struct rite_binary_header h; |
| 6951 |
} leading; |
| 6952 |
size_t bufsize; |
| 6953 |
|
| 6954 |
if (mrb == NULL || fp == NULL) { |
| 6955 |
return mrb_nil_value(); |
| 6956 |
} |
| 6957 |
|
| 6958 |
bufsize = fread(leading.b, sizeof(char), sizeof(leading), fp); |
| 6959 |
if (bufsize < sizeof(leading.h) || |
| 6960 |
memcmp(leading.h.binary_ident, RITE_BINARY_IDENT, sizeof(leading.h.binary_ident)) != 0 || |
| 6961 |
memchr(leading.b, '\0', bufsize) == NULL) { |
| 6962 |
return mrb_load_exec(mrb, mrb_parse_file_continue(mrb, fp, leading.b, bufsize, c), c); |
| 6963 |
} |
| 6964 |
else { |
| 6965 |
mrb_int binsize; |
| 6966 |
uint8_t *bin; |
| 6967 |
mrb_value bin_obj = mrb_nil_value(); /* temporary string object */ |
| 6968 |
mrb_value result; |
| 6969 |
|
| 6970 |
binsize = bin_to_uint32(leading.h.binary_size); |
| 6971 |
bin_obj = mrb_str_new(mrb, NULL, binsize); |
| 6972 |
bin = (uint8_t*)RSTRING_PTR(bin_obj); |
| 6973 |
if ((size_t)binsize > bufsize) { |
| 6974 |
memcpy(bin, leading.b, bufsize); |
| 6975 |
if (fread(bin + bufsize, binsize - bufsize, 1, fp) == 0) { |
| 6976 |
binsize = bufsize; |
| 6977 |
/* The error is reported by mrb_load_irep_buf_cxt() */ |
| 6978 |
} |
| 6979 |
} |
| 6980 |
|
| 6981 |
result = mrb_load_irep_buf_cxt(mrb, bin, binsize, c); |
| 6982 |
if (mrb_string_p(bin_obj)) mrb_str_resize(mrb, bin_obj, 0); |
| 6983 |
return result; |
| 6984 |
} |
| 6985 |
} |
| 6986 |
#endif |
| 6987 |
|
| 6988 |
MRB_API mrb_value |
| 6989 |
mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrbc_context *c) |
| 6990 |
{ |
| 6991 |
return mrb_load_exec(mrb, mrb_parse_nstring(mrb, s, len, c), c); |
| 6992 |
} |
| 6993 |
|
| 6994 |
MRB_API mrb_value |
| 6995 |
mrb_load_nstring(mrb_state *mrb, const char *s, size_t len) |
| 6996 |
{ |
| 6997 |
return mrb_load_nstring_cxt(mrb, s, len, NULL); |
| 6998 |
} |
| 6999 |
|
| 7000 |
MRB_API mrb_value |
| 7001 |
mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *c) |
| 7002 |
{ |
| 7003 |
return mrb_load_nstring_cxt(mrb, s, strlen(s), c); |
| 7004 |
} |
| 7005 |
|
| 7006 |
MRB_API mrb_value |
| 7007 |
mrb_load_string(mrb_state *mrb, const char *s) |
| 7008 |
{ |
| 7009 |
return mrb_load_string_cxt(mrb, s, NULL); |
| 7010 |
} |
| 7011 |
|
| 7012 |
#ifndef MRB_NO_STDIO |
| 7013 |
|
| 7014 |
static void |
| 7015 |
dump_prefix(node *tree, int offset) |
| 7016 |
{ |
| 7017 |
printf("%05d ", tree->lineno); |
| 7018 |
while (offset--) { |
| 7019 |
putc(' ', stdout); |
| 7020 |
putc(' ', stdout); |
| 7021 |
} |
| 7022 |
} |
| 7023 |
|
| 7024 |
static void |
| 7025 |
dump_recur(mrb_state *mrb, node *tree, int offset) |
| 7026 |
{ |
| 7027 |
while (tree) { |
| 7028 |
mrb_parser_dump(mrb, tree->car, offset); |
| 7029 |
tree = tree->cdr; |
| 7030 |
} |
| 7031 |
} |
| 7032 |
|
| 7033 |
static void |
| 7034 |
dump_args(mrb_state *mrb, node *n, int offset) |
| 7035 |
{ |
| 7036 |
if (n->car) { |
| 7037 |
dump_prefix(n, offset+1); |
| 7038 |
printf("mandatory args:\n"); |
| 7039 |
dump_recur(mrb, n->car, offset+2); |
| 7040 |
} |
| 7041 |
n = n->cdr; |
| 7042 |
if (n->car) { |
| 7043 |
dump_prefix(n, offset+1); |
| 7044 |
printf("optional args:\n"); |
| 7045 |
{ |
| 7046 |
node *n2 = n->car; |
| 7047 |
|
| 7048 |
while (n2) { |
| 7049 |
dump_prefix(n2, offset+2); |
| 7050 |
printf("%s=\n", mrb_sym_name(mrb, sym(n2->car->car))); |
| 7051 |
mrb_parser_dump(mrb, n2->car->cdr, offset+3); |
| 7052 |
n2 = n2->cdr; |
| 7053 |
} |
| 7054 |
} |
| 7055 |
} |
| 7056 |
n = n->cdr; |
| 7057 |
if (n->car) { |
| 7058 |
mrb_sym rest = sym(n->car); |
| 7059 |
|
| 7060 |
dump_prefix(n, offset+1); |
| 7061 |
if (rest == MRB_OPSYM(mul)) |
| 7062 |
printf("rest=*\n"); |
| 7063 |
else |
| 7064 |
printf("rest=*%s\n", mrb_sym_name(mrb, rest)); |
| 7065 |
} |
| 7066 |
n = n->cdr; |
| 7067 |
if (n->car) { |
| 7068 |
dump_prefix(n, offset+1); |
| 7069 |
printf("post mandatory args:\n"); |
| 7070 |
dump_recur(mrb, n->car, offset+2); |
| 7071 |
} |
| 7072 |
|
| 7073 |
n = n->cdr; |
| 7074 |
if (n) { |
| 7075 |
mrb_assert(intn(n->car) == NODE_ARGS_TAIL); |
| 7076 |
mrb_parser_dump(mrb, n, offset); |
| 7077 |
} |
| 7078 |
} |
| 7079 |
|
| 7080 |
/* |
| 7081 |
* This function restores the GC arena on return. |
| 7082 |
* For this reason, if a process that further generates an object is |
| 7083 |
* performed at the caller, the string pointer returned as the return |
| 7084 |
* value may become invalid. |
| 7085 |
*/ |
| 7086 |
static const char* |
| 7087 |
str_dump(mrb_state *mrb, const char *str, int len) |
| 7088 |
{ |
| 7089 |
int ai = mrb_gc_arena_save(mrb); |
| 7090 |
mrb_value s; |
| 7091 |
# if INT_MAX > MRB_INT_MAX / 4 |
| 7092 |
/* check maximum length with "\xNN" character */ |
| 7093 |
if (len > MRB_INT_MAX / 4) { |
| 7094 |
len = MRB_INT_MAX / 4; |
| 7095 |
} |
| 7096 |
# endif |
| 7097 |
s = mrb_str_new(mrb, str, (mrb_int)len); |
| 7098 |
s = mrb_str_dump(mrb, s); |
| 7099 |
mrb_gc_arena_restore(mrb, ai); |
| 7100 |
return RSTRING_PTR(s); |
| 7101 |
} |
| 7102 |
#endif |
| 7103 |
|
| 7104 |
void |
| 7105 |
mrb_parser_dump(mrb_state *mrb, node *tree, int offset) |
| 7106 |
{ |
| 7107 |
#ifndef MRB_NO_STDIO |
| 7108 |
int nodetype; |
| 7109 |
|
| 7110 |
if (!tree) return; |
| 7111 |
again: |
| 7112 |
dump_prefix(tree, offset); |
| 7113 |
nodetype = intn(tree->car); |
| 7114 |
tree = tree->cdr; |
| 7115 |
switch (nodetype) { |
| 7116 |
case NODE_BEGIN: |
| 7117 |
printf("NODE_BEGIN:\n"); |
| 7118 |
dump_recur(mrb, tree, offset+1); |
| 7119 |
break; |
| 7120 |
|
| 7121 |
case NODE_RESCUE: |
| 7122 |
printf("NODE_RESCUE:\n"); |
| 7123 |
if (tree->car) { |
| 7124 |
dump_prefix(tree, offset+1); |
| 7125 |
printf("body:\n"); |
| 7126 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7127 |
} |
| 7128 |
tree = tree->cdr; |
| 7129 |
if (tree->car) { |
| 7130 |
node *n2 = tree->car; |
| 7131 |
|
| 7132 |
dump_prefix(n2, offset+1); |
| 7133 |
printf("rescue:\n"); |
| 7134 |
while (n2) { |
| 7135 |
node *n3 = n2->car; |
| 7136 |
if (n3->car) { |
| 7137 |
dump_prefix(n2, offset+2); |
| 7138 |
printf("handle classes:\n"); |
| 7139 |
dump_recur(mrb, n3->car, offset+3); |
| 7140 |
} |
| 7141 |
if (n3->cdr->car) { |
| 7142 |
dump_prefix(n3, offset+2); |
| 7143 |
printf("exc_var:\n"); |
| 7144 |
mrb_parser_dump(mrb, n3->cdr->car, offset+3); |
| 7145 |
} |
| 7146 |
if (n3->cdr->cdr->car) { |
| 7147 |
dump_prefix(n3, offset+2); |
| 7148 |
printf("rescue body:\n"); |
| 7149 |
mrb_parser_dump(mrb, n3->cdr->cdr->car, offset+3); |
| 7150 |
} |
| 7151 |
n2 = n2->cdr; |
| 7152 |
} |
| 7153 |
} |
| 7154 |
tree = tree->cdr; |
| 7155 |
if (tree->car) { |
| 7156 |
dump_prefix(tree, offset+1); |
| 7157 |
printf("else:\n"); |
| 7158 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7159 |
} |
| 7160 |
break; |
| 7161 |
|
| 7162 |
case NODE_ENSURE: |
| 7163 |
printf("NODE_ENSURE:\n"); |
| 7164 |
dump_prefix(tree, offset+1); |
| 7165 |
printf("body:\n"); |
| 7166 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7167 |
dump_prefix(tree, offset+1); |
| 7168 |
printf("ensure:\n"); |
| 7169 |
mrb_parser_dump(mrb, tree->cdr->cdr, offset+2); |
| 7170 |
break; |
| 7171 |
|
| 7172 |
case NODE_LAMBDA: |
| 7173 |
printf("NODE_LAMBDA:\n"); |
| 7174 |
dump_prefix(tree, offset); |
| 7175 |
goto block; |
| 7176 |
|
| 7177 |
case NODE_BLOCK: |
| 7178 |
block: |
| 7179 |
printf("NODE_BLOCK:\n"); |
| 7180 |
tree = tree->cdr; |
| 7181 |
if (tree->car) { |
| 7182 |
dump_args(mrb, tree->car, offset+1); |
| 7183 |
} |
| 7184 |
dump_prefix(tree, offset+1); |
| 7185 |
printf("body:\n"); |
| 7186 |
mrb_parser_dump(mrb, tree->cdr->car, offset+2); |
| 7187 |
break; |
| 7188 |
|
| 7189 |
case NODE_IF: |
| 7190 |
printf("NODE_IF:\n"); |
| 7191 |
dump_prefix(tree, offset+1); |
| 7192 |
printf("cond:\n"); |
| 7193 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7194 |
dump_prefix(tree, offset+1); |
| 7195 |
printf("then:\n"); |
| 7196 |
mrb_parser_dump(mrb, tree->cdr->car, offset+2); |
| 7197 |
if (tree->cdr->cdr->car) { |
| 7198 |
dump_prefix(tree, offset+1); |
| 7199 |
printf("else:\n"); |
| 7200 |
mrb_parser_dump(mrb, tree->cdr->cdr->car, offset+2); |
| 7201 |
} |
| 7202 |
break; |
| 7203 |
|
| 7204 |
case NODE_AND: |
| 7205 |
printf("NODE_AND:\n"); |
| 7206 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7207 |
mrb_parser_dump(mrb, tree->cdr, offset+1); |
| 7208 |
break; |
| 7209 |
|
| 7210 |
case NODE_OR: |
| 7211 |
printf("NODE_OR:\n"); |
| 7212 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7213 |
mrb_parser_dump(mrb, tree->cdr, offset+1); |
| 7214 |
break; |
| 7215 |
|
| 7216 |
case NODE_CASE: |
| 7217 |
printf("NODE_CASE:\n"); |
| 7218 |
if (tree->car) { |
| 7219 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7220 |
} |
| 7221 |
tree = tree->cdr; |
| 7222 |
while (tree) { |
| 7223 |
dump_prefix(tree, offset+1); |
| 7224 |
printf("case:\n"); |
| 7225 |
dump_recur(mrb, tree->car->car, offset+2); |
| 7226 |
dump_prefix(tree, offset+1); |
| 7227 |
printf("body:\n"); |
| 7228 |
mrb_parser_dump(mrb, tree->car->cdr, offset+2); |
| 7229 |
tree = tree->cdr; |
| 7230 |
} |
| 7231 |
break; |
| 7232 |
|
| 7233 |
case NODE_WHILE: |
| 7234 |
printf("NODE_WHILE:\n"); |
| 7235 |
dump_prefix(tree, offset+1); |
| 7236 |
printf("cond:\n"); |
| 7237 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7238 |
dump_prefix(tree, offset+1); |
| 7239 |
printf("body:\n"); |
| 7240 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7241 |
break; |
| 7242 |
|
| 7243 |
case NODE_UNTIL: |
| 7244 |
printf("NODE_UNTIL:\n"); |
| 7245 |
dump_prefix(tree, offset+1); |
| 7246 |
printf("cond:\n"); |
| 7247 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7248 |
dump_prefix(tree, offset+1); |
| 7249 |
printf("body:\n"); |
| 7250 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7251 |
break; |
| 7252 |
|
| 7253 |
case NODE_FOR: |
| 7254 |
printf("NODE_FOR:\n"); |
| 7255 |
dump_prefix(tree, offset+1); |
| 7256 |
printf("var:\n"); |
| 7257 |
{ |
| 7258 |
node *n2 = tree->car; |
| 7259 |
|
| 7260 |
if (n2->car) { |
| 7261 |
dump_prefix(n2, offset+2); |
| 7262 |
printf("pre:\n"); |
| 7263 |
dump_recur(mrb, n2->car, offset+3); |
| 7264 |
} |
| 7265 |
n2 = n2->cdr; |
| 7266 |
if (n2) { |
| 7267 |
if (n2->car) { |
| 7268 |
dump_prefix(n2, offset+2); |
| 7269 |
printf("rest:\n"); |
| 7270 |
mrb_parser_dump(mrb, n2->car, offset+3); |
| 7271 |
} |
| 7272 |
n2 = n2->cdr; |
| 7273 |
if (n2) { |
| 7274 |
if (n2->car) { |
| 7275 |
dump_prefix(n2, offset+2); |
| 7276 |
printf("post:\n"); |
| 7277 |
dump_recur(mrb, n2->car, offset+3); |
| 7278 |
} |
| 7279 |
} |
| 7280 |
} |
| 7281 |
} |
| 7282 |
tree = tree->cdr; |
| 7283 |
dump_prefix(tree, offset+1); |
| 7284 |
printf("in:\n"); |
| 7285 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7286 |
tree = tree->cdr; |
| 7287 |
dump_prefix(tree, offset+1); |
| 7288 |
printf("do:\n"); |
| 7289 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7290 |
break; |
| 7291 |
|
| 7292 |
case NODE_SCOPE: |
| 7293 |
printf("NODE_SCOPE:\n"); |
| 7294 |
{ |
| 7295 |
node *n2 = tree->car; |
| 7296 |
mrb_bool first_lval = TRUE; |
| 7297 |
|
| 7298 |
if (n2 && (n2->car || n2->cdr)) { |
| 7299 |
dump_prefix(n2, offset+1); |
| 7300 |
printf("local variables:\n"); |
| 7301 |
dump_prefix(n2, offset+2); |
| 7302 |
while (n2) { |
| 7303 |
if (n2->car) { |
| 7304 |
if (!first_lval) printf(", "); |
| 7305 |
printf("%s", mrb_sym_name(mrb, sym(n2->car))); |
| 7306 |
first_lval = FALSE; |
| 7307 |
} |
| 7308 |
n2 = n2->cdr; |
| 7309 |
} |
| 7310 |
printf("\n"); |
| 7311 |
} |
| 7312 |
} |
| 7313 |
tree = tree->cdr; |
| 7314 |
offset++; |
| 7315 |
goto again; |
| 7316 |
|
| 7317 |
case NODE_FCALL: |
| 7318 |
case NODE_CALL: |
| 7319 |
case NODE_SCALL: |
| 7320 |
switch (nodetype) { |
| 7321 |
case NODE_FCALL: |
| 7322 |
printf("NODE_FCALL:\n"); break; |
| 7323 |
case NODE_CALL: |
| 7324 |
printf("NODE_CALL(.):\n"); break; |
| 7325 |
case NODE_SCALL: |
| 7326 |
printf("NODE_SCALL(&.):\n"); break; |
| 7327 |
default: |
| 7328 |
break; |
| 7329 |
} |
| 7330 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7331 |
dump_prefix(tree, offset+1); |
| 7332 |
printf("method='%s' (%d)\n", |
| 7333 |
mrb_sym_dump(mrb, sym(tree->cdr->car)), |
| 7334 |
intn(tree->cdr->car)); |
| 7335 |
tree = tree->cdr->cdr->car; |
| 7336 |
if (tree) { |
| 7337 |
dump_prefix(tree, offset+1); |
| 7338 |
printf("args:\n"); |
| 7339 |
dump_recur(mrb, tree->car, offset+2); |
| 7340 |
if (tree->cdr) { |
| 7341 |
if (tree->cdr->car) { |
| 7342 |
dump_prefix(tree, offset+1); |
| 7343 |
printf("kwargs:\n"); |
| 7344 |
mrb_parser_dump(mrb, tree->cdr->car, offset+2); |
| 7345 |
} |
| 7346 |
if (tree->cdr->cdr) { |
| 7347 |
dump_prefix(tree, offset+1); |
| 7348 |
printf("block:\n"); |
| 7349 |
mrb_parser_dump(mrb, tree->cdr->cdr, offset+2); |
| 7350 |
} |
| 7351 |
} |
| 7352 |
} |
| 7353 |
break; |
| 7354 |
|
| 7355 |
case NODE_DOT2: |
| 7356 |
printf("NODE_DOT2:\n"); |
| 7357 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7358 |
mrb_parser_dump(mrb, tree->cdr, offset+1); |
| 7359 |
break; |
| 7360 |
|
| 7361 |
case NODE_DOT3: |
| 7362 |
printf("NODE_DOT3:\n"); |
| 7363 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7364 |
mrb_parser_dump(mrb, tree->cdr, offset+1); |
| 7365 |
break; |
| 7366 |
|
| 7367 |
case NODE_COLON2: |
| 7368 |
printf("NODE_COLON2:\n"); |
| 7369 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7370 |
dump_prefix(tree, offset+1); |
| 7371 |
printf("::%s\n", mrb_sym_name(mrb, sym(tree->cdr))); |
| 7372 |
break; |
| 7373 |
|
| 7374 |
case NODE_COLON3: |
| 7375 |
printf("NODE_COLON3: ::%s\n", mrb_sym_name(mrb, sym(tree))); |
| 7376 |
break; |
| 7377 |
|
| 7378 |
case NODE_ARRAY: |
| 7379 |
printf("NODE_ARRAY:\n"); |
| 7380 |
dump_recur(mrb, tree, offset+1); |
| 7381 |
break; |
| 7382 |
|
| 7383 |
case NODE_HASH: |
| 7384 |
printf("NODE_HASH:\n"); |
| 7385 |
while (tree) { |
| 7386 |
dump_prefix(tree, offset+1); |
| 7387 |
printf("key:\n"); |
| 7388 |
mrb_parser_dump(mrb, tree->car->car, offset+2); |
| 7389 |
dump_prefix(tree, offset+1); |
| 7390 |
printf("value:\n"); |
| 7391 |
mrb_parser_dump(mrb, tree->car->cdr, offset+2); |
| 7392 |
tree = tree->cdr; |
| 7393 |
} |
| 7394 |
break; |
| 7395 |
|
| 7396 |
case NODE_KW_HASH: |
| 7397 |
printf("NODE_KW_HASH:\n"); |
| 7398 |
while (tree) { |
| 7399 |
dump_prefix(tree, offset+1); |
| 7400 |
printf("key:\n"); |
| 7401 |
mrb_parser_dump(mrb, tree->car->car, offset+2); |
| 7402 |
dump_prefix(tree, offset+1); |
| 7403 |
printf("value:\n"); |
| 7404 |
mrb_parser_dump(mrb, tree->car->cdr, offset+2); |
| 7405 |
tree = tree->cdr; |
| 7406 |
} |
| 7407 |
break; |
| 7408 |
|
| 7409 |
case NODE_SPLAT: |
| 7410 |
printf("NODE_SPLAT:\n"); |
| 7411 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7412 |
break; |
| 7413 |
|
| 7414 |
case NODE_ASGN: |
| 7415 |
printf("NODE_ASGN:\n"); |
| 7416 |
dump_prefix(tree, offset+1); |
| 7417 |
printf("lhs:\n"); |
| 7418 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7419 |
dump_prefix(tree, offset+1); |
| 7420 |
printf("rhs:\n"); |
| 7421 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7422 |
break; |
| 7423 |
|
| 7424 |
case NODE_MASGN: |
| 7425 |
printf("NODE_MASGN:\n"); |
| 7426 |
dump_prefix(tree, offset+1); |
| 7427 |
printf("mlhs:\n"); |
| 7428 |
{ |
| 7429 |
node *n2 = tree->car; |
| 7430 |
|
| 7431 |
if (n2->car) { |
| 7432 |
dump_prefix(tree, offset+2); |
| 7433 |
printf("pre:\n"); |
| 7434 |
dump_recur(mrb, n2->car, offset+3); |
| 7435 |
} |
| 7436 |
n2 = n2->cdr; |
| 7437 |
if (n2) { |
| 7438 |
if (n2->car) { |
| 7439 |
dump_prefix(n2, offset+2); |
| 7440 |
printf("rest:\n"); |
| 7441 |
if (n2->car == nint(-1)) { |
| 7442 |
dump_prefix(n2, offset+2); |
| 7443 |
printf("(empty)\n"); |
| 7444 |
} |
| 7445 |
else { |
| 7446 |
mrb_parser_dump(mrb, n2->car, offset+3); |
| 7447 |
} |
| 7448 |
} |
| 7449 |
n2 = n2->cdr; |
| 7450 |
if (n2 && n2->car) { |
| 7451 |
dump_prefix(n2, offset+2); |
| 7452 |
printf("post:\n"); |
| 7453 |
dump_recur(mrb, n2->car, offset+3); |
| 7454 |
} |
| 7455 |
} |
| 7456 |
} |
| 7457 |
dump_prefix(tree, offset+1); |
| 7458 |
printf("rhs:\n"); |
| 7459 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7460 |
break; |
| 7461 |
|
| 7462 |
case NODE_OP_ASGN: |
| 7463 |
printf("NODE_OP_ASGN:\n"); |
| 7464 |
dump_prefix(tree, offset+1); |
| 7465 |
printf("lhs:\n"); |
| 7466 |
mrb_parser_dump(mrb, tree->car, offset+2); |
| 7467 |
tree = tree->cdr; |
| 7468 |
dump_prefix(tree, offset+1); |
| 7469 |
printf("op='%s' (%d)\n", mrb_sym_name(mrb, sym(tree->car)), intn(tree->car)); |
| 7470 |
tree = tree->cdr; |
| 7471 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7472 |
break; |
| 7473 |
|
| 7474 |
case NODE_SUPER: |
| 7475 |
printf("NODE_SUPER:\n"); |
| 7476 |
if (tree) { |
| 7477 |
dump_prefix(tree, offset+1); |
| 7478 |
printf("args:\n"); |
| 7479 |
dump_recur(mrb, tree->car, offset+2); |
| 7480 |
if (tree->cdr) { |
| 7481 |
dump_prefix(tree, offset+1); |
| 7482 |
printf("block:\n"); |
| 7483 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7484 |
} |
| 7485 |
} |
| 7486 |
break; |
| 7487 |
|
| 7488 |
case NODE_ZSUPER: |
| 7489 |
printf("NODE_ZSUPER:\n"); |
| 7490 |
if (tree) { |
| 7491 |
dump_prefix(tree, offset+1); |
| 7492 |
printf("args:\n"); |
| 7493 |
dump_recur(mrb, tree->car, offset+2); |
| 7494 |
if (tree->cdr) { |
| 7495 |
dump_prefix(tree, offset+1); |
| 7496 |
printf("block:\n"); |
| 7497 |
mrb_parser_dump(mrb, tree->cdr, offset+2); |
| 7498 |
} |
| 7499 |
} |
| 7500 |
break; |
| 7501 |
|
| 7502 |
case NODE_RETURN: |
| 7503 |
printf("NODE_RETURN:\n"); |
| 7504 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7505 |
break; |
| 7506 |
|
| 7507 |
case NODE_YIELD: |
| 7508 |
printf("NODE_YIELD:\n"); |
| 7509 |
dump_recur(mrb, tree, offset+1); |
| 7510 |
break; |
| 7511 |
|
| 7512 |
case NODE_BREAK: |
| 7513 |
printf("NODE_BREAK:\n"); |
| 7514 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7515 |
break; |
| 7516 |
|
| 7517 |
case NODE_NEXT: |
| 7518 |
printf("NODE_NEXT:\n"); |
| 7519 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7520 |
break; |
| 7521 |
|
| 7522 |
case NODE_REDO: |
| 7523 |
printf("NODE_REDO\n"); |
| 7524 |
break; |
| 7525 |
|
| 7526 |
case NODE_RETRY: |
| 7527 |
printf("NODE_RETRY\n"); |
| 7528 |
break; |
| 7529 |
|
| 7530 |
case NODE_LVAR: |
| 7531 |
printf("NODE_LVAR %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7532 |
break; |
| 7533 |
|
| 7534 |
case NODE_GVAR: |
| 7535 |
printf("NODE_GVAR %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7536 |
break; |
| 7537 |
|
| 7538 |
case NODE_IVAR: |
| 7539 |
printf("NODE_IVAR %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7540 |
break; |
| 7541 |
|
| 7542 |
case NODE_CVAR: |
| 7543 |
printf("NODE_CVAR %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7544 |
break; |
| 7545 |
|
| 7546 |
case NODE_NVAR: |
| 7547 |
printf("NODE_NVAR %d\n", intn(tree)); |
| 7548 |
break; |
| 7549 |
|
| 7550 |
case NODE_CONST: |
| 7551 |
printf("NODE_CONST %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7552 |
break; |
| 7553 |
|
| 7554 |
case NODE_MATCH: |
| 7555 |
printf("NODE_MATCH:\n"); |
| 7556 |
dump_prefix(tree, offset + 1); |
| 7557 |
printf("lhs:\n"); |
| 7558 |
mrb_parser_dump(mrb, tree->car, offset + 2); |
| 7559 |
dump_prefix(tree, offset + 1); |
| 7560 |
printf("rhs:\n"); |
| 7561 |
mrb_parser_dump(mrb, tree->cdr, offset + 2); |
| 7562 |
break; |
| 7563 |
|
| 7564 |
case NODE_BACK_REF: |
| 7565 |
printf("NODE_BACK_REF: $%c\n", intn(tree)); |
| 7566 |
break; |
| 7567 |
|
| 7568 |
case NODE_NTH_REF: |
| 7569 |
printf("NODE_NTH_REF: $%d\n", intn(tree)); |
| 7570 |
break; |
| 7571 |
|
| 7572 |
case NODE_ARG: |
| 7573 |
printf("NODE_ARG %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7574 |
break; |
| 7575 |
|
| 7576 |
case NODE_BLOCK_ARG: |
| 7577 |
printf("NODE_BLOCK_ARG:\n"); |
| 7578 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7579 |
break; |
| 7580 |
|
| 7581 |
case NODE_INT: |
| 7582 |
printf("NODE_INT %s base %d\n", (char*)tree->car, intn(tree->cdr->car)); |
| 7583 |
break; |
| 7584 |
|
| 7585 |
case NODE_FLOAT: |
| 7586 |
printf("NODE_FLOAT %s\n", (char*)tree); |
| 7587 |
break; |
| 7588 |
|
| 7589 |
case NODE_NEGATE: |
| 7590 |
printf("NODE_NEGATE:\n"); |
| 7591 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7592 |
break; |
| 7593 |
|
| 7594 |
case NODE_STR: |
| 7595 |
printf("NODE_STR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); |
| 7596 |
break; |
| 7597 |
|
| 7598 |
case NODE_DSTR: |
| 7599 |
printf("NODE_DSTR:\n"); |
| 7600 |
dump_recur(mrb, tree, offset+1); |
| 7601 |
break; |
| 7602 |
|
| 7603 |
case NODE_XSTR: |
| 7604 |
printf("NODE_XSTR %s len %d\n", str_dump(mrb, (char*)tree->car, intn(tree->cdr)), intn(tree->cdr)); |
| 7605 |
break; |
| 7606 |
|
| 7607 |
case NODE_DXSTR: |
| 7608 |
printf("NODE_DXSTR:\n"); |
| 7609 |
dump_recur(mrb, tree, offset+1); |
| 7610 |
break; |
| 7611 |
|
| 7612 |
case NODE_REGX: |
| 7613 |
printf("NODE_REGX /%s/\n", (char*)tree->car); |
| 7614 |
if (tree->cdr->car) { |
| 7615 |
dump_prefix(tree, offset+1); |
| 7616 |
printf("opt: %s\n", (char*)tree->cdr->car); |
| 7617 |
} |
| 7618 |
if (tree->cdr->cdr) { |
| 7619 |
dump_prefix(tree, offset+1); |
| 7620 |
printf("enc: %s\n", (char*)tree->cdr->cdr); |
| 7621 |
} |
| 7622 |
break; |
| 7623 |
|
| 7624 |
case NODE_DREGX: |
| 7625 |
printf("NODE_DREGX:\n"); |
| 7626 |
dump_recur(mrb, tree->car, offset+1); |
| 7627 |
dump_prefix(tree, offset+1); |
| 7628 |
printf("tail: %s\n", (char*)tree->cdr->cdr->car); |
| 7629 |
if (tree->cdr->cdr->cdr->car) { |
| 7630 |
dump_prefix(tree, offset+1); |
| 7631 |
printf("opt: %s\n", (char*)tree->cdr->cdr->cdr->car); |
| 7632 |
} |
| 7633 |
if (tree->cdr->cdr->cdr->cdr) { |
| 7634 |
dump_prefix(tree, offset+1); |
| 7635 |
printf("enc: %s\n", (char*)tree->cdr->cdr->cdr->cdr); |
| 7636 |
} |
| 7637 |
break; |
| 7638 |
|
| 7639 |
case NODE_SYM: |
| 7640 |
printf("NODE_SYM :%s (%d)\n", mrb_sym_dump(mrb, sym(tree)), |
| 7641 |
intn(tree)); |
| 7642 |
break; |
| 7643 |
|
| 7644 |
case NODE_DSYM: |
| 7645 |
printf("NODE_DSYM:\n"); |
| 7646 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7647 |
break; |
| 7648 |
|
| 7649 |
case NODE_WORDS: |
| 7650 |
printf("NODE_WORDS:\n"); |
| 7651 |
dump_recur(mrb, tree, offset+1); |
| 7652 |
break; |
| 7653 |
|
| 7654 |
case NODE_SYMBOLS: |
| 7655 |
printf("NODE_SYMBOLS:\n"); |
| 7656 |
dump_recur(mrb, tree, offset+1); |
| 7657 |
break; |
| 7658 |
|
| 7659 |
case NODE_LITERAL_DELIM: |
| 7660 |
printf("NODE_LITERAL_DELIM\n"); |
| 7661 |
break; |
| 7662 |
|
| 7663 |
case NODE_SELF: |
| 7664 |
printf("NODE_SELF\n"); |
| 7665 |
break; |
| 7666 |
|
| 7667 |
case NODE_NIL: |
| 7668 |
printf("NODE_NIL\n"); |
| 7669 |
break; |
| 7670 |
|
| 7671 |
case NODE_TRUE: |
| 7672 |
printf("NODE_TRUE\n"); |
| 7673 |
break; |
| 7674 |
|
| 7675 |
case NODE_FALSE: |
| 7676 |
printf("NODE_FALSE\n"); |
| 7677 |
break; |
| 7678 |
|
| 7679 |
case NODE_ALIAS: |
| 7680 |
printf("NODE_ALIAS %s %s:\n", |
| 7681 |
mrb_sym_dump(mrb, sym(tree->car)), |
| 7682 |
mrb_sym_dump(mrb, sym(tree->cdr))); |
| 7683 |
break; |
| 7684 |
|
| 7685 |
case NODE_UNDEF: |
| 7686 |
printf("NODE_UNDEF"); |
| 7687 |
{ |
| 7688 |
node *t = tree; |
| 7689 |
while (t) { |
| 7690 |
printf(" %s", mrb_sym_dump(mrb, sym(t->car))); |
| 7691 |
t = t->cdr; |
| 7692 |
} |
| 7693 |
} |
| 7694 |
printf(":\n"); |
| 7695 |
break; |
| 7696 |
|
| 7697 |
case NODE_CLASS: |
| 7698 |
printf("NODE_CLASS:\n"); |
| 7699 |
if (tree->car->car == nint(0)) { |
| 7700 |
dump_prefix(tree, offset+1); |
| 7701 |
printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7702 |
} |
| 7703 |
else if (tree->car->car == nint(1)) { |
| 7704 |
dump_prefix(tree, offset+1); |
| 7705 |
printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7706 |
} |
| 7707 |
else { |
| 7708 |
mrb_parser_dump(mrb, tree->car->car, offset+1); |
| 7709 |
dump_prefix(tree, offset+1); |
| 7710 |
printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7711 |
} |
| 7712 |
if (tree->cdr->car) { |
| 7713 |
dump_prefix(tree, offset+1); |
| 7714 |
printf("super:\n"); |
| 7715 |
mrb_parser_dump(mrb, tree->cdr->car, offset+2); |
| 7716 |
} |
| 7717 |
dump_prefix(tree, offset+1); |
| 7718 |
printf("body:\n"); |
| 7719 |
mrb_parser_dump(mrb, tree->cdr->cdr->car->cdr, offset+2); |
| 7720 |
break; |
| 7721 |
|
| 7722 |
case NODE_MODULE: |
| 7723 |
printf("NODE_MODULE:\n"); |
| 7724 |
if (tree->car->car == nint(0)) { |
| 7725 |
dump_prefix(tree, offset+1); |
| 7726 |
printf(":%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7727 |
} |
| 7728 |
else if (tree->car->car == nint(1)) { |
| 7729 |
dump_prefix(tree, offset+1); |
| 7730 |
printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7731 |
} |
| 7732 |
else { |
| 7733 |
mrb_parser_dump(mrb, tree->car->car, offset+1); |
| 7734 |
dump_prefix(tree, offset+1); |
| 7735 |
printf("::%s\n", mrb_sym_name(mrb, sym(tree->car->cdr))); |
| 7736 |
} |
| 7737 |
dump_prefix(tree, offset+1); |
| 7738 |
printf("body:\n"); |
| 7739 |
mrb_parser_dump(mrb, tree->cdr->car->cdr, offset+2); |
| 7740 |
break; |
| 7741 |
|
| 7742 |
case NODE_SCLASS: |
| 7743 |
printf("NODE_SCLASS:\n"); |
| 7744 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7745 |
dump_prefix(tree, offset+1); |
| 7746 |
printf("body:\n"); |
| 7747 |
mrb_parser_dump(mrb, tree->cdr->car->cdr, offset+2); |
| 7748 |
break; |
| 7749 |
|
| 7750 |
case NODE_DEF: |
| 7751 |
printf("NODE_DEF:\n"); |
| 7752 |
dump_prefix(tree, offset+1); |
| 7753 |
printf("%s\n", mrb_sym_dump(mrb, sym(tree->car))); |
| 7754 |
tree = tree->cdr; |
| 7755 |
{ |
| 7756 |
node *n2 = tree->car; |
| 7757 |
mrb_bool first_lval = TRUE; |
| 7758 |
|
| 7759 |
if (n2 && (n2->car || n2->cdr)) { |
| 7760 |
dump_prefix(n2, offset+1); |
| 7761 |
printf("local variables:\n"); |
| 7762 |
dump_prefix(n2, offset+2); |
| 7763 |
while (n2) { |
| 7764 |
if (n2->car) { |
| 7765 |
if (!first_lval) printf(", "); |
| 7766 |
printf("%s", mrb_sym_name(mrb, sym(n2->car))); |
| 7767 |
first_lval = FALSE; |
| 7768 |
} |
| 7769 |
n2 = n2->cdr; |
| 7770 |
} |
| 7771 |
printf("\n"); |
| 7772 |
} |
| 7773 |
} |
| 7774 |
tree = tree->cdr; |
| 7775 |
if (tree->car) { |
| 7776 |
dump_args(mrb, tree->car, offset); |
| 7777 |
} |
| 7778 |
mrb_parser_dump(mrb, tree->cdr->car, offset+1); |
| 7779 |
break; |
| 7780 |
|
| 7781 |
case NODE_SDEF: |
| 7782 |
printf("NODE_SDEF:\n"); |
| 7783 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7784 |
tree = tree->cdr; |
| 7785 |
dump_prefix(tree, offset+1); |
| 7786 |
printf(":%s\n", mrb_sym_dump(mrb, sym(tree->car))); |
| 7787 |
tree = tree->cdr->cdr; |
| 7788 |
if (tree->car) { |
| 7789 |
dump_args(mrb, tree->car, offset+1); |
| 7790 |
} |
| 7791 |
tree = tree->cdr; |
| 7792 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7793 |
break; |
| 7794 |
|
| 7795 |
case NODE_POSTEXE: |
| 7796 |
printf("NODE_POSTEXE:\n"); |
| 7797 |
mrb_parser_dump(mrb, tree, offset+1); |
| 7798 |
break; |
| 7799 |
|
| 7800 |
case NODE_HEREDOC: |
| 7801 |
printf("NODE_HEREDOC (<<%s):\n", ((parser_heredoc_info*)tree)->term); |
| 7802 |
dump_recur(mrb, ((parser_heredoc_info*)tree)->doc, offset+1); |
| 7803 |
break; |
| 7804 |
|
| 7805 |
case NODE_ARGS_TAIL: |
| 7806 |
printf("NODE_ARGS_TAIL:\n"); |
| 7807 |
{ |
| 7808 |
node *kws = tree->car; |
| 7809 |
|
| 7810 |
while (kws) { |
| 7811 |
mrb_parser_dump(mrb, kws->car, offset+1); |
| 7812 |
kws = kws->cdr; |
| 7813 |
} |
| 7814 |
} |
| 7815 |
tree = tree->cdr; |
| 7816 |
if (tree->car) { |
| 7817 |
mrb_assert(intn(tree->car->car) == NODE_KW_REST_ARGS); |
| 7818 |
mrb_parser_dump(mrb, tree->car, offset+1); |
| 7819 |
} |
| 7820 |
tree = tree->cdr; |
| 7821 |
if (tree->car) { |
| 7822 |
dump_prefix(tree, offset+1); |
| 7823 |
printf("block='%s'\n", mrb_sym_name(mrb, sym(tree->car))); |
| 7824 |
} |
| 7825 |
break; |
| 7826 |
|
| 7827 |
case NODE_KW_ARG: |
| 7828 |
printf("NODE_KW_ARG %s:\n", mrb_sym_name(mrb, sym(tree->car))); |
| 7829 |
mrb_parser_dump(mrb, tree->cdr->car, offset + 1); |
| 7830 |
break; |
| 7831 |
|
| 7832 |
case NODE_KW_REST_ARGS: |
| 7833 |
if (tree) |
| 7834 |
printf("NODE_KW_REST_ARGS %s\n", mrb_sym_name(mrb, sym(tree))); |
| 7835 |
else |
| 7836 |
printf("NODE_KW_REST_ARGS\n"); |
| 7837 |
break; |
| 7838 |
|
| 7839 |
default: |
| 7840 |
printf("node type: %d (0x%x)\n", nodetype, (unsigned)nodetype); |
| 7841 |
break; |
| 7842 |
} |
| 7843 |
#endif |
| 7844 |
} |
| 7845 |
|
| 7846 |
typedef mrb_bool mrb_parser_foreach_top_variable_func(mrb_state *mrb, mrb_sym sym, void *user); |
| 7847 |
void mrb_parser_foreach_top_variable(mrb_state *mrb, struct mrb_parser_state *p, mrb_parser_foreach_top_variable_func *func, void *user); |
| 7848 |
|
| 7849 |
void |
| 7850 |
mrb_parser_foreach_top_variable(mrb_state *mrb, struct mrb_parser_state *p, mrb_parser_foreach_top_variable_func *func, void *user) |
| 7851 |
{ |
| 7852 |
const mrb_ast_node *n = p->tree; |
| 7853 |
if ((intptr_t)n->car == NODE_SCOPE) { |
| 7854 |
n = n->cdr->car; |
| 7855 |
for (; n; n = n->cdr) { |
| 7856 |
mrb_sym sym = sym(n->car); |
| 7857 |
if (sym && !func(mrb, sym, user)) break; |
| 7858 |
} |
| 7859 |
} |
| 7860 |
} |