From e4df40c822467d6e394b7522b7c4dae79c712692 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 30 Mar 2023 22:08:17 +1300 Subject: Implement custom error messages for Parser.flunk --- src/parcom/parser.cr | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr index e7442db..2b058d4 100644 --- a/src/parcom/parser.cr +++ b/src/parcom/parser.cr @@ -36,9 +36,16 @@ module Parcom Parser(T, U).new("Pure #{value}") { |tokens| Result.new(tokens, value) } end + # Creates a parser that always fails with the given error message. + def self.flunk(message : String) : Parser(T, U) + Parser(T, U).new("Flunk") do |_| + raise ParserFail.new(message) + end + end + # Creates a parser that always fails. def self.flunk : Parser(T, U) - Parser(T, U).new("Flunk") { |_| raise ParserFail.new("Flunked parser") } + flunk("Flunked parser") end # Creates a parser that returns the first token in the input stream. -- cgit v1.2.1