mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.8 KiB
62 lines
1.8 KiB
//! Declaration parsing. |
|
//! |
|
//! More information: |
|
//! - [MDN documentation][mdn] |
|
//! - [ECMAScript specification][spec] |
|
//! |
|
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#Declarations |
|
//! [spec]:https://tc39.es/ecma262/#sec-declarations-and-the-variable-statement |
|
|
|
mod hoistable; |
|
mod lexical; |
|
#[cfg(test)] |
|
mod tests; |
|
|
|
use self::{hoistable::HoistableDeclaration, lexical::LexicalDeclaration}; |
|
use crate::syntax::{ |
|
ast::{keyword::Keyword, node::Node, token::TokenKind}, |
|
parser::{AllowAwait, AllowYield, Cursor, ParseError, ParseResult, TokenParser}, |
|
}; |
|
|
|
/// Parses a declaration. |
|
/// |
|
/// More information: |
|
/// - [ECMAScript specification][spec] |
|
/// |
|
/// [spec]: https://tc39.es/ecma262/#prod-Declaration |
|
#[derive(Debug, Clone, Copy)] |
|
pub(super) struct Declaration { |
|
allow_yield: AllowYield, |
|
allow_await: AllowAwait, |
|
} |
|
|
|
impl Declaration { |
|
pub(super) fn new<Y, A>(allow_yield: Y, allow_await: A) -> Self |
|
where |
|
Y: Into<AllowYield>, |
|
A: Into<AllowAwait>, |
|
{ |
|
Self { |
|
allow_yield: allow_yield.into(), |
|
allow_await: allow_await.into(), |
|
} |
|
} |
|
} |
|
|
|
impl TokenParser for Declaration { |
|
type Output = Node; |
|
|
|
fn parse(self, cursor: &mut Cursor<'_>) -> ParseResult { |
|
let tok = cursor.peek(0).ok_or(ParseError::AbruptEnd)?; |
|
|
|
match tok.kind { |
|
TokenKind::Keyword(Keyword::Function) => { |
|
HoistableDeclaration::new(self.allow_yield, self.allow_await, false).parse(cursor) |
|
} |
|
TokenKind::Keyword(Keyword::Const) | TokenKind::Keyword(Keyword::Let) => { |
|
LexicalDeclaration::new(true, self.allow_yield, self.allow_await).parse(cursor) |
|
} |
|
_ => unreachable!("unknown token found"), |
|
} |
|
} |
|
}
|
|
|