module Antelope::Grammar::Loading::ClassMethods

Defines class methods on the grammar.

Public Instance Methods

from_ace_file(file_name) click to toggle source
# File lib/antelope/grammar/loading.rb, line 34
def from_ace_file(file_name)
  body   = File.read(file_name)
  output = File.dirname(file_name)
  name   = File.basename(file_name, ".ace")
  from_ace_string(name, output, body)
end
from_ace_string(name, output, string) click to toggle source

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

# File lib/antelope/grammar/loading.rb, line 68
def from_ace_string(name, output, string)
  scanner  = Ace::Scanner.scan(string, name)
  compiler = Ace::Compiler.compile(scanner)
  new(name, output, compiler)
end
from_dsl_file(file_name) click to toggle source
# File lib/antelope/grammar/loading.rb, line 28
def from_dsl_file(file_name)
  body   = File.read(file_name)
  output = File.dirname(file_name, File.extname(file_name))
  from_dsl_string(file_name, output, body)
end
from_dsl_string(file, output, string) click to toggle source

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 file [String] the path of the grammar. This is

used for eval.

@param output [String] the output directory. @param string [String] the grammar body. @return [Grammar] @see DSL::Compiler

# File lib/antelope/grammar/loading.rb, line 51
def from_dsl_string(file, output, string)
  eval(string, TOPLEVEL_BINDING, file, 0)
  grammar = Antelope.grammar
  compiler = DSL::Compiler.compile(grammar[1], &grammar[2])
  new(File.basename(file), output, compiler)
end
from_file(file_name) click to toggle source

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

# File lib/antelope/grammar/loading.rb, line 15
def from_file(file_name)
  ext = File.extname(file_name)
  case ext
  when ".rb", ".ate"
    from_dsl_file(file_name)
  when ".ace"
    from_ace_file(file_name)
  else
    raise ArgumentError, "Unexpected file extension #{ext},"\
      " expected one of .rb, .ate, or .ace"
  end
end