Class: CodeRay::Scanners::LLVM

Inherits:
Scanner
  • Object
show all
Includes:
Streamable
Defined in:
app/rbx-llvm.rb

Instance Method Summary collapse

Instance Method Details

#scan_tokens(tokens, options) ⇒ Object



37
38
39
40
41
42
43
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/rbx-llvm.rb', line 37

def scan_tokens(tokens, options)
  
  state = :initial
  number_expected = true

  until eos?

    kind = nil
    match = nil
    
    case state

    when :initial
      if match = scan(/^\s*/)
        tokens << [match, :space]  unless match.empty?
      end
      state = 
        if match = scan(/^$/)
          :initial
        else
          :expect_label
        end

    when :expect_label
      state = 
        if match = scan(/\d+:/x)
          tokens << [match, :label]
          :expect_opcode
        else 
          match = scan(/^.*$/)
          tokens << [match, :error]
          :initial
      end
      
    when :expect_opcode
      state = 
        if scan(/(\s+)(\S+)/)
          tokens << [@match[1], :space]
          opcode = @match[2]
          tokens << [opcode, :reserved]
          if %w(push_literal create_block set_literal).member?(opcode)
            :expect_literal
          else
            :expect_operand
          end
        else
          match = scan(/^(.*)$/)
          tokens << [match, :error]
          :initial
        end

    when :expect_literal
      space_parse(tokens)
      if match = scan(/^#<.+>/)
        tokens << [match, :content]
      elsif scan(/^(\d+)/)
        tokens << [@match[0], :integer]
      elsif scan(/^([:][^: ,\n]+)/)
        tokens << [@match[0], :symbol]
      elsif string_parse(tokens)
        # 
      elsif match = scan(/nil|true|false/)
        tokens << [match, :pre_constant]
      elsif match = scan(/\/.*\//)
        tokens << [match, :entity]
      else
        match = scan(/^.*$/)              
        tokens << [match, :error] unless match.empty?
        end
      state = :expect_opt_comment
      
    when :expect_operand
      space_parse(tokens)
      state = 
        if scan(/^(\d+)/)
          tokens << [@match[0], :integer]
          :expect_another_operand
        elsif scan(/^([:][^: ,\n]+)/)
          tokens << [@match[0], :symbol]
          :expect_another_operand
        elsif string_parse(tokens)
          :expect_another_operand
        else
          :expect_opt_comment
        end
      
    when :expect_another_operand
      state = 
        if match = scan(/^,/)
          tokens << [@match[0], :operator]
          :expect_operand
        else
          :expect_opt_comment
        end
    when :expect_opt_comment
      space_parse(tokens)
      if match = scan(/^#.*$/)
        tokens << [match, :comment] 
      else
        match = scan(/^.*$/)              
        tokens << [match, :error] unless match.empty?
      end
      state = :initial
    end
  end
  tokens
end

#space_parse(tokens) ⇒ Object



31
32
33
34
35
# File 'app/rbx-llvm.rb', line 31

def space_parse(tokens)
  if match = scan(/^[ \t]*/)
    tokens << [match, :space] if match
  end
end

#string_parse(tokens) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/rbx-llvm.rb', line 16

def string_parse(tokens)
  if match = scan(/^"[^"]*"/)
    string = match.dup
    while  "\\" == match[-2..-2]
      match = scan(/^.*"/)
      break unless match 
      string << match 
    end
    tokens << [string, :string]
    true
  else
    false
  end
end