-
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//! Arrow function parsing. | ||
//! | ||
//! More information: | ||
//! - [MDN documentation][mdn] | ||
//! - [ECMAScript specification][spec] | ||
//! | ||
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | ||
//! [spec]: https://tc39.es/ecma262/#sec-arrow-function-definitions | ||
|
||
use super::read_formal_parameters; | ||
use crate::syntax::{ | ||
ast::{ | ||
node::{FormalParameter, Node}, | ||
punc::Punctuator, | ||
token::TokenKind, | ||
}, | ||
parser::{read_statements, AssignmentExpression, Cursor, ParseError, ParseResult, TokenParser}, | ||
}; | ||
|
||
/// Arrow function parsing. | ||
/// | ||
/// More information: | ||
/// - [MDN documentation][mdn] | ||
/// - [ECMAScript specification][spec] | ||
/// | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | ||
/// [spec]: https://tc39.es/ecma262/#prod-ArrowFunction | ||
#[derive(Debug, Clone, Copy)] | ||
pub(in crate::syntax::parser) struct ArrowFunction; | ||
|
||
impl TokenParser for ArrowFunction { | ||
fn parse(cursor: &mut Cursor<'_>) -> ParseResult { | ||
let next_token = cursor.next().ok_or(ParseError::AbruptEnd)?; | ||
let params = match &next_token.kind { | ||
TokenKind::Punctuator(Punctuator::OpenParen) => read_formal_parameters(cursor)?, | ||
TokenKind::Identifier(param_name) => vec![FormalParameter { | ||
init: None, | ||
name: param_name.clone(), | ||
is_rest_param: false, | ||
}], | ||
_ => { | ||
return Err(ParseError::Expected( | ||
vec![ | ||
TokenKind::Punctuator(Punctuator::OpenParen), | ||
TokenKind::identifier("identifier"), | ||
], | ||
next_token.clone(), | ||
"arrow function", | ||
)) | ||
} | ||
}; | ||
|
||
cursor.expect_punc(Punctuator::Arrow, "arrow function")?; | ||
|
||
cursor.skip(|tk| tk.kind == TokenKind::LineTerminator); | ||
let body = match cursor.peek(0) { | ||
Some(tk) if tk.kind == TokenKind::Punctuator(Punctuator::OpenBlock) => { | ||
let _ = cursor.next(); | ||
let body = read_statements(cursor, true).map(Node::StatementList)?; | ||
cursor.expect_punc(Punctuator::CloseBlock, "arrow function")?; | ||
body | ||
} | ||
_ => Node::return_node(AssignmentExpression::parse(cursor)?), | ||
}; | ||
|
||
Ok(Node::arrow_function_decl(params, body)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Function definition parsing. | ||
//! | ||
//! More information: | ||
//! - [MDN documentation][mdn] | ||
//! - [ECMAScript specification][spec] | ||
//! | ||
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function | ||
//! [spec]: https://tc39.es/ecma262/#sec-function-definitions | ||
|
||
use super::read_formal_parameters; | ||
use crate::syntax::{ | ||
ast::{node::Node, punc::Punctuator, token::TokenKind}, | ||
parser::{read_statements, Cursor, ParseError, ParseResult, TokenParser}, | ||
}; | ||
|
||
/// Function expression parsing. | ||
/// | ||
/// More information: | ||
/// - [MDN documentation][mdn] | ||
/// - [ECMAScript specification][spec] | ||
/// | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function | ||
/// [spec]: https://tc39.es/ecma262/#prod-FunctionExpression | ||
#[derive(Debug, Clone, Copy)] | ||
pub(in crate::syntax::parser) struct FunctionExpression; | ||
|
||
impl TokenParser for FunctionExpression { | ||
fn parse(cursor: &mut Cursor<'_>) -> ParseResult { | ||
let name = if let TokenKind::Identifier(name) = | ||
&cursor.peek(0).ok_or(ParseError::AbruptEnd)?.kind | ||
{ | ||
Some(name) | ||
} else { | ||
None | ||
}; | ||
if name.is_some() { | ||
// We move the cursor forward. | ||
let _ = cursor.next().expect("nex token disappeared"); | ||
} | ||
|
||
cursor.expect_punc(Punctuator::OpenParen, "function expression")?; | ||
|
||
let params = read_formal_parameters(cursor)?; | ||
|
||
cursor.expect_punc(Punctuator::OpenBlock, "function expression")?; | ||
|
||
let body = read_statements(cursor, true).map(Node::StatementList)?; | ||
|
||
cursor.expect_punc(Punctuator::CloseBlock, "function expression")?; | ||
|
||
Ok(Node::function_decl::<_, &String, _, _>(name, params, body)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters