Class: DMark::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dmark/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser



3
4
5
6
7
# File 'lib/dmark/parser.rb', line 3

def initialize(tokens)
  @tokens = tokens

  @root_node = DMark::Nodes::RootNode.new
end

Instance Method Details

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dmark/parser.rb', line 9

def run
  node_stack = [@root_node]

  @tokens.each do |token|
    case token
    when DMark::Tokens::TextToken
      node_stack.last.children << DMark::Nodes::TextNode.new(text: token.text)
    when DMark::Tokens::TagBeginToken
      new_node = DMark::Nodes::ElementNode.new(name: token.name, attributes: token.attributes)
      node_stack.last.children << new_node
      node_stack.push(new_node)
    when DMark::Tokens::TagEndToken
      node_stack.pop
    end
  end

  @root_node
end