cfgrammar/yacc/
mod.rs

1#![deny(unreachable_pub)]
2
3pub mod ast;
4pub mod firsts;
5pub mod follows;
6pub mod grammar;
7pub mod parser;
8
9pub use self::{
10    grammar::{AssocKind, Precedence, SentenceGenerator, YaccGrammar},
11    parser::{YaccGrammarError, YaccGrammarErrorKind, YaccGrammarWarning, YaccGrammarWarningKind},
12};
13use proc_macro2::TokenStream;
14use quote::quote;
15
16#[cfg(feature = "serde")]
17use serde::{Deserialize, Serialize};
18
19/// The particular Yacc variant this grammar makes use of.
20#[derive(Clone, Copy, Debug)]
21#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
22#[non_exhaustive]
23pub enum YaccKind {
24    /// The original Yacc style as documented by
25    /// [Johnson](http://dinosaur.compilertools.net/yacc/index.html),
26    Original(YaccOriginalActionKind),
27    /// Similar to the original Yacc style, but allowing individual rules' actions to have their
28    /// own return type.
29    Grmtools,
30    /// The variant used in the [Eco language composition editor](http://soft-dev.org/src/eco/)
31    Eco,
32}
33
34impl quote::ToTokens for YaccKind {
35    fn to_tokens(&self, tokens: &mut TokenStream) {
36        tokens.extend(match *self {
37            YaccKind::Grmtools => quote!(::cfgrammar::yacc::YaccKind::Grmtools),
38            YaccKind::Original(action_kind) => {
39                quote!(::cfgrammar::yacc::YaccKind::Original(#action_kind))
40            }
41            YaccKind::Eco => quote!(::cfgrammar::yacc::YaccKind::Eco),
42        })
43    }
44}
45
46#[derive(Clone, Copy, Debug)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48pub enum YaccOriginalActionKind {
49    /// Execute user-specified actions attached to each production; also requires a %actiontype
50    /// declaration.
51    UserAction,
52    /// Automatically create a parse tree instead of user-specified actions.
53    GenericParseTree,
54    /// Do not do execute actions of any sort.
55    NoAction,
56}
57
58impl quote::ToTokens for YaccOriginalActionKind {
59    fn to_tokens(&self, tokens: &mut TokenStream) {
60        tokens.extend(match *self {
61            YaccOriginalActionKind::UserAction => {
62                quote!(::cfgrammar::yacc::YaccOriginalActionKind::UserAction)
63            }
64            YaccOriginalActionKind::GenericParseTree => {
65                quote!(::cfgrammar::yacc::YaccOriginalActionKind::GenericParseTree)
66            }
67            YaccOriginalActionKind::NoAction => {
68                quote!(::cfgrammar::yacc::YaccOriginalActionKind::NoAction)
69            }
70        })
71    }
72}