Sha256: bff051ab405e5154efa67b1451cb0ea728c09c7d77bd78aee9dd5b96d9e6ac30

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

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}_parser.rb" do |body|
          sprintf(grammar.compiler.body, :write => body)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
antelope-0.1.1 lib/antelope/generator/ruby.rb
antelope-0.1.0 lib/antelope/generator/ruby.rb
antelope-0.0.1 lib/antelope/generator/ruby.rb