class ANTLR3::FileStream

FileStream is a character stream that uses data stored in some external file. It is nearly identical to StringStream and functions as use data located in a file while automatically setting up the source_name and line parameters. It does not actually use any buffered IO operations throughout the stream navigation process. Instead, it reads the file data once when the stream is initialized.

Public Class Methods

new( file, options = {} ) click to toggle source

creates a new FileStream object using the given file object. If file is a path string, the file will be read and the contents will be used and the name attribute will be set to the path. If file is an IO-like object (that responds to :read), the content of the object will be used and the stream will attempt to set its name object first trying the method name on the object, then trying the method path on the object.

see StringStream.new for a list of additional options the constructer accepts

Calls superclass method ANTLR3::StringStream::new
# File lib/antlr3/streams.rb, line 689
def initialize( file, options = {} )
  case file
  when $stdin then
    data = $stdin.read
    @name = '(stdin)'
  when ARGF
    data = file.read
    @name = file.path
  when ::File then
    file = file.clone
    file.reopen( file.path, 'r' )
    @name = file.path
    data = file.read
    file.close
  else
    if file.respond_to?( :read )
      data = file.read
      if file.respond_to?( :name ) then @name = file.name
      elsif file.respond_to?( :path ) then @name = file.path
      end
    else
      @name = file.to_s
      if test( ?f, @name ) then data = File.read( @name )
      else raise ArgumentError, "could not find an existing file at %p" % @name
      end
    end
  end
  super( data, options )
end