Class: JSONP3::Lexer

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

Overview

JSONPath query expression lexical scanner.

See Also:

Constant Summary collapse

RE_INT =
/-?[0-9]+/
RE_NAME =
/[\u0080-\uFFFFa-zA-Z_][\u0080-\uFFFFa-zA-Z0-9_-]*/
RE_WHITESPACE =
/[ \n\r\t]+/
S_ESCAPES =
Set["b", "f", "n", "r", "t", "u", "/", "\\"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Lexer

Returns a new instance of Lexer.



38
39
40
41
42
43
44
45
# File 'lib/json_p3/lexer.rb', line 38

def initialize(query)
  @filter_depth = 0
  @paren_stack = []
  @tokens = []
  @start = 0
  @query = query.freeze
  @scanner = StringScanner.new(query)
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Class Method Details

.lex_string_factory(quote, state, token) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/json_p3/lexer.rb', line 375

def lex_string_factory(quote, state, token)
  proc {
    # @type self: Lexer
    ignore # move past opening quote

    loop do
      c = self.next

      case c
      when ""
        error "unclosed string starting at index #{@start}"
        return nil
      when "\\"
        peeked = peek
        if S_ESCAPES.member?(peeked) || peeked == quote
          self.next
        else
          error "invalid escape"
          return nil
        end
      when quote
        backup
        emit(token)
        self.next
        ignore # move past closing quote
        return state
      end
    end
  }
end

Instance Method Details

#runObject



47
48
49
50
# File 'lib/json_p3/lexer.rb', line 47

def run
  state = :lex_root
  state = send(state) until state.nil?
end