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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! The visitor module for the AST.
//!
//! The visitor is based on an Enum over every AST node.
//!
//! Example usages:
//! ```rust,ignore
//! struct MyVisitor<'a, 'f> {
//!     some_data: Vec<u32>,
//! }
//!
//! impl<'a, 'f> MyVisitor<'a, 'f> {
//!     fn new() -> Self {
//!         Self {
//!             some_data: Vec::new(),
//!         }
//!     }
//!
//!     fn do_visit(&mut self, node: &NodeKind<'a, 'f>) {
//!         use self::NodeKind::*;
//!         node.for_each_child(&mut |child| {
//!             match child {
//!                 AST(_) => (),
//!                 Program(_) => {
//!                     // some code
//!                 }
//!                 _ => (),
//!             }
//!
//!             self.do_visit(&child)
//!         })
//!     }
//! }
//! ```
use super::ast::{self, *};
use asciifile::Spanned;
use strum_macros::EnumDiscriminants;

#[strum_discriminants(derive(Display))]
#[derive(EnumDiscriminants)]
pub enum NodeKind<'a, 't> {
    AST(&'a AST<'t>),
    Program(&'a Spanned<'t, Program<'t>>),
    ClassDeclaration(&'a Spanned<'t, ClassDeclaration<'t>>),
    ClassMember(&'a Spanned<'t, ClassMember<'t>>),
    Parameter(&'a Spanned<'t, Parameter<'t>>),
    ParameterList(&'a Spanned<'t, ParameterList<'t>>),
    Type(&'a Spanned<'t, Type<'t>>),
    BasicType(&'a Spanned<'t, BasicType<'t>>),
    Block(&'a Spanned<'t, Block<'t>>),
    Stmt(&'a Spanned<'t, Stmt<'t>>),
    Expr(&'a Spanned<'t, Expr<'t>>),
    BinaryOp(&'a BinaryOp),
    UnaryOp(&'a UnaryOp),
}

#[macro_export]
macro_rules! gen_nodekind_match {
    ($nodekindvar:expr, $varname:ident => $rhs:expr) => {
        match $nodekindvar {
            NodeKind::AST($varname) => $rhs,
            NodeKind::ClassDeclaration($varname) => $rhs,
            NodeKind::Program($varname) => $rhs,
            NodeKind::ClassMember($varname) => $rhs,
            NodeKind::Parameter($varname) => $rhs,
            NodeKind::ParameterList($varname) => $rhs,
            NodeKind::Type($varname) => $rhs,
            NodeKind::BasicType($varname) => $rhs,
            NodeKind::Block($varname) => $rhs,
            NodeKind::Stmt($varname) => $rhs,
            NodeKind::Expr($varname) => $rhs,
            NodeKind::BinaryOp($varname) => $rhs,
            NodeKind::UnaryOp($varname) => $rhs,
        }
    };
}

use std::fmt::Debug;
impl Debug for NodeKind<'_, '_> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        gen_nodekind_match!(self, v => v.fmt(fmt))
    }
}

pub trait VisitResult {
    fn stop_visit(&self) -> bool;
}

impl VisitResult for () {
    fn stop_visit(&self) -> bool {
        false
    }
}

impl<X, E> VisitResult for Result<X, E> {
    fn stop_visit(&self) -> bool {
        self.is_err()
    }
}

impl<'a, 't> NodeKind<'a, 't> {
    /// Visit the children of `self`, invoking `cb` on each.
    /// If `self` has no children, this method returns `None, otherwise
    /// `Some(res)` where `res` is the return value of `cb`.
    #[allow(clippy::cyclomatic_complexity)]
    pub fn for_each_child<R>(&self, cb: &mut dyn FnMut(NodeKind<'a, 't>) -> R) -> Option<R>
    where
        R: VisitResult,
    {
        use self::NodeKind::*;
        macro_rules! ccb {
            ($astvar:expr) => {{
                let res = cb(NodeKind::from($astvar));
                if res.stop_visit() {
                    return Some(res);
                }
                Some(res)
            }};
            (for_each $vec:expr) => {{
                let iterable = $vec;
                let mut ret = None;
                for n in iterable {
                    let res = cb(NodeKind::from(n));
                    if res.stop_visit() {
                        return Some(res);
                    }
                    ret = Some(res);
                }
                ret
            }};
        }
        match self {
            AST(ast) => {
                use crate::ast::AST::*;
                match ast {
                    Empty => None,
                    Program(p) => ccb!(p),
                }
            }
            Program(p) => ccb!(for_each p.classes.iter()),
            ClassDeclaration(d) => ccb!(for_each d.members.iter()),
            ClassMember(cm) => {
                use crate::ast::ClassMemberKind::*;
                match &cm.kind {
                    Field(t) => ccb!(t),
                    Method(ty, pl, block) => {
                        ccb!(ty);
                        cb(NodeKind::ParameterList(&pl));
                        ccb!(block)
                    }
                    MainMethod(_, block) => ccb!(block),
                }
            }
            Parameter(p) => ccb!(&p.ty),
            ParameterList(l) => ccb!(for_each l.iter()),
            Type(t) => Some(cb(NodeKind::from(&t.basic))),
            Block(b) => ccb!(for_each b.statements.iter()),
            Stmt(s) => {
                use crate::ast::Stmt::*;
                match &s.data {
                    Empty => None,
                    Block(b) => ccb!(b),
                    Expression(e) => ccb!(e.as_ref()),
                    If(expr, then_stmt, else_stmt) => {
                        ccb!(expr.as_ref());
                        ccb!(then_stmt.as_ref());
                        ccb!(for_each else_stmt.iter().map(|x| x.as_ref()))
                    }
                    LocalVariableDeclaration(t, _, expr) => {
                        ccb!(t);
                        ccb!(for_each expr.iter().map(|x| x.as_ref()))
                    }
                    Return(expr) => ccb!(for_each expr.iter().map(|x| x.as_ref())),
                    While(cond, stmt) => {
                        ccb!(cond.as_ref());
                        ccb!(stmt.as_ref())
                    }
                }
            }
            Expr(e) => {
                use crate::ast::Expr::*;
                match &e.data {
                    Binary(_, lhs, rhs) => {
                        ccb!(lhs.as_ref());
                        ccb!(rhs.as_ref())
                    }
                    Unary(_, expr) | NewArray(_, expr, _) => ccb!(expr.as_ref()),
                    MethodInvocation(target_expr, _, al) => {
                        ccb!(target_expr.as_ref());
                        ccb!(for_each al.data.iter())
                    }
                    FieldAccess(target_expr, _) => ccb!(target_expr.as_ref()),
                    ArrayAccess(target_expr, idx_expr) => {
                        ccb!(target_expr.as_ref());
                        ccb!(idx_expr.as_ref())
                    }
                    ThisMethodInvocation(_, al) => ccb!(for_each al.iter()),
                    Null | Boolean(_) | Int(_) | NegInt(_) | Var(_) | This | NewObject(_) => None,
                }
            }
            BasicType(_) | BinaryOp(_) | UnaryOp(_) => None,
        }
    }
}

macro_rules! gen_from_ast {
    ($nodekind:path, $spanned_asttype:ty) => {
        impl<'a, 'f> From<&'a $spanned_asttype> for NodeKind<'a, 'f> {
            fn from(a: &'a $spanned_asttype) -> Self {
                $nodekind(a)
            }
        }
    };
}

gen_from_ast!(NodeKind::AST<'a, 'f>, ast::AST<'f>);
gen_from_ast!(NodeKind::Program<'a, 'f>, Spanned<'f, ast::Program<'f>>);
gen_from_ast!(
    NodeKind::ClassDeclaration<'a, 'f>,
    Spanned<'f, ast::ClassDeclaration<'f>>
);
gen_from_ast!(
    NodeKind::ClassMember<'a, 'f>,
    Spanned<'f, ast::ClassMember<'f>>
);
gen_from_ast!(NodeKind::Parameter<'a, 'f>, Spanned<'f, ast::Parameter<'f>>);
gen_from_ast!(
    NodeKind::ParameterList<'a, 'f>,
    Spanned<'f, ast::ParameterList<'f>>
);
gen_from_ast!(NodeKind::Type<'a, 'f>, Spanned<'f, ast::Type<'f>>);
gen_from_ast!(NodeKind::BasicType<'a, 'f>, Spanned<'f, ast::BasicType<'f>>);
gen_from_ast!(NodeKind::Block<'a, 'f>, Spanned<'f, ast::Block<'f>>);
gen_from_ast!(NodeKind::Stmt<'a, 'f>, Spanned<'f, ast::Stmt<'f>>);
gen_from_ast!(NodeKind::Expr<'a, 'f>, Spanned<'f, ast::Expr<'f>>);
gen_from_ast!(NodeKind::BinaryOp<'a, 'f>, ast::BinaryOp);
gen_from_ast!(NodeKind::UnaryOp<'a, 'f>, ast::UnaryOp);