Class: NodeQueryLexer

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

Overview

The generated lexer NodeQueryLexer

Defined Under Namespace

Classes: LexerError, ScanError

Constant Summary collapse

OPEN_ATTRIBUTE =

:stopdoc:

/\[/
CLOSE_ATTRIBUTE =
/\]/
OPEN_ARRAY =
/\(/
CLOSE_ARRAY =
/\)/
OPEN_SELECTOR =
/\(/
CLOSE_SELECTOR =
/\)/
NODE_TYPE =
/\.[a-zA-Z_]+/
IDENTIFIER =
/[@\*\-\.\w]*\w/
IDENTIFIER_VALUE =
/[@\.\w!&:\?<>=]+/
OPERATOR =
/(\^=|\$=|\*=|!=|=~|!~|>=|<=|>|<|=|not includes|includes|not in|in)/i
FALSE =
/false/
FLOAT =
/\-?\d+\.\d+/
INTEGER =
/\-?\d+/
NIL =
/nil/
REGEXP_BODY =
/(?:[^\/]|\\\/)*/
REGEXP =
/\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
SYMBOL =
/:[\w!\?<>=]+/
TRUE =
/true/
SINGLE_QUOTE_STRING =
/'.*?'/
DOUBLE_QUOTE_STRING =
/".*?"/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNodeQueryLexer

def next_token



262
263
264
# File 'lib/node_query_lexer.rex.rb', line 262

def initialize
  @nested_count = 0
end

Instance Attribute Details

#filenameObject

The file name / path



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

def filename
  @filename
end

#ssObject Also known as: match

The StringScanner for this lexer.



51
52
53
# File 'lib/node_query_lexer.rex.rb', line 51

def ss
  @ss
end

#stateObject

The current lexical state.



56
57
58
# File 'lib/node_query_lexer.rex.rb', line 56

def state
  @state
end

Instance Method Details

#actionObject

Yields on the current action.



72
73
74
# File 'lib/node_query_lexer.rex.rb', line 72

def action
  yield
end

#do_parseObject



265
# File 'lib/node_query_lexer.rex.rb', line 265

def do_parse; end

#locationObject

The current location in the parse.



107
108
109
110
111
# File 'lib/node_query_lexer.rex.rb', line 107

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

#matchesObject

The match groups for the current scan.



63
64
65
66
67
# File 'lib/node_query_lexer.rex.rb', line 63

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.



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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/node_query_lexer.rex.rb', line 116

