Method: Parslet::Source#initialize

Defined in:
lib/parslet/source.rb

#initialize(str) ⇒ Source

Returns a new instance of Source.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/parslet/source.rb', line 12

def initialize(str)
  raise(
    ArgumentError, 
    "Must construct Source with a string like object."
  ) unless str.respond_to?(:to_str)

  @str = StringScanner.new(str)

  # maps 1 => /./m, 2 => /../m, etc...
  @re_cache = Hash.new { |h,k| 
    h[k] = /(.|$){#{k}}/m }

  @line_cache = LineCache.new
  @line_cache.scan_for_line_endings(0, str)
end