diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/ripper/depend | 21 | ||||
-rw-r--r-- | ext/ripper/eventids2.c | 6 | ||||
-rw-r--r-- | ext/ripper/extconf.rb | 2 | ||||
-rw-r--r-- | ext/ripper/lib/ripper/core.rb (renamed from ext/ripper/lib/ripper/core.rb.in) | 49 | ||||
-rw-r--r-- | ext/ripper/lib/ripper/filter.rb | 4 | ||||
-rw-r--r-- | ext/ripper/lib/ripper/lexer.rb | 4 | ||||
-rw-r--r-- | ext/ripper/lib/ripper/sexp.rb | 4 | ||||
-rwxr-xr-x | ext/ripper/tools/generate.rb | 122 |
8 files changed, 103 insertions, 109 deletions
diff --git a/ext/ripper/depend b/ext/ripper/depend index 44b09f1e80..c47f210186 100644 --- a/ext/ripper/depend +++ b/ext/ripper/depend @@ -1,18 +1,25 @@ -src: ripper.c eventids1.c $(srcdir)/lib/ripper/core.rb +GEN = $(srcdir)/tools/generate.rb +SRC1 = $(hdrdir)/parse.y +SRC2 = $(srcdir)/eventids2.c -ripper.o: ripper.c eventids1.c $(srcdir)/eventids2.c $(hdrdir)/lex.c +src: ripper.c eventids1.c eventids2table.c + +ripper.o: ripper.c $(hdrdir)/lex.c eventids1.c $(srcdir)/eventids2.c eventids2table.c .y.c: bison -t -v -o$@ $< -ripper.y: $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y +ripper.y: check $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y $(RUBY) $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y --output=$@ -$(srcdir)/lib/ripper/core.rb: $(srcdir)/tools/generate.rb $(srcdir)/lib/ripper/core.rb.in $(hdrdir)/parse.y $(srcdir)/eventids2.c - $(RUBY) $(srcdir)/tools/generate.rb --mode=ripper/core --template=$@.in --output=$@ --ids1src=$(hdrdir)/parse.y --ids2src=$(srcdir)/eventids2.c +check: $(GEN) $(SRC1) $(SRC2) + $(RUBY) $(GEN) --mode=check --ids1src=$(SRC1) --ids2src=$(SRC2) + +eventids1.c: $(srcdir)/tools/generate.rb $(SRC1) + $(RUBY) $(GEN) --mode=eventids1 --ids1src=$(SRC1) --output=$@ -eventids1.c: $(srcdir)/tools/generate.rb $(hdrdir)/parse.y - $(RUBY) $(srcdir)/tools/generate.rb --mode=eventids1 --ids1src=$(hdrdir)/parse.y --output=$@ +eventids2table.c: $(srcdir)/tools/generate.rb $(SRC2) + $(RUBY) $(GEN) --mode=eventids2table --ids2src=$(SRC2) --output=$@ # Entries for Ripper maintainer diff --git a/ext/ripper/eventids2.c b/ext/ripper/eventids2.c index 90e677ed85..88d7856202 100644 --- a/ext/ripper/eventids2.c +++ b/ext/ripper/eventids2.c @@ -58,8 +58,10 @@ static ID ripper_id_heredoc_end; static ID ripper_id___end__; static ID ripper_id_CHAR; +#include "eventids2table.c" + static void -ripper_init_eventids2() +ripper_init_eventids2(VALUE self) { ripper_id_backref = rb_intern("on_backref"); ripper_id_backtick = rb_intern("on_backtick"); @@ -109,6 +111,8 @@ ripper_init_eventids2() ripper_id_heredoc_end = rb_intern("on_heredoc_end"); ripper_id___end__ = rb_intern("on___end__"); ripper_id_CHAR = rb_intern("on_CHAR"); + + ripper_init_eventids2_table(self); } static struct token_assoc { diff --git a/ext/ripper/extconf.rb b/ext/ripper/extconf.rb index 069ed7dcc4..6b44073300 100644 --- a/ext/ripper/extconf.rb +++ b/ext/ripper/extconf.rb @@ -11,7 +11,7 @@ def main end end $objs = %w(ripper.o) - $cleanfiles.concat %w(ripper.y ripper.c ripper.E ripper.output eventids1.c ids1 ids2) + $cleanfiles.concat %w(ripper.y ripper.c ripper.E ripper.output eventids1.c eventids2table.c) $CPPFLAGS += ' -DRIPPER' $CPPFLAGS += ' -DRIPPER_DEBUG' if $debug create_makefile 'ripper' diff --git a/ext/ripper/lib/ripper/core.rb.in b/ext/ripper/lib/ripper/core.rb index 5f46c58190..35aa54d090 100644 --- a/ext/ripper/lib/ripper/core.rb.in +++ b/ext/ripper/lib/ripper/core.rb @@ -1,7 +1,7 @@ # -# ripper/core.rb +# $Id$ # -# Copyright (C) 2003-2005 Minero Aoki +# Copyright (c) 2003-2005 Minero Aoki # # This program is free software. # You can distribute and/or modify this program under the Ruby License. @@ -11,38 +11,36 @@ require 'ripper.so' class Ripper + # Parses Ruby program read from _src_. # _src_ must be a String or a IO or a object which has #gets method. def Ripper.parse(src, filename = '(ripper)', lineno = 1) new(src, filename, lineno).parse end - # This table contains name of parser events and its arity. - PARSER_EVENT_TABLE = { -#include ids1 - } - # This array contains name of parser events. PARSER_EVENTS = PARSER_EVENT_TABLE.keys - # This table contains name of scanner events and its arity - # (arity is always 1 for all scanner events). - SCANNER_EVENT_TABLE = { -#include ids2 - } - # This array contains name of scanner events. SCANNER_EVENTS = SCANNER_EVENT_TABLE.keys - # This table contains name of all ripper events. + # This array contains name of all ripper events. EVENTS = PARSER_EVENTS + SCANNER_EVENTS - ### ### - ### Event Handlers ### - ### ### - private + # + # Parser Events + # + + PARSER_EVENT_TABLE.each do |id, arity| + module_eval(<<-End, __FILE__, __LINE__ + 1) + def on_#{id}(#{ ('a'..'z').to_a[0, arity].join(', ') }) + #{arity == 0 ? 'nil' : 'a'} + end + End + end + # This method is called when weak warning is produced by the parser. # _fmt_ and _args_ is printf style. def warn(fmt, *args) @@ -58,12 +56,15 @@ class Ripper end # - # Parser Events + # Scanner Events # -#include handlers1 - # - # Lexer Events - # -#include handlers2 + SCANNER_EVENTS.each do |id| + module_eval(<<-End, __FILE__, __LINE__ + 1) + def on_#{id}(token) + token + end + End + end + end diff --git a/ext/ripper/lib/ripper/filter.rb b/ext/ripper/lib/ripper/filter.rb index 99c11182a1..898501b23c 100644 --- a/ext/ripper/lib/ripper/filter.rb +++ b/ext/ripper/lib/ripper/filter.rb @@ -1,7 +1,7 @@ # -# ripper/filter.rb +# $Id$ # -# Copyright (C) 2004 Minero Aoki +# Copyright (c) 2004,2005 Minero Aoki # # This program is free software. # You can distribute and/or modify this program under the Ruby License. diff --git a/ext/ripper/lib/ripper/lexer.rb b/ext/ripper/lib/ripper/lexer.rb index 20436f6f49..22048f161c 100644 --- a/ext/ripper/lib/ripper/lexer.rb +++ b/ext/ripper/lib/ripper/lexer.rb @@ -1,7 +1,7 @@ # -# ripper/lexer.rb +# $Id$ # -# Copyright (C) 2004,2005 Minero Aoki +# Copyright (c) 2004,2005 Minero Aoki # # This program is free software. # You can distribute and/or modify this program under the Ruby License. diff --git a/ext/ripper/lib/ripper/sexp.rb b/ext/ripper/lib/ripper/sexp.rb index 621dd0e752..d841f9d665 100644 --- a/ext/ripper/lib/ripper/sexp.rb +++ b/ext/ripper/lib/ripper/sexp.rb @@ -1,7 +1,7 @@ # -# ripper/sexp.rb +# $Id$ # -# Copyright (C) 2004,2005 Minero Aoki +# Copyright (c) 2004,2005 Minero Aoki # # This program is free software. # You can distribute and/or modify this program under the Ruby License. diff --git a/ext/ripper/tools/generate.rb b/ext/ripper/tools/generate.rb index 545bc81af6..0efb997604 100755 --- a/ext/ripper/tools/generate.rb +++ b/ext/ripper/tools/generate.rb @@ -10,8 +10,8 @@ def main output = nil parser = @parser = OptionParser.new - parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--template=PATH] [--output=PATH]" - parser.on('--mode=MODE', %"ripper/core eventids1") {|m| + parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--output=PATH]" + parser.on('--mode=MODE', 'check, eventids1, or eventids2table.') {|m| mode = m } parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path| @@ -20,9 +20,6 @@ def main parser.on('--ids2src=PATH', 'A source file of event-IDs 2 (eventids2.c).') {|path| ids2src = path } - parser.on('--template=PATH', 'A template file of ripper/core.rb.') {|path| - template = path - } parser.on('--output=PATH', 'An output file.') {|path| output = path } @@ -37,20 +34,23 @@ def main end usage 'no mode given' unless mode case mode - when 'ripper/core' + when 'check' usage 'no --ids1src' unless ids1src usage 'no --ids2src' unless ids2src - usage 'no --template' unless template - ids1 = read_ids1(ids1src) + h = read_ids1_with_locations(ids1src) + check_arity h ids2 = read_ids2(ids2src) - diff = ids1.map {|id, *| id} & ids2 - unless diff.empty? - abort "event crash: #{diff.join(' ')}" + common = h.keys & ids2 + unless common.empty? + abort "event crash: #{common.join(' ')}" end - result = generate_ripper_core(template, ids1, ids2) + exit 0 when 'eventids1' usage 'no --ids1src' unless ids1src result = generate_eventids1(read_ids1(ids1src)) + when 'eventids2table' + usage 'no --ids2src' unless ids2src + result = generate_eventids2_table(read_ids2(ids2src)) end if output File.open(output, 'w') {|f| @@ -67,69 +67,52 @@ def usage(msg) exit false end -def generate_ripper_core(template, ids1, ids2) - str = <<header -# This file is automatically generated from #{File.basename(template)} and parse.y. -# DO NOT MODIFY!!!!!! - -header - File.foreach(template) do |line| - case line - when /\A\#include ids1/ - str << ids1.map {|id, arity| - " #{id.intern.inspect} => #{arity}" - }.join(",\n") << "\n" - when /\A\#include ids2/ - str << ids2.map {|id| - " #{id.intern.inspect} => 1" - }.join(",\n") << "\n" - when /\A\#include handlers1/ - ids1.each do |id, arity| - str << $/ - str << " def on_#{id}#{paramdecl(arity)}" << $/ - str << " #{arity == 0 ? 'nil' : 'a'}" << $/ - str << " end" << $/ - end - when /\A\#include handlers2/ - ids2.each do |id| - str << $/ - str << " def on_#{id}(token)" << $/ - str << " token" << $/ - str << " end" << $/ - end - when /\A\#include (.*)/ - raise "unknown operation: #include #{$1}" - else - str << line - end - end - str -end - -def paramdecl(n) - return '' if n == 0 - '(' + ('a'..'z').to_a[0, n].join(', ') + ')' -end - def generate_eventids1(ids) - str = "" + buf = "" ids.each do |id, arity| - str << "static ID ripper_id_#{id};" << $/ + buf << %Q[static ID ripper_id_#{id};\n] end - str << $/ - str << 'static void' << $/ - str << 'ripper_init_eventids1()' << $/ - str << '{' << $/ + buf << %Q[\n] + buf << %Q[static void\n] + buf << %Q[ripper_init_eventids1(VALUE self)\n] + buf << %Q[{\n] + buf << %Q[ VALUE h;\n] + buf << %Q[ ID id;\n] ids.each do |id, arity| - str << %Q[ ripper_id_#{id} = rb_intern("on_#{id}");] << $/ + buf << %Q[ ripper_id_#{id} = rb_intern("on_#{id}");\n] end - str << '}' << $/ - str + buf << %Q[\n] + buf << %Q[ h = rb_hash_new();\n] + buf << %Q[ rb_define_const(self, "PARSER_EVENT_TABLE", h);\n] + ids.each do |id, arity| + buf << %Q[ id = rb_intern("#{id}");\n] + buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(#{arity}));\n] + end + buf << %Q[}\n] + buf +end + +def generate_eventids2_table(ids) + buf = "" + buf << %Q[static void\n] + buf << %Q[ripper_init_eventids2_table(VALUE self)\n] + buf << %Q[{\n] + buf << %Q[ VALUE h = rb_hash_new();\n] + buf << %Q[ ID id;\n] + buf << %Q[ rb_define_const(self, "SCANNER_EVENT_TABLE", h);\n] + ids.each do |id| + buf << %Q[ id = rb_intern("#{id}");\n] + buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(1));\n] + end + buf << %Q[}\n] + buf end def read_ids1(path) - h = read_ids1_with_locations(path) - check_arity h + strip_locations(read_ids1_with_locations(path)) +end + +def strip_locations(h) h.map {|event, list| [event, list.first[1]] }\ .sort_by {|event, arity| event.to_s } end @@ -139,9 +122,8 @@ def check_arity(h) h.each do |event, list| unless list.map {|line, arity| arity }.uniq.size == 1 invalid = true - $stderr.puts "arity crash [event=#{event}]: #{ # " - list.map {|line,a| "#{line}:#{a}" }.join(', ') - }" # " + locations = list.map {|line, a| "#{line}:#{a}" }.join(', ') + $stderr.puts "arity crash [event=#{event}]: #{locations}" end end abort if invalid |