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
use crate::ast::{Node, Loc, IdentifierNode, ExpressionNode};
use crate::ast::{BlockNode, Statement, PatternList, PropertyKey};

pub trait Name<'ast>: Copy {
    fn empty() -> Self;
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct EmptyName;

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct MandatoryName<'ast>(pub IdentifierNode<'ast>);

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct OptionalName<'ast>(pub Option<IdentifierNode<'ast>>);

pub type Method<'ast> = Function<'ast, EmptyName>;

impl<'ast> Name<'ast> for EmptyName {
    fn empty() -> Self {
        EmptyName
    }
}

impl<'ast> Name<'ast> for MandatoryName<'ast> {
    fn empty() -> Self {
        MandatoryName(Node::new(&Loc {
            start: 0,
            end: 0,
            item: ""
        }))
    }
}

impl<'ast> Name<'ast> for OptionalName<'ast> {
    fn empty() -> Self {
        OptionalName(None)
    }
}

#[cfg(test)]
impl<'ast> From<IdentifierNode<'ast>> for MandatoryName<'ast> {
    #[inline]
    fn from(name: IdentifierNode<'ast>) -> Self {
        MandatoryName(name)
    }
}

#[cfg(test)]
impl<'ast> From<IdentifierNode<'ast>> for OptionalName<'ast> {
    #[inline]
    fn from(name: IdentifierNode<'ast>) -> Self {
        OptionalName(Some(name))
    }
}

#[cfg(test)]
impl<'ast> From<Option<IdentifierNode<'ast>>> for OptionalName<'ast> {
    #[inline]
    fn from(name: Option<IdentifierNode<'ast>>) -> Self {
        OptionalName(name)
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Function<'ast, N: Name<'ast>> {
    pub name: N,
    pub generator: bool,
    pub params: PatternList<'ast>,
    pub body: BlockNode<'ast, Statement<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MethodKind {
    Constructor,
    Method,
    Get,
    Set,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ClassMember<'ast> {
    Error,
    Method {
        is_static: bool,
        key: Node<'ast, PropertyKey<'ast>>,
        kind: MethodKind,
        value: Node<'ast, Function<'ast, EmptyName>>,
    },
    Literal {
        is_static: bool,
        key: Node<'ast, PropertyKey<'ast>>,
        value: ExpressionNode<'ast>,
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Class<'ast, N: Name<'ast>> {
    pub name: N,
    pub extends: Option<ExpressionNode<'ast>>,
    pub body: BlockNode<'ast, ClassMember<'ast>>,
}