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
|