Class: Rhales::HandlebarsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rhales/parsers/handlebars_parser.rb

Overview

Hand-rolled recursive descent parser for Handlebars template syntax

This parser implements Handlebars parsing rules in Ruby code and produces an Abstract Syntax Tree (AST) for template processing. It handles:

  • Variable expressions: {variable}, {{raw}}

  • Block expressions: {{#if}{else}{/if}, {{#each}{/each}

  • Partials: partial_name}

  • Proper nesting and error reporting

  • Whitespace control (future)

Note: This class is a parser implementation, not a formal grammar definition. A formal grammar would be written in BNF/EBNF notation, while this class contains the actual parsing logic written in Ruby.

AST Node Types:

  • :template - Root template node

  • :text - Plain text content

  • :variable_expression - {variable} or {{variable}}

  • :if_block - {{#if}…{else}…{/if}

  • :unless_block - {{#unless}…{/unless}

  • :each_block - {{#each}…{/each}

  • :partial_expression - partial}

Defined Under Namespace

Classes: Location, Node, ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ HandlebarsParser

Returns a new instance of HandlebarsParser.



64
65
66
67
68
69
70
# File 'lib/rhales/parsers/handlebars_parser.rb', line 64

def initialize(content)
  @content  = content
  @position = 0
  @line     = 1
  @column   = 1
  @ast      = nil
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



62
63
64
# File 'lib/rhales/parsers/handlebars_parser.rb', line 62

def ast
  @ast
end

#contentObject (readonly)

Returns the value of attribute content.



62
63
64
# File 'lib/rhales/parsers/handlebars_parser.rb', line 62

def content
  @content
end

Instance Method Details

#blocksObject



89
90
91
92
93
# File 'lib/rhales/parsers/handlebars_parser.rb', line 89

def blocks
  return [] unless @ast

  collect_blocks(@ast)
end

#parse!Object



72
73
74
75
# File 'lib/rhales/parsers/handlebars_parser.rb', line 72

def parse!
  @ast = parse_template
  self
end

#partialsObject



83
84
85
86
87
# File 'lib/rhales/parsers/handlebars_parser.rb', line 83

def partials
  return [] unless @ast

  collect_partials(@ast)
end

#variablesObject



77
78
79
80
81
# File 'lib/rhales/parsers/handlebars_parser.rb', line 77

def variables
  return [] unless @ast

  collect_variables(@ast)
end