Class: H2o::ArgumentLexer

Inherits:
Object show all
Defined in:
lib/h2o/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argstring, pos = 0) ⇒ ArgumentLexer

Returns a new instance of ArgumentLexer.



155
156
157
# File 'lib/h2o/parser.rb', line 155

def initialize(argstring, pos = 0)
  @argument = argstring
end

Class Method Details

.lex(argstring) ⇒ Object



159
160
161
# File 'lib/h2o/parser.rb', line 159

def self.lex(argstring)
  new(argstring).lexer()
end

Instance Method Details

#lexerObject



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
# File 'lib/h2o/parser.rb', line 163

def lexer
  s = StringScanner.new(@argument)
  state = :initial
  result = []
  while ! s.eos?
    next if s.scan(WHITESPACE_RE)
    
    if state == :initial
      if match = s.scan(OPERATOR_RE)
        result << [:operator, match]
      elsif match = s.scan(BOOLEAN_RE)
        result << [:boolean, match]
      elsif match = s.scan(NIL_RE)
        result << [:nil, match]
      elsif match = s.scan(NAMED_ARGS_RE)
        result << [:named_argument, match]
      elsif match = s.scan(NAME_RE)
        result << [:name, match]
      elsif match = s.scan(PIPE_RE)
        state = :filter
        result << [:filter_start, nil]
      elsif match = s.scan(SEPERATOR_RE)
        result << [:seperator, nil]
      elsif match = s.scan(STRING_RE)
        result << [:string, match]
      elsif match = s.scan(NUMBER_RE)
        result << [:number, match]
      else 
        raise SyntaxError, "unexpected character #{s.getch} in tag"
      end
    elsif state == :filter
      if match = s.scan(PIPE_RE)
        result << [:filter_end, nil]
        result << [:filter_start, nil]
      elsif match = s.scan(SEPERATOR_RE)
        result << [:seperator, nil]
      elsif match = s.scan(FILTER_END_RE)
        result << [:filter_end, nil]
        state = :initial
      elsif match = s.scan(BOOLEAN_RE)
        result << [:boolean, match]
      elsif match = s.scan(NIL_RE)
        result << [:nil, match]
      elsif match = s.scan(NAMED_ARGS_RE)
        result << [:named_argument, match]
      elsif match = s.scan(NAME_RE)
        result << [:name, match]
      elsif match = s.scan(STRING_RE)
        result << [:string, match]
      elsif match = s.scan(NUMBER_RE)
        result << [:number, match]
      else 
        raise SyntaxError, "unexpected character #{s.getch} in filter"
      end
    end
  end
  result << [:filter_end, nil]  if state == :filter

  result
end