Class: Cog::Language
- Inherits:
-
Object
- Object
- Cog::Language
- Defined in:
- lib/cog/language.rb
Overview
Describes a language support by Cog
Instance Attribute Summary collapse
-
#comment_style ⇒ String
readonly
The style of comments used by this language.
-
#extensions ⇒ Array<String>
readonly
List of file extensions.
-
#include_guard_style ⇒ String
readonly
The style of include guards used by this language.
-
#key ⇒ String
readonly
Unique lower case identifier.
-
#name ⇒ String
readonly
Readable name for the language.
-
#seed_extension ⇒ Object
readonly
Returns the value of attribute seed_extension.
-
#seed_header ⇒ Object
readonly
Returns the value of attribute seed_header.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Sort by name.
-
#apply_comment_style(other) ⇒ Object
Called after all Cogfiles have been processed.
-
#apply_include_guard_style(other) ⇒ Object
Called after all Cogfiles have been processed.
-
#comment(text) ⇒ String
A comment appropriate for this language.
-
#comment_pattern(nested_pattern) ⇒ Regexp
Pattern for matching one line comments in this language.
-
#default_lit_for(name) ⇒ String
Default value literal for the given primitive cog type.
-
#include_guard_begin(name) ⇒ String
An include guard statement.
-
#include_guard_end(name) ⇒ String
An include guard end statement.
-
#initialize(key = :text) ⇒ Language
constructor
Initialize with default values.
- #multi_line_comment(text) ⇒ Object
-
#named_scope_begin(name) ⇒ String
Begin a named scope.
-
#named_scope_end(name) ⇒ String
End the given named scope.
- #one_line_comment(text) ⇒ Object
-
#to_boolean(obj) ⇒ String
Boolean literal representation of the object in this language.
-
#to_char(obj) ⇒ String
Char literal representation of the object in this language.
-
#to_double(obj) ⇒ String
Double literal representation of the object in this language.
-
#to_float(obj) ⇒ String
Float literal representation of the object in this language.
-
#to_ident(name) ⇒ String
An escaped version of the identifier, if it conflicted with a reserved word in the language.
-
#to_integer(obj) ⇒ String
Integer literal representation of the object in this language.
-
#to_lit(obj) ⇒ String
Literal representation of the object in this language.
-
#to_long(obj) ⇒ String
Long literal representation of the object in this language.
-
#to_null(obj) ⇒ String
Null literal representation of the object in this language.
-
#to_prim(name) ⇒ String
The representation of a primitive cog type in the native language.
-
#to_s(w = nil) ⇒ String
One line summary in two columns.
-
#to_string(obj) ⇒ String
String literal representation of the object in this language.
-
#use_named_scope(name) ⇒ String
A using statement for the named scope.
Constructor Details
#initialize(key = :text) ⇒ Language
Initialize with default values
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_style ⇒ String (readonly)
Returns the style of comments used by this language.
19 20 21 |
# File 'lib/cog/language.rb', line 19 def comment_style @comment_style end |
#extensions ⇒ Array<String> (readonly)
Returns list of file extensions.
7 8 9 |
# File 'lib/cog/language.rb', line 7 def extensions @extensions end |
#include_guard_style ⇒ String (readonly)
Returns 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 |
#key ⇒ String (readonly)
Returns unique lower case identifier.
13 14 15 |
# File 'lib/cog/language.rb', line 13 def key @key end |
#name ⇒ String (readonly)
Returns readable name for the language.
16 17 18 |
# File 'lib/cog/language.rb', line 16 def name @name end |
#seed_extension ⇒ Object (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_header ⇒ Object (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
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
118 119 120 |
# File 'lib/cog/language.rb', line 118 def use_named_scope(name) @use_named_scope_block.call name end |