Class: RPrec::RegexpStream Private

Inherits:
Stream
  • Object
show all
Defined in:
lib/rprec/regexp_lexer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

RegexpStream is a stream generated by a RegexpLexer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Stream

#expected, #unexpected

Constructor Details

#initialize(lexer, source) ⇒ RegexpStream

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RegexpStream.

Parameters:

  • lexer (RPrec::Lexer)
  • source (String)


39
40
41
42
43
44
45
# File 'lib/rprec/regexp_lexer.rb', line 39

def initialize(lexer, source)
  super()
  @lexer = lexer
  @source = source
  @offset = 0
  self.next
end

Instance Attribute Details

#currentRPrec::Token (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



48
49
50
# File 'lib/rprec/regexp_lexer.rb', line 48

def current
  @current
end

Instance Method Details

#eof?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


78
79
80
# File 'lib/rprec/regexp_lexer.rb', line 78

def eof?
  @source.size <= @offset
end

#nextvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rprec/regexp_lexer.rb', line 51

def next
  super

  skip = @source.match(@lexer.skip, @offset)
  @offset += skip.to_s.size if skip

  if eof?
    @current = Token.new('EOF', loc: @offset...@offset)
    return
  end

  match = @source.match(@lexer.pattern, @offset)
  raise ParseError.new("Unexpected character: #{@source[@offset].inspect}", loc: @offset...@offset + 1) unless match

  type_and_value = @lexer.block.call(match)
  type, value =
    if type_and_value.is_a?(Array)
      [type_and_value[0], type_and_value[1]]
    else
      [type_and_value, nil]
    end
  loc = @offset...@offset + match.to_s.size
  @offset += match.to_s.size
  @current = Token.new(type, value, loc:)
end