aboutsummaryrefslogtreecommitdiff
path: root/src/__OLD_parcom/optional.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-19 23:02:29 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-19 23:02:29 +1300
commit9734fa2d530b9496b70a388a117ea57fe5730772 (patch)
tree78a42db1e64e5148edfa96cb2a451a17ef362485 /src/__OLD_parcom/optional.cr
parent2ef8841e9c7a48eea0f66cfe09d8fe996f43c2b2 (diff)
Remove old files
Diffstat (limited to 'src/__OLD_parcom/optional.cr')
-rw-r--r--src/__OLD_parcom/optional.cr40
1 files changed, 0 insertions, 40 deletions
diff --git a/src/__OLD_parcom/optional.cr b/src/__OLD_parcom/optional.cr
deleted file mode 100644
index b368d4e..0000000
--- a/src/__OLD_parcom/optional.cr
+++ /dev/null
@@ -1,40 +0,0 @@
-require "./parser.cr"
-
-module Parcom
- # `Optional` is a `Parser` that tries to parse with another parser,
- # but does not fail the parser chain if the wrapped parser fails.
- # If the wrapped parser fails, an `Optional` will not modify the input
- # stream, and return `nil` instead.
- #
- # Example:
- # ```
- # digit = Satisfy(Char).new { |c| c.digit? }
- # digits = Many.new(digit)
- # plus_sign = Optional.new(Token.new('+'))
- # positive_int = plus_sign >> digits
- #
- # input1 = Tokens.from_string("+12345")
- # input2 = Tokens.from_string("12345")
- #
- # # Both of these has the same result
- # positive_int.parse(input1)
- # positive_int.parse(input2)
- # ```
- class Optional(T, V) < Parser(T, V?)
- # Accepts the parser to use.
- def initialize(@p : Parser(T, V))
- end
-
- # Tries to parse with the given parser, succeeds with `nil`
- # instead of failing. This parser does not consume input if
- # the wrapped parser fails.
- def parse(tokens : Tokens(T)) : Result(T, V?)
- r = @p.parse?(tokens)
-
- new_tokens = r.nil? ? tokens : r.tokens
- new_value = r.nil? ? nil : r.value
- Result.new(new_tokens, new_value)
- end
- end
-end
-