Module: Antelope::Grammar::Loading::ClassMethods

Defined in:
lib/antelope/grammar/loading.rb

Overview

Defines class methods on the grammar.

Instance Method Summary collapse

Instance Method Details

#from_ace_file(file_name) ⇒ Object



34
35
36
37
38
39
# 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)
  from_ace_string(name, output, body)
end

#from_ace_string(name, output, string) ⇒ Grammar

Loads a grammar from a string. First runs the scanner and compiler over the string, and then instantiates a new Grammar from the resultant.

Parameters:

  • name (String)

    the name of the grammar.

  • output (String)

    the output directory.

  • string (String)

    the grammar body.

Returns:

See Also:



68
69
70
71
72
# 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) ⇒ Object



28
29
30
31
32
# File 'lib/antelope/grammar/loading.rb', line 28

def from_dsl_file(file_name)
  body   = File.read(file_name)
  output = File.dirname(file_name)
  from_dsl_string(file_name, output, body)
end

#from_dsl_string(file, output, string) ⇒ Grammar

Loads a grammar from a string. First runs the scanner and compiler over the string, and then instantiates a new Grammar from the resultant.

Parameters:

  • file (String)

    the path of the grammar. This is used for eval.

  • output (String)

    the output directory.

  • string (String)

    the grammar body.

Returns:

See Also:



51
52
53
54
55
56
# 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) ⇒ Grammar

Loads a grammar from a file. Assumes the output directory and name from the file name.

Parameters:

  • file_name (String)

    the file name.

Returns:

See Also:

  • #from_string


15
16
17
18
19
20
21
22
23
24
25
26
# 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