Class: RParsec::ParseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rparsec/context.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, index = 0, error = nil) ⇒ ParseContext

Returns a new instance of ParseContext.



11
12
13
14
15
16
# File 'lib/rparsec/context.rb', line 11

def initialize(src, index=0, error=nil)
  @src = src
  @index = index
  @error = error
  @scanner = nil
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/rparsec/context.rb', line 9

def error
  @error
end

#indexObject

Returns the value of attribute index.



9
10
11
# File 'lib/rparsec/context.rb', line 9

def index
  @index
end

#resultObject

Returns the value of attribute result.



9
10
11
# File 'lib/rparsec/context.rb', line 9

def result
  @result
end

#srcObject (readonly)

Returns the value of attribute src.



8
9
10
# File 'lib/rparsec/context.rb', line 8

def src
  @src
end

Instance Method Details

#advance(n) ⇒ Object



65
66
67
# File 'lib/rparsec/context.rb', line 65

def advance n
  @index += n
end

#availableObject



53
54
55
# File 'lib/rparsec/context.rb', line 53

def available
  @src.length - @index
end

#currentObject



45
46
47
# File 'lib/rparsec/context.rb', line 45

def current
  @src[@index]
end

#eofObject



49
50
51
# File 'lib/rparsec/context.rb', line 49

def eof
  @index >= @src.length
end

#error_inputObject



34
35
36
37
38
39
# File 'lib/rparsec/context.rb', line 34

def error_input
  return nil if @error.nil?
  err = @error
  err = err.last if err.kind_of? Array
  err.input
end

#expecting(expected = nil) ⇒ Object



79
80
81
82
# File 'lib/rparsec/context.rb', line 79

def expecting(expected=nil)
  @error = Expected.new(@index, get_current_input, expected)
  return false
end

#failure(msg = nil) ⇒ Object



74
75
76
77
# File 'lib/rparsec/context.rb', line 74

def failure(msg=nil)
  @error = Failure.new(@index, get_current_input, msg)
  return false
end

#get_current_inputObject



84
85
86
87
# File 'lib/rparsec/context.rb', line 84

def get_current_input
  return nil if eof
  current
end

#nextObject



61
62
63
# File 'lib/rparsec/context.rb', line 61

def next
  @index += 1
end

#peek(i) ⇒ Object



57
58
59
# File 'lib/rparsec/context.rb', line 57

def peek i
  @src[@index + i]
end

#prepare_errorObject



24
25
26
# File 'lib/rparsec/context.rb', line 24

def prepare_error
  @error.flatten! if @error.kind_of?(Array)
end

#reset_errorObject



41
42
43
# File 'lib/rparsec/context.rb', line 41

def reset_error
  @error = nil
end

#retn(val) ⇒ Object



69
70
71
72
# File 'lib/rparsec/context.rb', line 69

def retn(val)
  @result = val
  true
end

#scannerObject



18
19
20
21
22
# File 'lib/rparsec/context.rb', line 18

def scanner
  @scanner = StringScanner.new(src) if @scanner.nil?
  @scanner.pos = @index
  @scanner
end

#to_msgObject



28
29
30
31
32
# File 'lib/rparsec/context.rb', line 28

def to_msg
  return '' if @error.nil?
  return @error.msg unless @error.kind_of?(Array)
  @error.map { |e| e.msg }.join(' or ')
end