class Cog::Language

Describes a language support by Cog

Attributes

comment_style[R]

@return [String] the style of comments used by this language

extensions[R]

@return [Array<String>] list of file extensions

include_guard_style[R]

@return [String] the style of include guards used by this language

key[R]

@return [String] unique lower case identifier

name[R]

@return [String] readable name for the language

seed_extension[R]
seed_header[R]

Public Class Methods

new(key = :text) click to toggle source

@api developer Initialize with default values @param key [String] unique case-insensitive identifier

# File lib/cog/language.rb, line 27
def initialize(key = :text)
  @key = key.to_s.downcase
  @name = key.to_s
  @comment_pattern = '^\s*(%s)\s*$'
  @comment_prefix = nil
  @multiline_comment_prefix = nil
  @multiline_comment_postfix = nil
  @extensions = []
  identibitch = lambda {|name| ''}
  @use_named_scope_block = identibitch
  @named_scope_begin_block = identibitch
  @named_scope_end_block = identibitch
  @include_guard_begin_block = identibitch
  @include_guard_end_block = identibitch
  @reserved = []
  @prim_ident = {} # :name => 'ident'
  @prim_to_lit = {} # :name => to_literal_block
end

Public Instance Methods

<=>(other) click to toggle source

Sort by name

# File lib/cog/language.rb, line 112
def <=>(other)
  @name <=> other.name
end
apply_comment_style(other) click to toggle source

@api developer Called after all Cogfiles have been processed @param other [Language] language to borrow notation from

# File lib/cog/language.rb, line 89
def apply_comment_style(other)
  @comment_prefix = other.instance_eval {@comment_prefix}
  @multiline_comment_prefix = other.instance_eval {@multiline_comment_prefix}
  @multiline_comment_postfix = other.instance_eval {@multiline_comment_postfix}
  @comment_pattern = other.instance_eval {@comment_pattern}
end
apply_include_guard_style(other) click to toggle source

@api developer Called after all Cogfiles have been processed @param other [Language] language to borrow notation from

# File lib/cog/language.rb, line 99
def apply_include_guard_style(other)
  @include_guard_begin_block = other.instance_eval {@include_guard_begin_block}
  @include_guard_end_block = other.instance_eval {@include_guard_end_block}
end
comment(text) click to toggle source

@param text [String] some text which should be rendered as a comment @return [String] a comment appropriate for this language

# File lib/cog/language.rb, line 54
def comment(text)
  if text =~ /\n/
    multi_line_comment text
  else
    one_line_comment text
  end
end
comment_pattern(nested_pattern) click to toggle source

@param nested_pattern [String] regular expression pattern (as a string) to embed in the regular expression which matches one line comments in this language @return [Regexp] pattern for matching one line comments in this language

# File lib/cog/language.rb, line 48
def comment_pattern(nested_pattern)
  Regexp.new(@comment_pattern % nested_pattern)
end
default_lit_for(name) click to toggle source

@param name [Symbol] name of a primitive cog type @return [String] default value literal for the given primitive cog type @example

# For C++
lang.to_default_value :string # => '""'
# File lib/cog/language.rb, line 211
def default_lit_for(name)
  case name
  when :boolean
    to_boolean false
  when :integer
    to_integer 0
  when :long
    to_long 0
  when :float
    to_lit 0.0
  when :double
    to_double 0.0
  when :char
    to_char ''
  when :string
    to_string ''
  when :null
    to_null nil
  end
end
include_guard_begin(name) click to toggle source

@param name [String] name of the module to protect @return [String] an include guard statement

# File lib/cog/language.rb, line 136
def include_guard_begin(name)
  @include_guard_begin_block.call name
end
include_guard_end(name) click to toggle source

@param name [String] name of the module to protect @return [String] an include guard end statement

# File lib/cog/language.rb, line 142
def include_guard_end(name)
  @include_guard_end_block.call name
end
multi_line_comment(text) click to toggle source

@api developer

# File lib/cog/language.rb, line 74
def multi_line_comment(text)
  if @multiline_comment_prefix
    "#{@multiline_comment_prefix}\n#{text}\n#{@multiline_comment_postfix}"
  elsif @comment_prefix
    text.split("\n").collect do |line|
      "#{@comment_prefix} #{line}"
    end.join("\n")
  else
    text
  end
end
named_scope_begin(name) click to toggle source

@param name [String] name of the scope @return [String] begin a named scope

