Class: Cog::Language

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

Overview

Describes a language support by Cog

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = :text) ⇒ Language

Initialize with default values

Parameters:

  • key (String) (defaults to: :text)

    unique case-insensitive identifier



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

Instance Attribute Details

#comment_styleString (readonly)

Returns the style of comments used by this language.

Returns:

  • (String)

    the style of comments used by this language



19
20
21
# File 'lib/cog/language.rb', line 19

def comment_style
  @comment_style
end

#extensionsArray<String> (readonly)

Returns list of file extensions.

Returns:



7
8
9
# File 'lib/cog/language.rb', line 7

def extensions
  @extensions
end

#include_guard_styleString (readonly)

Returns the style of include guards used by this language.

Returns:

  • (String)

    the style of include guards used by this language



22
23
24
# File 'lib/cog/language.rb', line 22

def include_guard_style
  @include_guard_style
end

#keyString (readonly)

Returns unique lower case identifier.

Returns:

  • (String)

    unique lower case identifier



13
14
15
# File 'lib/cog/language.rb', line 13

def key
  @key
end

#nameString (readonly)

Returns readable name for the language.

Returns:

  • (String)

    readable name for the language



16
17
18
# File 'lib/cog/language.rb', line 16

def name
  @name
end

#seed_extensionObject (readonly)

Returns the value of attribute seed_extension.



9
10
11
# File 'lib/cog/language.rb', line 9

def seed_extension
  @seed_extension
end

#seed_headerObject (readonly)

Returns the value of attribute seed_header.



10
11
12
# File 'lib/cog/language.rb', line 10

def seed_header
  @seed_header
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by name



112
113
114
# File 'lib/cog/language.rb', line 112

def <=>(other)
  @name <=> other.name
end

#apply_comment_style(other) ⇒ Object

Called after all Cogfiles have been processed

Parameters:

  • other (Language)

    language to borrow notation from



89
90
91
92
93
94
# 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) ⇒ Object

Called after all Cogfiles have been processed

Parameters:

  • other (Language)

    language to borrow notation from



99
100
101
102
# 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) ⇒ String

Returns a comment appropriate for this language.

Parameters:

  • text (String)

    some text which should be rendered as a comment

Returns:

  • (String)

    a comment appropriate for this language



54
55
56
57
58
59
60
# 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) ⇒ Regexp

Returns pattern for matching one line comments in this language.

Parameters:

  • nested_pattern (String)

    regular expression pattern (as a string) to embed in the regular expression which matches one line comments in this language

Returns:

  • (Regexp)

    pattern for matching one line comments in this language



48
49
50
# File 'lib/cog/language.rb', line 48

def comment_pattern(nested_pattern)
  Regexp.new(@comment_pattern % nested_pattern)
end

#default_lit_for(name) ⇒ String

Returns default value literal for the given primitive cog type.

Examples:

# For C++
lang.to_default_value :string # => '""'

Parameters:

  • name (Symbol)

    name of a primitive cog type

Returns:

  • (String)

    default value literal for the given primitive cog type



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 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) ⇒ String

Returns an include guard statement.

Parameters:

  • name (String)

    name of the module to protect

Returns:

  • (String)

    an include guard statement



136
137
138
# File 'lib/cog/language.rb', line 136

def include_guard_begin(name)
  @include_guard_begin_block.call name
end

#include_guard_end(name) ⇒ String

Returns an include guard end statement.

Parameters:

  • name (String)

    name of the module to protect

Returns:

  • (String)

    an include guard end statement



142
143
144
# File 'lib/cog/language.rb', line 142

def include_guard_end(name)
  @include_guard_end_block.call name
end

#multi_line_comment(text) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ String

Returns begin a named scope.

Parameters:

  • name (String)

    name of the scope

Returns:

  • (String)

    begin a named scope



124
125
126
# File 'lib/cog/language.rb', line 124

def named_scope_begin(name)
  @named_scope_begin_block.call name
end

#named_scope_end(name) ⇒ String

Returns end the given named scope.

Parameters:

  • name (String)

    name of the scope

Returns:

  • (String)

    end the given named scope



130
131
132
# File 'lib/cog/language.rb', line 130

def named_scope_end(name)
  @named_scope_end_block.call name
end

#one_line_comment(text) ⇒ Object



63
64
65
66
67
68
69
70
71
# 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) ⇒ String

Returns boolean literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    boolean literal representation of the object in this language



169
# File 'lib/cog/language.rb', line 169

def to_boolean(obj) ; try_to_lit(:boolean, obj) ; end

#to_char(obj) ⇒ String

Returns char literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    char literal representation of the object in this language



189
# File 'lib/cog/language.rb', line 189

def to_char(obj) ; try_to_lit(:char, obj) ; end

#to_double(obj) ⇒ String

Returns double literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    double literal representation of the object in this language



185
# File 'lib/cog/language.rb', line 185

def to_double(obj) ; try_to_lit(:double, obj) ; end

#to_float(obj) ⇒ String

Returns float literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    float literal representation of the object in this language



181
# File 'lib/cog/language.rb', line 181

def to_float(obj) ; try_to_lit(:float, obj) ; end

#to_ident(name) ⇒ String

Returns an escaped version of the identifier, if it conflicted with a reserved word in the language.

Parameters:

  • name (String)

    a potential identifier name

Returns:

  • (String)

    an escaped version of the identifier, if it conflicted with a reserved word in the language



148
149
150
151
152
153
154
# 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) ⇒ String

Returns integer literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    integer literal representation of the object in this language



173
# File 'lib/cog/language.rb', line 173

def to_integer(obj) ; try_to_lit(:integer, obj) ; end

#to_lit(obj) ⇒ String

Returns literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    literal representation of the object in this language



201
202
203
204
# 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) ⇒ String

Returns long literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    long literal representation of the object in this language



177
# File 'lib/cog/language.rb', line 177

def to_long(obj) ; try_to_lit(:long, obj) ; end

#to_null(obj) ⇒ String

Returns null literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    null literal representation of the object in this language



197
# File 'lib/cog/language.rb', line 197

def to_null(obj) ; try_to_lit(:null, obj) ; end

#to_prim(name) ⇒ String

Returns the representation of a primitive cog type in the native language.

Examples:

# For Objective-C
lang.to_prim :boolean # => 'BOOL'

Parameters:

  • name (Symbol)

    name of a primitive cog type

Returns:

  • (String)

    the representation of a primitive cog type in the native language



161
162
163
164
165
# 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) ⇒ String

Returns one line summary in two columns.

Parameters:

  • w (FixNum) (defaults to: nil)

    width of the first column

Returns:

  • (String)

    one line summary in two columns



106
107
108
109
# 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) ⇒ String

Returns string literal representation of the object in this language.

Parameters:

  • obj (Object)

    a ruby object

Returns:

  • (String)

    string literal representation of the object in this language



193
# File 'lib/cog/language.rb', line 193

def to_string(obj) ; try_to_lit(:string, obj) ; end

#use_named_scope(name) ⇒ String

Returns a using statement for the named scope.

Parameters:

  • name (String)

    name of the scope to use

Returns:

  • (String)

    a using statement for the named scope



118
119
120
# File 'lib/cog/language.rb', line 118

def use_named_scope(name)
  @use_named_scope_block.call name
end