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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use crate::toolshed::{ Arena, };

use crate::version::ECMAScriptVersion;
use crate::error::{ ErrorKind, Error, };

use crate::lexer::Lexer;
use crate::lexer::span::{ Loc, Span, LineColumn, };
use crate::lexer::token::{ Token, Punctuator, Identifier, LiteralString, LiteralNumeric, };
use crate::ast::statement::{ 
    Statement,
    VariableStatement, LexicalDeclarationKind, LexicalBinding,
};
use crate::ast::expression::{
    Expression, LiteralTemplateExpression,
    PrefixExpression, InfixExpression, PostfixExpression, AssignmentExpression,
    MemberExpression, NewTargetExpression, NewExpression,
    ConditionalExpression, YieldExpression, CommaExpression,
    TaggedTemplateExpression, SpreadExpression, ParenthesizedExpression,
    CallExpression, 
};
use crate::ast::function::{ FunctionExpression, Function, ArrowFunctionExpression, ConciseBody, };
use crate::ast::class::{
    ClassDeclaration, ClassExpression, Class, ClassMethodDefinition, 
    MethodDefinition, Method, Getter, Setter,
};


#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PropertyName<'ast> {
    Identifier(Identifier<'ast>),
    Numberic(LiteralNumeric<'ast>),
    String(LiteralString<'ast>),
    Computed(Expression<'ast>),
}

impl<'ast> PropertyName<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            PropertyName::Identifier(inner) => inner.loc,
            PropertyName::Numberic(inner) => inner.loc,
            PropertyName::String(inner) => inner.loc,
            PropertyName::Computed(inner) => inner.loc(),
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            PropertyName::Identifier(inner) => inner.span,
            PropertyName::Numberic(inner) => inner.span,
            PropertyName::String(inner) => inner.span,
            PropertyName::Computed(inner) => inner.span(),
        }
    }
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PropertyDefinition<'ast> {
    // Destructuring or ObjectLiteral
    // Ident,
    Identifier(Identifier<'ast>),
    // Destructuring
    // Key = Value
    CoverInitializedName {
        loc: Loc,
        span: Span,
        name: PropertyName<'ast>,
        init: Expression<'ast>,
    },
    // ObjectLiteral: Key: Value
    // Destructuring: Key: Alias(Ident) Option(= Value)
    // PropertyName(),
    Property {
        loc: Loc,
        span: Span,
        name: PropertyName<'ast>,
        // :
        puct: Punctuator,
        // Ident / Expr
        alias: Expression<'ast>,
        init: Expression<'ast>
    },
    // ObjectLiteral
    // Class Method
    MethodDefinition(MethodDefinition<'ast>),
    // Destructuring or ObjectLiteral
    Spread(SpreadExpression<'ast>),
}


// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-destructuring-binding-patterns
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum BindingPattern<'ast> {
    Object(ObjectBindingPattern<'ast>),
    Array(ArrayBindingPattern<'ast>),
}

impl<'ast> BindingPattern<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            BindingPattern::Object(inner) => inner.loc,
            BindingPattern::Array(inner) => inner.loc,
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            BindingPattern::Object(inner) => inner.span,
            BindingPattern::Array(inner) => inner.span,
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ObjectBindingPattern<'ast> {
    pub loc: Loc,
    pub span: Span,
    // NOTE: Only one REST property elem and must be last
    pub properties: &'ast [ BindingProperty<'ast> ],
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ArrayBindingPattern<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub elems: &'ast [ Option<BindingElement<'ast>> ],
    // NOTE: Only one REST property elem and must be last
    pub rest_elem: Option<&'ast BindingRestElement<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum BindingRestElement<'ast> {
    Identifier(Identifier<'ast>),
    BindingPattern(BindingPattern<'ast>),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum BindingElement<'ast> {
    SingleNameBinding {
        loc: Loc,
        span: Span,
        name: Identifier<'ast>,
        init: Option<Expression<'ast>>,
    },
    BindingPattern {
        loc: Loc,
        span: Span,
        pattern: BindingPattern<'ast>,
        init: Option<Expression<'ast>>,
    },
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum BindingProperty<'ast> {
    SingleNameBinding {
        loc: Loc,
        span: Span,
        name: Identifier<'ast>,
        init: Option<Expression<'ast>>,
    },
    Property {
        loc: Loc,
        span: Span,
        name: PropertyName<'ast>,
        puct: Punctuator,             // :
        value: BindingElement<'ast>,
    },
    Spread {
        loc: Loc,
        span: Span,
        puct: Punctuator,            // ...
        name: Identifier<'ast>,
    },
}


// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-destructuring-assignment
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AssignmentPattern<'ast> {
    Object(ObjectAssignmentPattern<'ast>),
    Array(ArrayAssignmentPattern<'ast>),
}

impl<'ast> AssignmentPattern<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            AssignmentPattern::Object(inner) => inner.loc,
            AssignmentPattern::Array(inner) => inner.loc,
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            AssignmentPattern::Object(inner) => inner.span,
            AssignmentPattern::Array(inner) => inner.span,
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ObjectAssignmentPattern<'ast> {
    pub loc: Loc,
    pub span: Span,
    // NOTE: Only one REST property elem and must be last
    pub properties: &'ast [ AssignmentProperty<'ast> ],
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ArrayAssignmentPattern<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub elems: &'ast [ Option<AssignmentElement<'ast>> ],
    // NOTE: Only one REST property elem and must be last
    pub rest_elem: Option<Expression<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct AssignmentElement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub elem: Expression<'ast>,         // LeftHandSideExpr
    pub init: Option<Expression<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AssignmentProperty<'ast> {
    Identifier {
        loc: Loc,
        span: Span,
        name: Identifier<'ast>,
        init: Option<Expression<'ast>>,
    },
    Property {
        loc: Loc,
        span: Span,
        name: PropertyName<'ast>,
        puct: Punctuator,                // :
        // NOTE: elem 看起来似乎必须为 Identifier , 这个在 AST 生成后,再做正确性检查。
        value: AssignmentElement<'ast>,
    },
    Spread {
        loc: Loc,
        span: Span,
        puct: Punctuator,               // ...
        target: Expression<'ast>,       // LeftHandSideExpr
    }
}




// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-initializer
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ObjectLiteral<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub properties: &'ast [ ObjectProperty<'ast> ],
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ObjectProperty<'ast> {
    Identifier(Identifier<'ast>),
    Property {
        loc: Loc,
        span: Span,
        name: PropertyName<'ast>,
        puct: Punctuator,                      // :
        value: Expression<'ast>
    },
    MethodDefinition(MethodDefinition<'ast>),
    Spread {
        puct: Punctuator,                     // ...
        target: Expression<'ast>,
    },
}

// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array-initializer
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct ArrayLiteral<'ast> {
    pub loc: Loc,
    pub span: Span,
    // AssignmentExpression 
    // SpreadElement
    pub elems: &'ast [ Option<Expression<'ast>> ],
}