Class: Rhales::RueFormatParser
- Inherits:
-
Object
- Object
- Rhales::RueFormatParser
- Defined in:
- lib/rhales/parsers/rue_format_parser.rb
Overview
Hand-rolled recursive descent parser for .rue files
This parser implements .rue file parsing rules in Ruby code and produces an Abstract Syntax Tree (AST) for .rue file processing. It handles:
-
Section-based parsing: <data>, <template>, <logic>
-
Attribute extraction from section tags
-
Delegation to HandlebarsParser for template content
-
Validation of required sections
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.
File format structure: rue_file := section+ section := ‘<’ tag_name attributes? ‘>’ content ‘</’ tag_name ‘>’ tag_name := ‘data’ | ‘template’ | ‘logic’ attributes := attribute+ attribute := key ‘=’ quoted_value content := (text | handlebars_expression)* handlebars_expression := ‘expression ‘}’
Defined Under Namespace
Classes: Location, Node, ParseError
Constant Summary collapse
- REQUIRES_ONE_OF_SECTIONS =
At least one of these sections must be present
%w[data template].freeze
- KNOWN_SECTIONS =
%w[data template logic].freeze
- ALL_SECTIONS =
KNOWN_SECTIONS.freeze
- COMMENT_REGEX =
Regular expression to match HTML/XML comments outside of sections
/<!--.*?-->/m
Instance Attribute Summary collapse
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
Instance Method Summary collapse
-
#initialize(content, file_path = nil) ⇒ RueFormatParser
constructor
A new instance of RueFormatParser.
- #parse! ⇒ Object
- #sections ⇒ Object
Constructor Details
#initialize(content, file_path = nil) ⇒ RueFormatParser
Returns a new instance of RueFormatParser.
68 69 70 71 72 73 74 75 |
# File 'lib/rhales/parsers/rue_format_parser.rb', line 68 def initialize(content, file_path = nil) @content = preprocess_content(content) @file_path = file_path @position = 0 @line = 1 @column = 1 @ast = nil end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
83 84 85 |
# File 'lib/rhales/parsers/rue_format_parser.rb', line 83 def ast @ast end |
Instance Method Details
#parse! ⇒ Object
77 78 79 80 81 |
# File 'lib/rhales/parsers/rue_format_parser.rb', line 77 def parse! @ast = parse_rue_file validate_ast! self end |
#sections ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/rhales/parsers/rue_format_parser.rb', line 85 def sections return {} unless @ast @ast.children.each_with_object({}) do |section_node, sections| sections[section_node.value[:tag]] = section_node end end |