lib/dolos/parsers.rb in dolos-0.1.2 vs lib/dolos/parsers.rb in dolos-0.1.3
- old
+ new
@@ -8,18 +8,21 @@
utf8_str = str.encode('UTF-8')
if state.input.matches?(utf8_str)
Success.new(utf8_str, str.bytesize)
else
advanced = state.input.offset
+ got_error = state.input.io.string.byteslice(state.input.backup, advanced)
state.input.rollback
Failure.new(
- "Expected #{str.inspect} but got #{state.input.io.string.inspect}",
- advanced
+ "Expected #{str.inspect} but got #{got_error.inspect}",
+ advanced,
+ state
)
end
end
end
+
alias_method :c, :string
def regex(pattern)
Parser.new do |state|
state.input.mark_offset
@@ -28,17 +31,17 @@
else
advanced = state.input.offset
state.input.rollback
Failure.new(
"Expected pattern #{pattern.inspect} but got #{state.input.io.string.inspect}",
- advanced
+ advanced,
+ state
)
end
end
end
-
def any_char
Parser.new do |state|
state.input.mark_offset
char, = state.input.peek(1)
@@ -46,11 +49,15 @@
if char && !char.empty?
Success.new(char, char.bytesize)
else
advanced = state.input.offset
state.input.rollback
- Failure.new('Expected any character but got end of input', advanced)
+ Failure.new(
+ 'Expected any character but got end of input',
+ advanced,
+ state
+ )
end
end
end
# Matches any character in a string
@@ -69,11 +76,12 @@
else
advanced = state.input.offset
state.input.rollback
Failure.new(
"Expected one of #{characters_array.inspect} but got #{char.inspect}",
- advanced
+ advanced,
+ state
)
end
end
end
@@ -90,10 +98,14 @@
state.input.advance(bytesize)
end
if buffer.empty?
advanced = state.input.offset
- Failure.new("Predicate never returned true", advanced)
+ Failure.new(
+ "Predicate never returned true",
+ advanced,
+ state
+ )
else
Success.new(buffer, 0)
end
end
end