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
$(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

View File

@ -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