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



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)



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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)
      expected = Token.type_to_english(type)
      unexpected = Token.type_to_english(token.type)
      token_value = token.value.empty? ? '' : " of value \"#{token.value}\""
      value = " with value \"#{value}\"" if value
      message = "#{message} " if message

      raise Error::Syntax.new(
        "#{message}Unexpected token \"#{unexpected}\"#{token_value} (\"#{expected}\" expected#{value}).",
        token.lineno,
        source
      )
    end

    self.next

    token
  end

  # @param [Integer] number
  # @return [Token]
  def look(number = 1)
    unless tokens.length >= @current + number
      raise Error::Syntax.new('Unexpected end of template.', tokens[@current].lineno, source)
    end

    tokens[@current + number]
  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})" }
  end

  # @param [Array<Token>] tokens
  def inject(tokens)
    @tokens.insert(@current, *tokens)
  end
end

#tokensArray<Token> (readonly)



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

def tokens
  @tokens
end

Instance Method Details

#currentToken



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

def current
  tokens[@current]
end

#debugObject



82
83
84
85
86
# File 'lib/twig/token_stream.rb', line 82

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

#eof?Boolean



78
79
80
# File 'lib/twig/token_stream.rb', line 78

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

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



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

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

  unless token.test(type, value)
    expected = Token.type_to_english(type)
    unexpected = Token.type_to_english(token.type)
    token_value = token.value.empty? ? '' : " of value \"#{token.value}\""
    value = " with value \"#{value}\"" if value
    message = "#{message} " if message

    raise Error::Syntax.new(
      "#{message}Unexpected token \"#{unexpected}\"#{token_value} (\"#{expected}\" expected#{value}).",
      token.lineno,
      source
    )
  end

  self.next

  token
end

#inject(tokens) ⇒ Object



89
90
91
# File 'lib/twig/token_stream.rb', line 89

def inject(tokens)
  @tokens.insert(@current, *tokens)
end

#look(number = 1) ⇒ Token



62
63
64
65
66
67
68
# File 'lib/twig/token_stream.rb', line 62

def look(number = 1)
  unless tokens.length >= @current + number
    raise Error::Syntax.new('Unexpected end of template.', tokens[@current].lineno, source)
  end

  tokens[@current + number]
end

#nextToken



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



74
75
76
# File 'lib/twig/token_stream.rb', line 74

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

#test(primary, secondary = nil) ⇒ Object



70
71
72
# File 'lib/twig/token_stream.rb', line 70

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