Class: Paco::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, pos: 0, with_callstack: false) ⇒ Context

Returns a new instance of Context.



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

def initialize(input, pos: 0, with_callstack: false)
  @input = input
  @pos = pos
  @callstack = Callstack.new if with_callstack
end

Instance Attribute Details

#callstackObject (readonly)

Returns the value of attribute callstack.



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

def callstack
  @callstack
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#last_posObject (readonly)

Returns the value of attribute last_pos.



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

def last_pos
  @last_pos
end

#posObject

Returns the value of attribute pos.



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

def pos
  @pos
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/paco/context.rb', line 25

def eof?
  pos >= input.length
end

#failure_parse(parser) ⇒ Object

Parameters:



36
37
38
# File 'lib/paco/context.rb', line 36

def failure_parse(parser)
  @callstack&.failure(pos: pos, parser: parser.desc)
end

#index(from = nil) ⇒ Paco::Index

Parameters:

  • from (Integer) (defaults to: nil)

Returns:



31
32
33
# File 'lib/paco/context.rb', line 31

def index(from = nil)
  Index.calculate(input: input, pos: from || pos)
end

#read(n) ⇒ Object



17
18
19
# File 'lib/paco/context.rb', line 17

def read(n)
  input[pos, n]
end

#read_allObject



21
22
23
# File 'lib/paco/context.rb', line 21

def read_all
  input[pos..]
end

#start_parse(parser) ⇒ Object

Parameters:



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

def start_parse(parser)
  @callstack&.start(pos: pos, parser: parser.desc)
end

#success_parse(result, parser) ⇒ Object

Parameters:



47
48
49
# File 'lib/paco/context.rb', line 47

def success_parse(result, parser)
  @callstack&.success(pos: pos, result: result, parser: parser.desc)
end