Class: Rexical::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rexical/generator.rb

Overview


Constant Summary collapse

REX_HEADER =

<<-REX_EOT
#--
# DO NOT MODIFY!!!!
# This file is automatically generated by rex %s
# from lexical definition file "%s".
#++

REX_EOT
REX_UTIL =
<<-REX_EOT
  require 'strscan'

  class ScanError < StandardError ; end

  attr_reader   :lineno
  attr_reader   :filename
  attr_accessor :state

  def scan_setup(str)
    @ss = StringScanner.new(str)
    @lineno =  1
    @state  = nil
  end

  def action
    yield
  end

  def scan_str(str)
    scan_setup(str)
    do_parse
  end
  alias :scan :scan_str

  def load_file( filename )
    @filename = filename
    open(filename, "r") do |f|
      scan_setup(f.read)
    end
  end

  def scan_file( filename )
    load_file(filename)
    do_parse
  end

REX_EOT
REX_STUB =
<<-REX_EOT

if __FILE__ == $0
  exit  if ARGV.size != 1
  filename = ARGV.shift
  rex = %s.new
  begin
    rex.load_file  filename
    while  token = rex.next_token
      p token
    end
  rescue
    $stderr.printf  %s, rex.filename, rex.lineno, $!.message
  end
end
REX_EOT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Generator




34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rexical/generator.rb', line 34

def initialize(opts)
  @lineno  =  0
  @macro  =  {}
  @rules  =  []
  @exclusive_states = [nil]
  @grammar_lines  =  nil
  @scanner_header  =  ""
  @scanner_footer  =  ""
  @scanner_inner  =  ""
  @opt  =  opts
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



25
26
27
# File 'lib/rexical/generator.rb', line 25

def class_name
  @class_name
end

#debugObject

Returns the value of attribute debug.



31
32
33
# File 'lib/rexical/generator.rb', line 31

def debug
  @debug
end

#exclusive_statesObject

Returns the value of attribute exclusive_states.



28
29
30
# File 'lib/rexical/generator.rb', line 28

def exclusive_states
  @exclusive_states
end

#grammar_fileObject




21
22
23
# File 'lib/rexical/generator.rb', line 21

def grammar_file
  @grammar_file
end

#grammar_linesObject

Returns the value of attribute grammar_lines.



22
23
24
# File 'lib/rexical/generator.rb', line 22

def grammar_lines
  @grammar_lines
end

#ignorecaseObject

Returns the value of attribute ignorecase.



29
30
31
# File 'lib/rexical/generator.rb', line 29

def ignorecase
  @ignorecase
end

#independentObject

Returns the value of attribute independent.



30
31
32
# File 'lib/rexical/generator.rb', line 30

def independent
  @independent
end

#linenoObject

Returns the value of attribute lineno.



26
27
28
# File 'lib/rexical/generator.rb', line 26

def lineno
  @lineno
end

#module_nameObject

Returns the value of attribute module_name.



24
25
26
# File 'lib/rexical/generator.rb', line 24

def module_name
  @module_name
end

#rulesObject

Returns the value of attribute rules.



27
28
29
# File 'lib/rexical/generator.rb', line 27

def rules
  @rules
end

#scanner_fileObject

Returns the value of attribute scanner_file.



23
24
25
# File 'lib/rexical/generator.rb', line 23

def scanner_file
  @scanner_file
end

Instance Method Details




52
53
54
# File 'lib/rexical/generator.rb', line 52

def add_footer( st )
  @scanner_footer  +=  "#{st}\n"
end

#add_header(st) ⇒ Object




47
48
49
# File 'lib/rexical/generator.rb', line 47

def add_header( st )
  @scanner_header  +=  "#{st}\n"
end

#add_inner(st) ⇒ Object




57
58
59
# File 'lib/rexical/generator.rb', line 57

def add_inner( st )
  @scanner_inner  +=  "#{st}\n"
end

#add_macro(st) ⇒ Object




77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rexical/generator.rb', line 77

