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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Attributes can be attached to Symbols and to Productions.
//! They convey information that is temporarily available during the phase of grammar
//! transformation.
use std::fmt::{Debug, Display, Error, Formatter, Write};

use serde::{Deserialize, Serialize};
use ts_rs::TS;

/// Id type for tracking of optionals during grammar transformation
#[derive(Debug, Clone, Copy, Hash, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct OptionalId(usize);

/// Used to decorate an object's printed format
pub trait Decorate<T, W>
where
    T: Display,
    W: Write,
{
    /// Function used for decorated formatting
    fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error>;
}

///
/// Attributes applicable to a production or an alternation
///
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum ProductionAttribute {
    /// No valid attribute, default value
    None,
    /// Indicates a start of repetition, i.e. a collection
    CollectionStart,
    /// Add to a collection
    AddToCollection,
    /// Some case of an optional
    OptionalSome,
    /// None case of an optional
    OptionalNone,
}

impl Display for ProductionAttribute {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
        match self {
            Self::None => write!(f, "-"),
            Self::CollectionStart => write!(f, "Vec<T>::New"),
            Self::AddToCollection => write!(f, "Vec<T>::Push"),
            Self::OptionalSome => write!(f, "Option<T>::Some"),
            Self::OptionalNone => write!(f, "Option<T>::None"),
        }
    }
}

impl Default for ProductionAttribute {
    fn default() -> Self {
        Self::None
    }
}

impl<T, W> Decorate<T, W> for ProductionAttribute
where
    T: Display,
    W: Write,
{
    fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error> {
        match self {
            Self::None => out.write_fmt(format_args!("{}", decoratee)),
            Self::CollectionStart => {
                out.write_fmt(format_args!("{} /* `Vec<T>::New` */", decoratee))
            }
            Self::AddToCollection => {
                out.write_fmt(format_args!("{} /* `Vec<T>::Push` */", decoratee))
            }
            Self::OptionalSome => {
                out.write_fmt(format_args!("{} /* `Option<T>::Some` */", decoratee))
            }
            Self::OptionalNone => {
                out.write_fmt(format_args!("{} /* `Option<T>::None` */", decoratee))
            }
        }
    }
}

///
/// Attributes applicable to a grammar symbol
///
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, TS)]
#[ts(export)]
pub enum SymbolAttribute {
    /// No valid attribute, default value
    None,

    /// The symbol is actually a collection, i.e. a vector
    /// Is attached to a non-terminal symbol.
    /// If an argument with this attribute appears in the argument list of a semantic action
    /// this collection should be reversed.
    RepetitionAnchor,

    /// The symbol is an option with the inner type that is determined by the non-terminal
    Option,

    /// The symbol is not relevant for the AST and should not be propagated.
    Clipped,
}

impl Display for SymbolAttribute {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
        match self {
            Self::None => Ok(()),
            Self::RepetitionAnchor => write!(f, "`Vec<T>`"),
            Self::Option => write!(f, "`Option<T>`"),
            Self::Clipped => write!(f, "Clipped"),
        }
    }
}

impl Default for SymbolAttribute {
    fn default() -> Self {
        Self::None
    }
}

impl<T, W> Decorate<T, W> for SymbolAttribute
where
    T: Display,
    W: std::fmt::Write,
{
    fn decorate(&self, out: &mut W, decoratee: &T) -> std::result::Result<(), Error> {
        match self {
            Self::None => out.write_fmt(format_args!("{}", decoratee)),
            Self::RepetitionAnchor => out.write_fmt(format_args!("{} /* Vec */", decoratee)),
            Self::Option => out.write_fmt(format_args!("{} /* Option */", decoratee)),
            Self::Clipped => out.write_fmt(format_args!("{}^ /* Clipped */", decoratee)),
        }
    }
}