Sha256: 30dd9c7e82aa774173688426a8fda3aa8a50bf5e19815d8a200e0ace5e4b6886

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8

require "pp"

module Antelope
  class Generator

    # Generates a ruby parser.
    class Ruby < Generator

      # Creates an action table for the parser.
      #
      # @return [String]
      def generate_action_table
        out = ""
        PP.pp(mods[:tableizer].table, out)
        out
      end

      # Outputs an array of all of the productions.
      #
      # @return [String]
      def generate_productions_list
        out = "["

        grammar.all_productions.each do |production|
          out                              <<
            "["                            <<
            production.label.name.inspect  <<
            ", "                           <<
            production.items.size.inspect  <<
            ", "

          block = if production.block.empty?
            "proc { |_| _ }"
          else
            "proc #{production.block}"
          end

          out << block << "],\n"
        end

        out.chomp!(",\n")

        out << "]"
      end

      # Actually performs the generation.  Takes the template from
      # ruby.erb and outputs it to `<file>_parser.rb`.
      #
      # @return [void]
      def generate
        template "ruby.erb", "#{file}.rb" do |body|
          sprintf(grammar.compiler.body, :write => body)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
antelope-0.1.7 lib/antelope/generator/ruby.rb
antelope-0.1.6 lib/antelope/generator/ruby.rb
antelope-0.1.5 lib/antelope/generator/ruby.rb
antelope-0.1.4 lib/antelope/generator/ruby.rb