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
use crate::lexer::span::{ Loc, Span, LineColumn, };
use crate::lexer::token::Identifier;
use crate::ast::statement::Statement;
use crate::ast::expression::{ Expression, ParenthesizedExpression, };
use crate::ast::function::{ Function, FunctionBody, };


// PropertyName
//      IdentifierName
//      StringLiteral
//      NumericLiteral
//      Expression

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ClassDeclaration<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: Identifier<'ast>,
    pub class: Class<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ClassExpression<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: Option<Identifier<'ast>>,
    pub class: Class<'ast>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Method<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub is_async: bool,
    pub is_generator: bool,
    pub name: Expression<'ast>,
    pub params: ParenthesizedExpression<'ast>,
    pub body: FunctionBody<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Getter<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: Expression<'ast>,
    pub body: FunctionBody<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Setter<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: Expression<'ast>,
    pub params: ParenthesizedExpression<'ast>,
    pub body: FunctionBody<'ast>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MethodDefinition<'ast> {
    Method(Method<'ast>),
    Getter(Getter<'ast>),
    Setter(Setter<'ast>),
}

impl<'ast> MethodDefinition<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            MethodDefinition::Method(inner) => inner.loc,
            MethodDefinition::Getter(inner) => inner.loc,
            MethodDefinition::Setter(inner) => inner.loc,
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            MethodDefinition::Method(inner) => inner.span,
            MethodDefinition::Getter(inner) => inner.span,
            MethodDefinition::Setter(inner) => inner.span,
        }
    }

    pub fn name(&self) -> &Expression<'ast> {
        match *self {
            MethodDefinition::Getter(ref getter) => &getter.name,
            MethodDefinition::Setter(ref setter) => &setter.name,
            MethodDefinition::Method(ref method) => &method.name,
        }
    }

    pub fn is_async(&self) -> bool {
        match *self {
            MethodDefinition::Getter(_)
            | MethodDefinition::Setter(_) => false,
            MethodDefinition::Method(ref method) => method.is_async,
        }
    }

    pub fn is_generator(&self) -> bool {
        match *self {
            MethodDefinition::Getter(_)
            | MethodDefinition::Setter(_) => false,
            MethodDefinition::Method(ref method) => method.is_generator,
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ClassMethodDefinition<'ast> {
    pub is_static: bool,
    pub method: MethodDefinition<'ast>,
}

impl<'ast> ClassMethodDefinition<'ast> {
    pub fn loc(&self) -> Loc {
        self.method.loc()
    }

    pub fn span(&self) -> Span {
        self.method.span()
    }

    pub fn name(&self) -> &Expression<'ast> {
        self.method.name()
    }
    
    pub fn is_static(&self) -> bool {
        self.is_static
    }
    
    pub fn is_async(&self) -> bool {
        self.method.is_async()
    }

    pub fn is_generator(&self) -> bool {
        self.method.is_generator()
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Class<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub heritage: Option<Expression<'ast>>, // extend
    pub body: &'ast [ ClassMethodDefinition<'ast> ],
}