Class: ApiQueryLanguage::Filtering::ExpressionLexer

Inherits:
Object
  • Object
show all
Defined in:
lib/api_query_language/filtering/expression_lexer.rex.rb

Overview

The generated lexer ApiQueryLanguage::Filtering::ExpressionLexer

Defined Under Namespace

Classes: LexerError, ScanError

Constant Summary collapse

FIELD_NAME =

:stopdoc:

/[A-Za-z][0-9A-Za-z_\-.]+/
COMPARISON_OPERATOR =
/eq|ieq|neq|gt|gte|lt|lte/
WILDCARD =
/[*+]/
URL_ENCODED_VALUE =
/(?:[A-Za-z0-9\-_.~]|%[0-9A-Fa-f]{2})+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

The file name / path



29
30
31
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 29

def filename
  @filename
end

#ssObject Also known as: match

The StringScanner for this lexer.



34
35
36
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 34

def ss
  @ss
end

#stateObject

The current lexical state.



39
40
41
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 39

def state
  @state
end

Instance Method Details

#actionObject

Yields on the current action.



55
56
57
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 55

def action
  yield
end

#locationObject

The current location in the parse.



91
92
93
94
95
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 91

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

#matchesObject

The match groups for the current scan.



46
47
48
49
50
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 46

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

#next_tokenObject

Lex the next token.



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
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 100

def next_token
  token = nil

  until ss.eos? or token
    token =
      case state
      when nil
        if ss.skip(/(#{FIELD_NAME})\{(#{COMPARISON_OPERATOR})\}:/o)

          action { [:FIELD_WITH_COMPARISON_OP, matches] }
        elsif ss.skip(/(#{FIELD_NAME}):/o)

          action { [:FIELD, matches[0]] }
        elsif ss.skip(/null\((#{FIELD_NAME})\)/o)

          action { [:NULL_FIELD, matches[0]] }
        elsif ss.skip(/NULL\((#{FIELD_NAME})\)/o)

          action { [:NULL_FIELD, matches[0]] }
        elsif text = ss.scan(/#{URL_ENCODED_VALUE}/o)

          action { [:ENCODED_VALUE, text] }
        elsif text = ss.scan(/\[and\]/i)

          action { [:AND_CONDITION, text] }
        elsif text = ss.scan(/\[or\]/i)

          action { [:OR_CONDITION, text] }
        elsif text = ss.scan(/\[not\]/i)

          action { [:NOT_CONDITION, text] }
        elsif text = ss.scan("(")

          action { [:GROUP_START, text] }
        elsif text = ss.scan(")")

          action { [:GROUP_END, text] }
        elsif text = ss.scan("|")

          action { [:VALUE_OR, text] }
        elsif text = ss.scan("&")

          action { [:VALUE_AND, text] }
        elsif text = ss.scan(/#{WILDCARD}/o)

          action { [:VALUE_WILDCARD, text] }
        elsif ss.skip(/\s+/)

          # do nothing
        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, parser) ⇒ Object

def next_token



71
72
73
74
75
76
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 71

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

  do_parse
end

#parse_file(path) ⇒ Object

Read in and parse the file at path.



81
82
83
84
85
86
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 81

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

#scanner_classObject



63
64
65
# File 'lib/api_query_language/filtering/expression_lexer.rex.rb', line 63

def scanner_class
  StringScanner
end