def next_token

  token = nil

  until ss.eos? or token do
    token =
      case state
      when nil then
        case
        when ss.skip(/\s+/) then
          # do nothing
        when text = ss.scan(/,/) then
          action { [:tCOMMA, text] }
        when text = ss.scan(/:first-child/) then
          action { [:tPOSITION, text[1..-1]] }
        when text = ss.scan(/:last-child/) then
          action { [:tPOSITION, text[1..-1]] }
        when text = ss.scan(/:has/) then
          action { [:tPSEUDO_CLASS, text[1..-1]] }
        when text = ss.scan(/:not_has/) then
          action { [:tPSEUDO_CLASS, text[1..-1]] }
        when text = ss.scan(/#{NODE_TYPE}/) then
          action { [:tNODE_TYPE, text[1..]] }
        when text = ss.scan(/#{IDENTIFIER}/) then
          action { [:tGOTO_SCOPE, text] }
        when text = ss.scan(/>/) then
          action { [:tRELATIONSHIP, text] }
        when text = ss.scan(/~/) then
          action { [:tRELATIONSHIP, text] }
        when text = ss.scan(/\+/) then
          action { [:tRELATIONSHIP, text] }
        when text = ss.scan(/#{OPEN_SELECTOR}/) then
          action { [:tOPEN_SELECTOR, text] }
        when text = ss.scan(/#{CLOSE_SELECTOR}/) then
          action { [:tCLOSE_SELECTOR, text] }
        when text = ss.scan(/#{OPEN_ATTRIBUTE}/) then
          action { @nested_count += 1; @state = :KEY; [:tOPEN_ATTRIBUTE, text] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :KEY then
        case
        when ss.skip(/\s+/) then
          # do nothing
        when text = ss.scan(/#{OPERATOR}/) then
          action { @state = :VALUE; [:tOPERATOR, text.downcase.sub(' ', '_')] }
        when text = ss.scan(/#{IDENTIFIER}/) then
          action { [:tKEY, text] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :VALUE then
        case
        when ss.skip(/\s+/) then
          # do nothing
        when text = ss.scan(/\[\]=/) then
          action { [:tIDENTIFIER_VALUE, text] }
        when text = ss.scan(/\[\]/) then
          action { [:tIDENTIFIER_VALUE, text] }
        when text = ss.scan(/:\[\]=/) then
          action { [:tSYMBOL, text[1..-1].to_sym] }
        when text = ss.scan(/:\[\]/) then
          action { [:tSYMBOL, text[1..-1].to_sym] }
        when text = ss.scan(/#{OPEN_ARRAY}/) then
          action { @state = :ARRAY_VALUE; [:tOPEN_ARRAY, text] }
        when text = ss.scan(/#{CLOSE_ATTRIBUTE}/) then
          action { @nested_count -= 1; @state = @nested_count == 0 ? nil : :VALUE; [:tCLOSE_ATTRIBUTE, text] }
        when text = ss.scan(/#{NIL}\?/) then
          action { [:tIDENTIFIER_VALUE, text] }
        when ss.skip(/#{NIL}/) then
          action { [:tNIL, nil] }
        when ss.skip(/#{TRUE}/) then
          action { [:tBOOLEAN, true] }
        when ss.skip(/#{FALSE}/) then
          action { [:tBOOLEAN, false] }
        when text = ss.scan(/#{SYMBOL}/) then
          action { [:tSYMBOL, text[1..-1].to_sym] }
        when text = ss.scan(/#{FLOAT}/) then
          action { [:tFLOAT, text.to_f] }
        when text = ss.scan(/#{INTEGER}/) then
          action { [:tINTEGER, text.to_i] }
        when text = ss.scan(/#{REGEXP}/) then
          action { [:tREGEXP, eval(text)] }
        when text = ss.scan(/#{DOUBLE_QUOTE_STRING}/) then
          action { [:tSTRING, text[1...-1]] }
        when text = ss.scan(/#{SINGLE_QUOTE_STRING}/) then
          action { [:tSTRING, text[1...-1]] }
        when text = ss.scan(/#{NODE_TYPE}/) then
          action { [:tNODE_TYPE, text[1..]] }
        when text = ss.scan(/#{OPEN_ATTRIBUTE}/) then
          action { @nested_count += 1; @state = :KEY; [:tOPEN_ATTRIBUTE, text] }
        when text = ss.scan(/#{IDENTIFIER_VALUE}/) then
          action { [:tIDENTIFIER_VALUE, text] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :ARRAY_VALUE then
        case
        when ss.skip(/\s+/) then
          # do nothing
        when text = ss.scan(/#{CLOSE_ARRAY}/) then
          action { @state = :VALUE; [:tCLOSE_ARRAY, text] }
        when text = ss.scan(/#{NIL}\?/) then
          action { [:tIDENTIFIER_VALUE, text] }
        when ss.skip(/#{NIL}/) then
          action { [:tNIL, nil] }
        when ss.skip(/#{TRUE}/) then
          action { [:tBOOLEAN, true] }
        when ss.skip(/#{FALSE}/) then
          action { [:tBOOLEAN, false] }
        when text = ss.scan(/#{SYMBOL}/) then
          action { [:tSYMBOL, text[1..-1].to_sym] }
        when text = ss.scan(/#{FLOAT}/) then
          action { [:tFLOAT, text.to_f] }
        when text = ss.scan(/#{INTEGER}/) then
          action { [:tINTEGER, text.to_i] }
        when text = ss.scan(/#{REGEXP}/) then
          action { [:tREGEXP, eval(text)] }
        when text = ss.scan(/#{DOUBLE_QUOTE_STRING}/) then
          action { [:tSTRING, text[1...-1]] }
        when text = ss.scan(/#{SINGLE_QUOTE_STRING}/) then
          action { [:tSTRING, text[1...-1]] }
        when text = ss.scan(/#{IDENTIFIER_VALUE}/) then
          action { [:tIDENTIFIER_VALUE, 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

Parse the given string.



87
88
89
90
91
92
# File 'lib/node_query_lexer.rex.rb', line 87

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.



97
98
99
100
101
102
# File 'lib/node_query_lexer.rex.rb', line 97

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

#scanner_classObject

The current scanner class. Must be overridden in subclasses.



80
81
82
# File 'lib/node_query_lexer.rex.rb', line 80

def scanner_class
  StringScanner
end