Class: ANTLR3::Template::Group

Inherits:
Module
  • Object
show all
Defined in:
lib/antlr3/template.rb,
lib/antlr3/template/group-file.rb

Constant Summary collapse

Lexer =
GroupFile::Lexer
Parser =
GroupFile::Parser

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#modspace

Class Method Details

.load(group_file, options = {}) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/antlr3/template.rb', line 104

def self.load( group_file, options = {} )
  unless( File.file?( group_file ) )
    dir = $LOAD_PATH.find do | d |
      File.file?( File.join( dir, group_file ) )
    end or raise( LoadError, "no such template group file to load %s" % group_file )
    group_file = File.join( dir, group_file )
  end
  namespace = options.fetch( :namespace, ::Object )
  input = ANTLR3::FileStream.new( group_file, options )
  lexer = Lexer.new( input, options )
  parser = Parser.new( lexer, options )
  return( parser.group( namespace ) )
end

.new(&block) ⇒ Object


118
119
120
121
122
123
# File 'lib/antlr3/template.rb', line 118

def self.new( &block )
  super do
    const_set( :TEMPLATES, {} )
    block_given? and module_eval( &block )
  end
end

.parse(source, options = {}) ⇒ Object


97
98
99
100
101
102
# File 'lib/antlr3/template.rb', line 97

def self.parse( source, options = {} )
  namespace = options.fetch( :namespace, ::Object )
  lexer  = Lexer.new( source, options )
  parser = Parser.new( lexer, options )
  return( parser.group( namespace ) )
end

Instance Method Details

#alias_template(new_name, old_name) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/antlr3/template.rb', line 157

def alias_template( new_name, old_name )
  new_name, old_name = new_name.to_s.dup.freeze, old_name.to_s
  context = self::TEMPLATES.fetch( old_name.to_s ) do
    raise( NameError,
      "undefined template `%s' for template group %p" % [ old_name, self ]
    )
  end
  context.define_alias( new_name ) do | tclass |
    self::TEMPLATES[ new_name ] = tclass
    define_template_methods( tclass )
  end
  return( self )
end

#define_template(name, source, parameters = nil, &block) ⇒ Object


146
147
148
149
150
151
152
153
154
155
# File 'lib/antlr3/template.rb', line 146

def define_template( name, source, parameters = nil, &block )
  name = name.to_s.dup.freeze
  Context.define( self, name, parameters ) do | tclass |
    self::TEMPLATES[ name ] = tclass
    ERB.new( source, nil, '%' ).def_method( tclass, 'to_s' )
    
    define_template_methods( tclass )
  end
  return( self )
end

#fetch(name, values = {}) ⇒ Object


134
135
136
# File 'lib/antlr3/template.rb', line 134

def fetch( name, values = {} )
  self::TEMPLATES.fetch( name.to_s ).new( values )
end

#new(source, values = {}) ⇒ Object


125
126
127
128
129
130
131
132
# File 'lib/antlr3/template.rb', line 125

def new( source, values = {} )
  erb = ERB.new( source, nil, '%' )
  template = Context.new( values )
  template.extend( self )
  sclass = class << template; self; end
  erb.def_method( sclass, 'to_s' )
  return( template )
end

#template_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)

142
143
144
# File 'lib/antlr3/template.rb', line 142

def template_defined?( name )
  self::TEMPLATES.has_key?( name.to_s )
end

#templatesObject


138
139
140
# File 'lib/antlr3/template.rb', line 138

def templates
  self::TEMPLATES
end