aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthew Hall <hallmatthew314@gmail.com>2023-03-11 18:35:54 +1300
committerMatthew Hall <hallmatthew314@gmail.com>2023-03-11 18:35:54 +1300
commit350aae53131d70979108023ab0ac11379e024011 (patch)
treee8754e4fe6913d092e13e67117c389b8ba4d6228 /src
parent15315d9ea672441779615b3426710c765f5e8f1e (diff)
Rename Eof to EOF + documentation
Diffstat (limited to 'src')
-rw-r--r--src/parcom.cr12
-rw-r--r--src/parcom/eof.cr23
2 files changed, 24 insertions, 11 deletions
diff --git a/src/parcom.cr b/src/parcom.cr
index f6d1e05..4612887 100644
--- a/src/parcom.cr
+++ b/src/parcom.cr
@@ -81,16 +81,6 @@ module Parcom
end
end
- class Eof(T) < Parser(T, Nil)
- 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
-
class Peek(T, V) < Parser(T, V)
def initialize(@p : Parser(T, V))
end
@@ -179,7 +169,7 @@ module Parcom
@p : Map(T, {V, Nil}, V)
def initialize(p : Parser(T, V))
- @p = (p + Eof(T).new).map &.first
+ @p = (p + EOF(T).new).map &.first
end
def parse(tokens : Tokens(T)) : Result(T, V)
diff --git a/src/parcom/eof.cr b/src/parcom/eof.cr
new file mode 100644
index 0000000..650da56
--- /dev/null
+++ b/src/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
+