Sha256: 68c8f21b01e83a1152fc4984d0713675cf4179b22c8d60d78936c321137eafe5

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8

module Antelope
  module Ace
    class Grammar

      # Handles loading to and from files and strings.
      module Loading

        # Defines class methods on the grammar.
        module ClassMethods

          # Loads a grammar from a file.  Assumes the output
          # directory and name from the file name.
          #
          # @param file_name [String] the file name.
          # @return [Grammar]
          # @see #from_string
          def from_file(file_name)
            body     = File.read(file_name)
            output   = File.dirname(file_name)
            name     = File.basename(file_name).gsub(/\.[A-Za-z]+/, "")
            from_string(name, output, body)
          end

          # Loads a grammar from a string.  First runs the scanner and
          # compiler over the string, and then instantiates a new
          # Grammar from the resultant.
          #
          # @param name [String] the name of the grammar.
          # @param output [String] the output directory.
          # @param string [String] the grammar body.
          # @return [Grammar]
          # @see Ace::Scanner
          # @see Ace::Compiler
          def from_string(name, output, string)
            scanner  = Ace::Scanner.scan(string, name)
            compiler = Ace::Compiler.compile(scanner)
            new(name, output, compiler)
          end
        end

        # Extends the grammar with the class methods.
        #
        # @param receiver [Grammar]
        # @see ClassMethods
        def self.included(receiver)
          receiver.extend ClassMethods
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
antelope-0.2.0 lib/antelope/ace/grammar/loading.rb
antelope-0.1.11 lib/antelope/ace/grammar/loading.rb
antelope-0.1.10 lib/antelope/ace/grammar/loading.rb
antelope-0.1.9 lib/antelope/ace/grammar/loading.rb
antelope-0.1.8 lib/antelope/ace/grammar/loading.rb