Class: InputStream

Inherits:
Object show all
Defined in:
lib/antlr4/InputStream.rb

Overview

Vacuum all input from a string and then treat it like a buffer.

Direct Known Subclasses

FileStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ InputStream

Returns a new instance of InputStream.



6
7
8
9
10
11
12
# File 'lib/antlr4/InputStream.rb', line 6

def initialize(data)
    @name = "<empty>"
    @strdata = data
    @index = 0
    @data = @strdata.bytes 
    @size = @data.length
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/antlr4/InputStream.rb', line 5

def data
  @data
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/antlr4/InputStream.rb', line 5

def index
  @index
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/antlr4/InputStream.rb', line 5

def name
  @name
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/antlr4/InputStream.rb', line 5

def size
  @size
end

#strdataObject

Returns the value of attribute strdata.



5
6
7
# File 'lib/antlr4/InputStream.rb', line 5

def strdata
  @strdata
end

Instance Method Details

#consumeObject



22
23
24
25
26
27
28
# File 'lib/antlr4/InputStream.rb', line 22

def consume()
    if self.index >= self.size then
        # assert self.LA(1) == Token::EOF
        raise Exception.new("cannot consume EOF")
    end
    self.index = self.index + 1
end

#getText(start, stop) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/antlr4/InputStream.rb', line 67

def getText(start, stop)
    if stop >= self.size then
        stop = self.size - 1
    end
    if start >= self.size then
        return ""
    else
        return self.strdata[start..stop] # start = inital, stop == offset?
    end
end

#LA(offset) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/antlr4/InputStream.rb', line 29

def LA(offset)
    if offset==0 then
        return 0 # undefined
    end
    if offset<0 then
        offset = offset + 1 # e.g., translate LA(-1) to use offset=0
    end
    pos = self.index + offset - 1
    if pos < 0 or pos >= @size then # invalid
        return Token::EOF
    end
    return self.data[pos]
end

#LT(offset) ⇒ Object



43
44
45
# File 'lib/antlr4/InputStream.rb', line 43

def LT(offset)
    return self.LA(offset)
end

#markObject

mark/release do nothing; we have entire buffer



48
49
50
# File 'lib/antlr4/InputStream.rb', line 48

def mark()
    return -1
end

#release(marker) ⇒ Object



52
53
# File 'lib/antlr4/InputStream.rb', line 52

def release(marker)
end

#resetObject

Reset the stream so that it’s in the same state it was

when the object was created *except* the data array is not
touched.


18
19
20
# File 'lib/antlr4/InputStream.rb', line 18

def reset()
    @index = 0
end

#seek(_index) ⇒ Object

consume() ahead until p==_index; can’t just set p=_index as we must update line and column. If we seek backwards, just set p



58
59
60
61
62
63
64
65
# File 'lib/antlr4/InputStream.rb', line 58

def seek(_index)
    if _index<=self.index then
        self.index = _index # just jump; don't update stream state (line, ...)
        return
    end
    # seek forward
    self.index = [_index, self.size].min
end

#to_sObject



78
79
80
# File 'lib/antlr4/InputStream.rb', line 78

def to_s
    return self.strdata
end