Class: Faml::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



14
15
16
# File 'lib/faml/parser.rb', line 14

def initialize(options = {})
  @filename = options[:filename]
end

Instance Method Details

#call(template_str) ⇒ Object



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/faml/parser.rb', line 18

def call(template_str)
  @ast = Ast::Root.new
  @stack = []
  @line_parser = LineParser.new(@filename, template_str)
  @indent_tracker = IndentTracker.new(on_enter: method(:indent_enter), on_leave: method(:indent_leave))
  @filter_parser = FilterParser.new(@indent_tracker)

  while @line_parser.has_next?
    line = @line_parser.next_line
    if !@ast.is_a?(Ast::HamlComment) && @filter_parser.enabled?
      ast = @filter_parser.append(line)
      if ast
        @ast << ast
      end
    end
    unless @filter_parser.enabled?
      parse_line(line)
    end
  end

  ast = @filter_parser.finish
  if ast
    @ast << ast
  end
  @indent_tracker.finish
  @ast
rescue Error => e
  if @filename && e.lineno
    e.backtrace.unshift "#{@filename}:#{e.lineno}"
  end
  raise e
end