Class: Cog::DSL::LanguageDSL
- Inherits:
-
Object
- Object
- Cog::DSL::LanguageDSL
- Defined in:
- lib/cog/dsl/language_dsl.rb
Overview
DSL for defining a language
Instance Method Summary collapse
-
#comment(prefix) ⇒ nil
Define single line comment notation.
-
#comment_style(lang_key) ⇒ nil
Borrow comment notation from another language.
-
#extension(*values) ⇒ nil
Define file extensions.
-
#finalize ⇒ Cog::Language
Compute the comment pattern.
-
#include_guard_begin {|name| ... } ⇒ nil
Define a block to call when beginning an include guard in this language.
-
#include_guard_end {|name| ... } ⇒ nil
Define a block to call when ending an include guard in this language.
-
#include_guard_style(lang_key) ⇒ nil
Borrow include guard notation from another language.
-
#initialize(key) ⇒ LanguageDSL
constructor
A new instance of LanguageDSL.
-
#map_boolean(ident) {|obj| ... } ⇒ nil
Map the cog boolean type to a native type in this language.
-
#map_char(ident) {|obj| ... } ⇒ nil
Map the cog char type to a native type in this language.
-
#map_double(ident) {|obj| ... } ⇒ nil
Map the cog double type to a native type in this language.
-
#map_float(ident) {|obj| ... } ⇒ nil
Map the cog float type to a native type in this language.
-
#map_integer(ident) {|obj| ... } ⇒ nil
Map the cog integer type to a native type in this language.
-
#map_long(ident) {|obj| ... } ⇒ nil
Map the cog long type to a native type in this language.
-
#map_null(ident) {|obj| ... } ⇒ nil
Map the cog null type to a native type in this language.
-
#map_primitive(name, ident) {|obj| ... } ⇒ nil
Map a cog primitive type to a native type in this language.
-
#map_string(ident) {|obj| ... } ⇒ nil
Map the cog string type to a native type in this language.
- #map_void(ident) ⇒ Object
-
#multiline_comment(prefix, postfix) ⇒ nil
Define multi-line comment notation.
-
#name(value) ⇒ nil
Define a readable name for the language.
-
#named_scope_begin {|name| ... } ⇒ nil
Define a block to call when beginning a named scope in this language.
-
#named_scope_end {|name| ... } ⇒ nil
Define a block to call when ending a named scope in this language.
-
#reserved(words) ⇒ nil
Enumerate reserved identifiers in the language.
- #seed_extension(ext, opt = {}) ⇒ Object
-
#use_named_scope {|name| ... } ⇒ nil
Define a block to call when using a named scopes in this language.
Constructor Details
#initialize(key) ⇒ LanguageDSL
Returns a new instance of LanguageDSL.
9 10 11 |
# File 'lib/cog/dsl/language_dsl.rb', line 9 def initialize(key) @lang = Cog::Language.new key end |
Instance Method Details
#comment(prefix) ⇒ nil
Define single line comment notation
24 25 26 27 |
# File 'lib/cog/dsl/language_dsl.rb', line 24 def comment(prefix) lang_eval { @comment_prefix = prefix } nil end |
#comment_style(lang_key) ⇒ nil
Borrow comment notation from another language
44 45 46 47 |
# File 'lib/cog/dsl/language_dsl.rb', line 44 def comment_style(lang_key) lang_eval { @comment_style = lang_key.to_s.downcase } nil end |
#extension(*values) ⇒ nil
Define file extensions
52 53 54 55 56 |
# File 'lib/cog/dsl/language_dsl.rb', line 52 def extension(*values) lang_eval do @extensions = values.collect {|key| key.to_s.downcase} end end |
#finalize ⇒ Cog::Language
Compute the comment pattern
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/cog/dsl/language_dsl.rb', line 201 def finalize pattern = /[*]/ esc = lambda do |x| x.gsub(pattern) {|match| "\\#{match}"} end lang_eval do @include_guard_style ||= key @comment_style ||= key @comment_pattern = if @comment_prefix && @multiline_comment_prefix '^\s*(?:%s|%s)\s*%%s\s*(?:%s)?\s*$' % [@comment_prefix, @multiline_comment_prefix, @multiline_comment_postfix].collect(&esc) elsif @comment_prefix '^\s*%s\s*%%s\s*$' % esc.call(@comment_prefix) elsif @multiline_comment_prefix '^\s*%s\s*%%s\s*%s\s*$' % [@multiline_comment_prefix, @multiline_comment_postfix].collect(&esc) else '^\s*%s\s*$' end end @lang end |
#include_guard_begin {|name| ... } ⇒ nil
Define a block to call when beginning an include guard in this language
96 97 98 99 |
# File 'lib/cog/dsl/language_dsl.rb', line 96 def include_guard_begin(&block) lang_eval { @include_guard_begin_block = block } nil end |
#include_guard_end {|name| ... } ⇒ nil
Define a block to call when ending an include guard in this language
105 106 107 108 |
# File 'lib/cog/dsl/language_dsl.rb', line 105 def include_guard_end(&block) lang_eval { @include_guard_end_block = block } nil end |
#include_guard_style(lang_key) ⇒ nil
Borrow include guard notation from another language
113 114 115 116 |
# File 'lib/cog/dsl/language_dsl.rb', line 113 def include_guard_style(lang_key) lang_eval { @include_guard_style = lang_key.to_s.downcase } nil end |
#map_boolean(ident) {|obj| ... } ⇒ nil
Map the cog boolean type to a native type in this language
145 |
# File 'lib/cog/dsl/language_dsl.rb', line 145 def map_boolean(ident, &block) ; map_primitive(:boolean, ident, &block) ; end |
#map_char(ident) {|obj| ... } ⇒ nil
Map the cog char type to a native type in this language
180 |
# File 'lib/cog/dsl/language_dsl.rb', line 180 def map_char(ident, &block) ; map_primitive(:char, ident, &block) ; end |
#map_double(ident) {|obj| ... } ⇒ nil
Map the cog double type to a native type in this language
173 |
# File 'lib/cog/dsl/language_dsl.rb', line 173 def map_double(ident, &block) ; map_primitive(:double, ident, &block) ; end |
#map_float(ident) {|obj| ... } ⇒ nil
Map the cog float type to a native type in this language
166 |
# File 'lib/cog/dsl/language_dsl.rb', line 166 def map_float(ident, &block) ; map_primitive(:float, ident, &block) ; end |
#map_integer(ident) {|obj| ... } ⇒ nil
Map the cog integer type to a native type in this language
152 |
# File 'lib/cog/dsl/language_dsl.rb', line 152 def map_integer(ident, &block) ; map_primitive(:integer, ident, &block) ; end |
#map_long(ident) {|obj| ... } ⇒ nil
Map the cog long type to a native type in this language
159 |
# File 'lib/cog/dsl/language_dsl.rb', line 159 def map_long(ident, &block) ; map_primitive(:long, ident, &block) ; end |
#map_null(ident) {|obj| ... } ⇒ nil
Map the cog null type to a native type in this language
194 |
# File 'lib/cog/dsl/language_dsl.rb', line 194 def map_null(ident, &block) ; map_primitive(:null, ident, &block) ; end |
#map_primitive(name, ident) {|obj| ... } ⇒ nil
Map a cog primitive type to a native type in this language
132 133 134 135 136 137 138 |
# File 'lib/cog/dsl/language_dsl.rb', line 132 def map_primitive(name, ident, &block) lang_eval do @prim_ident[name.to_sym] = ident.to_s @prim_to_lit[name.to_sym] = block end nil end |
#map_string(ident) {|obj| ... } ⇒ nil
Map the cog string type to a native type in this language
187 |
# File 'lib/cog/dsl/language_dsl.rb', line 187 def map_string(ident, &block) ; map_primitive(:string, ident, &block) ; end |
#map_void(ident) ⇒ Object
196 |
# File 'lib/cog/dsl/language_dsl.rb', line 196 def map_void(ident) ; map_primitive(:void, ident) ; end |
#multiline_comment(prefix, postfix) ⇒ nil
Define multi-line comment notation
33 34 35 36 37 38 39 |
# File 'lib/cog/dsl/language_dsl.rb', line 33 def multiline_comment(prefix, postfix) lang_eval do @multiline_comment_prefix = prefix @multiline_comment_postfix = postfix end nil end |
#name(value) ⇒ nil
Define a readable name for the language
16 17 18 19 |
# File 'lib/cog/dsl/language_dsl.rb', line 16 def name(value) lang_eval { @name = value } nil end |
#named_scope_begin {|name| ... } ⇒ nil
Define a block to call when beginning a named scope in this language
78 79 80 81 |
# File 'lib/cog/dsl/language_dsl.rb', line 78 def named_scope_begin(&block) lang_eval { @named_scope_begin_block = block } nil end |
#named_scope_end {|name| ... } ⇒ nil
Define a block to call when ending a named scope in this language
87 88 89 90 |
# File 'lib/cog/dsl/language_dsl.rb', line 87 def named_scope_end(&block) lang_eval { @named_scope_end_block = block } nil end |
#reserved(words) ⇒ nil
Enumerate reserved identifiers in the language
121 122 123 124 |
# File 'lib/cog/dsl/language_dsl.rb', line 121 def reserved(words) lang_eval { @reserved = words } nil end |
#seed_extension(ext, opt = {}) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/cog/dsl/language_dsl.rb', line 58 def seed_extension(ext, opt={}) lang_eval do @seed_extension = ext.to_s.downcase @seed_header = opt[:header].to_s.downcase if opt[:header] end end |
#use_named_scope {|name| ... } ⇒ nil
Define a block to call when using a named scopes in this language
69 70 71 72 |
# File 'lib/cog/dsl/language_dsl.rb', line 69 def use_named_scope(&block) lang_eval { @use_named_scope_block = block } nil end |