Class: RPrec::Stream Abstract

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

Overview

This class is abstract.

current, next and eof? must be implemented.

Stream is a token stream.

Direct Known Subclasses

ArrayStream, RegexpStream

Instance Method Summary collapse

Constructor Details

#initializeStream

Returns a new instance of Stream.



8
9
10
# File 'lib/rprec/stream.rb', line 8

def initialize
  @expected = []
end

Instance Method Details

#currentRPrec::Token

Returns:

Raises:

  • (ScriptError)


13
14
15
# File 'lib/rprec/stream.rb', line 13

def current
  raise ScriptError, 'Not implemented'
end

#eof?Boolean

Returns:

  • (Boolean)

Raises:

  • (ScriptError)


23
24
25
# File 'lib/rprec/stream.rb', line 23

def eof?
  raise ScriptError, 'Not implemented'
end

#expected(tokens) ⇒ void

This method returns an undefined value.

Parameters:

  • tokens (Array<String>)


29
30
31
# File 'lib/rprec/stream.rb', line 29

def expected(tokens)
  @expected += tokens
end

#nextvoid

This method returns an undefined value.



18
19
20
# File 'lib/rprec/stream.rb', line 18

def next
  @expected = []
end

#unexpectedvoid

This method returns an undefined value.

Raises:



34
35
36
37
38
39
40
# File 'lib/rprec/stream.rb', line 34

def unexpected
  token = current
  raise ParseError.new("Unexpected token '#{token.type}'", loc: token.loc) if @expected.empty?

  expected = @expected.sort.map { |tok| "'#{tok}'" }.join(', ')
  raise ParseError.new("Expected token(s) #{expected}, but the unexpected token '#{token.type}' comes", loc: token.loc)
end