Class: Parslet::Source

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/csscss/parslet_optimizations.rb

Defined Under Namespace

Classes: LineCache

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Source

Returns a new instance of Source.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
# File 'lib/csscss/parslet_optimizations.rb', line 14

def initialize(str)
  raise ArgumentError unless str.respond_to?(:to_str)

  @str = StringScanner.new(str)

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

Instance Method Details

#chars_leftObject



40
41
42
# File 'lib/csscss/parslet_optimizations.rb', line 40

def chars_left
  @str.rest_size
end

#consume(n) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/csscss/parslet_optimizations.rb', line 29

def consume(n)
  original_pos = @str.pos
  slice_str = n.times.map { @str.getch }.join
  slice = Parslet::Slice.new(
    slice_str,
    original_pos,
    @line_cache)

  return slice
end

#matches?(pattern) ⇒ Boolean Also known as: match

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/csscss/parslet_optimizations.rb', line 23

def matches?(pattern)
  regexp = pattern.is_a?(String) ? Regexp.new(Regexp.escape(pattern)) : pattern
  !@str.match?(regexp).nil?
end

#pos=(n) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/csscss/parslet_optimizations.rb', line 45

def pos=(n)
  if n > @str.string.bytesize
    @str.pos = @str.string.bytesize
  else
    @str.pos = n
  end
end