class ANTLR3::Main::Main
The base-class for the three primary Main
script-runner classes. It defines the skeletal structure shared by all main scripts, but isn’t particularly useful on its own.
Attributes
error[RW]
output[RW]
Public Class Methods
new( options = {} ) { |self| ... }
click to toggle source
Calls superclass method
ANTLR3::Main::Options::new
# File lib/antlr3/main.rb, line 124 def initialize( options = {} ) @input = options.fetch( :input, $stdin ) @output = options.fetch( :output, $stdout ) @error = options.fetch( :error, $stderr ) @name = options.fetch( :name, File.basename( $0, '.rb' ) ) super block_given? and yield( self ) end
Public Instance Methods
execute( argv = ARGV )
click to toggle source
runs the script
# File lib/antlr3/main.rb, line 135 def execute( argv = ARGV ) args = parse_options( argv ) setup @interactive and return execute_interactive in_stream = case when @input.is_a?( ::String ) then StringStream.new( @input ) when args.length == 1 && args.first != '-' ANTLR3::FileStream.new( args[ 0 ] ) else ANTLR3::FileStream.new( @input ) end if @ruby_prof load_ruby_prof profile = RubyProf.profile do recognize( in_stream ) end printer = RubyProf::FlatPrinter.new( profile ) printer.print( @output ) else recognize( in_stream ) end end
Private Instance Methods
attempt( lib, message = nil, exit_status = nil ) { || ... }
click to toggle source
# File lib/antlr3/main.rb, line 219 def attempt( lib, message = nil, exit_status = nil ) yield rescue LoadError => error message or raise @error.puts( message ) report_error( error ) report_load_path exit( exit_status ) if exit_status rescue => error @error.puts( "received an error while attempting to load %p" % lib ) report_error( error ) exit( exit_status ) if exit_status end
constant_exists?( name )
click to toggle source
# File lib/antlr3/main.rb, line 268 def constant_exists?( name ) eval( "defined?(#{ name })" ) == 'constant' end
execute_interactive()
click to toggle source
# File lib/antlr3/main.rb, line 166 def execute_interactive @output.puts( tidy( <<-END ) ) | =================================================================== | Ruby ANTLR Console for #{ $0 } | ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ | * Enter source code lines | * Enter EOF to finish up and exit | (control+D on Mac/Linux/Unix or control+Z on Windows) | =================================================================== | END read_method = begin require 'readline' line_number = 0 lambda do begin if line = Readline.readline( "#@name:#{ line_number += 1 }> ", true ) line << $/ else @output.print( "\n" ) # ensures result output is on a new line after EOF is entered nil end rescue Interrupt, EOFError retry end line << "\n" if line end rescue LoadError lambda do begin printf( "%s:%i> ", @name, @input.lineno ) flush line = @input.gets or @output.print( "\n" ) # ensures result output is on a new line after EOF is entered line rescue Interrupt, EOFError retry end line end end stream = InteractiveStringStream.new( :name => @name, &read_method ) recognize( stream ) end
fetch_class( name )
click to toggle source
# File lib/antlr3/main.rb, line 254 def fetch_class( name ) name.nil? || name.empty? and return( nil ) unless constant_exists?( name ) try_to_load( name ) constant_exists?( name ) or return( nil ) end name.split( /::/ ).inject( Object ) do |mod, name| # ::SomeModule splits to ['', 'SomeModule'] - so ignore empty strings name.empty? and next( mod ) mod.const_get( name ) end end
recognize( *args )
click to toggle source
# File lib/antlr3/main.rb, line 162 def recognize( *args ) # overriden by subclasses end
report_error( error )
click to toggle source
# File lib/antlr3/main.rb, line 233 def report_error( error ) puts!( "~ error details:" ) puts!( ' [ %s ]' % error.class.name ) message = error.to_s.gsub( /\n/, "\n " ) puts!( ' -> ' << message ) for call in error.backtrace puts!( ' ' << call ) end end
report_load_path()
click to toggle source
# File lib/antlr3/main.rb, line 243 def report_load_path puts!( "~ content of $LOAD_PATH: " ) for dir in $LOAD_PATH puts!( " - #{ dir }" ) end end
screen_width()
click to toggle source
# File lib/antlr3/main.rb, line 215 def screen_width ( ENV[ 'COLUMNS' ] || 80 ).to_i end
setup()
click to toggle source
# File lib/antlr3/main.rb, line 250 def setup # hook end
try_to_load( name )
click to toggle source
# File lib/antlr3/main.rb, line 272 def try_to_load( name ) if name =~ /(\w+)::(Lexer|Parser|TreeParser)$/ retry_ok = true module_name, recognizer_type = $1, $2 script = name.gsub( /::/, '' ) begin return( require( script ) ) rescue LoadError if retry_ok script, retry_ok = module_name, false retry else return( nil ) end end end end