Class: Liquidice::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strict_mode: false) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
# File 'lib/liquidice/parser.rb', line 7

def initialize(strict_mode: false)
  @strict_mode = strict_mode
  @parser = LiquidiceGrammarParser.new
  @transformer = Liquidice::Transformer::Transformer.new
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



5
6
7
# File 'lib/liquidice/parser.rb', line 5

def ast
  @ast
end

#parserObject

Returns the value of attribute parser.



5
6
7
# File 'lib/liquidice/parser.rb', line 5

def parser
  @parser
end

#strict_modeObject

Returns the value of attribute strict_mode.



5
6
7
# File 'lib/liquidice/parser.rb', line 5

def strict_mode
  @strict_mode
end

#transformed_astObject

Returns the value of attribute transformed_ast.



5
6
7
# File 'lib/liquidice/parser.rb', line 5

def transformed_ast
  @transformed_ast
end

#transformerObject

Returns the value of attribute transformer.



5
6
7
# File 'lib/liquidice/parser.rb', line 5

def transformer
  @transformer
end

Instance Method Details

#parse(wysiwyg_template) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/liquidice/parser.rb', line 13

def parse(wysiwyg_template)
  @ast = parser.parse(wysiwyg_template)

  raise Liquidice::Errors::InvalidSyntax, parser.failure_reason if @ast.nil? && strict_mode

  @ast
end

#parse_and_transform(wysiwyg_template) ⇒ Object



25
26
27
28
29
# File 'lib/liquidice/parser.rb', line 25

def parse_and_transform(wysiwyg_template)
  parse(wysiwyg_template)
  transformer_tree.transform!
  transformer_tree.to_s
end

#transformer_treeObject



21
22
23
# File 'lib/liquidice/parser.rb', line 21

def transformer_tree
  @transformer_tree ||= transformer.apply(ast)
end