Class: ANTLR3::FileStream

Inherits:
StringStream show all
Defined in:
lib/antlr3/streams.rb

Overview

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.

Constant Summary

Constants inherited from StringStream

StringStream::NEWLINE

Constants included from Constants

Constants::BUILT_IN_TOKEN_NAMES, Constants::DEFAULT, Constants::DOWN, Constants::EOF, Constants::EOF_TOKEN, Constants::EOR_TOKEN_TYPE, Constants::HIDDEN, Constants::INVALID, Constants::INVALID_TOKEN, Constants::MEMO_RULE_FAILED, Constants::MEMO_RULE_UNKNOWN, Constants::MIN_TOKEN_TYPE, Constants::SKIP_TOKEN, Constants::UP

Instance Attribute Summary

Attributes inherited from StringStream

#column, #data, #line, #name, #position, #string

Attributes included from CharacterStream

#column, #line

Attributes included from Stream

#size, #source_name

Instance Method Summary collapse

Methods inherited from StringStream

#<<, #[], #beginning_of_line?, #beginning_of_string?, #consume, #end_of_line?, #end_of_string?, #inspect, #last_marker, #look, #mark, #mark_depth, #peek, #release, #reset, #rewind, #seek, #size, #substring, #through

Methods included from CharacterStream

#substring

Methods included from Stream

#consume, #index, #look, #mark, #peek, #release, #rewind, #seek

Constructor Details

#initialize(file, options = {}) ⇒ FileStream

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


689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# 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