diff options
| author | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-30 22:08:17 +1300 |
|---|---|---|
| committer | Matthew Hall <hallmatthew314@gmail.com> | 2023-03-30 22:08:17 +1300 |
| commit | e4df40c822467d6e394b7522b7c4dae79c712692 (patch) | |
| tree | 1786502ada3c955e09cfea66b957ef44e4ef8619 /src | |
| parent | 4a502b4f55e7c49e8a8322b694d7e00468aa431d (diff) | |
Implement custom error messages for Parser.flunk
Diffstat (limited to 'src')
| -rw-r--r-- | src/parcom/parser.cr | 9 |
1 files changed, 8 insertions, 1 deletions
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. |
