Class: Einstein::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/einstein/parser.rex.rb,
lib/einstein/parser.racc.rb

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
'$end',
'error',
'IDENT',
'NUMBER',
'"("',
'")"',
'"+"',
'"-"',
'"~"',
'MULT2',
'"*"',
'"/"',
'"%"',
'LSHIFT',
'RSHIFT',
'"&"',
'"|"',
'"^"',
'$start',
'exp',
'bitwise_or',
'literal',
'primary',
'unary',
'exponent',
'multiplicative',
'additive',
'shift',
'bitwise_and']
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



15
16
17
# File 'lib/einstein/parser.rex.rb', line 15

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



14
15
16
# File 'lib/einstein/parser.rex.rb', line 14

def lineno
  @lineno
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



327
328
329
# File 'lib/einstein/parser.racc.rb', line 327

def _reduce_none( val, _values, result )
 result
end

#action(&block) ⇒ Object



19
20
21
# File 'lib/einstein/parser.rex.rb', line 19

def action &block
  yield
end

#load_file(filename) ⇒ Object



28
29
30
31
32
33
# File 'lib/einstein/parser.rex.rb', line 28

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

#next_tokenObject



40
41
42
# File 'lib/einstein/parser.rex.rb', line 40

def next_token
  @rex_tokens.shift
end

#scan_evaluate(str) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/einstein/parser.rex.rb', line 44

def scan_evaluate( str )
  scan_setup
  @rex_tokens = []
  @lineno  =  1
  ss = StringScanner.new(str)
  state = nil
  until ss.eos?
    text = ss.peek(1)
    @lineno  +=  1  if text == "\n"
    case state
    when nil
      case
      when (text = ss.scan(/\s+/))
        ;

      when (text = ss.scan(/0[bB][01]+/))
         @rex_tokens.push action { [:NUMBER, text.to_i(2)] }

      when (text = ss.scan(/0[xX][0-9a-fA-F]+/))
         @rex_tokens.push action { [:NUMBER, text.hex] }

      when (text = ss.scan(/0[0-9]+/))
         @rex_tokens.push action { [:NUMBER, text.oct] }

      when (text = ss.scan(/[0-9]+\.[0-9]+([eE][\-\+][0-9]+)?/))
         @rex_tokens.push action { [:NUMBER, text.to_f] }

      when (text = ss.scan(/[1-9][0-9]*/))
         @rex_tokens.push action { [:NUMBER, text.to_i] }

      when (text = ss.scan(/[a-zA-Z_]+/))
         @rex_tokens.push action { [:IDENT, text] }

      when (text = ss.scan(/\*{2}/))
         @rex_tokens.push action { [:MULT2, text] }

      when (text = ss.scan(/<</))
         @rex_tokens.push action { [:LSHIFT, text] }

      when (text = ss.scan(/>>/))
         @rex_tokens.push action { [:RSHIFT, text] }

      when (text = ss.scan(/.|\n/))
         @rex_tokens.push action { [text, text] }

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

    else
      raise  ScanError, "undefined state: '" + state.to_s + "'"
    end  # case state
  end  # until ss
end

#scan_file(filename) ⇒ Object



35
36
37
38
# File 'lib/einstein/parser.rex.rb', line 35

def scan_file( filename )
  load_file  filename
  do_parse
end

#scan_setupObject



17
# File 'lib/einstein/parser.rex.rb', line 17

def scan_setup ; end

#scan_str(str) ⇒ Object



23
24
25
26
# File 'lib/einstein/parser.rex.rb', line 23

def scan_str( str )
  scan_evaluate  str
  do_parse
end