cfgrammar/yacc/
mod.rs

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
#![deny(unreachable_pub)]

pub mod ast;
pub mod firsts;
pub mod follows;
pub mod grammar;
pub mod parser;

pub use self::{
    grammar::{AssocKind, Precedence, SentenceGenerator, YaccGrammar},
    parser::{YaccGrammarError, YaccGrammarErrorKind, YaccGrammarWarning, YaccGrammarWarningKind},
};
use proc_macro2::TokenStream;
use quote::quote;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum YaccKindResolver {
    /// The user can specify `%grmtools` in their grammar but unless it is compatible with this `YaccKind`, it's an error
    Force(YaccKind),
    /// Use `YaccKind` if the user doesn't specify `%grmtools` in their grammar
    Default(YaccKind),
    /// The user must specify `%grmtools` in their grammars or we throw an error
    NoDefault,
}

/// The particular Yacc variant this grammar makes use of.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum YaccKind {
    /// The original Yacc style as documented by
    /// [Johnson](http://dinosaur.compilertools.net/yacc/index.html),
    Original(YaccOriginalActionKind),
    /// Similar to the original Yacc style, but allowing individual rules' actions to have their
    /// own return type.
    Grmtools,
    /// The variant used in the [Eco language composition editor](http://soft-dev.org/src/eco/)
    Eco,
}

impl quote::ToTokens for YaccKind {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        tokens.extend(match *self {
            YaccKind::Grmtools => quote!(::cfgrammar::yacc::YaccKind::Grmtools),
            YaccKind::Original(action_kind) => {
                quote!(::cfgrammar::yacc::YaccKind::Original(#action_kind))
            }
            YaccKind::Eco => quote!(::cfgrammar::yacc::YaccKind::Eco),
        })
    }
}

#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum YaccOriginalActionKind {
    /// Execute user-specified actions attached to each production; also requires a %actiontype
    /// declaration.
    UserAction,
    /// Automatically create a parse tree instead of user-specified actions.
    GenericParseTree,
    /// Do not do execute actions of any sort.
    NoAction,
}

impl quote::ToTokens for YaccOriginalActionKind {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        tokens.extend(match *self {
            YaccOriginalActionKind::UserAction => {
                quote!(::cfgrammar::yacc::YaccOriginalActionKind::UserAction)
            }
            YaccOriginalActionKind::GenericParseTree => {
                quote!(::cfgrammar::yacc::YaccOriginalActionKind::GenericParseTree)
            }
            YaccOriginalActionKind::NoAction => {
                quote!(::cfgrammar::yacc::YaccOriginalActionKind::NoAction)
            }
        })
    }
}