ielr/output/
lookahead.rs

1/*
2 * Copyright 2022 Arnaud Golfouse
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use crate::input::Token;
10use std::{fmt, hash};
11
12/// Either a [`Token`], or `EOF`.
13#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
14pub enum Lookahead {
15    /// End-of-file.
16    ///
17    /// Sometimes, a rule will have this as lookahead: this means that this rule may
18    /// appear at the very end of the text.
19    ///
20    /// For example, in Rust code:
21    /// - A `let` binding _may not_ appear at the end of a file, and thus can never be
22    /// followed by `Eof`.
23    /// - A function _may_ appear at the end of a file, and thus can be followed by
24    /// `Eof`.
25    Eof,
26    /// A normal token: see [`Token`].
27    Token(Token),
28}
29
30impl Lookahead {
31    fn plain(self) -> crate::indices::PlainToken {
32        match self {
33            Lookahead::Eof => 0,
34            Lookahead::Token(t) => t.get(),
35        }
36    }
37}
38
39impl PartialEq for Lookahead {
40    fn eq(&self, other: &Self) -> bool {
41        self.plain() == other.plain()
42    }
43}
44
45impl hash::Hash for Lookahead {
46    fn hash<H: hash::Hasher>(&self, state: &mut H) {
47        self.plain().hash(state);
48    }
49}
50
51impl fmt::Debug for Lookahead {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            Self::Eof => write!(f, "Eof"),
55            Self::Token(t) => write!(f, "Token({:?})", t.get()),
56        }
57    }
58}