aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-26 15:37:26 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-26 15:37:26 +1300
commitf156d80cc3c80385456bc3ec80adb5ad892f5039 (patch)
treeb0f651e9c5c2dee779e941ac9d1eb9a20ddaabb7 /src
parented57fac2de48c7a86536ae22c77540f787d49a81 (diff)
Implement map_const
Diffstat (limited to 'src')
-rw-r--r--src/parcom/parser.cr12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/parcom/parser.cr b/src/parcom/parser.cr
index fd161f9..57c7b44 100644
--- a/src/parcom/parser.cr
+++ b/src/parcom/parser.cr
@@ -207,6 +207,18 @@ module Parcom
map(block)
end
+ # Replaces the value inside `self` when parsing is successful.
+ # This is effectively a shorthand for the following:
+ # ```
+ # a = Parser.token('a')
+ # # These are equivalent:
+ # into42_map = a.map { |_| 42 }
+ # into42_map_const = a.map_const(42)
+ # ```
+ def map_const(value : V) : Parser(T, V) forall V
+ map { |_| value }.named("#{@name} (const #{value})")
+ end
+
# Creates a new parser from `self` and another parser that will
# try to parse with either of them. If the first parser succeeds,
# it will return the result of the first parser. Otherwise, it will