Class: Twig::TokenStream
- Inherits:
-
Object
- Object
- Twig::TokenStream
- Defined in:
- lib/twig/token_stream.rb
Instance Attribute Summary collapse
- #source ⇒ Source readonly
- #tokens ⇒ Array<Token> readonly
Instance Method Summary collapse
- #current ⇒ Token
- #debug ⇒ Object
- #eof? ⇒ Boolean
- #expect(type, value = nil, message = nil) ⇒ Token
-
#initialize(tokens, source) ⇒ TokenStream
constructor
A new instance of TokenStream.
- #inject(tokens) ⇒ Object
- #look(number = 1) ⇒ Token
- #next ⇒ Token
- #next_if(primary, secondary = nil) ⇒ Object
- #test(primary, secondary = nil) ⇒ Object
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
#source ⇒ Source (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, = 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} " if 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 |
#tokens ⇒ Array<Token> (readonly)
8 9 10 |
# File 'lib/twig/token_stream.rb', line 8 def tokens @tokens end |
Instance Method Details
#current ⇒ Token
33 34 35 |
# File 'lib/twig/token_stream.rb', line 33 def current tokens[@current] end |
#debug ⇒ Object
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, = 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} " if 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 |
#next ⇒ Token
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 |