From b481b673d753339204290d7582dbb91a6e14447a Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sun, 28 May 2023 20:00:20 +0900 Subject: [Feature #19719] Universal Parser Introduce Universal Parser mode for the parser. This commit includes these changes: * Introduce `UNIVERSAL_PARSER` macro. All of CRuby related functions are passed via `struct rb_parser_config_struct` when this macro is enabled. * Add CI task with 'cppflags=-DUNIVERSAL_PARSER' for ubuntu. --- ext/ripper/tools/generate.rb | 39 ++++++++++++++++++++++++++++++++------- ext/ripper/tools/preproc.rb | 33 +++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 15 deletions(-) (limited to 'ext/ripper/tools') diff --git a/ext/ripper/tools/generate.rb b/ext/ripper/tools/generate.rb index 883e6ef2df..bb64d2fe8b 100644 --- a/ext/ripper/tools/generate.rb +++ b/ext/ripper/tools/generate.rb @@ -11,7 +11,7 @@ def main parser = @parser = OptionParser.new parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--output=PATH]" - parser.on('--mode=MODE', 'check, eventids1, or eventids2table.') {|m| + parser.on('--mode=MODE', 'check, eventids1_h, eventids1, or eventids2table.') {|m| mode = m } parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path| @@ -45,6 +45,9 @@ def main abort "event crash: #{common.join(' ')}" end exit 0 + when 'eventids1_h' + usage 'no --ids1src' unless ids1src + result = generate_eventids1_h(read_ids1(ids1src)) when 'eventids1' usage 'no --ids1src' unless ids1src result = generate_eventids1(read_ids1(ids1src)) @@ -67,19 +70,35 @@ def usage(msg) exit false end -def generate_eventids1(ids) +def generate_eventids1_h(ids) buf = "".dup - buf << %Q[static struct {\n] + buf << %Q[#ifndef RIPPER_EVENTIDS1\n] + buf << %Q[#define RIPPER_EVENTIDS1\n] + buf << %Q[\n] + buf << %Q[void ripper_init_eventids1(void);\n] + buf << %Q[void ripper_init_eventids1_table(VALUE self);\n] + buf << %Q[\n] + buf << %Q[struct ripper_parser_ids {\n] ids.each do |id, arity| buf << %Q[ ID id_#{id};\n] end - buf << %Q[} ripper_parser_ids;\n] + buf << %Q[};\n] buf << %Q[\n] ids.each do |id, arity| buf << %Q[#define ripper_id_#{id} ripper_parser_ids.id_#{id}\n] end + buf << %Q[#endif /* RIPPER_EVENTIDS1 */\n] buf << %Q[\n] - buf << %Q[static void\n] +end + +def generate_eventids1(ids) + buf = "".dup + buf << %Q[#include "ruby/ruby.h"\n] + buf << %Q[#include "eventids1.h"\n] + buf << %Q[\n] + buf << %Q[struct ripper_parser_ids ripper_parser_ids;\n] + buf << %Q[\n] + buf << %Q[void\n] buf << %Q[ripper_init_eventids1(void)\n] buf << %Q[{\n] buf << %Q[#define set_id1(name) ripper_id_##name = rb_intern_const("on_"#name)\n] @@ -88,7 +107,9 @@ def generate_eventids1(ids) end buf << %Q[}\n] buf << %Q[\n] - buf << %Q[static void\n] + buf << %Q[#define intern_sym(name) ID2SYM(rb_intern_const(name))\n] + buf << %Q[\n] + buf << %Q[void\n] buf << %Q[ripper_init_eventids1_table(VALUE self)\n] buf << %Q[{\n] buf << %Q[ VALUE h = rb_hash_new();\n] @@ -102,7 +123,11 @@ end def generate_eventids2_table(ids) buf = "".dup - buf << %Q[static void\n] + buf << %Q[#include "ruby/ruby.h"\n] + buf << %Q[\n] + buf << %Q[#define intern_sym(name) ID2SYM(rb_intern_const(name))\n] + buf << %Q[\n] + buf << %Q[void\n] buf << %Q[ripper_init_eventids2_table(VALUE self)\n] buf << %Q[{\n] buf << %Q[ VALUE h = rb_hash_new();\n] diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index f419fc3dbe..07ef627cf3 100644 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -5,11 +5,15 @@ require 'optparse' def main output = nil + template = nil parser = OptionParser.new - parser.banner = "Usage: #{File.basename($0)} [--output=PATH] " + parser.banner = "Usage: #{File.basename($0)} [--output=PATH] [--template=PATH] " parser.on('--output=PATH', 'An output file.') {|path| output = path } + parser.on('--template=PATH', 'An template file.') {|path| + template = path + } parser.on('--help', 'Prints this message and quit.') { puts parser.help exit true @@ -25,13 +29,13 @@ def main unless ARGV.size == 2 abort "wrong number of arguments (#{ARGV.size} for 2)" end - process STDIN, out, ARGV[1] + process STDIN, out, ARGV[1], template 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] + process f, out, ARGV[0], template } end if output @@ -41,10 +45,10 @@ def main end end -def process(f, out, path) +def process(f, out, path, template) prelude f, out grammar f, out - usercode f, out, path + usercode f, out, path, template end def prelude(f, out) @@ -103,12 +107,25 @@ def grammar(f, out) end end -def usercode(f, out, path) +def usercode(f, out, path, template) require 'erb' + lineno = nil + src = nil compiler = ERB::Compiler.new('%-') compiler.put_cmd = compiler.insert_cmd = "out.<<" - lineno = f.lineno - src, = compiler.compile(f.read) + + if template + File.open(template) do |f| + out.clear + lineno = f.lineno + src, = compiler.compile(f.read) + path = template + end + else + lineno = f.lineno + src, = compiler.compile(f.read) + end + eval(src, binding, path, lineno) end -- cgit v1.2.3