Class: Antelope::Generator::Base Abstract
- Inherits:
-
Object
- Object
- Antelope::Generator::Base
- Defined in:
- lib/antelope/generator/base.rb,
lib/antelope/generator/base/extra.rb,
lib/antelope/generator/base/coerce.rb
Overview
Subclass and redefine #generate to create a generator.
Generates a parser. This is normally the parent class, and the specific implementations inherit from this. The generated parser should, ideally, be completely independent (not requiring any external source code), as well as be under a permissive license.
Defined Under Namespace
Constant Summary collapse
- Boolean =
Object.new
Instance Attribute Summary collapse
-
#file ⇒ String
readonly
The file name (not including the extension) that the grammar should output to.
-
#grammar ⇒ Ace::Grammar
readonly
The grammar that the generator is for.
-
#mods ⇒ Hash<(Symbol, Object)>
readonly
The modifiers that were applied to the grammar.
Class Method Summary collapse
-
.directive(directive, type = nil) ⇒ void
(also: has_directives, has_directive)
Allows a directive for this generator.
-
.directives ⇒ Hash
The directives in the class.
-
.inherited(subclass) ⇒ void
Called by ruby on subclassing.
- .register_as(*names) ⇒ Object
-
.source_root ⇒ Pathname
The source root directory for templates.
Instance Method Summary collapse
-
#generate ⇒ void
Actually does the generation.
-
#initialize(grammar, mods) ⇒ Base
constructor
Initialize the generator.
-
#template(source, destination) {|content| ... } ⇒ void
Copies a template from the source, runs it through mote (in the context of this class), and then outputs it at the destination.
Methods included from Extra
Methods included from Coerce
#coerce_directive_class, #coerce_directive_value, #coerce_nested_hash, #directives
Constructor Details
#initialize(grammar, mods) ⇒ Base
Initialize the generator.
89 90 91 92 93 |
# File 'lib/antelope/generator/base.rb', line 89 def initialize(grammar, mods) @file = grammar.name @grammar = grammar @mods = mods end |
Instance Attribute Details
#file ⇒ String (readonly)
The file name (not including the extension) that the grammar should output to.
29 30 31 |
# File 'lib/antelope/generator/base.rb', line 29 def file @file end |
#grammar ⇒ Ace::Grammar (readonly)
The grammar that the generator is for.
34 35 36 |
# File 'lib/antelope/generator/base.rb', line 34 def grammar @grammar end |
#mods ⇒ Hash<(Symbol, Object)> (readonly)
The modifiers that were applied to the grammar.
23 24 25 |
# File 'lib/antelope/generator/base.rb', line 23 def mods @mods end |
Class Method Details
.directive(directive, type = nil) ⇒ void Also known as: has_directives, has_directive
This method returns an undefined value.
Allows a directive for this generator. This is checked in the compiler to allow the option. If the compiler encounters a bad directive, it'll error (to give the developer a warning).
67 68 69 70 |
# File 'lib/antelope/generator/base.rb', line 67 def self.directive(directive, type = nil) directive = directive.to_s directives[directive] = [self, type] end |
.directives ⇒ Hash
The directives in the class.
76 77 78 |
# File 'lib/antelope/generator/base.rb', line 76 def self.directives @_directives ||= {} end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Called by ruby on subclassing.
51 52 53 54 55 |
# File 'lib/antelope/generator/base.rb', line 51 def self.inherited(subclass) directives.each do |name, (_, type)| subclass.directive(name, type) end end |
.register_as(*names) ⇒ Object
43 44 45 |
# File 'lib/antelope/generator/base.rb', line 43 def self.register_as(*names) Generator.register_generator(self, *names) end |
.source_root ⇒ Pathname
The source root directory for templates. Overwrite to change.
39 40 41 |
# File 'lib/antelope/generator/base.rb', line 39 def self.source_root Pathname.new('../templates').(__FILE__) end |
Instance Method Details
#generate ⇒ void
This method returns an undefined value.
Actually does the generation. A subclass should implement this.
100 101 102 |
# File 'lib/antelope/generator/base.rb', line 100 def generate raise NotImplementedError end |
#template(source, destination) {|content| ... } ⇒ void
This method returns an undefined value.
Copies a template from the source, runs it through mote (in the context of this class), and then outputs it at the destination. If given a block, it will call the block after the template is run through erb with the content from erb; the result of the block is then used as the content instead.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/antelope/generator/base.rb', line 117 def template(source, destination) src = Pathname.new("#{source}.erb") .(self.class.source_root) template = ERB.new(src.read, nil, '-') content = template.result(instance_eval('binding')) block_given? && content = yield(content) dest = Pathname.new(destination).(grammar.output) dest.open('w') do |file| file.write(content) end end |