diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-03-02 16:08:39 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-03-02 16:34:33 +0900 |
commit | 20a2ab0825b7e441e303002f0feeb1b643198ffc (patch) | |
tree | 4386406461c038b49840b7f77fb1a2051e907b19 /parse.y | |
parent | 761528e8aa7c54ec92c90335fe26a584b992918b (diff) |
Packed stacked bit flags into one struct
Diffstat (limited to 'parse.y')
-rw-r--r-- | parse.y | 96 |
1 files changed, 51 insertions, 45 deletions
@@ -26,6 +26,13 @@ #include <errno.h> #include <stdio.h> +struct lex_flags { + unsigned int defined: 1; + unsigned int kwarg: 1; + unsigned int def: 1; + unsigned int class: 1; +}; + #include "internal.h" #include "internal/compile.h" #include "internal/complex.h" @@ -291,15 +298,13 @@ struct parser_params { int max_numparam; + struct lex_flags in; + unsigned int command_start:1; unsigned int eofp: 1; unsigned int ruby__end__seen: 1; unsigned int debug: 1; unsigned int has_shebang: 1; - unsigned int in_defined: 1; - unsigned int in_kwarg: 1; - unsigned int in_def: 1; - unsigned int in_class: 1; unsigned int token_seen: 1; unsigned int token_info_enabled: 1; # if WARN_PAST_SCOPE @@ -1001,6 +1006,7 @@ static int looking_at_eol_p(struct parser_params *p); st_table *tbl; const struct vtable *vars; struct rb_strterm_struct *strterm; + struct lex_flags flags; } %token <id> @@ -1428,7 +1434,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem } | keyword_END '{' compstmt '}' { - if (p->in_def) { + if (p->in.def) { rb_warn0("END in method; use at_exit"); } /*%%%*/ @@ -1576,14 +1582,14 @@ expr : command_call value_expr($1); SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); p->command_start = FALSE; - $<num>$ = p->in_kwarg; - p->in_kwarg = 1; + $<flags>$ = p->in; + p->in.kwarg = 1; } {$<tbl>$ = push_pvtbl(p);} p_expr {pop_pvtbl(p, $<tbl>4);} { - p->in_kwarg = !!$<num>3; + p->in.kwarg = $<flags>3.kwarg; /*%%%*/ $$ = new_case3(p, $1, NEW_IN($5, 0, 0, &@5), &@$); /*% %*/ @@ -2351,9 +2357,9 @@ arg : lhs '=' arg_rhs { $$ = logop(p, idOROP, $1, $3, &@2, &@$); } - | keyword_defined opt_nl {p->in_defined = 1;} arg + | keyword_defined opt_nl {p->in.defined = 1;} arg { - p->in_defined = 0; + p->in.defined = 0; $$ = new_defined(p, $4, &@$); } | arg '?' arg opt_nl ':' arg @@ -2754,9 +2760,9 @@ primary : literal /*% %*/ /*% ripper: yield0! %*/ } - | keyword_defined opt_nl '(' {p->in_defined = 1;} expr rparen + | keyword_defined opt_nl '(' {p->in.defined = 1;} expr rparen { - p->in_defined = 0; + p->in.defined = 0; $$ = new_defined(p, $5, &@$); } | keyword_not '(' expr rparen @@ -2925,12 +2931,12 @@ primary : literal } | k_class cpath superclass { - if (p->in_def) { + if (p->in.def) { YYLTYPE loc = code_loc_gen(&@1, &@2); yyerror1(&loc, "class definition in method body"); } - $<num>1 = p->in_class; - p->in_class = 1; + $<flags>1 = p->in; + p->in.class = 1; local_push(p, 0); } bodystmt @@ -2944,13 +2950,13 @@ primary : literal /*% %*/ /*% ripper: class!($2, $3, $5) %*/ local_pop(p); - p->in_class = $<num>1 & 1; + p->in.class = $<flags>1.class; } | k_class tLSHFT expr { - $<num>$ = (p->in_class << 1) | p->in_def; - p->in_def = 0; - p->in_class = 0; + $<flags>$ = p->in; + p->in.def = 0; + p->in.class = 0; local_push(p, 0); } term @@ -2965,17 +2971,17 @@ primary : literal /*% %*/ /*% ripper: sclass!($3, $6) %*/ local_pop(p); - p->in_def = $<num>4 & 1; - p->in_class = ($<num>4 >> 1) & 1; + p->in.def = $<flags>4.def; + p->in.class = $<flags>4.class; } | k_module cpath { - if (p->in_def) { + if (p->in.def) { YYLTYPE loc = code_loc_gen(&@1, &@2); yyerror1(&loc, "module definition in method body"); } - $<num>1 = p->in_class; - p->in_class = 1; + $<flags>1 = p->in; + p->in.class = 1; local_push(p, 0); } bodystmt @@ -2989,7 +2995,7 @@ primary : literal /*% %*/ /*% ripper: module!($2, $4) %*/ local_pop(p); - p->in_class = $<num>1 & 1; + p->in.class = $<flags>1.class; } | k_def fname { @@ -2999,8 +3005,8 @@ primary : literal p->cur_arg = 0; } { - $<num>$ = p->in_def; - p->in_def = 1; + $<flags>$ = p->in; + p->in.def = 1; } f_arglist bodystmt @@ -3015,14 +3021,14 @@ primary : literal /*% %*/ /*% ripper: def!($2, $5, $6) %*/ local_pop(p); - p->in_def = $<num>4 & 1; + p->in.def = $<flags>4.def; p->cur_arg = $<id>3; } | k_def singleton dot_or_colon {SET_LEX_STATE(EXPR_FNAME);} fname { numparam_name(p, get_id($5)); - $<num>4 = p->in_def; - p->in_def = 1; + $<flags>1 = p->in; + p->in.def = 1; SET_LEX_STATE(EXPR_ENDFN|EXPR_LABEL); /* force for args */ local_push(p, 0); $<id>$ = p->cur_arg; @@ -3041,7 +3047,7 @@ primary : literal /*% %*/ /*% ripper: defs!($2, $3, $5, $7, $8) %*/ local_pop(p); - p->in_def = $<num>4 & 1; + p->in.def = $<flags>1.def; p->cur_arg = $<id>6; } | keyword_break @@ -3212,7 +3218,7 @@ k_end : keyword_end k_return : keyword_return { - if (p->in_class && !p->in_def && !dyna_in_block(p)) + if (p->in.class && !p->in.def && !dyna_in_block(p)) yyerror1(&@1, "Invalid return in class/module body"); } ; @@ -3802,8 +3808,8 @@ p_case_body : keyword_in { SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); p->command_start = FALSE; - $<num>$ = p->in_kwarg; - p->in_kwarg = 1; + $<flags>1 = p->in; + p->in.kwarg = 1; } {$<tbl>$ = push_pvtbl(p);} {$<tbl>$ = push_pktbl(p);} @@ -3811,7 +3817,7 @@ p_case_body : keyword_in {pop_pktbl(p, $<tbl>4);} {pop_pvtbl(p, $<tbl>3);} { - p->in_kwarg = !!$<num>2; + p->in.kwarg = $<flags>1.kwarg; } compstmt p_cases @@ -3958,7 +3964,7 @@ p_expr_basic : p_value | tLBRACE { $<tbl>$ = push_pktbl(p); - p->in_kwarg = 0; + p->in.kwarg = 0; } p_kwargs rbrace { @@ -4856,13 +4862,13 @@ f_arglist : '(' f_args rparen p->command_start = TRUE; } | { - $<num>$ = p->in_kwarg; - p->in_kwarg = 1; + $<flags>$ = p->in; + p->in.kwarg = 1; SET_LEX_STATE(p->lex.state|EXPR_LABEL); /* force for args */ } - f_args term + f_args term { - p->in_kwarg = !!$<num>1; + p->in.kwarg = $<flags>1.kwarg; $$ = $2; SET_LEX_STATE(EXPR_BEG); p->command_start = TRUE; @@ -8881,7 +8887,7 @@ parser_yylex(struct parser_params *p) dispatch_scan_event(p, tIGNORED_NL); } fallthru = FALSE; - if (!c && p->in_kwarg) { + if (!c && p->in.kwarg) { goto normal_newline; } goto retry; @@ -10005,7 +10011,7 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) return node; } # if WARN_PAST_SCOPE - if (!p->in_defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) { + if (!p->in.defined && RTEST(ruby_verbose) && past_dvar_p(p, id)) { rb_warning1("possible reference to past scope - %"PRIsWARN, rb_id2str(id)); } # endif @@ -10479,7 +10485,7 @@ assignable0(struct parser_params *p, ID id, const char **err) case ID_GLOBAL: return NODE_GASGN; case ID_INSTANCE: return NODE_IASGN; case ID_CONST: - if (!p->in_def) return NODE_CDECL; + if (!p->in.def) return NODE_CDECL; *err = "dynamic constant assignment"; return -1; case ID_CLASS: return NODE_CVASGN; @@ -11691,7 +11697,7 @@ new_const_op_assign(struct parser_params *p, NODE *lhs, ID op, NODE *rhs, const static NODE * const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc) { - if (p->in_def) { + if (p->in.def) { yyerror1(loc, "dynamic constant assignment"); } return NEW_CDECL(0, 0, (path), loc); @@ -11700,7 +11706,7 @@ const_decl(struct parser_params *p, NODE *path, const YYLTYPE *loc) static VALUE const_decl(struct parser_params *p, VALUE path) { - if (p->in_def) { + if (p->in.def) { path = dispatch1(assign_error, path); ripper_error(p); } |