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.



44
45
46
47
48
49
50
51
52
# File 'lib/json_p3/lexer.rb', line 44

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

Instance Attribute Details

#bracket_stackObject (readonly)

Returns the value of attribute bracket_stack.



42
43
44
# File 'lib/json_p3/lexer.rb', line 42

def bracket_stack
  @bracket_stack
end

#tokensObject (readonly)

Returns the value of attribute tokens.



42
43
44
# File 'lib/json_p3/lexer.rb', line 42

def tokens
  @tokens
end

Class Method Details

.lex_string_factory(quote, state, token) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/json_p3/lexer.rb', line 396

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



54
55
56
57
# File 'lib/json_p3/lexer.rb', line 54

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