mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 04:42:10 +00:00
feat(parser): add preserve_parens option (default: true) (#1474)
closes #1461
This commit is contained in:
parent
0f7d6a5800
commit
07b010912a
3 changed files with 21 additions and 2 deletions
|
|
@ -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
|
/// Section 13.2.2 This Expression
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ impl<'a> Lexer<'a> {
|
||||||
/// See Section 12:
|
/// See Section 12:
|
||||||
/// The `InputElementRegExp` goal symbol is used in all syntactic grammar contexts
|
/// The `InputElementRegExp` goal symbol is used in all syntactic grammar contexts
|
||||||
/// where a `RegularExpressionLiteral` is permitted
|
/// 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`
|
/// `RegularExpressionLiteral` only appear on the right hand side of `PrimaryExpression`
|
||||||
pub fn next_regex(&mut self, kind: Kind) -> Token<'a> {
|
pub fn next_regex(&mut self, kind: Kind) -> Token<'a> {
|
||||||
self.current.token.start = self.offset()
|
self.current.token.start = self.offset()
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,10 @@ pub struct Parser<'a> {
|
||||||
|
|
||||||
/// Ast builder for creating AST spans
|
/// Ast builder for creating AST spans
|
||||||
ast: AstBuilder<'a>,
|
ast: AstBuilder<'a>,
|
||||||
|
|
||||||
|
/// Emit `ParenthesizedExpression` in AST.
|
||||||
|
/// Default: `true`
|
||||||
|
preserve_parens: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl<'a> Parser<'a> {
|
||||||
|
|
@ -141,6 +145,7 @@ impl<'a> Parser<'a> {
|
||||||
state: ParserState::new(allocator),
|
state: ParserState::new(allocator),
|
||||||
ctx: Self::default_context(source_type),
|
ctx: Self::default_context(source_type),
|
||||||
ast: AstBuilder::new(allocator),
|
ast: AstBuilder::new(allocator),
|
||||||
|
preserve_parens: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,6 +159,16 @@ impl<'a> Parser<'a> {
|
||||||
self
|
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
|
/// Main entry point
|
||||||
///
|
///
|
||||||
/// Returns an empty `Program` on unrecoverable error,
|
/// Returns an empty `Program` on unrecoverable error,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue