Class: Liquid::Parser

### Home Classes Methods

### In Files

- lib/liquid/parser.rb

### Parent

Object

### Methods

- ::new
- #argument
- #consume
- #consume?
- #expression
- #id?
- #jump
- #look
- #variable_signature

### Class/Module Index

Quicksearch

- Liquid
- Liquid::ArgumentError
- Liquid::Assign
- Liquid::BlankFileSystem
- Liquid::Block
- Liquid::Break
- Liquid::BreakInterrupt
- Liquid::Capture
- Liquid::Case
- Liquid::Comment
- Liquid::Context
- Liquid::ContextError
- Liquid::Continue
- Liquid::ContinueInterrupt
- Liquid::Cycle
- Liquid::Decrement
- Liquid::Document
- Liquid::Drop
- Liquid::ElseCondition
- Liquid::Error
- Liquid::FileSystemError
- Liquid::FilterNotFound
- Liquid::For
- Liquid::I18n
- Liquid::I18n::TranslationError
- Liquid::If
- Liquid::Ifchanged
- Liquid::Include
- Liquid::Increment
- Liquid::Interrupt
- Liquid::Lexer
- Liquid::LocalFileSystem
- Liquid::MemoryError
- Liquid::Parser
- Liquid::Raw
- Liquid::StackLevelError
- Liquid::StandardError
- Liquid::StandardFilters
- Liquid::SyntaxError
- Liquid::TableRow
- Liquid::Tag
- Liquid::Template
- Liquid::Unless
- Liquid::Utils
- Liquid::Variable
- FalseClass
- LiquidView
- Module
- NilClass
- TrueClass

No matching classes.

# Liquid::Parser

### Public Class Methods

new(input) click to toggle source

    # File lib/liquid/parser.rb, line 3
    def initialize(input)
      l = Lexer.new(input)
      @tokens = l.tokenize
      @p = 0 # pointer to current location
    end

### Public Instance Methods

argument() click to toggle source

    # File lib/liquid/parser.rb, line 66
    def argument
      str = ""
      # might be a keyword argument (identifier: expression)
      if look(:id) && look(:colon, 1)
        str << consume << consume << ' '
      end

      str << expression
    end

consume(type = nil) click to toggle source

    # File lib/liquid/parser.rb, line 13
    def consume(type = nil)
      token = @tokens[@p]
      if type && token[0] != type
        raise SyntaxError, "Expected #{type} but found #{@tokens[@p].first}"
      end
      @p += 1
      token[1]
    end

consume?(type) click to toggle source

Only consumes the token if it matches the type Returns the token's contents if it was consumed or false otherwise.

    # File lib/liquid/parser.rb, line 25
    def consume?(type)
      token = @tokens[@p]
      return false unless token && token[0] == type
      @p += 1
      token[1]
    end

expression() click to toggle source

    # File lib/liquid/parser.rb, line 47
    def expression
      token = @tokens[@p]
      if token[0] == :id
        variable_signature
      elsif [:string, :number].include? token[0]
        consume
      elsif token.first == :open_round
        consume
        first = expression
        consume(:dot)
        consume(:dot)
        last = expression
        consume(:close_round)
        "(#{first}..#{last})"
      else
        raise SyntaxError, "#{token} is not a valid expression"
      end
    end

id?(str) click to toggle source

Like consume? Except for an :id token of a certain name

    # File lib/liquid/parser.rb, line 33
    def id?(str)
      token = @tokens[@p]
      return false unless token && token[0] == :id
      return false unless token[1] == str
      @p += 1
      token[1]
    end

jump(point) click to toggle source

    # File lib/liquid/parser.rb, line 9
    def jump(point)
      @p = point
    end

look(type, ahead = 0) click to toggle source

    # File lib/liquid/parser.rb, line 41
    def look(type, ahead = 0)
      tok = @tokens[@p + ahead]
      return false unless tok
      tok[0] == type
    end

variable_signature() click to toggle source

    # File lib/liquid/parser.rb, line 76
    def variable_signature
      str = consume(:id)
      if look(:open_square)
        str << consume
        str << expression
        str << consume(:close_square)
      end
      if look(:dot)
        str << consume
        str << variable_signature
      end
      str
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.