Class: ANTLR3::UnicodeStream

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

Constant Summary collapse

PACK_MASK =
'U*'.freeze

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, #mark, #mark_depth, #peek, #release, #reset, #rewind, #seek, #size

Methods included from Stream

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

Constructor Details

#initialize(data, options = {}) ⇒ UnicodeStream

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


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/antlr3/streams/unicode.rb', line 25

def initialize( data, options = {} )    # for 1.8
  @string = data.to_s
  @string.equal?( data ) and @string = @string.clone
  @string.freeze
  @data = @string.unpack( PACK_MASK )
  @position = options.fetch :position, 0
  @line = options.fetch :line, 1
  @column = options.fetch :column, 0
  @markers = []
  @name ||= options[ :file ] || options[ :name ] # || '(string)'
  mark
end

Instance Method Details

#[](start, *args) ⇒ Object

identical to String#[]


74
75
76
# File 'lib/antlr3/streams/unicode.rb', line 74

def []( start, *args )
  @data[ start, *args ].pack( PACK_MASK )
end

#look(k = 1) ⇒ Object

identical to #peek, except it returns the character value as a String


41
42
43
44
45
46
47
48
49
# File 'lib/antlr3/streams/unicode.rb', line 41

def look( k = 1 )                        # for 1.8
  k == 0 and return nil
  k += 1 if k < 0
  
  index = @position + k - 1
  index.between?( 0, @data.length - 1) or return nil
  
  @data[ index, 1 ].pack( PACK_MASK )
end

#substring(start, stop) ⇒ Object

return the string slice between position start and stop


66
67
68
# File 'lib/antlr3/streams/unicode.rb', line 66

def substring( start, stop )
  @data[ start, stop - start + 1 ].pack( PACK_MASK )
end

#through(k) ⇒ Object

return a substring around the stream cursor at a distance k if k >= 0, return the next k characters if k < 0, return the previous |k| characters


56
57
58
59
60
61
# File 'lib/antlr3/streams/unicode.rb', line 56

def through( k )
  if k >= 0 then @data[ @position, k ].pack( PACK_MASK ) else
    start = ( @position + k ).at_least( 0 ) # start cannot be negative or index will wrap around
    @data[ start ... @position ].pack( PACK_MASK )
  end
end