def add_macro( st )
  ss  =  StringScanner.new(st)
  ss.scan(/\s+/)
  key = ss.scan(/\S+/)
  ss.scan(/\s+/)
  st = ss.post_match
  len  =  st.size
  ndx  =  0
  while ndx <= len
    c  =  st[ndx,1]
    ndx  +=  1
    case  c
    when '\\'
      ndx  +=  1
      next
    when '#', ' '
      ndx  -=  1
      break
    end
  end
  expr = st[0,ndx]
  expr.gsub!('\ ', ' ')
  key  =  '{' + key + '}'
  @macro.each_pair do |k, e|
    expr.gsub!(k) { |m| e }
  end
  @macro[key]  =  expr
rescue
  raise ParseError, "parse error in add_macro:'#{st}'"
end

#add_option(st) ⇒ Object




62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rexical/generator.rb', line 62

def add_option( st )
  opts = st.split
  opts.each do |opt|
    case opt
    when /ignorecase/i
      @opt['--ignorecase'] = true
    when /stub/i
      @opt['--stub'] = true
    when /independent/i
      @opt['--independent'] = true
    end
  end
end

#add_rule(rule_state, rule_expr, rule_action = nil) ⇒ Object




109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rexical/generator.rb', line 109

def add_rule( rule_state, rule_expr, rule_action=nil )
  st = rule_expr.dup
  @macro.each_pair do |k, e|
    rule_expr.gsub!(k) { |m| e }
  end
  if rule_state.to_s[1,1] =~ /[A-Z]/
    @exclusive_states << rule_state  unless @exclusive_states.include?(rule_state)
    exclusive_state = rule_state
    start_state = nil
  else
    exclusive_state = nil
    start_state = rule_state
  end
  rule = [exclusive_state, start_state, rule_expr, rule_action]
  @rules << rule
rescue
  raise ParseError, "parse error in add_rule:'#{st}'"
end

#next_lineObject



132
133
134
135
136
137
# File 'lib/rexical/generator.rb', line 132

def next_line
  @lineno += 1
  @grammar_lines.scan_until(/\n/).chomp
rescue
  nil
end

#parseObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/rexical/generator.rb', line 139

