Class: Twig::TokenStream

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/token_stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, source) ⇒ TokenStream

Returns a new instance of TokenStream.

Parameters:



15
16
17
18
19
# File 'lib/twig/token_stream.rb', line 15

def initialize(tokens, source)
  @tokens = tokens
  @source = source
  @current = 0
end

Instance Attribute Details

#sourceSource (readonly)

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twig/token_stream.rb', line 6

class TokenStream
  # @return [Array<Token>]
  attr_reader :tokens

  # @return [Source]
  attr_reader :source

  # @param [Array<Token>] tokens
  # @param [Source] source
  def initialize(tokens, source)
    @tokens = tokens
    @source = source
    @current = 0
  end

  # @return [Token]
  def next
    @current += 1

    unless tokens[@current]
      raise Error::Syntax.new('Unexpected end of template.', tokens[@current - 1].lineno, source)
    end

    tokens[@current - 1]
  end

  # @return [Token]
  def current
    tokens[@current]
  end

  # @return [Token]
  def expect(type, value = nil, message = nil)
    token = current

    unless token.test(type, value)
      raise Error::Syntax.new(
        "Expected #{type}(#{value}) but got #{token.type}(#{token.value}) #{message}".rstrip,
        token.lineno,
        source
      )
    end

    self.next

    token
  end

  def test(primary, secondary = nil)
    current.test(primary, secondary)
  end

  def next_if(primary, secondary = nil)
    current.test(primary, secondary) ? self.next : nil
  end

  def eof?
    current.type == Token::EOF_TYPE
  end

  def debug
    tokens.
      map(&:debug).
      map { |type, value| "#{type}(#{value})" }.
      join("\n")
  end
end

#tokensArray<Token> (readonly)

Returns:



8
9
10
# File 'lib/twig/token_stream.rb', line 8

def tokens
  @tokens
end

Instance Method Details

#currentToken

Returns:



33
34
35
# File 'lib/twig/token_stream.rb', line 33

def current
  tokens[@current]
end

#debugObject



66
67
68
69
70
71
# File 'lib/twig/token_stream.rb', line 66

def debug
  tokens.
    map(&:debug).
    map { |type, value| "#{type}(#{value})" }.
    join("\n")
end

#eof?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/twig/token_stream.rb', line 62

def eof?
  current.type == Token::EOF_TYPE
end

#expect(type, value = nil, message = nil) ⇒ Token

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/twig/token_stream.rb', line 38

def expect(type, value = nil, message = nil)
  token = current

  unless token.test(type, value)
    raise Error::Syntax.new(
      "Expected #{type}(#{value}) but got #{token.type}(#{token.value}) #{message}".rstrip,
      token.lineno,
      source
    )
  end

  self.next

  token
end

#nextToken

Returns:



22
23
24
25
26
27
28
29
30
# File 'lib/twig/token_stream.rb', line 22

def next
  @current += 1

  unless tokens[@current]
    raise Error::Syntax.new('Unexpected end of template.', tokens[@current - 1].lineno, source)
  end

  tokens[@current - 1]
end

#next_if(primary, secondary = nil) ⇒ Object



58
59
60
# File 'lib/twig/token_stream.rb', line 58

def next_if(primary, secondary = nil)
  current.test(primary, secondary) ? self.next : nil
end

#test(primary, secondary = nil) ⇒ Object



54
55
56
# File 'lib/twig/token_stream.rb', line 54

def test(primary, secondary = nil)
  current.test(primary, secondary)
end