Class: Cog::DSL::LanguageDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/cog/dsl/language_dsl.rb

Overview

DSL for defining a language

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ LanguageDSL

Returns a new instance of LanguageDSL.

Parameters:

  • key (String)

    unique case-insensitive identifier



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

Parameters:

  • prefix (String)

    starts a single line comment in the language

Returns:

  • (nil)


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

Parameters:

  • lang_key (String)

    use comment notation from the language with the given key

Returns:

  • (nil)


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

Parameters:

  • values (Array<String>)

    list of file extensions for this language

Returns:

  • (nil)


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

#finalizeCog::Language

Compute the comment pattern

Returns:



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

Yield Parameters:

  • name (String)

    name of the guard

Yield Returns:

  • (String)

    a begin guard in this language

Returns:

  • (nil)


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

Yield Parameters:

  • name (String)

    name of the guard

Yield Returns:

  • (String)

    an end guard in this language

Returns:

  • (nil)


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

Parameters:

  • lang_key (String)

    use include guard notation from the language with the given key

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the boolean type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the boolean literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the char type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the char literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the double type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the double literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the float type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the float literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the integer type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the integer literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the long type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the long literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the null type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the null literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • name (Symbol)

    name of the cog primitive type

  • ident (String)

    identifier of the mapped type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • ident (String)

    identifier of the string type in this language

Yield Parameters:

  • obj (Object)

    a ruby object

Yield Returns:

  • (String, nil)

    the string literal representation of the given obj in this language, or nil if the object can not be mapped

Returns:

  • (nil)


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

Parameters:

  • prefix (String)

    starts a multi-line comment in the language

  • postfix (String)

    ends a multi-line comment in the language

Returns:

  • (nil)


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

Parameters:

  • value (String)

    readable name of the language

Returns:

  • (nil)


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

Yield Parameters:

  • name (String)

    name of the scope to begin

Yield Returns:

  • (String)

    a begin named scope statement in this language

Returns:

  • (nil)


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

Yield Parameters:

  • name (String)

    name of the scope to end

Yield Returns:

  • (String)

    an end named scope statement in this language

Returns:

  • (nil)


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

Parameters:

  • words (Array<String>)

    a list of words which must not be used as identifiers in the language

Returns:

  • (nil)


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

Yield Parameters:

  • name (String)

    name of the scope to use

Yield Returns:

  • (String)

    a using named scope statement in this language

Returns:

  • (nil)


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