Class: ANTLR3::Main::Main

Inherits:
Object
  • Object
show all
Includes:
Options, Util
Defined in:
lib/antlr3/main.rb

Overview

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.

Direct Known Subclasses

LexerMain, ParserMain, WalkerMain

Instance Attribute Summary collapse

Attributes included from Options

#debug_socket, #encoding, #input, #interactive, #no_output, #profile, #ruby_prof

Instance Method Summary collapse

Methods included from Util

parse_version, silence_warnings, snake_case, tidy

Methods included from Options

#parse_options

Constructor Details

#initialize(options = {}) ⇒ Main


124
125
126
127
128
129
130
131
# 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

Instance Attribute Details

#errorObject

Returns the value of attribute error


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

def error
  @error
end

#outputObject

Returns the value of attribute output


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

def output
  @output
end

Instance Method Details

#execute(argv = ARGV) ⇒ Object

runs the script


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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