Top Level Namespace

Defined Under Namespace

Classes: Ripper

Instance Method Summary collapse

Instance Method Details

#check_arity(h) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
# File 'ext/tools/generate.rb', line 120

def check_arity(h)
  invalid = false
  h.each do |event, list|
    unless list.map {|line, arity| arity }.uniq.size == 1
      invalid = true
      locations = list.map {|line, a| "#{line}:#{a}" }.join(', ')
      $stderr.puts "arity crash [event=#{event}]: #{locations}"
    end
  end
  abort if invalid
end

#generate_eventids1(ids) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/tools/generate.rb', line 70

def generate_eventids1(ids)
  buf = ""
  ids.each do |id, arity|
    buf << %Q[static ID ripper_id_#{id};\n]
  end
  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|
    buf << %Q[    ripper_id_#{id} = rb_intern_const("on_#{id}");\n]
  end
  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_const("#{id}");\n]
    buf << %Q[    rb_hash_aset(h, ID2SYM(id), INT2NUM(#{arity}));\n]
  end
  buf << %Q[}\n]
  buf
end

#generate_eventids2_table(ids) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/tools/generate.rb', line 95

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_const("#{id}");\n]
    buf << %Q[    rb_hash_aset(h, ID2SYM(id), INT2NUM(1));\n]
  end
  buf << %Q[}\n]
  buf
end

#grammar(f, out) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/tools/preproc.rb', line 63

def grammar(f, out)
  while line = f.gets
    case line
    when %r</\*%%%\*/>
      out << '#if 0' << $/
    when %r</\*%c%\*/>
      out << '/*' << $/
    when %r</\*%c>
      out << '*/' << $/
    when %r</\*%>
      out << '#endif' << $/
    when %r<%\*/>
      out << $/
    when /\A%%/
      out << '%%' << $/
      return
    else
      out << line
    end
  end
end

#mainObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'ext/extconf.rb', line 7

def main
  unless find_executable('bison')
    unless File.exist?('ripper.c') or File.exist?("#{$srcdir}/ripper.c")
      Logging.message 'missing bison; abort'
      return
    end
  end
  $objs = %w(ripper.o)
  $cleanfiles.concat %w(ripper.y ripper.c ripper.E ripper.output y.output eventids1.c eventids2table.c)
  $defs << '-DRIPPER'
  $defs << '-DRIPPER_DEBUG' if $debug
  $VPATH = []
  if RUBY_VERSION < "1.9.1"
    $INCFLAGS = " -I$(srcdir)/backports #{$INCFLAGS} "
  else
    $INCFLAGS = " -I$(srcdir)/extra #{$INCFLAGS} "
  end
  create_makefile 'ripper'
end

#prelude(f, out) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/tools/preproc.rb', line 41

def prelude(f, out)
  while line = f.gets
    case line
    when %r</\*%%%\*/>
      out << '/*' << $/
    when %r</\*%>
      out << '*/' << $/
    when %r<%\*/>
      out << $/
    when /\A%%/
      out << '%%' << $/
      return
    when /\A%token/
      out << line.sub(/<\w+>/, '<val>')
    when /\A%type/
      out << line.sub(/<\w+>/, '<val>')
    else
      out << line
    end
  end
end

#read_ids1(path) ⇒ Object


111
112
113
# File 'ext/tools/generate.rb', line 111

def read_ids1(path)
  strip_locations(read_ids1_with_locations(path))
end

#read_ids1_with_locations(path) ⇒ Object


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/tools/generate.rb', line 132

def read_ids1_with_locations(path)
  h = {}
  File.open(path) {|f|
    f.each do |line|
      next if /\A\#\s*define\s+s?dispatch/ =~ line
      next if /ripper_dispatch/ =~ line
      line.scan(/dispatch(\d)\((\w+)/) do |arity, event|
        (h[event] ||= []).push [f.lineno, arity.to_i]
      end
    end
  }
  h
end

#read_ids2(path) ⇒ Object


146
147
148
149
150
# File 'ext/tools/generate.rb', line 146

def read_ids2(path)
  File.open(path) {|f|
    return f.read.scan(/ripper_id_(\w+)/).flatten.uniq.sort
  }
end

#strip_locations(h) ⇒ Object


115
116
117
118
# File 'ext/tools/generate.rb', line 115

def strip_locations(h)
  h.map {|event, list| [event, list.first[1]] }\
      .sort_by {|event, arity| event.to_s }
end

#usage(msg) ⇒ Object


64
65
66
67
68
# File 'ext/tools/generate.rb', line 64

def usage(msg)
  $stderr.puts msg
  $stderr.puts @parser.help
  exit false
end

#usercode(f, out) ⇒ Object


85
86
87
88
89
# File 'ext/tools/preproc.rb', line 85

def usercode(f, out)
  while line = f.gets
    out << line
  end
end