Class: JSONP3::Stream

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

Overview

Step through tokens

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Stream

Returns a new instance of Stream.



17
18
19
20
21
# File 'lib/json_p3/parser.rb', line 17

def initialize(tokens)
  @tokens = tokens
  @index = 0
  @eoi = tokens.last
end

Instance Method Details

#expect(token_type) ⇒ Object



37
38
39
40
41
42
# File 'lib/json_p3/parser.rb', line 37

def expect(token_type)
  return if peek.type == token_type

  token = self.next
  raise JSONPathSyntaxError.new("expected #{token_type}, found #{token.type}", token)
end

#expect_not(token_type, message) ⇒ Object



44
45
46
47
48
49
# File 'lib/json_p3/parser.rb', line 44

def expect_not(token_type, message)
  return unless peek.type == token_type

  token = self.next
  raise JSONPathSyntaxError.new(message, token)
end

#nextObject



23
24
25
26
27
28
29
# File 'lib/json_p3/parser.rb', line 23

def next
  token = @tokens.fetch(@index)
  @index += 1
  token
rescue IndexError
  @eor
end

#peekObject



31
32
33
34
35
# File 'lib/json_p3/parser.rb', line 31

def peek
  @tokens.fetch(@index)
rescue IndexError
  @eor
end

#to_sObject



51
52
53
# File 'lib/json_p3/parser.rb', line 51

def to_s
  "JSONP3::stream(head=#{peek.inspect})"
end