Class: TokenLexer

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

Overview

! /bin/env ruby

Author: Alice “Duchess” Archer Copyright © 2016 under the MIT License see COPYING.md for full copyright Name: rFlex Description: lexical analysis library

Instance Method Summary collapse

Constructor Details

#initialize(type, regexp) ⇒ TokenLexer

Returns a new instance of TokenLexer.



12
13
14
15
16
17
# File 'lib/rflex.rb', line 12

def initialize type, regexp
  @type = type
  @regexp = regexp
  @blockType = nil
  @block = nil
end

Instance Method Details

#analyze(token, regex, type) ⇒ Object



100
101
102
# File 'lib/rflex.rb', line 100

def analyze token, regex, type
  @block.call(token, regex, type)
end

#blockObject



112
113
114
# File 'lib/rflex.rb', line 112

def block
  return @block
end

#do(type, &block) ⇒ Object



19
20
21
22
# File 'lib/rflex.rb', line 19

def do type, &block
  @block = block
  @blockType = type
end

#lex(input) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

#regexpObject



108
109
110
# File 'lib/rflex.rb', line 108

def regexp
  return @regexp
end

#sliceL(input) ⇒ Object



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/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

#typeObject



104
105
106
# File 'lib/rflex.rb', line 104

def type
  return @type
end