simple_ternary/
lib.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

extern crate proc_macro;

use quote::quote;
use syn::{self, parse::{Parse, ParseStream}, parse_macro_input, Token};

// ternary: expression '?' expression ':' expression
// expression: ternary | simple

struct Ternary {
    condition: syn::Expr,
    true_branch: Expression,
    false_branch: Expression,
}

impl Parse for Ternary {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let condition = input.parse()?;
        input.parse::<Token![?]>()?;
        let true_branch = input.parse()?;
        input.parse::<Token![:]>()?;
        let false_branch = input.parse()?;

        Ok(Self {
            condition,
            true_branch,
            false_branch,
        })
    }
}

impl quote::ToTokens for Ternary {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let condition = &self.condition;

        let true_branch = match &self.true_branch {
            Expression::Ternary(ternary) => quote! { #ternary },
            Expression::Simple(simple) => quote! { #simple },
        };

        let false_branch = match &self.false_branch {
            Expression::Ternary(ternary) => quote! { #ternary },
            Expression::Simple(simple) => quote! { #simple },
        };

        tokens.extend(quote! { if #condition { #true_branch } else { #false_branch } });
    }
}
struct SimpleExpression {
    expr: syn::Expr,
}

impl Parse for SimpleExpression {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        Ok(Self {
            expr: input.parse()?,
        })
    }
}

impl quote::ToTokens for SimpleExpression {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let expr = &self.expr;
        tokens.extend(quote! { #expr });
    }
}

enum Expression {
    Ternary(Box<Ternary>),
    Simple(SimpleExpression),
}

impl Parse for Expression {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let expr: syn::Expr = input.parse()?;

        if input.peek(Token![?]) {
            input.parse::<Token![?]>()?;
            let true_branch = input.parse()?;
            input.parse::<Token![:]>()?;
            let false_branch = input.parse()?;

            Ok(Self::Ternary(Box::new(Ternary {
                condition: expr,
                true_branch,
                false_branch,
            })))
        } 
        else {
            Ok(Self::Simple(SimpleExpression { expr }))
        }
    }
}

#[proc_macro]
pub fn tnr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let tern = parse_macro_input!(input as Ternary);
    quote! { #tern }.into()
}