Class: ANTLR3::InteractiveStringStream

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

Overview

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.

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?, #end_of_line?, #end_of_string?, #inspect, #last_marker, #mark, #mark_depth, #release, #reset, #rewind, #seek, #size, #through

Methods included from Stream

#index, #mark, #release, #rewind, #seek

Constructor Details

#initialize(options = {}, &block) ⇒ InteractiveStringStream

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


24
25
26
27
28
29
30
31
32
33
34
35
# 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

Instance Method Details

#consumeObject


90
91
92
93
94
95
96
# 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) ⇒ Object


112
113
114
# File 'lib/antlr3/streams/interactive.rb', line 112

def look( i = 1 )
  peek( i ).chr rescue EOF
end

#peek(i = 1) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object


116
117
118
119
# File 'lib/antlr3/streams/interactive.rb', line 116

def substring( start, stop )
  fill_through( stop )
  @string[ start .. stop ]
end