Method: ParsingMachine#initialize

Defined in:
lib/rpeg/parsing_machine.rb

#initialize(program, subject, initial_pos, extra_args) ⇒ ParsingMachine

program: the program to run subject: the string to match against initial_pos: the position in subject to start the search at extra_args may have been supplied in the initial #match call. These are consumed by Argument Captures.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rpeg/parsing_machine.rb', line 117

def initialize(program, subject, initial_pos, extra_args)
  @program = program.clone.freeze.must_only_contain(Instruction)
  @prog_len = @program_size

  # When benchmarking with a large subject (4.5 MB) I found that the search took an enormous amount of time (1300 s), and 95% of
  # it was due to 3.6 million calls to String::[]. I don't know why this was so slow, as accessing a large string in a small
  # scratch loop is fast. I am very confused. Converting the string to an array of chars is much faster (1300 s became 9 s).
  # @original_subject = subject.clone.freeze
  @subject = subject.chars.freeze
  @subject_size = @subject.size

  @i_ptr = 0 # index in @program of the next instruction
  @subject_index = initial_pos
  @stack = []
  @breadcrumbs = [].must_only_ever_contain(Capture::Breadcrumb) # the records of the captures we make during parsing
  @bread_count = 0 # the number of breadcrumbs in @breadcrumbs (some in the array may be stale)

  @extra_args = extra_args.clone
end