Module: Antelope::Ace::Grammar::Generation

Included in:
Antelope::Ace::Grammar
Defined in:
lib/antelope/ace/grammar/generation.rb

Overview

Handles the generation of output for the grammar.

Instance Method Summary collapse

Instance Method Details

#find_generators(generators, options) ⇒ Array<Generator> (private)

Find the corresponding generators. If the first argument isn't :guess, it returns the first argument. Otherwise, it tries to "intelligently guess" by checking the type from the options or the compiler. If it is unable to find the type, it will raise a NoTypeError.

Parameters:

  • generators (Symbol, Array<Generator>)
  • options (Hash)

Returns:

Raises:

  • (NoTypeError)

    if it could not determine the type of the generator.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/antelope/ace/grammar/generation.rb', line 61

def find_generators(generators, options)
  return generators unless generators == :guess

  generators = [Generator::Output]

  # command line precedence...
  type = options[:type] || options["type"] ||
    compiler.options.fetch(:type)

  generators << Generator.generators.fetch(type)

  generators

rescue KeyError => e
  raise NoTypeError, "Undefined type #{type}"
end

#generate(options = {}, generators = :guess, modifiers = DEFAULT_MODIFIERS) ⇒ void

This method returns an undefined value.

Generates the output. First, it runs through every given modifier, and instintates it. It then calls every modifier, turns it into a hash, and passes that hash to each of the given generators.

Parameters:

  • options (Hash) (defaults to: {})

    options.

  • generators (Array<Generator>) (defaults to: :guess)

    a list of generators to use in generation.

  • modifiers (Array<Array<(Symbol, #call)>>) (defaults to: DEFAULT_MODIFIERS)

    a list of modifiers to apply to the grammar.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/antelope/ace/grammar/generation.rb', line 30

def generate(options    = {},
             generators = :guess,
             modifiers  = DEFAULT_MODIFIERS)
  mods = modifiers.map(&:last).
    map  { |x| x.new(self) }
  mods.each do |mod|
    puts "Running mod #{mod.class}..."
    mod.call
  end
  hash = Hash[modifiers.map(&:first).zip(mods)]
  # This is when we'd generate

  find_generators(generators, options).each do |gen|
    puts "Running generator #{gen}..."
    gen.new(self, hash).generate
  end
end