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
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, Identifier, LiteralString, LiteralRegularExpression, LiteralTemplate, };
use crate::lexer::operator::{ PrefixOperator, InfixOperator, PostfixOperator, AssignmentOperator, };
use crate::lexer::punctuator::PunctuatorKind;
use crate::lexer::keyword::KeywordKind;

use crate::lexer::LexerErrorKind;

use crate::parser::parser::Parser;
use crate::parser::parser::ParserErrorKind::{ self, * };

use crate::ast::numberic::{ Numberic, Float, };
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::{
    FunctionDeclaration, FunctionExpression, Function, 
    ArrowFunctionExpression, ConciseBody, FunctionBody, 
};


impl<'ast> Parser<'ast> {
    pub fn parse_function(&mut self, token: Token<'ast>) -> Result<Function<'ast>, Error> {
        let (mut loc, mut span) = match token {
            Token::Punctuator(punct) => {
                match punct.kind {
                    PunctuatorKind::LParen => {
                        // (
                        (punct.loc, punct.span)
                    },
                    _ => {
                        return Err(self.unexpected_token(token));
                    }
                }
            },
            _ => {
                return Err(self.unexpected_token(token));
            }
        };
        
        let params = match self.parse_expression(token, 20i8)? {
            Expression::Parenthesized(inner) => inner.to_owned(),
            _ => unreachable!(),
        };
        
        let token2 = self.token4()?;
        match token2 {
            Token::Punctuator(punct) => {
                match punct.kind {
                    PunctuatorKind::LBrace => {
                        // {
                    },
                    _ => {
                        return Err(self.unexpected_token(token2));
                    }
                }
            },
            _ => {
                return Err(self.unexpected_token(token2));
            }
        }

        let block = self.parse_block_statement(token2)?;

        loc.end = block.loc.end;
        span.end = block.span.end;

        let body = block.body;

        let item = Function { loc, span, params, body, };

        Ok(item)
    }

    pub fn parse_function_declaration(&mut self, token: Token<'ast>) -> Result<FunctionDeclaration<'ast>, Error> {
        let (mut loc, mut span) = match token {
            Token::Keyword(kw) => {
                assert_eq!(kw.kind, KeywordKind::Function);
                (kw.loc, kw.span)
            },
            _ => unreachable!(),
        };

        let is_async = false;
        let mut is_generator: bool = false;
        let name: Identifier<'ast>;
        
        let mut token2 = self.token4()?;
        match token2 {
            Token::Punctuator(punct) => {
                match punct.kind {
                    PunctuatorKind::Mul => {
                        is_generator = true;
                        token2 = self.token4()?;
                    },
                    _ => { },
                }
            },
            _ => { }
        }

        match token2 {
            Token::Identifier(ident) => {
                name = ident;
                token2 = self.token4()?;
            },
            _ => {
                return Err(self.unexpected_token(token2));
            }
        }
        
        let func = self.parse_function(token2)?;
        loc.end = func.loc.end;
        span.end = func.span.end;

        let item = FunctionDeclaration { loc, span, is_async, is_generator, name, func, };
        
        Ok(item)
    }

    pub fn parse_function_expression(&mut self, token: Token<'ast>) -> Result<FunctionExpression<'ast>, Error> {
        // AsyncFunctionDeclaration       EXPR
        // AsyncGeneratorDeclaration      EXPR
        let (mut loc, mut span) = match token {
            Token::Keyword(kw) => {
                assert_eq!(kw.kind, KeywordKind::Function);
                (kw.loc, kw.span)
            },
            _ => unreachable!(),
        };

        let is_async = false;
        let mut is_generator: bool = false;
        let mut name: Option<Identifier<'ast>> = None;
        
        let mut token2 = self.token4()?;
        match token2 {
            Token::Punctuator(punct) => {
                match punct.kind {
                    PunctuatorKind::Mul => {
                        is_generator = true;
                        token2 = self.token4()?;
                    },
                    _ => { },
                }
            },
            _ => { }
        }

        match token2 {
            Token::Identifier(ident) => {
                name = Some(ident);
                token2 = self.token4()?;
            },
            _ => { }
        }
        
        let func = self.parse_function(token2)?;
        loc.end = func.loc.end;
        span.end = func.span.end;

        let item = FunctionExpression { loc, span, is_async, is_generator, name, func, };
        
        Ok(item)
    }

    pub fn parse_async_expression(&mut self, token: Token<'ast>) -> Result<Expression<'ast>, Error> {
        // AsyncFunctionDeclaration       EXPR
        // AsyncGeneratorDeclaration      EXPR
        // AsyncArrowFunctionExpression   EXPR
        let (mut loc, mut span) = match token {
            Token::Keyword(kw) => {
                assert_eq!(kw.kind, KeywordKind::Async);
                (kw.loc, kw.span)
            },
            _ => unreachable!(),
        };

        let token2 = self.token2()?;
        match token2 {
            Token::LineTerminator => {
                return Err(self.unexpected_token(token2));
            },
            _ => { }
        }

        let expr = self.parse_expression(token2, -1i8)?;

        match expr {
            Expression::Function(function) => {
                let mut f = function.to_owned();
                f.loc.start = loc.start;
                f.span.start = span.start;
                f.is_async = true;
                Ok(Expression::Function(self.alloc(f)))
            },
            Expression::ArrowFunction(arrow_function) => {
                let mut f = arrow_function.to_owned();
                f.loc.start = loc.start;
                f.span.start = span.start;
                f.is_async = true;
                Ok(Expression::ArrowFunction(self.alloc(f)))
            },
            _ => {
                return Err(self.unexpected_token(token2));
            }
        }
    }

    pub fn parse_arrow_function_expression(&mut self, params: Expression<'ast>) -> Result<Expression<'ast>, Error> {
        let mut loc = params.loc();
        let mut span = params.span();
        
        let token = self.token2()?;
        match token {
            Token::Punctuator(punct) => {
                match punct.kind {
                    PunctuatorKind::LBrace => {
                        // {
                        let block = self.parse_block_statement(token)?;
                        
                        loc.end = block.loc.end;
                        span.end = block.span.end;

                        let is_async = false;
                        let body = ConciseBody::Stmt(block.body);

                        let item = ArrowFunctionExpression { loc, span, is_async, params, body, };
                        return Ok(Expression::ArrowFunction(self.alloc(item)));
                    },
                    _ => {

                    }
                }
            },
            _ => {

            }
        }

        // FIXME: 或许需要把优先级设定为 0 ?这样 `逗号表达式` 将不会被允许作为 函数的 Body.
        let precedence = 0; // -1 or 0
        let expr = self.parse_expression(token, precedence)?;
        loc.end = expr.loc().end;
        span.end = expr.span().end;

        let is_async = false;
        let body = ConciseBody::Expr(expr);
        
        let item = ArrowFunctionExpression { loc, span, is_async, params, body, };
        
        Ok(Expression::ArrowFunction(self.alloc(item)))
    }

}