class ANTLR3::CompileTask::GrammarSet

class CompileTask::GrammarSet

Attributes

antlr_jar[RW]
compile_options[RW]
debug[RW]
grammars[R]
java_options[RW]
load_path[R]
output_directory[W]
profile[RW]
trace[RW]

Public Class Methods

new( grammar_files, options = {} ) click to toggle source
# 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

Public Instance Methods

build_command( grammar ) click to toggle source
# 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
clean() click to toggle source
# 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 ) click to toggle source
# 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_tasks() click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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
output_directory() click to toggle source
# File lib/antlr3/task.rb, line 161
def output_directory
  @output_directory || '.'
end
target_files() click to toggle source
# File lib/antlr3/task.rb, line 157
def target_files
  @grammars.map { | gram | gram.target_files }.flatten
end