File: grammar.rb

package info (click to toggle)
ruby-treetop 1.6.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: ruby: 8,918; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Treetop
  module Compiler
    class Grammar < Runtime::SyntaxNode
      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 << "#{parser_name} = #{grammar_name.text_value}::Parser"
      end

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

      def parser_name
        grammar_name.text_value + 'Parser'
      end
    end
  end
end