From 3150516aab92c63fc22cf73d588e4584a8753b76 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 14 May 2023 09:13:29 +0900 Subject: Preprocess input parse.y from stdin --- ext/ripper/depend | 5 ++--- ext/ripper/tools/preproc.rb | 40 ++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/ext/ripper/depend b/ext/ripper/depend index 9aba267533..a06330b645 100644 --- a/ext/ripper/depend +++ b/ext/ripper/depend @@ -21,9 +21,8 @@ static: check ripper.y: $(srcdir)/tools/preproc.rb $(srcdir)/tools/dsl.rb $(top_srcdir)/parse.y $(top_srcdir)/defs/id.def $(ECHO) extracting $@ from $(top_srcdir)/parse.y - $(Q) $(RUBY) $(top_srcdir)/tool/id2token.rb $(top_srcdir)/parse.y > ripper.tmp.y - $(Q) $(RUBY) $(srcdir)/tools/preproc.rb ripper.tmp.y --output=$@ - $(Q) $(RM) ripper.tmp.y + $(Q) $(RUBY) $(top_srcdir)/tool/id2token.rb $(top_srcdir)/parse.y | \ + $(RUBY) $(srcdir)/tools/preproc.rb --output=$@ - ripper.y check: .eventids2-check diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index cd85a5da61..f419fc3dbe 100644 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -17,28 +17,36 @@ def main begin parser.parse! rescue OptionParser::ParseError => err - $stderr.puts err.message - $stderr.puts parser.help - exit false - end - unless ARGV.size == 1 - abort "wrong number of arguments (#{ARGV.size} for 1)" + warn err.message + abort parser.help end out = "".dup - File.open(ARGV[0]) {|f| - prelude f, out - grammar f, out - usercode f, out - } - if output - File.open(output, 'w') {|f| - f.write out + if ARGV[0] == "-" + unless ARGV.size == 2 + abort "wrong number of arguments (#{ARGV.size} for 2)" + end + process STDIN, out, ARGV[1] + else + unless ARGV.size == 1 + abort "wrong number of arguments (#{ARGV.size} for 1)" + end + File.open(ARGV[0]) {|f| + process f, out, ARGV[0] } + end + if output + File.write(output, out) else print out end end +def process(f, out, path) + prelude f, out + grammar f, out + usercode f, out, path +end + def prelude(f, out) @exprs = {} lex_state_def = false @@ -95,13 +103,13 @@ def grammar(f, out) end end -def usercode(f, out) +def usercode(f, out, path) require 'erb' compiler = ERB::Compiler.new('%-') compiler.put_cmd = compiler.insert_cmd = "out.<<" lineno = f.lineno src, = compiler.compile(f.read) - eval(src, binding, f.path, lineno) + eval(src, binding, path, lineno) end main -- cgit v1.2.1