class LexicalAnalyzer

Public Class Methods

new(file, config) click to toggle source
# File lib/rflex.rb, line 118
def initialize file, config
        @types = {}
        @typeList = []
        @file = file
        @streamTokens = []
        @lexers = []
        @typeList = []
        configure(config)
end

Public Instance Methods

configure(config) click to toggle source
# 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
lex() click to toggle source
# 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
lexers() click to toggle source
# File lib/rflex.rb, line 212
def lexers
        return @lexers
end
types() click to toggle source
# File lib/rflex.rb, line 216
def types
        return @typeList
end