Class: Rng::ParseTreeProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rng/parse_tree_processor.rb

Overview

Normalizes parse tree structure into consistent grammar format

Handles three different RNC file structures:

  1. Top-level includes (Metanorma-style)

  2. Grammar block wrapper

  3. Flat grammar

Examples:

Basic Usage

tree = parser.parse(rnc_content)
processor = ParseTreeProcessor.new(tree)
normalized = processor.normalize
grammar_tree = normalized.grammar_tree
namespace = normalized.namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ ParseTreeProcessor

Initialize with parse tree

Parameters:

  • tree (Hash)

    Raw parse tree from Parslet parser



24
25
26
27
28
29
# File 'lib/rng/parse_tree_processor.rb', line 24

def initialize(tree)
  @tree = tree
  @namespace = nil
  @preamble = nil # NEW: SchemaPreamble object
  @grammar_tree = nil
end

Instance Attribute Details

#grammar_treeObject (readonly)

Returns the value of attribute grammar_tree.



19
20
21
# File 'lib/rng/parse_tree_processor.rb', line 19

def grammar_tree
  @grammar_tree
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



19
20
21
# File 'lib/rng/parse_tree_processor.rb', line 19

def namespace
  @namespace
end

#preambleObject (readonly)

Returns the value of attribute preamble.



19
20
21
# File 'lib/rng/parse_tree_processor.rb', line 19

def preamble
  @preamble
end

#treeObject (readonly)

Returns the value of attribute tree.



19
20
21
# File 'lib/rng/parse_tree_processor.rb', line 19

def tree
  @tree
end

Instance Method Details

#normalizeself

Normalize the parse tree

Extracts namespace and builds consistent grammar structure regardless of input format. Processes raw override blocks.

Returns:

  • (self)

    Returns self for chaining



37
38
39
40
41
42
43
44
# File 'lib/rng/parse_tree_processor.rb', line 37

def normalize
  @preamble = extract_preamble_section # NEW: Extract preamble first
  @namespace = extract_namespace         # KEEP: Legacy namespace extraction
  @grammar_tree = build_grammar_tree
  process_raw_overrides!(@grammar_tree)
                  # MODIFIED: Add both old and new metadata
  self
end