Class: ANTLR3::CompileTask::GrammarSet

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/antlr3/task.rb

Overview

class CompileTask::GrammarSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar_files, options = {}) ⇒ GrammarSet

Returns a new instance of GrammarSet.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/antlr3/task.rb', line 125

def initialize( grammar_files, options = {} )
  @load_path = grammar_files.map { | f | File.dirname( f ) }
  @load_path.push( '.', @output_directory )
  
  if extra_load = options[ :load_path ]
    extra_load = [ extra_load ].flatten
    @load_path.unshift( extra_load )
  end
  @load_path.uniq!
  
  @grammars = grammar_files.map do | file |
    GrammarFile.new( self, file )
  end
  @output_directory = '.'
  dir = options[ :output_directory ] and @output_directory = dir.to_s
  
  @antlr_jar = options.fetch( :antlr_jar, ANTLR3.antlr_jar )
  @debug = options.fetch( :debug, false )
  @trace = options.fetch( :trace, false )
  @profile = options.fetch( :profile, false )
  @compile_options =
    case opts = options[ :compile_options ]
    when Array then opts
    else Shellwords.shellwords( opts.to_s )
    end
  @java_options =
    case opts = options[ :java_options ]
    when Array then opts
    else Shellwords.shellwords( opts.to_s )
    end
end

Instance Attribute Details

#antlr_jarObject

Returns the value of attribute antlr_jar.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def antlr_jar
  @antlr_jar
end

#compile_optionsObject

Returns the value of attribute compile_options.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def compile_options
  @compile_options
end

#debugObject

Returns the value of attribute debug.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def debug
  @debug
end

#grammarsObject (readonly)

Returns the value of attribute grammars.



122
123
124
# File 'lib/antlr3/task.rb', line 122

def grammars
  @grammars
end

#java_optionsObject

Returns the value of attribute java_options.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def java_options
  @java_options
end

#load_pathObject (readonly)

Returns the value of attribute load_path.



122
123
124
# File 'lib/antlr3/task.rb', line 122

def load_path
  @load_path
end

#output_directoryObject



161
162
163
# File 'lib/antlr3/task.rb', line 161

def output_directory
  @output_directory || '.'
end

#profileObject

Returns the value of attribute profile.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def profile
  @profile
end

#traceObject

Returns the value of attribute trace.



119
120
121
# File 'lib/antlr3/task.rb', line 119

def trace
  @trace
end

Instance Method Details

#build_command(grammar) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/antlr3/task.rb', line 212

def build_command( grammar )
  parts = [ 'java', '-cp', @antlr_jar ]
  parts.concat( @java_options )
  parts << 'org.antlr.Tool' << '-fo' << output_directory
  parts << '-debug' if @debug
  parts << '-profile' if @profile
  parts << '-trace' if @trace
  parts.concat( @compile_options )
  parts << grammar.path
  return parts.map! { | t | escape( t ) }.join( ' ' )
end

#cleanObject



179
180
181
182
183
184
185
186
# File 'lib/antlr3/task.rb', line 179

def clean
  for grammar in @grammars
    grammar.clean
  end
  if test( ?d, output_directory ) and ( Dir.entries( output_directory ) - %w( . .. ) ).empty?
    rmdir( output_directory )
  end
end

#compile(grammar) ⇒ Object



206
207
208
209
210
# File 'lib/antlr3/task.rb', line 206

def compile( grammar )
  dir = output_directory
  test( ?d, dir ) or FileUtils.mkpath( dir )
  sh( build_command( grammar ) )
end

#define_tasksObject



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/antlr3/task.rb', line 165

def define_tasks
  file( @antlr_jar )
  
  for grammar in @grammars
    deps = [ @antlr_jar ]
    if  vocab = grammar.token_vocab and
        tfile = find_tokens_file( vocab, grammar )
      file( tfile )
      deps << tfile
    end
    grammar.define_tasks( deps )
  end
end

#escape(token) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/antlr3/task.rb', line 224

def escape( token )
  token = token.to_s.dup
  token.empty? and return( %('') )
  token.gsub!( /([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1" )
  token.gsub!( /\n/, "'\n'" )
  return( token )
end

#find_tokens_file(vocab, grammar) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/antlr3/task.rb', line 188

def find_tokens_file( vocab, grammar )
  gram = @grammars.find { | gram | gram.name == vocab } and
    return( gram.tokens_file )
  file = locate( "#{ vocab }.tokens" ) and return( file )
  warn( Util.tidy( <<-END, true ) )
  | unable to locate .tokens file `#{ vocab }' referenced in #{ grammar.path }
  | -- ignoring dependency
  END
  return( nil )
end

#locate(file_name) ⇒ Object



199
200
201
202
203
204
# File 'lib/antlr3/task.rb', line 199

def locate( file_name )
  dir = @load_path.find do | dir |
    File.file?( File.join( dir, file_name ) )
  end
  dir and return( File.join( dir, file_name ) )
end

#target_filesObject



157
158
159
# File 'lib/antlr3/task.rb', line 157

def target_files
  @grammars.map { | gram | gram.target_files }.flatten
end