Class: LexicalAnalyzer

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

Instance Method Summary collapse

Constructor Details

#initialize(file, config) ⇒ LexicalAnalyzer

Returns a new instance of LexicalAnalyzer.



118
119
120
121
122
123
124
125
126
# File 'lib/rflex.rb', line 118

def initialize file, config
  @types = {}
  @typeList = []
  @file = file
  @streamTokens = []
  @lexers = []
  @typeList = []
  configure(config)
end

Instance Method Details

#configure(config) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rflex.rb', line 128

def configure config
  typeList = File.readlines(config)

  0.upto(typeList.length.to_i - 1) do |i|
    typeList[i].chomp!
  end

  typeList.each do |type|
    
    at_Reg = false

    cur_Type = ""
    cur_Reg = ""

    index = 0

    0.upto(type.length - 1) do |i|
      if type[i] == "{"
        index = i
        break
      else
        cur_Type.concat(type[i])
      end
    end

    cur_Reg = type[(index+1)..-2]

    cur_Type.delete! " "

    @types.store(cur_Type, Regexp.new(cur_Reg))
    cur_Reg = Regexp.new(cur_Reg)
    @lexers.push(TokenLexer.new(cur_Type, cur_Reg))
    @typeList.push(cur_Type)
  end
end

#lexObject



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
# File 'lib/rflex.rb', line 164

def lex
  lines = File.readlines(@file)
  input = ""
  lines.each do |line|
    input.concat("#{line}")
  end

  # input = input[0..-1].to_s

  inputT = input

  len = @lexers.length - 1
  j = 0

  matched = false

  until inputT.length == 0
    tempLex = { :success => false, :inputS => inputT }
    0.upto(len) do |i|
      j = i
      tempLex = @lexers[i].lex(inputT)

      if tempLex[:success]
        matched = tempLex[:success]
        inputT = tempLex[:inputS]
        if inputT.length == 0
          break
        end
      else
        matched = false
      end

      # j = i

    end

    # if we tried all options

    # and

    # we didn't match

    # and

    # there is remaining input

    ## puts "j: #{j}, len: #{len}, matched: #{matched}, input: #{inputT}, length: #{inputT.length}, done: #{tempLex[:done]}, error: #{tempLex[:error]}"

    if (j == len) and tempLex[:error]
      raise "\n\nUnconsumed Input:\n\n\"#{inputT}\"\n\n"
    end

    matched = false
  end
end

#lexersObject



212
213
214
# File 'lib/rflex.rb', line 212

def lexers
  return @lexers
end

#typesObject



216
217
218
# File 'lib/rflex.rb', line 216

def types
  return @typeList
end