class Curly::Parser

Public Class Methods

new(tokens) click to toggle source
# File lib/curly/parser.rb, line 104
def initialize(tokens)
  @tokens = tokens
  @root = Root.new
  @stack = [@root]
end
parse(tokens) click to toggle source
# File lib/curly/parser.rb, line 100
def self.parse(tokens)
  new(tokens).parse
end

Public Instance Methods

parse() click to toggle source
# File lib/curly/parser.rb, line 110
def parse
  @tokens.each do |token, *args|
    send("parse_#{token}", *args)
  end

  unless @stack.size == 1
    raise Curly::IncompleteBlockError,
      "block `#{@stack.last}` is not closed"
  end

  @root.nodes
end

Private Instance Methods

parse_block(type, *args) click to toggle source
# File lib/curly/parser.rb, line 162
def parse_block(type, *args)
  component = Component.new(*args)

  component.contexts.each do |context|
    parse_context_block_start(context)
  end

  block = Block.new(type, component)
  tree << block
  @stack.push(block)
end
parse_block_end(*args) click to toggle source
# File lib/curly/parser.rb, line 174
def parse_block_end(*args)
  component = Component.new(*args)
  block = @stack.pop

  unless block.closed_by?(component)
    raise Curly::IncorrectEndingError,
      "block `#{block}` cannot be closed by `#{component}`"
  end

  component.contexts.reverse.each do |context|
    parse_block_end(context)
  end
end
parse_collection_block_start(*args) click to toggle source
# File lib/curly/parser.rb, line 154
def parse_collection_block_start(*args)
  parse_block(:collection, *args)
end
parse_comment(comment) click to toggle source
# File lib/curly/parser.rb, line 188
def parse_comment(comment)
  tree << Comment.new(comment)
end
parse_component(*args) click to toggle source
# File lib/curly/parser.rb, line 129
def parse_component(*args)
  component = Component.new(*args)

  # If the component is namespaced by a list of context names, open a context
  # block for each.
  component.contexts.each do |context|
    parse_context_block_start(context)
  end

  tree << component

  # Close each context block in the namespace.
  component.contexts.reverse.each do |context|
    parse_block_end(context)
  end
end
parse_conditional_block_start(*args) click to toggle source
# File lib/curly/parser.rb, line 146
def parse_conditional_block_start(*args)
  parse_block(:conditional, *args)
end
parse_context_block_start(*args) click to toggle source
# File lib/curly/parser.rb, line 158
def parse_context_block_start(*args)
  parse_block(:context, *args)
end
parse_inverse_conditional_block_start(*args) click to toggle source
# File lib/curly/parser.rb, line 150
def parse_inverse_conditional_block_start(*args)
  parse_block(:inverse_conditional, *args)
end
parse_text(value) click to toggle source
# File lib/curly/parser.rb, line 125
def parse_text(value)
  tree << Text.new(value)
end
tree() click to toggle source
# File lib/curly/parser.rb, line 192
def tree
  @stack.last
end