def parse
  state1  =  :HEAD
  state2  =  nil
  state3  =  nil
  lastmodes  =  []
  while st = next_line
    case state1
    when :FOOT
      add_footer  st

    when :HEAD
      ss  =  StringScanner.new(st)
      if ss.scan(/class/)
        state1  =  :CLASS
        st  =  ss.post_match.strip
        @class_name  =  st
      else
        add_header  st
      end

    when :CLASS
      s = st.strip
      next  if s.size == 0 or s[0,1] == '#'

      ss  =  StringScanner.new(st)
      if ss.scan(/option.*$/)
        state2 = :OPTION
        next
      end
      if ss.scan(/inner.*$/)
        state2 = :INNER
        next
      end
      if ss.scan(/macro.*$/)
        state2 = :MACRO
        next
      end
      if ss.scan(/rule.*$/)
        state2 = :RULE
        next
      end
      if ss.scan(/end.*$/)
        state1 = :FOOT
        next
      end

      case state2
      when :OPTION
        add_option  st

      when :INNER
        add_inner  st

      when :MACRO
        add_macro  st

      when :RULE
        case state3
        when nil
          rule_state, rule_expr, rule_action  =  parse_rule(st)
          if rule_action =~ /\s*\{/
            lastmodes = parse_action(rule_action, lastmodes)
            if lastmodes.empty?
              add_rule  rule_state, rule_expr, rule_action
            else
              state3  =  :CONT
              rule_action  +=  "\n"
            end
          else
            add_rule  rule_state, rule_expr
          end

        when :CONT
          rule_action  +=  "#{st}\n"
          lastmodes = parse_action(st, lastmodes)
          if lastmodes.empty?
            state3  =  nil
            add_rule  rule_state, rule_expr, rule_action
          else
          end

        end # case state3

      end # case state2

    end # case state1

  end # while

end

#parse_action(st, lastmodes = []) ⇒ Object




244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/rexical/generator.rb', line 244

def parse_action(st, lastmodes=[])
  modes  =  lastmodes
  mode  =  lastmodes[-1]
  ss  =  StringScanner.new(st)
  until ss.eos?
    c  =  ss.scan(/./)
    case  c
    when '#'
      if (mode == :brace) or (mode == nil)
        #p [c, mode, modes]
        return  modes
      end
    when '{'
      if (mode == :brace) or (mode == nil)
        mode = :brace
        modes.push  mode
      end
    when '}'
      if (mode == :brace)
        modes.pop
        mode = modes[0]
      end
    when "'"
      if (mode == :brace)
        mode = :quote
        modes.push  mode
      elsif (mode == :quote)
        modes.pop
        mode = modes[0]
      end
    when '"'
      if (mode == :brace)
        mode = :doublequote
        modes.push  mode
      elsif (mode == :doublequote)
        modes.pop
        mode = modes[0]
      end
    when '`'
      if (mode == :brace)
        mode = :backquote
        modes.push  mode
      elsif (mode == :backquote)
        modes.pop
        mode = modes[0]
      end
    end
  end
  #p [c, mode, modes]
  return  modes
end

#parse_rule(st) ⇒ Object




231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rexical/generator.rb', line 231

def parse_rule(st)
  st.strip!
  return  if st.size == 0 or st[0,1] == '#'
  ss  =  StringScanner.new(st)
  ss.scan(/\s+/)
  rule_state  =  ss.scan(/\:\S+/)
  ss.scan(/\s+/)
  rule_expr  =  ss.scan(/\S+/)
  ss.scan(/\s+/)
  [rule_state, rule_expr, ss.post_match]
end

#read_grammarObject



128
129
130
# File 'lib/rexical/generator.rb', line 128

def read_grammar
  @grammar_lines = StringScanner.new File.read(grammar_file)
end

#write_scanner(f = scanner_io) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/rexical/generator.rb', line 374

def write_scanner f = scanner_io
  ## scan flag
  flag = ""
  flag += "i"  if @opt['--ignorecase']
  ## header
  f.printf REX_HEADER, Rexical::VERSION, grammar_file

  unless @opt['--independent']
    f.printf "require 'racc/parser'\n"
  end

  @scanner_header.each_line do |s|
    f.print s
  end
  if @opt['--independent']
    f.puts "class #{@class_name}"
  else
    f.puts "class #{@class_name} < Racc::Parser"
  end

  ## utility method
  f.print REX_UTIL

  ## scanner method

  f.print <<-REX_EOT

  def next_token
return if @ss.eos?

# skips empty actions
until token = _next_token or @ss.eos?; end
token
  end

  def _next_token
text = @ss.peek(1)
@lineno  +=  1  if text == "\\n"
token = case @state
REX_EOT

exclusive_states.each do |es|
  f.printf <<-REX_EOT
when #{es ? es.to_s : "nil"}
  case
  REX_EOT
  rules.each do |rule|
    exclusive_state, start_state, rule_expr, rule_action = *rule
    if es == exclusive_state

      if rule_action
        if start_state
          f.print <<-REX_EOT
  when((state == #{start_state}) and (text = @ss.scan(/#{rule_expr}/#{flag})))
     action #{rule_action}

          REX_EOT
        else
          f.print <<-REX_EOT
  when (text = @ss.scan(/#{rule_expr}/#{flag}))
     action #{rule_action}

          REX_EOT
        end
      else
        if start_state
          f.print <<-REX_EOT
  when (state == #{start_state}) and (text = @ss.scan(/#{rule_expr}/#{flag}))
    ;

          REX_EOT
        else
          f.print <<-REX_EOT
  when (text = @ss.scan(/#{rule_expr}/#{flag}))
    ;

          REX_EOT
        end
      end

    end
  end
  f.print <<-REX_EOT
  else
    text = @ss.string[@ss.pos .. -1]
    raise  ScanError, "can not match: '" + text + "'"
  end  # if

  REX_EOT
end
f.print <<-REX_EOT
else
  raise  ScanError, "undefined state: '" + state.to_s + "'"
end  # case state
  REX_EOT
if @opt['--debug']
  f.print <<-REX_EOT
p token
  REX_EOT
end
f.print <<-REX_EOT
token
  end  # def _next_token

  REX_EOT

  ## inner method
  @scanner_inner.each_line do |s|
    f.print s
  end
  f.puts "end # class"

  ## footer
  @scanner_footer.each_line do |s|
    f.print s
  end # case

  ## stub main
  f.printf REX_STUB, @class_name, '"%s:%d:%s\n"'  if @opt['--stub']
  f.close

end