summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-05-14 09:13:29 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-05-14 15:38:48 +0900
commit3150516aab92c63fc22cf73d588e4584a8753b76 (patch)
tree6a0bed73c6826f8d40badb7dc57467e41ed36c4a
parentedca57e6e37032cb4583f8d731eb81db94e3992d (diff)
downloadruby-3150516aab92c63fc22cf73d588e4584a8753b76.tar.gz
Preprocess input parse.y from stdin
-rw-r--r--ext/ripper/depend5
-rw-r--r--ext/ripper/tools/preproc.rb40
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