File: gobbleup.rb

package info (click to toggle)
ruby-parslet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 473
  • sloc: ruby: 5,220; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

require 'parslet'

# This is where the famous GobbleUp atom is defined. This parslet atom consumes
# all chars until a given string (absent) is encountered.
#
class GobbleUp < Parslet::Atoms::Base
  def initialize absent, min_chars=0
    @absent = absent
    @min_chars = min_chars
  end

  def try(source, context, consume_all)
    excluding_length = source.chars_until(@absent)

    if excluding_length >= @min_chars
      return succ(source.consume(excluding_length))
    else
      return context.err(self, source, "No such string in input: #{@absent.inspect}.")
    end
  end

  def to_s_inner(prec)
    "until('#{@absent}')"
  end
end