aboutsummaryrefslogtreecommitdiff
path: root/src/__OLD_parcom/eof.cr
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-16 20:36:03 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-16 20:36:03 +1300
commitb274828831fec26cd8b3089ffef14cb96ce2de2f (patch)
treeff5927b85a59b4d85c9e4aa269a475a7a37a54a0 /src/__OLD_parcom/eof.cr
parent77c370d27be174e0b036b33d1469e84e67a7153a (diff)
Second rewrite attempt, this one should work, monkaS
Diffstat (limited to 'src/__OLD_parcom/eof.cr')
-rw-r--r--src/__OLD_parcom/eof.cr23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/__OLD_parcom/eof.cr b/src/__OLD_parcom/eof.cr
new file mode 100644
index 0000000..650da56
--- /dev/null
+++ b/src/__OLD_parcom/eof.cr
@@ -0,0 +1,23 @@
+require "./parser.cr"
+
+module Parcom
+ # `EOF` is a `Parser` succeeds if the input stream is empty.
+ #
+ # This parser retuns `nil` when successful because there is no
+ # other meaningful value to return.
+ #
+ # This parser is also one of the few cases where it is ideal to not
+ # modify or take from the input stream, as it should be empty anyway.
+ class EOF(T) < Parser(T, Nil)
+ # Succeeds is the input stream is empty and returns `nil`.
+ # Raises a `ParserFail` exception when the input is not empty.
+ def parse(tokens : Tokens(T)) : Result(T, Nil)
+ if tokens.empty?
+ Result.new(tokens, nil)
+ else
+ raise ParserFail.new("Eof: input was not empty")
+ end
+ end
+ end
+end
+