class Cog::DSL::LanguageDSL

DSL for defining a language

Public Class Methods

new(key) click to toggle source

@api developer @param key [String] unique case-insensitive identifier

# File lib/cog/dsl/language_dsl.rb, line 9
def initialize(key)
  @lang = Cog::Language.new key
end

Public Instance Methods

comment(prefix) click to toggle source

Define single line comment notation @param prefix [String] starts a single line comment in the language @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 24
def comment(prefix)
  lang_eval { @comment_prefix = prefix }
  nil
end
comment_style(lang_key) click to toggle source

Borrow comment notation from another language @param lang_key [String] use comment notation from the language with the given key @return [nil]

# 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) click to toggle source

Define file extensions @param values [Array<String>] list of file extensions for this language @return [nil]

# 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() click to toggle source

@api developer Compute the comment pattern @return [Cog::Language] the defined language

# 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(&block) click to toggle source

Define a block to call when beginning an include guard in this language @yieldparam name [String] name of the guard @yieldreturn [String] a begin guard in this language @return [nil]

# 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(&block) click to toggle source

Define a block to call when ending an include guard in this language @yieldparam name [String] name of the guard @yieldreturn [String] an end guard in this language @return [nil]

# 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) click to toggle source

Borrow include guard notation from another language @param lang_key [String] use include guard notation from the language with the given key @return [nil]

# 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, &block) click to toggle source

Map the cog boolean type to a native type in this language @param ident [String] identifier of the boolean type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the boolean literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 145
def map_boolean(ident, &block) ; map_primitive(:boolean, ident, &block) ; end
map_char(ident, &block) click to toggle source

Map the cog char type to a native type in this language @param ident [String] identifier of the char type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the char literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 180
def map_char(ident, &block) ; map_primitive(:char, ident, &block) ; end
map_double(ident, &block) click to toggle source

Map the cog double type to a native type in this language @param ident [String] identifier of the double type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the double literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 173
def map_double(ident, &block) ; map_primitive(:double, ident, &block) ; end
map_float(ident, &block) click to toggle source

Map the cog float type to a native type in this language @param ident [String] identifier of the float type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the float literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 166
def map_float(ident, &block) ; map_primitive(:float, ident, &block) ; end
map_integer(ident, &block) click to toggle source

Map the cog integer type to a native type in this language @param ident [String] identifier of the integer type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the integer literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 152
def map_integer(ident, &block) ; map_primitive(:integer, ident, &block) ; end
map_long(ident, &block) click to toggle source

Map the cog long type to a native type in this language @param ident [String] identifier of the long type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the long literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 159
def map_long(ident, &block) ; map_primitive(:long, ident, &block) ; end
map_null(ident, &block) click to toggle source

Map the cog null type to a native type in this language @param ident [String] identifier of the null type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the null literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 194
def map_null(ident, &block) ; map_primitive(:null, ident, &block) ; end
map_primitive(name, ident, &block) click to toggle source

Map a cog primitive type to a native type in this language @param name [Symbol] name of the cog primitive type @param ident [String] identifier of the mapped type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# 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, &block) click to toggle source

Map the cog string type to a native type in this language @param ident [String] identifier of the string type in this language @yieldparam obj [Object] a ruby object @yieldreturn [String,nil] the string literal representation of the given obj in this language, or nil if the object can not be mapped @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 187
def map_string(ident, &block) ; map_primitive(:string, ident, &block) ; end
map_void(ident) click to toggle source
# File lib/cog/dsl/language_dsl.rb, line 196
def map_void(ident) ; map_primitive(:void, ident) ; end
multiline_comment(prefix, postfix) click to toggle source

Define multi-line comment notation @param prefix [String] starts a multi-line comment in the language @param postfix [String] ends a multi-line comment in the language @return [nil]

# 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) click to toggle source

Define a readable name for the language @param value [String] readable name of the language @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 16
def name(value)
  lang_eval { @name = value }
  nil
end
named_scope_begin(&block) click to toggle source

Define a block to call when beginning a named scope in this language @yieldparam name [String] name of the scope to begin @yieldreturn [String] a begin named scope statement in this language @return [nil]

# 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(&block) click to toggle source

Define a block to call when ending a named scope in this language @yieldparam name [String] name of the scope to end @yieldreturn [String] an end named scope statement in this language @return [nil]

# 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) click to toggle source

Enumerate reserved identifiers in the language @param words [Array<String>] a list of words which must not be used as identifiers in the language @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 121
def reserved(words)
  lang_eval { @reserved = words }
  nil
end
seed_extension(ext, opt={}) click to toggle source
# 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(&block) click to toggle source

Define a block to call when using a named scopes in this language @yieldparam name [String] name of the scope to use @yieldreturn [String] a using named scope statement in this language @return [nil]

# File lib/cog/dsl/language_dsl.rb, line 69
def use_named_scope(&block)
  lang_eval { @use_named_scope_block = block }
  nil
end

Private Instance Methods

lang_eval(&block) click to toggle source
# File lib/cog/dsl/language_dsl.rb, line 225
def lang_eval(&block)
  @lang.instance_eval &block
end