feat(parser): add preserve_parens option (default: true) (#1474)

closes #1461
This commit is contained in:
Boshen 2023-11-21 11:16:30 +08:00 committed by GitHub
parent 0f7d6a5800
commit 07b010912a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View file

@ -215,7 +215,11 @@ impl<'a> Parser<'a> {
)
};
Ok(self.ast.parenthesized_expression(paren_span, expression))
Ok(if self.preserve_parens {
self.ast.parenthesized_expression(paren_span, expression)
} else {
expression
})
}
/// Section 13.2.2 This Expression

View file

@ -180,7 +180,7 @@ impl<'a> Lexer<'a> {
/// See Section 12:
/// The `InputElementRegExp` goal symbol is used in all syntactic grammar contexts
/// where a `RegularExpressionLiteral` is permitted
/// Which meams the parser needs to re-tokenize on `PrimaryExpression`,
/// Which means the parser needs to re-tokenize on `PrimaryExpression`,
/// `RegularExpressionLiteral` only appear on the right hand side of `PrimaryExpression`
pub fn next_regex(&mut self, kind: Kind) -> Token<'a> {
self.current.token.start = self.offset()

View file

@ -126,6 +126,10 @@ pub struct Parser<'a> {
/// Ast builder for creating AST spans
ast: AstBuilder<'a>,
/// Emit `ParenthesizedExpression` in AST.
/// Default: `true`
preserve_parens: bool,
}
impl<'a> Parser<'a> {
@ -141,6 +145,7 @@ impl<'a> Parser<'a> {
state: ParserState::new(allocator),
ctx: Self::default_context(source_type),
ast: AstBuilder::new(allocator),
preserve_parens: true,
}
}
@ -154,6 +159,16 @@ impl<'a> Parser<'a> {
self
}
/// Emit `ParenthesizedExpression` in AST.
///
/// If this option is true, parenthesized expressions are represented by (non-standard)
/// `ParenthesizedExpression` nodes that have a single expression property containing the expression inside parentheses.
#[must_use]
pub fn preserve_parens(mut self, allow: bool) -> Self {
self.preserve_parens = allow;
self
}
/// Main entry point
///
/// Returns an empty `Program` on unrecoverable error,