class TokenLexer
Author: Alice “Duchess” Archer Copyright © 2016 under the MIT License see COPYING.md for full copyright Name: rFlex Description: lexical analysis library
Public Class Methods
new(type, regexp)
click to toggle source
# File lib/rflex.rb, line 12 def initialize type, regexp @type = type @regexp = regexp @blockType = nil @block = nil end
Public Instance Methods
analyze(token, regex, type)
click to toggle source
# File lib/rflex.rb, line 100 def analyze token, regex, type @block.call(token, regex, type) end
block()
click to toggle source
# File lib/rflex.rb, line 112 def block return @block end
do(type, &block)
click to toggle source
# File lib/rflex.rb, line 19 def do type, &block @block = block @blockType = type end
lex(input)
click to toggle source
# File lib/rflex.rb, line 24 def lex input inputT = input temp = self.sliceL(inputT) token = "" if temp == nil raise "\n\nUnconsumed Input:\n\n[#{inputT}]\n\n" else token = temp[:token] end if token.length >= 1 if @block != nil analyze(token, @regexp, @type) end if temp[:inputS].length == 0 # token is not empty and remaining input is empty return { :success => true, :inputS => temp[:inputS], :done => true, :error => false } else # token is not empty and remaining input is not empty return { :success => true, :inputS => temp[:inputS], :done => false, :error => false } end else if temp[:inputS].length >= 1 # token is empty and remaining input is not empty # error #raise "\n\nUnconsumed Input:\n\n[#{temp[:inputS]}]\n\n" return { :success => false, :inputS => input, :done => false, :error => true } else # token is empty and input is not return { :success => false, :inputS => input, :done => false, :error => false } end end end
regexp()
click to toggle source
# File lib/rflex.rb, line 108 def regexp return @regexp end
sliceL(input)
click to toggle source
# File lib/rflex.rb, line 56 def sliceL input # puts "\n\n" if input == nil return { :token => "", :input => input } end len = input.length ii = 0 temp = "" curMatch = "" matched = false inputT = input until ii == len do temp.concat(input[ii]) # print "[#{input[ii]}]" if temp.match(@regexp) matched = true curMatch = temp ii += 1 if ii == len inputT = inputT[(ii)..-1].to_s return { :token => curMatch[0..-1], :inputS => inputT } end next else ii += 1 if matched inputT = inputT[(ii - 1)..-1].to_s return { :token => curMatch[0..-2], :inputS => inputT } else if ii == len return { :token => "", :inputS => inputT } else next end end end end end
type()
click to toggle source
# File lib/rflex.rb, line 104 def type return @type end