aboutsummaryrefslogtreecommitdiff
path: root/src/__OLD_parcom/eof.cr
blob: 650da56d078fce4cc8686fd532d50902e693ded1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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