1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use thiserror::Error;

use super::LRConflictError;

// ---------------------------------------------------
// Part of the Public API
// *Changes will affect crate's version according to semver*
// ---------------------------------------------------

/// Error type used by the [crate::analysis] module
#[derive(Error, Debug)]
pub enum GrammarAnalysisError {
    /// Left-recursions detected
    #[error("Grammar contains left-recursions")]
    LeftRecursion {
        /// Recursions
        recursions: Vec<RecursiveNonTerminal>,
    },

    /// Unreachable non-terminals are not allowed.
    #[error("Grammar contains unreachable non-terminals")]
    UnreachableNonTerminals {
        /// Non-terminals
        non_terminals: Vec<RelatedHint>,
    },

    /// Nonproductive non-terminals are not allowed.
    #[error("Grammar contains nonproductive non-terminals")]
    NonProductiveNonTerminals {
        /// Non-terminals
        non_terminals: Vec<RelatedHint>,
    },

    /// Maximum lookahead exceeded.
    #[error("Maximum lookahead of {max_k} exceeded")]
    MaxKExceeded {
        /// Maximum lookahead
        max_k: usize,
    },

    /// The LALR(1) parse table construction failed with conflicts.
    #[error("LALR(1) parse table construction failed with conflicts")]
    LALR1ParseTableConstructionFailed {
        /// Conflict
        conflict: LRConflictError,
    },
}

/// A single recursive non-terminal
#[derive(Error, Debug)]
#[error("Recursive non-terminal #{number}: '{name}'")]
pub struct RecursiveNonTerminal {
    /// The number of the recursion path
    pub number: usize,
    /// non-terminal
    pub name: String,
}

/// Related information
#[derive(Error, Debug)]
#[error("{topic}: {hint}")]
pub struct RelatedHint {
    /// A topic or a category to describe the hint
    pub topic: String,
    /// Information
    pub hint: String,
}