modules/mruby/src/parse.y in webruby-0.2.2 vs modules/mruby/src/parse.y in webruby-0.2.4

- old
+ new

@@ -35,10 +35,11 @@ static int yylex(void *lval, parser_state *p); static void yyerror(parser_state *p, const char *s); static void yywarn(parser_state *p, const char *s); static void yywarning(parser_state *p, const char *s); static void backref_error(parser_state *p, node *n); +static void tokadd(parser_state *p, int c); #ifndef isascii #define isascii(c) (((c) & ~0x7f) == 0) #endif @@ -63,27 +64,27 @@ #define sym(x) ((mrb_sym)(intptr_t)(x)) #define nsym(x) ((node*)(intptr_t)(x)) static inline mrb_sym -intern_gen(parser_state *p, const char *s) +intern_cstr_gen(parser_state *p, const char *s) { return mrb_intern_cstr(p->mrb, s); } -#define intern(s) intern_gen(p,(s)) +#define intern_cstr(s) intern_cstr_gen(p,(s)) static inline mrb_sym -intern_gen2(parser_state *p, const char *s, size_t len) +intern_gen(parser_state *p, const char *s, size_t len) { - return mrb_intern2(p->mrb, s, len); + return mrb_intern(p->mrb, s, len); } -#define intern2(s,len) intern_gen2(p,(s),(len)) +#define intern(s,len) intern_gen(p,(s),(len)) static inline mrb_sym intern_gen_c(parser_state *p, const char c) { - return mrb_intern2(p->mrb, &c, 1); + return mrb_intern(p->mrb, &c, 1); } #define intern_c(c) intern_gen_c(p,(c)) static void cons_free_gen(parser_state *p, node *cons) @@ -535,11 +536,11 @@ new_strsym(parser_state *p, node* str) { const char *s = (const char*)str->cdr->car; size_t len = (size_t)str->cdr->cdr; - return mrb_intern2(p->mrb, s, len); + return mrb_intern(p->mrb, s, len); } // (:lvar . a) static node* new_lvar(parser_state *p, mrb_sym sym) @@ -797,18 +798,18 @@ // (:call a op) static node* call_uni_op(parser_state *p, node *recv, char *m) { - return new_call(p, recv, intern(m), 0); + return new_call(p, recv, intern_cstr(m), 0); } // (:call a op b) static node* call_bin_op(parser_state *p, node *recv, char *m, node *arg1) { - return new_call(p, recv, intern(m), list1(list1(arg1))); + return new_call(p, recv, intern_cstr(m), list1(list1(arg1))); } static void args_with_block(parser_state *p, node *a, node *b) { @@ -1281,11 +1282,11 @@ { $$ = new_op_asgn(p, $1, $2, $3); } | primary_value '[' opt_call_args rbracket tOP_ASGN command_call { - $$ = new_op_asgn(p, new_call(p, $1, intern2("[]",2), $3), $5, $6); + $$ = new_op_asgn(p, new_call(p, $1, intern("[]",2), $3), $5, $6); } | primary_value '.' tIDENTIFIER tOP_ASGN command_call { $$ = new_op_asgn(p, new_call(p, $1, $3, 0), $4, $5); } @@ -1520,11 +1521,11 @@ { assignable(p, $1); } | primary_value '[' opt_call_args rbracket { - $$ = new_call(p, $1, intern2("[]",2), $3); + $$ = new_call(p, $1, intern("[]",2), $3); } | primary_value '.' tIDENTIFIER { $$ = new_call(p, $1, $3, 0); } @@ -1559,11 +1560,11 @@ { assignable(p, $1); } | primary_value '[' opt_call_args rbracket { - $$ = new_call(p, $1, intern2("[]",2), $3); + $$ = new_call(p, $1, intern("[]",2), $3); } | primary_value '.' tIDENTIFIER { $$ = new_call(p, $1, $3, 0); } @@ -1645,35 +1646,35 @@ ; op : '|' { $$ = intern_c('|'); } | '^' { $$ = intern_c('^'); } | '&' { $$ = intern_c('&'); } - | tCMP { $$ = intern2("<=>",3); } - | tEQ { $$ = intern2("==",2); } - | tEQQ { $$ = intern2("===",3); } - | tMATCH { $$ = intern2("=~",2); } - | tNMATCH { $$ = intern2("!~",2); } + | tCMP { $$ = intern("<=>",3); } + | tEQ { $$ = intern("==",2); } + | tEQQ { $$ = intern("===",3); } + | tMATCH { $$ = intern("=~",2); } + | tNMATCH { $$ = intern("!~",2); } | '>' { $$ = intern_c('>'); } - | tGEQ { $$ = intern2(">=",2); } + | tGEQ { $$ = intern(">=",2); } | '<' { $$ = intern_c('<'); } - | tLEQ { $$ = intern2("<=",2); } - | tNEQ { $$ = intern2("!=",2); } - | tLSHFT { $$ = intern2("<<",2); } - | tRSHFT { $$ = intern2(">>",2); } + | tLEQ { $$ = intern("<=",2); } + | tNEQ { $$ = intern("!=",2); } + | tLSHFT { $$ = intern("<<",2); } + | tRSHFT { $$ = intern(">>",2); } | '+' { $$ = intern_c('+'); } | '-' { $$ = intern_c('-'); } | '*' { $$ = intern_c('*'); } | tSTAR { $$ = intern_c('*'); } | '/' { $$ = intern_c('/'); } | '%' { $$ = intern_c('%'); } - | tPOW { $$ = intern2("**",2); } + | tPOW { $$ = intern("**",2); } | '!' { $$ = intern_c('!'); } | '~' { $$ = intern_c('~'); } - | tUPLUS { $$ = intern2("+@",2); } - | tUMINUS { $$ = intern2("-@",2); } - | tAREF { $$ = intern2("[]",2); } - | tASET { $$ = intern2("[]=",3); } + | tUPLUS { $$ = intern("+@",2); } + | tUMINUS { $$ = intern("-@",2); } + | tAREF { $$ = intern("[]",2); } + | tASET { $$ = intern("[]=",3); } | '`' { $$ = intern_c('`'); } ; reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__ | keyword_BEGIN | keyword_END @@ -1705,11 +1706,11 @@ { $$ = new_op_asgn(p, $1, $2, new_rescue(p, $3, list1(list3(0, 0, $5)), 0)); } | primary_value '[' opt_call_args rbracket tOP_ASGN arg { - $$ = new_op_asgn(p, new_call(p, $1, intern2("[]",2), $3), $5, $6); + $$ = new_op_asgn(p, new_call(p, $1, intern("[]",2), $3), $5, $6); } | primary_value '.' tIDENTIFIER tOP_ASGN arg { $$ = new_op_asgn(p, new_call(p, $1, $3, 0), $4, $5); } @@ -2517,15 +2518,15 @@ { $$ = new_call(p, $1, $3, 0); } | primary_value '.' paren_args { - $$ = new_call(p, $1, intern2("call",4), $3); + $$ = new_call(p, $1, intern("call",4), $3); } | primary_value tCOLON2 paren_args { - $$ = new_call(p, $1, intern2("call",4), $3); + $$ = new_call(p, $1, intern("call",4), $3); } | keyword_super paren_args { $$ = new_super(p, $2); } @@ -2533,11 +2534,11 @@ { $$ = new_zsuper(p); } | primary_value '[' opt_call_args rbracket { - $$ = new_call(p, $1, intern2("[]",2), $3); + $$ = new_call(p, $1, intern("[]",2), $3); } ; brace_block : '{' { @@ -3313,34 +3314,34 @@ cons_free(tmp); } else { #ifdef ENABLE_STDIO if (p->f) { - if (feof(p->f)) goto end_retry; + if (feof(p->f)) goto eof; c = fgetc(p->f); - if (c == EOF) goto end_retry; + if (c == EOF) goto eof; } else #endif if (!p->s || p->s >= p->send) { - goto end_retry; + goto eof; } else { c = (unsigned char)*p->s++; } } p->column++; return c; - end_retry: + eof: if (!p->cxt) return -1; else { mrbc_context *cxt = p->cxt; if (cxt->partial_hook(p) < 0) return -1; - p->cxt = NULL; - c = nextc(p); + c = '\n'; + p->lineno = 1; p->cxt = cxt; return c; } } @@ -3864,10 +3865,13 @@ if (c == -1) { yyerror(p, "unterminated here document identifier"); return 0; } } else { + if (c == -1) { + return 0; /* missing here document identifier */ + } if (! identchar(c)) { pushback(p, c); if (indent) pushback(p, '-'); return 0; } @@ -3927,11 +3931,14 @@ last_state = p->lstate; switch (c = nextc(p)) { case '\0': /* NUL */ case '\004': /* ^D */ case '\032': /* ^Z */ + return 0; case -1: /* end of script. */ + if (p->heredocs_from_nextline) + goto maybe_heredoc; return 0; /* white spaces */ case ' ': case '\t': case '\f': case '\r': case '\13': /* '\v' */ @@ -3940,10 +3947,11 @@ case '#': /* it's a comment */ skip(p, '\n'); /* fall through */ case '\n': + maybe_heredoc: heredoc_treat_nextline(p); switch (p->lstate) { case EXPR_BEG: case EXPR_FNAME: case EXPR_DOT: @@ -3986,11 +3994,11 @@ return '\n'; case '*': if ((c = nextc(p)) == '*') { if ((c = nextc(p)) == '=') { - yylval.id = intern2("**",2); + yylval.id = intern("**",2); p->lstate = EXPR_BEG; return tOP_ASGN; } pushback(p, c); c = tPOW; @@ -4095,11 +4103,11 @@ pushback(p, c); return tLEQ; } if (c == '<') { if ((c = nextc(p)) == '=') { - yylval.id = intern2("<<",2); + yylval.id = intern("<<",2); p->lstate = EXPR_BEG; return tOP_ASGN; } pushback(p, c); return tLSHFT; @@ -4116,11 +4124,11 @@ if ((c = nextc(p)) == '=') { return tGEQ; } if (c == '>') { if ((c = nextc(p)) == '=') { - yylval.id = intern2(">>",2); + yylval.id = intern(">>",2); p->lstate = EXPR_BEG; return tOP_ASGN; } pushback(p, c); return tRSHFT; @@ -4230,11 +4238,11 @@ case '&': if ((c = nextc(p)) == '&') { p->lstate = EXPR_BEG; if ((c = nextc(p)) == '=') { - yylval.id = intern2("&&",2); + yylval.id = intern("&&",2); p->lstate = EXPR_BEG; return tOP_ASGN; } pushback(p, c); return tANDOP; @@ -4264,11 +4272,11 @@ case '|': if ((c = nextc(p)) == '|') { p->lstate = EXPR_BEG; if ((c = nextc(p)) == '=') { - yylval.id = intern2("||",2); + yylval.id = intern("||",2); p->lstate = EXPR_BEG; return tOP_ASGN; } pushback(p, c); return tOROP; @@ -4868,21 +4876,21 @@ case '>': /* $>: default output handle */ case '\"': /* $": already loaded files */ tokadd(p, '$'); tokadd(p, c); tokfix(p); - yylval.id = intern(tok(p)); + yylval.id = intern_cstr(tok(p)); return tGVAR; case '-': tokadd(p, '$'); tokadd(p, c); c = nextc(p); pushback(p, c); gvar: tokfix(p); - yylval.id = intern(tok(p)); + yylval.id = intern_cstr(tok(p)); return tGVAR; case '&': /* $&: last match */ case '`': /* $`: string before last match */ case '\'': /* $': string after last match */ @@ -5030,11 +5038,11 @@ if (IS_LABEL_POSSIBLE()) { if (IS_LABEL_SUFFIX(0)) { p->lstate = EXPR_BEG; nextc(p); tokfix(p); - yylval.id = intern(tok(p)); + yylval.id = intern_cstr(tok(p)); return tLABEL; } } if (p->lstate != EXPR_DOT) { const struct kwtable *kw; @@ -5043,11 +5051,11 @@ kw = mrb_reserved_word(tok(p), toklen(p)); if (kw) { enum mrb_lex_state_enum state = p->lstate; p->lstate = kw->state; if (state == EXPR_FNAME) { - yylval.id = intern(kw->name); + yylval.id = intern_cstr(kw->name); return kw->id[0]; } if (p->lstate == EXPR_BEG) { p->cmd_start = TRUE; } @@ -5088,11 +5096,11 @@ else { p->lstate = EXPR_END; } } { - mrb_sym ident = intern(tok(p)); + mrb_sym ident = intern_cstr(tok(p)); yylval.id = ident; #if 0 if (last_state != EXPR_DOT && islower(tok(p)[0]) && lvar_defined(ident)) { p->lstate = EXPR_END; @@ -5152,11 +5160,11 @@ for (i=0, n=n0; n; i++,n=n->cdr) { cxt->syms[i] = sym(n->car); } } -void codedump_all(mrb_state*, int); +void codedump_all(mrb_state*, struct RProc*); void parser_dump(mrb_state *mrb, node *tree, int offset); void mrb_parser_parse(parser_state *p, mrbc_context *c) { @@ -5343,19 +5351,19 @@ static mrb_value load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) { struct RClass *target = mrb->object_class; struct RProc *proc; - int n; mrb_value v; if (!p) { return mrb_undef_value(); } if (!p->tree || p->nerr) { if (p->capture_errors) { char buf[256]; + int n; n = snprintf(buf, sizeof(buf), "line %d: %s\n", p->error_buffer[0].lineno, p->error_buffer[0].message); mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n)); mrb_parser_free(p); @@ -5366,29 +5374,28 @@ mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SYNTAX_ERROR, msg, sizeof(msg) - 1)); mrb_parser_free(p); return mrb_undef_value(); } } - n = mrb_generate_code(mrb, p); + proc = mrb_generate_code(mrb, p); mrb_parser_free(p); - if (n < 0) { + if (proc == NULL) { static const char msg[] = "codegen error"; mrb->exc = mrb_obj_ptr(mrb_exc_new(mrb, E_SCRIPT_ERROR, msg, sizeof(msg) - 1)); - return mrb_nil_value(); + return mrb_undef_value(); } if (c) { - if (c->dump_result) codedump_all(mrb, n); - if (c->no_exec) return mrb_fixnum_value(n); + if (c->dump_result) codedump_all(mrb, proc); + if (c->no_exec) return mrb_obj_value(proc); if (c->target_class) { target = c->target_class; } } - proc = mrb_proc_new(mrb, mrb->irep[n]); proc->target_class = target; if (mrb->c->ci) { mrb->c->ci->target_class = target; } - v = mrb_run(mrb, proc, mrb_top_self(mrb)); + v = mrb_context_run(mrb, proc, mrb_top_self(mrb), 0); if (mrb->exc) return mrb_nil_value(); return v; } #ifdef ENABLE_STDIO