Preprocess input parse.y from stdin

This commit is contained in:
Nobuyoshi Nakada 2023-05-14 09:13:29 +09:00
parent edca57e6e3
commit 3150516aab
Notes: git 2023-05-14 06:39:08 +00:00
2 changed files with 26 additions and 19 deletions

View File

@ -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 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 $(ECHO) extracting $@ from $(top_srcdir)/parse.y
$(Q) $(RUBY) $(top_srcdir)/tool/id2token.rb $(top_srcdir)/parse.y > ripper.tmp.y $(Q) $(RUBY) $(top_srcdir)/tool/id2token.rb $(top_srcdir)/parse.y | \
$(Q) $(RUBY) $(srcdir)/tools/preproc.rb ripper.tmp.y --output=$@ $(RUBY) $(srcdir)/tools/preproc.rb --output=$@ - ripper.y
$(Q) $(RM) ripper.tmp.y
check: .eventids2-check check: .eventids2-check

View File

@ -17,28 +17,36 @@ def main
begin begin
parser.parse! parser.parse!
rescue OptionParser::ParseError => err rescue OptionParser::ParseError => err
$stderr.puts err.message warn err.message
$stderr.puts parser.help abort parser.help
exit false
end
unless ARGV.size == 1
abort "wrong number of arguments (#{ARGV.size} for 1)"
end end
out = "".dup out = "".dup
File.open(ARGV[0]) {|f| if ARGV[0] == "-"
prelude f, out unless ARGV.size == 2
grammar f, out abort "wrong number of arguments (#{ARGV.size} for 2)"
usercode f, out end
} process STDIN, out, ARGV[1]
if output else
File.open(output, 'w') {|f| unless ARGV.size == 1
f.write out 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 else
print out print out
end end
end end
def process(f, out, path)
prelude f, out
grammar f, out
usercode f, out, path
end
def prelude(f, out) def prelude(f, out)
@exprs = {} @exprs = {}
lex_state_def = false lex_state_def = false
@ -95,13 +103,13 @@ def grammar(f, out)
end end
end end
def usercode(f, out) def usercode(f, out, path)
require 'erb' require 'erb'
compiler = ERB::Compiler.new('%-') compiler = ERB::Compiler.new('%-')
compiler.put_cmd = compiler.insert_cmd = "out.<<" compiler.put_cmd = compiler.insert_cmd = "out.<<"
lineno = f.lineno lineno = f.lineno
src, = compiler.compile(f.read) src, = compiler.compile(f.read)
eval(src, binding, f.path, lineno) eval(src, binding, path, lineno)
end end
main main