Class: Predicator::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/predicator/lexer.rex.rb

Overview

– This file is automatically generated. Do not modify it. Generated by: oedipus_lex version 2.5.0. Source: lib/predicator/lexer.rex ++

Defined Under Namespace

Classes: LexerError, ScanError

Constant Summary collapse

SPACE =
/[ \t\r\n]/
LPAREN =
/\(/
RPAREN =
/\)/
LBRACKET =
/\[/
RBRACKET =
/\]/
TRUE =
/true\b/
FALSE =
/false\b/
BETWEEN =
/between\b/
IN =
/in\b/
BANG =
/!/
NOT =
/not\b/
DOT =
/\./
COMMA =
/,/
AND =
/and\b/
OR =
/or\b/
EQ =
/=/
GT =
/>/
LT =
/</
ENDSWITH =
/ends with/
STARTSWITH =
/starts with/
BEGINSWITH =
/begins with/
BLANK =
/is blank/
PRESENT =
/is present/
AGO =
/ago/
FROMNOW =
/from now/
DATE =
/\d{4}[-|\/]\d{2}[-|\/]\d{2}/i
DURATION =
/\d+d/
INTEGER =
/[+-]?\d(_?\d)*\b/
STRING =
/(["'])(?:\\?.)*?\1/
IDENTIFIER =
/[a-z][A-Za-z0-9_]*\b/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



46
47
48
# File 'lib/predicator/lexer.rex.rb', line 46

def filename
  @filename
end

#linenoObject

Returns the value of attribute lineno.



45
46
47
# File 'lib/predicator/lexer.rex.rb', line 45

def lineno
  @lineno
end

#old_posObject

Returns the value of attribute old_pos.



62
63
64
# File 'lib/predicator/lexer.rex.rb', line 62

def old_pos
  @old_pos
end

#ssObject Also known as: match

Returns the value of attribute ss.



47
48
49
# File 'lib/predicator/lexer.rex.rb', line 47

def ss
  @ss
end

#stateObject

Returns the value of attribute state.



48
49
50
# File 'lib/predicator/lexer.rex.rb', line 48

def state
  @state
end

Instance Method Details

#actionObject



58
59
60
# File 'lib/predicator/lexer.rex.rb', line 58

def action
  yield
end

#columnObject



64
65
66
67
# File 'lib/predicator/lexer.rex.rb', line 64

def column
  idx = ss.string.rindex("\n", old_pos) || -1
  old_pos - idx - 1
end

#do_parseObject

def next_token



186
# File 'lib/predicator/lexer.rex.rb', line 186

def do_parse; end

#locationObject



88
89
90
91
92
93
94
# File 'lib/predicator/lexer.rex.rb', line 88

def location
  [
    (filename || "<input>"),
    lineno,
    column,
  ].compact.join(":")
end

#matchesObject



52
53
54
55
56
# File 'lib/predicator/lexer.rex.rb', line 52

def matches
  m = (1..9).map { |i| ss[i] }
  m.pop until m[-1] or m.empty?
  m
end

#next_tokenObject



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/predicator/lexer.rex.rb', line 96

def next_token

  token = nil

  until ss.eos? or token do
    self.lineno += 1 if ss.peek(1) == "\n"
    self.old_pos = ss.pos
    token =
      case state
      when nil then
        case
        when ss.skip(/#{SPACE}/) then
          # do nothing
        when text = ss.scan(/#{LPAREN}/) then
          action { [:LPAREN, text] }
        when text = ss.scan(/#{RPAREN}/) then
          action { [:RPAREN, text] }
        when text = ss.scan(/#{LBRACKET}/) then
          action { [:LBRACKET, text] }
        when text = ss.scan(/#{RBRACKET}/) then
          action { [:RBRACKET, text] }
        when text = ss.scan(/#{TRUE}/) then
          action { [:TRUE, text] }
        when text = ss.scan(/#{FALSE}/) then
          action { [:FALSE, text] }
        when text = ss.scan(/#{BETWEEN}/) then
          action { [:BETWEEN, text] }
        when text = ss.scan(/#{IN}/) then
          action { [:IN, text] }
        when text = ss.scan(/#{BANG}/) then
          action { [:BANG, text] }
        when text = ss.scan(/#{NOT}/) then
          action { [:NOT, text] }
        when text = ss.scan(/#{DOT}/) then
          action { [:DOT, text] }
        when text = ss.scan(/#{COMMA}/) then
          action { [:COMMA, text] }
        when text = ss.scan(/#{AND}/) then
          action { [:AND, text] }
        when text = ss.scan(/#{OR}/) then
          action { [:OR, text] }
        when text = ss.scan(/#{EQ}/) then
          action { [:EQ, text] }
        when text = ss.scan(/#{GT}/) then
          action { [:GT, text] }
        when text = ss.scan(/#{LT}/) then
          action { [:LT, text] }
        when text = ss.scan(/#{AGO}/) then
          action { [:AGO, text] }
        when text = ss.scan(/#{FROMNOW}/) then
          action { [:FROMNOW, text] }
        when text = ss.scan(/#{ENDSWITH}/) then
          action { [:ENDSWITH, text] }
        when text = ss.scan(/#{STARTSWITH}/) then
          action { [:STARTSWITH, text] }
        when text = ss.scan(/#{BEGINSWITH}/) then
          action { [:STARTSWITH, text] }
        when text = ss.scan(/#{BLANK}/) then
          action { [:BLANK, text] }
        when text = ss.scan(/#{PRESENT}/) then
          action { [:PRESENT, text] }
        when text = ss.scan(/#{DATE}/) then
          action { [:DATE, text] }
        when text = ss.scan(/#{DURATION}/) then
          action { [:DURATION, text] }
        when text = ss.scan(/#{INTEGER}/) then
          action { [:INTEGER, text] }
        when text = ss.scan(/#{STRING}/) then
          action { [:STRING, text[1...-1]] }
        when text = ss.scan(/#{IDENTIFIER}/) then
          action { [:IDENTIFIER, text] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      else
        raise ScanError, "undefined state at #{location}: '#{state}'"
      end # token = case state

    next unless token # allow functions to trigger redo w/ nil
  end # while

  raise LexerError, "bad lexical result at #{location}: #{token.inspect}" unless
    token.nil? || (Array === token && token.size >= 2)

  # auto-switch state
  self.state = token.last if token && token.first == :state

  token
end

#parse(str) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/predicator/lexer.rex.rb', line 73

def parse str
  self.ss     = scanner_class.new str
  self.lineno = 1
  self.state  ||= nil

  do_parse
end

#parse_file(path) ⇒ Object



81
82
83
84
85
86
# File 'lib/predicator/lexer.rex.rb', line 81

def parse_file path
  self.filename = path
  open path do |f|
    parse f.read
  end
end

#scanner_classObject



69
70
71
# File 'lib/predicator/lexer.rex.rb', line 69

def scanner_class
  StringScanner
end