# File lib/cog/language.rb, line 124
def named_scope_begin(name)
  @named_scope_begin_block.call name
end
named_scope_end(name) click to toggle source

@param name [String] name of the scope @return [String] end the given named scope

# File lib/cog/language.rb, line 130
def named_scope_end(name)
  @named_scope_end_block.call name
end
one_line_comment(text) click to toggle source

@api developer

# File lib/cog/language.rb, line 63
def one_line_comment(text)
  if @comment_prefix
    "#{@comment_prefix} #{text}"
  elsif @multiline_comment_prefix
    "#{@multiline_comment_prefix} #{text} #{@multiline_comment_postfix}"
  else
    text
  end
end
to_boolean(obj) click to toggle source

@param obj [Object] a ruby object @return [String] boolean literal representation of the object in this language

# File lib/cog/language.rb, line 169
def to_boolean(obj) ; try_to_lit(:boolean, obj) ; end
to_char(obj) click to toggle source

@param obj [Object] a ruby object @return [String] char literal representation of the object in this language

# File lib/cog/language.rb, line 189
def to_char(obj) ; try_to_lit(:char, obj) ; end
to_double(obj) click to toggle source

@param obj [Object] a ruby object @return [String] double literal representation of the object in this language

# File lib/cog/language.rb, line 185
def to_double(obj) ; try_to_lit(:double, obj) ; end
to_float(obj) click to toggle source

@param obj [Object] a ruby object @return [String] float literal representation of the object in this language

# File lib/cog/language.rb, line 181
def to_float(obj) ; try_to_lit(:float, obj) ; end
to_ident(name) click to toggle source

@param name [String] a potential identifier name @return [String] an escaped version of the identifier, if it conflicted with a reserved word in the language

# File lib/cog/language.rb, line 148
def to_ident(name)
  if @reserved.member? name.to_s
    "#{name}_"
  else
    name.to_s
  end
end
to_integer(obj) click to toggle source

@param obj [Object] a ruby object @return [String] integer literal representation of the object in this language

# File lib/cog/language.rb, line 173
def to_integer(obj) ; try_to_lit(:integer, obj) ; end
to_lit(obj) click to toggle source

@param obj [Object] a ruby object @return [String] literal representation of the object in this language

# File lib/cog/language.rb, line 201
def to_lit(obj)
  return obj.to_lit if obj.respond_to?(:to_lit)
  raise Errors::PrimitiveNotSupported.new :object => obj
end
to_long(obj) click to toggle source

@param obj [Object] a ruby object @return [String] long literal representation of the object in this language

# File lib/cog/language.rb, line 177
def to_long(obj) ; try_to_lit(:long, obj) ; end
to_null(obj) click to toggle source

@param obj [Object] a ruby object @return [String] null literal representation of the object in this language

# File lib/cog/language.rb, line 197
def to_null(obj) ; try_to_lit(:null, obj) ; end
to_prim(name) click to toggle source

@param name [Symbol] name of a primitive cog type @return [String] the representation of a primitive cog type in the native language @example

# For Objective-C
lang.to_prim :boolean # => 'BOOL'
# File lib/cog/language.rb, line 161
def to_prim(name)
  ident = @prim_ident[name.to_sym]
  raise Errors::PrimitiveNotSupported.new :type => name, :language => @name unless ident
  ident
end
to_s(w=nil) click to toggle source

@param w [FixNum] width of the first column @return [String] one line summary in two columns

# File lib/cog/language.rb, line 106
def to_s(w=nil)
  w ||= @name.length
  "#{@name.ljust w} -> #{@extensions.collect {|x| x.to_s}.sort.join ', '}"
end
to_string(obj) click to toggle source

@param obj [Object] a ruby object @return [String] string literal representation of the object in this language

# File lib/cog/language.rb, line 193
def to_string(obj) ; try_to_lit(:string, obj) ; end
use_named_scope(name) click to toggle source

@param name [String] name of the scope to use @return [String] a using statement for the named scope

# File lib/cog/language.rb, line 118
def use_named_scope(name)
  @use_named_scope_block.call name
end

Private Instance Methods

try_to_lit(name, obj) click to toggle source

@api developer

# File lib/cog/language.rb, line 235
def try_to_lit(name, obj)
  to_lit_block = @prim_to_lit[name]
  raise Errors::PrimitiveNotSupported.new :type => name, :language => @name unless to_lit_block
  to_lit_block.call obj if to_lit_block
end