class ANTLR3::Test::Grammar

Constants

CLASS_TO_TYPE
GRAMMAR_TYPES
TYPE_TO_CLASS

Attributes

name[R]

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################

output_directory[RW]
source[R]

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################

type[R]

ATTRIBUTES AND ATTRIBUTE-ISH METHODS ####################

verbose[RW]

Public Class Methods

global_dependency( path ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 53
def self.global_dependency( path )
  path = File.expand_path path.to_s
  GLOBAL_DEPENDENCIES << path if test( ?f, path )
  return path
end
inline( source, *args ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 59
def self.inline( source, *args )
  InlineGrammar.new( source, *args )
end
new( path, options = {} ) { |self| ... } click to toggle source

CONSTRUCTOR #############################################

# File lib/antlr3/test/grammar.rb, line 66
def initialize( path, options = {} )
  @path = path.to_s
  @source = File.read( @path )
  @output_directory = options.fetch( :output_directory, '.' )
  @verbose = options.fetch( :verbose, $VERBOSE )
  study
  build_dependencies
  
  yield( self ) if block_given?
end

Public Instance Methods

clean!() click to toggle source
# File lib/antlr3/test/grammar.rb, line 190
def clean!
  deleted = []
  for target in target_files
    if test( ?f, target )
      File.delete( target )
      deleted << target
    end
  end
  return deleted
end
combined?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 137
def combined?
  @type == "combined"
end
compile( options = {} ) click to toggle source

COMMAND METHODS ############################################

# File lib/antlr3/test/grammar.rb, line 167
def compile( options = {} )
  if options[ :force ] or stale?
    compile!( options )
  end
end
compile!( options = {} ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 173
def compile!( options = {} )
  command = build_command( options )
  
  blab( command )
  output = IO.popen( command ) do |pipe|
    pipe.read
  end
  
  case status = $?.exitstatus
  when 0, 130
    post_compile( options )
  else compilation_failure!( command, status, output )
  end
  
  return target_files
end
has_lexer?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 115
def has_lexer?
  @type == 'combined' || @type == 'lexer'
end
has_parser?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 119
def has_parser?
  @type == 'combined' || @type == 'parser'
end
has_tree?()
Alias for: tree?
imported_target_files() click to toggle source
# File lib/antlr3/test/grammar.rb, line 158
def imported_target_files
  imports.map! do |delegate|
    output_directory / "#{ @name }_#{ delegate }.rb"
  end
end
imports() click to toggle source
# File lib/antlr3/test/grammar.rb, line 153
def imports
  @source.scan( /^\s*import\s+(\w+)\s*;/ ).
    tap { |list| list.flatten! }
end
inspect() click to toggle source
# File lib/antlr3/test/grammar.rb, line 201
def inspect
  sprintf( "grammar %s (%s)", @name, @path )
end
lexer?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 123
def lexer?
  @type == "lexer"
end
lexer_class_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 83
def lexer_class_name
  self.name + "::Lexer"
end
lexer_file_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 87
def lexer_file_name
  if lexer? then base = name
  elsif combined? then base = name + 'Lexer'
  else return( nil )
  end
  return( base + '.rb' )
end
parser?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 127
def parser?
  @type == "parser"
end
parser_class_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 95
def parser_class_name
  name + "::Parser"
end
parser_file_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 99
def parser_file_name
  if parser? then base = name
  elsif combined? then base = name + 'Parser'
  else return( nil )
  end
  return( base + '.rb' )
end
target_files( include_imports = true ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 141
def target_files( include_imports = true )
  targets = []
  
  for target_type in %w(lexer parser tree_parser)
    target_name = self.send( :"#{ target_type }_file_name" ) and
      targets.push( output_directory / target_name )
  end
  
  targets.concat( imported_target_files ) if include_imports
  return targets
end
tree?() click to toggle source
# File lib/antlr3/test/grammar.rb, line 131
def tree?
  @type == "tree"
end
Also aliased as: has_tree?
tree_parser_class_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 107
def tree_parser_class_name
  name + "::TreeParser"
end
tree_parser_file_name() click to toggle source
# File lib/antlr3/test/grammar.rb, line 111
def tree_parser_file_name
  tree? and name + '.rb'
end

Private Instance Methods

blab( string, *args ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 211
def blab( string, *args )
  $stderr.printf( string + "\n", *args ) if @verbose
end
build_command( options ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 246
def build_command( options )
  parts = %w(java)
  jar_path = options.fetch( :antlr_jar, default_antlr_jar )
  parts.push( '-cp', jar_path )
  parts << 'org.antlr.Tool'
  parts.push( '-fo', output_directory )
  options[ :profile ] and parts << '-profile'
  options[ :debug ]   and parts << '-debug'
  options[ :trace ]   and parts << '-trace'
  options[ :debug_st ] and parts << '-XdbgST'
  parts << File.expand_path( @path )
  parts.map! { |part| shell_escape( part ) }.join( ' ' ) << ' 2>&1'
end
build_dependencies() click to toggle source
# File lib/antlr3/test/grammar.rb, line 226
def build_dependencies
  depends_on( @path )
  
  if @source =~ /tokenVocab\s*=\s*(\S+)\s*;/
    foreign_grammar_name = $1
    token_file = output_directory / foreign_grammar_name + '.tokens'
    grammar_file = File.dirname( path ) / foreign_grammar_name << '.g'
    depends_on( token_file )
    depends_on( grammar_file )
  end    
end
compilation_failure!( command, status, output ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 219
def compilation_failure!( command, status, output )
  for f in target_files
    test( ?f, f ) and File.delete( f )
  end
  raise CompilationFailure.new( self, command, status, output )
end
default_antlr_jar() click to toggle source
# File lib/antlr3/test/grammar.rb, line 215
def default_antlr_jar
  ENV[ 'ANTLR_JAR' ] || ANTLR3.antlr_jar
end
post_compile( options ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 207
def post_compile( options )
  # do nothing for now
end
shell_escape( token ) click to toggle source
# File lib/antlr3/test/grammar.rb, line 238
def shell_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
study() click to toggle source
# File lib/antlr3/test/grammar.rb, line 260
def study
  @source =~ /^\s*(lexer|parser|tree)?\s*grammar\s*(\S+)\s*;/ or
    raise Grammar::FormatError[ source, path ]
  @name = $2
  @type = $1 || 'combined'
end