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



37
38
39
# File 'lib/predicator/lexer.rex.rb', line 37

def filename
  @filename
end

#linenoObject

Returns the value of attribute lineno.



36
37
38
# File 'lib/predicator/lexer.rex.rb', line 36

def lineno
  @lineno
end

#old_posObject

Returns the value of attribute old_pos.



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

def old_pos
  @old_pos
end

#ssObject Also known as: match

Returns the value of attribute ss.



38
39
40
# File 'lib/predicator/lexer.rex.rb', line 38

def ss
  @ss
end

#stateObject

Returns the value of attribute state.



39
40
41
# File 'lib/predicator/lexer.rex.rb', line 39

def state
  @state
end

Instance Method Details

#actionObject



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

def action
  yield
end

#columnObject



55
56
57
58
# File 'lib/predicator/lexer.rex.rb', line 55

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

#do_parseObject

def next_token



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

def do_parse; end

#locationObject



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

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

#matchesObject



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

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

#next_tokenObject



87
88
89
90
91
92
93
94
95
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
# File 'lib/predicator/lexer.rex.rb', line 87

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(/#{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



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

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

  do_parse
end

#parse_file(path) ⇒ Object



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

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

#scanner_classObject



60
61
62
# File 'lib/predicator/lexer.rex.rb', line 60

def scanner_class
  StringScanner
end