class ANTLR3::InteractiveStringStream

A special stream used in the interactive mode of the Main scripts. It uses Readline (if available) or standard IO#gets to fetch data on demand.

Public Class Methods

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

creates a new StringStream object where data is the string data to stream. accepts the following options in a symbol-to-value hash:

:file or :name

the (file) name to associate with the stream; default: '(string)'

:line

the initial line number; default: 1

:column

the initial column number; default: 0

# File lib/antlr3/streams/interactive.rb, line 24
def initialize( options = {}, &block )      # for 1.9
  @string = ''
  @data   = []
  @position = options.fetch :position, 0
  @line     = options.fetch :line, 1
  @column   = options.fetch :column, 0
  @markers  = []
  mark
  @initialized = @eof = false
  @readline = block or raise( ArgumentError, "no line-reading block was provided" )
  @name ||= options[ :file ] || options[ :name ] || '(interactive)'
end

Public Instance Methods

consume() click to toggle source
Calls superclass method ANTLR3::StringStream#consume
# File lib/antlr3/streams/interactive.rb, line 90
def consume
  @position < @data.size and return( super )
  unless @eof
    readline
    consume
  end
end
look( i = 1 ) click to toggle source
# File lib/antlr3/streams/interactive.rb, line 112
def look( i = 1 )
  peek( i ).chr rescue EOF
end
peek( i = 1 ) click to toggle source
# File lib/antlr3/streams/interactive.rb, line 98
def peek( i = 1 )
  i.zero? and return 0
  i += 1 if i < 0
  index = @position + i - 1
  index < 0 and return 0
  
  if index < @data.size
    char = @data[ index ]
  elsif readline
    peek( i )
  else EOF
  end
end
substring( start, stop ) click to toggle source
# File lib/antlr3/streams/interactive.rb, line 116
def substring( start, stop )
  fill_through( stop )
  @string[ start .. stop ]
end

Private Instance Methods

fill_out() click to toggle source
# File lib/antlr3/streams/interactive.rb, line 130
def fill_out
  @eof and return
  readline until @eof
end
fill_through( position ) click to toggle source
# File lib/antlr3/streams/interactive.rb, line 123
def fill_through( position )
  @eof and return
  if @position < 0 then fill_out
  else readline until ( @data.size > @position or @eof )
  end
end
readline() click to toggle source
# File lib/antlr3/streams/interactive.rb, line 37
def readline
  @initialized = true
  unless @eof
    if line = @readline.call
      line = line.to_s.encode( Encoding::UTF_8 )
      @string << line
      @data.concat( line.codepoints.to_a )
      return true
    else
      @eof = true
      return false
    end
  end
end