-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
7 changed files
with
138 additions
and
39 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
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
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,38 @@ | ||
use super::Node; | ||
use gc::{Finalize, Trace}; | ||
use std::fmt; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Clone, Debug, Trace, Finalize, PartialEq)] | ||
pub struct Label { | ||
stmt: Box<Node>, | ||
label: Box<str>, | ||
} | ||
|
||
impl Label { | ||
pub(super) fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", format!("{}: {}", self.label, self.stmt)) | ||
} | ||
|
||
pub fn new(stmt: Node, label: Box<str>) -> Self { | ||
Self { | ||
stmt: stmt.into(), | ||
label, | ||
} | ||
} | ||
} | ||
|
||
impl From<Label> for Node { | ||
fn from(label_stmt: Label) -> Node { | ||
Node::Label(label_stmt) | ||
} | ||
} | ||
|
||
impl fmt::Display for Label { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.display(f) | ||
} | ||
} |
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
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
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,52 @@ | ||
use super::{LabelIdentifier, Statement}; | ||
use crate::{ | ||
syntax::{ | ||
ast::{node::Label, Punctuator}, | ||
parser::{ | ||
cursor::Cursor, error::ParseError, AllowAwait, AllowReturn, AllowYield, TokenParser, | ||
}, | ||
}, | ||
BoaProfiler, | ||
}; | ||
/// Labelled Statement Parsing | ||
/// | ||
/// More information | ||
/// - [MDN documentation][mdn] | ||
/// - [ECMAScript specification][spec] | ||
/// | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label | ||
/// [spec]: https://tc39.es/ecma262/#sec-labelled-statements | ||
#[derive(Debug, Clone, Copy)] | ||
pub(super) struct LabelledStatement { | ||
allow_yield: AllowYield, | ||
allow_await: AllowAwait, | ||
allow_return: AllowReturn, | ||
} | ||
|
||
impl LabelledStatement { | ||
pub(super) fn new<Y, A, R>(allow_yield: Y, allow_await: A, allow_return: R) -> Self | ||
where | ||
Y: Into<AllowYield>, | ||
A: Into<AllowAwait>, | ||
R: Into<AllowReturn>, | ||
{ | ||
Self { | ||
allow_yield: allow_yield.into(), | ||
allow_await: allow_await.into(), | ||
allow_return: allow_return.into(), | ||
} | ||
} | ||
} | ||
|
||
impl TokenParser for LabelledStatement { | ||
type Output = Label; | ||
|
||
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> { | ||
let _timer = BoaProfiler::global().start_event("Label", "Parsing"); | ||
let name = LabelIdentifier::new(self.allow_yield, self.allow_await).parse(cursor)?; | ||
cursor.expect(Punctuator::Colon, "Labelled Statement")?; | ||
let stmt = | ||
Statement::new(self.allow_yield, self.allow_await, self.allow_return).parse(cursor)?; | ||
Ok(Label::new(stmt, name)) | ||
} | ||
} |
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