Class: ANTLR3::Template::Group

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

Constant Summary

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Module

#modspace

Class Method Details

+ (Object) load(group_file, options = {})



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/antlr3/template.rb', line 101

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

+ (Object) new(&block)



115
116
117
118
119
120
# File 'lib/antlr3/template.rb', line 115

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

+ (Object) parse(source, options = {})



94
95
96
97
98
99
# File 'lib/antlr3/template.rb', line 94

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

- (Object) alias_template(new_name, old_name)



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/antlr3/template.rb', line 154

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

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



143
144
145
146
147
148
149
150
151
152
# File 'lib/antlr3/template.rb', line 143

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

- (Object) fetch(name, values = {})



131
132
133
# File 'lib/antlr3/template.rb', line 131

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

- (Object) new(source, values = {})



122
123
124
125
126
127
128
129
# File 'lib/antlr3/template.rb', line 122

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

- (Boolean) template_defined?(name)

Returns:

  • (Boolean)


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

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

- (Object) templates



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

def templates
  self::TEMPLATES
end