diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index a54302fb91..25cd46e1be 100644 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -51,6 +51,10 @@ def process(f, out, path, template) usercode f, out, path, template end +require_relative 'dsl' + +RIPPER_CODE_PATTERN = %r + def prelude(f, out) @exprs = {} while line = f.gets @@ -58,6 +62,9 @@ def prelude(f, out) when /\A%%/ out << "%%\n" return + when RIPPER_CODE_PATTERN # %rule actions may contain /*% ripper: ... %*/ DSL + out << DSL.new($2, ($1 || "").split(",")).generate << "\n" + next else if (/^enum lex_state_(?:bits|e) \{/ =~ line)..(/^\}/ =~ line) case line @@ -74,12 +81,10 @@ def prelude(f, out) end end -require_relative "dsl" - def grammar(f, out) while line = f.gets case line - when %r + when RIPPER_CODE_PATTERN out << DSL.new($2, ($1 || "").split(",")).generate << "\n" when %r out << "#if 0\n" diff --git a/parse.y b/parse.y index e8acda2d19..cf217a313f 100644 --- a/parse.y +++ b/parse.y @@ -2951,6 +2951,16 @@ rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary) %token tLAST_TOKEN +/* + * parameterizing rules + */ +%rule words(begin, word_list): begin ' '+ word_list tSTRING_END + { + $$ = make_list($3, &@$); + /*% ripper: array!($:3) %*/ + } + ; + %% program : { SET_LEX_STATE(EXPR_BEG); @@ -6104,11 +6114,7 @@ regexp : tREGEXP_BEG regexp_contents tREGEXP_END } ; -words : tWORDS_BEG ' '+ word_list tSTRING_END - { - $$ = make_list($3, &@$); - /*% ripper: array!($:3) %*/ - } +words : words(tWORDS_BEG, word_list) ; word_list : /* none */ @@ -6132,11 +6138,7 @@ word : string_content } ; -symbols : tSYMBOLS_BEG ' '+ symbol_list tSTRING_END - { - $$ = make_list($3, &@$); - /*% ripper: array!($:3) %*/ - } +symbols : words(tSYMBOLS_BEG, symbol_list) ; symbol_list : /* none */ @@ -6151,18 +6153,10 @@ symbol_list : /* none */ } ; -qwords : tQWORDS_BEG ' '+ qword_list tSTRING_END - { - $$ = make_list($3, &@$); - /*% ripper: array!($:3) %*/ - } +qwords : words(tQWORDS_BEG, qword_list) ; -qsymbols : tQSYMBOLS_BEG ' '+ qsym_list tSTRING_END - { - $$ = make_list($3, &@$); - /*% ripper: array!($:3) %*/ - } +qsymbols : words(tQSYMBOLS_BEG, qsym_list) ; qword_list : /* none */