Class: ComponentEmbeddedRuby::Parser::RootParser

Inherits:
Base
  • Object
show all
Defined in:
lib/component_embedded_ruby/parser/root_parser.rb

Overview

Internal: Used for parsing multiple adjacent tag, string, and emedded ruby into an array.

This parser is used to parse top-level documents and the children of ‘tag` nodes, which may have any combination of adjacent tag, string, and ruby nodes.

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from ComponentEmbeddedRuby::Parser::Base

Instance Method Details

#callObject



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
# File 'lib/component_embedded_ruby/parser/root_parser.rb', line 13

def call
  results = []

  while current_token
    case current_token.type
    when :open_carrot
      # If we run into a </ we are likely at the end of parsing a tag so
      # this should return and let the `TagParser` complete parsing
      #
      # e.g.
      # 1. <h1>Hello</h1> would start a `TagParser` after reading <h1
      # 2. `TagParser` reads <h1>, sees that it has
      #     children, and will use another instance of `RootParser` to reads its children
      # 3. The new RootParser reads `Hello`, then runs into `</`, so it should return `["Hello"]`
      #     and allow the `TagParser` to finish reading `</h1>`
      return results if peek_token.type == :slash

      results << TagParser.new(token_reader).call
    when :string, :identifier
      # If we're reading a string, or some other identifier that is on
      # its own, we can skip instantiating a new parser and parse it directly ourselves
      results << Node.new(nil, nil, current_token.value)
      token_reader.next
    when :ruby, :ruby_no_eval
      # If we run into Ruby code that should be evaluated inside of the
      # template, we want to create an `Eval`. The compliation step
      # handles `Eval` objects specially since it's making template
      # provided ruby code compatibile with the compiled template code.
      value = Eval.new(current_token.value, output: current_token.type == :ruby)

      results << Node.new(nil, nil, value)
      token_reader.next
    end
  end

  results
end