Class: Treetop::Compiler::Grammar

Inherits:
Runtime::SyntaxNode show all
Defined in:
lib/treetop/compiler/node_classes/grammar.rb

Instance Attribute Summary

Attributes inherited from Runtime::SyntaxNode

#input, #interval, #parent

Instance Method Summary collapse

Methods inherited from Runtime::SyntaxNode

#<=>, #dot_id, #elements, #empty?, #extension_modules, #initialize, #inspect, #inspect_children, #inspect_self, #nonterminal?, #terminal?, #text_value, #write_dot, #write_dot_file

Constructor Details

This class inherits a constructor from Treetop::Runtime::SyntaxNode

Instance Method Details

#compileObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/treetop/compiler/node_classes/grammar.rb', line 4

def compile
  builder = RubyBuilder.new

  builder.module_declaration "#{grammar_name.text_value}" do
    builder.in(indent_level) # account for initial indentation of grammar declaration
    builder << "include Treetop::Runtime"
    builder.newline
    declaration_sequence.compile(builder)
    builder.newline
    builder.class_declaration "Parser < Treetop::Runtime::CompiledParser" do
      builder << "include #{grammar_name.text_value}"
    end
  end
  builder.newline
  builder << "(remove_const(:#{parser_name}) if const_defined?(:#{parser_name})) rescue nil; #{parser_name} = #{grammar_name.text_value}::Parser"
end

#indent_levelObject



21
22
23
# File 'lib/treetop/compiler/node_classes/grammar.rb', line 21

def indent_level
  input.column_of(interval.begin) - 1
end

#parser_nameObject



25
26
27
# File 'lib/treetop/compiler/node_classes/grammar.rb', line 25

def parser_name
  grammar_name.text_value + 'Parser'
end