Browse Source

[parser Expression] minor expression macro simplification (#824)

* [parser Expression] minor expression macro simplification

* Slightly improve the macro comment

* Minor reword
pull/829/head
croraf 4 years ago committed by GitHub
parent
commit
456c2dee4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      boa/src/syntax/parser/expression/mod.rs

50
boa/src/syntax/parser/expression/mod.rs

@ -45,16 +45,21 @@ impl PartialEq<Punctuator> for Keyword {
} }
} }
/// Generates an expression parser. /// Generates an expression parser for a number of expressions whose production rules are of the following pattern.
/// <TargetExpression>[allowed_identifiers]
/// => <InnerExpression>[?allowed_identifiers]
/// => <TargetExpression>[?allowed_identifiers] <op1> <InnerExpression>[?allowed_identifiers]
/// => <TargetExpression>[?allowed_identifiers] <op2> <InnerExpression>[?allowed_identifiers]
/// ...
/// ///
/// This macro has 2 mandatory identifiers: /// This macro has 2 mandatory identifiers:
/// - The `$name` identifier will contain the name of the parsing structure. /// - The `$name` identifier is the name of the TargetExpression struct that the parser will be implemented for.
/// - The `$lower` identifier will contain the parser for lower level expressions. /// - The `$lower` identifier is the name of the InnerExpression struct according to the pattern above.
/// ///
/// Those exressions are divided by the punctuators passed as the third parameter. /// A list of punctuators (operands between the <TargetExpression> and <InnerExpression>) are passed as the third parameter.
/// ///
/// The fifth parameter is an Option<InputElement> which sets the goal symbol to set before parsing (or None to leave it as is). /// The fifth parameter is an Option<InputElement> which sets the goal symbol to set before parsing (or None to leave it as is).
macro_rules! expression { ($name:ident, $lower:ident, [$( $op:path ),*], [$( $low_param:ident ),*], $goal:expr, $profile:expr ) => { macro_rules! expression { ($name:ident, $lower:ident, [$( $op:path ),*], [$( $low_param:ident ),*], $goal:expr ) => {
impl<R> TokenParser<R> for $name impl<R> TokenParser<R> for $name
where where
R: Read R: Read
@ -62,7 +67,7 @@ macro_rules! expression { ($name:ident, $lower:ident, [$( $op:path ),*], [$( $lo
type Output = Node; type Output = Node;
fn parse(self, cursor: &mut Cursor<R>)-> ParseResult { fn parse(self, cursor: &mut Cursor<R>)-> ParseResult {
let _timer = BoaProfiler::global().start_event($profile, "Parsing"); let _timer = BoaProfiler::global().start_event(stringify!($name), "Parsing");
if $goal.is_some() { if $goal.is_some() {
cursor.set_goal($goal.unwrap()); cursor.set_goal($goal.unwrap());
@ -132,8 +137,7 @@ expression!(
AssignmentExpression, AssignmentExpression,
[Punctuator::Comma], [Punctuator::Comma],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"Expression"
); );
/// Parses a logical `OR` expression. /// Parses a logical `OR` expression.
@ -172,8 +176,7 @@ expression!(
LogicalANDExpression, LogicalANDExpression,
[Punctuator::BoolOr], [Punctuator::BoolOr],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"LogicalOrExpression"
); );
/// Parses a logical `AND` expression. /// Parses a logical `AND` expression.
@ -212,8 +215,7 @@ expression!(
BitwiseORExpression, BitwiseORExpression,
[Punctuator::BoolAnd], [Punctuator::BoolAnd],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"LogicalANDExpression"
); );
/// Parses a bitwise `OR` expression. /// Parses a bitwise `OR` expression.
@ -252,8 +254,7 @@ expression!(
BitwiseXORExpression, BitwiseXORExpression,
[Punctuator::Or], [Punctuator::Or],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"BitwiseORExpression"
); );
/// Parses a bitwise `XOR` expression. /// Parses a bitwise `XOR` expression.
@ -292,8 +293,7 @@ expression!(
BitwiseANDExpression, BitwiseANDExpression,
[Punctuator::Xor], [Punctuator::Xor],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"BitwiseXORExpression"
); );
/// Parses a bitwise `AND` expression. /// Parses a bitwise `AND` expression.
@ -332,8 +332,7 @@ expression!(
EqualityExpression, EqualityExpression,
[Punctuator::And], [Punctuator::And],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"BitwiseANDExpression"
); );
/// Parses an equality expression. /// Parses an equality expression.
@ -377,8 +376,7 @@ expression!(
Punctuator::StrictNotEq Punctuator::StrictNotEq
], ],
[allow_in, allow_yield, allow_await], [allow_in, allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"EqualityExpression"
); );
/// Parses a relational expression. /// Parses a relational expression.
@ -424,8 +422,7 @@ expression!(
Keyword::In Keyword::In
], ],
[allow_yield, allow_await], [allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"RelationoalExpression"
); );
/// Parses a bitwise shift expression. /// Parses a bitwise shift expression.
@ -465,8 +462,7 @@ expression!(
Punctuator::URightSh Punctuator::URightSh
], ],
[allow_yield, allow_await], [allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"ShiftExpression"
); );
/// Parses an additive expression. /// Parses an additive expression.
@ -504,8 +500,7 @@ expression!(
MultiplicativeExpression, MultiplicativeExpression,
[Punctuator::Add, Punctuator::Sub], [Punctuator::Add, Punctuator::Sub],
[allow_yield, allow_await], [allow_yield, allow_await],
None::<InputElement>, None::<InputElement>
"AdditiveExpression"
); );
/// Parses a multiplicative expression. /// Parses a multiplicative expression.
@ -543,6 +538,5 @@ expression!(
ExponentiationExpression, ExponentiationExpression,
[Punctuator::Mul, Punctuator::Div, Punctuator::Mod], [Punctuator::Mul, Punctuator::Div, Punctuator::Mod],
[allow_yield, allow_await], [allow_yield, allow_await],
Some(InputElement::Div), Some(InputElement::Div)
"MultiplicativeExpression"
); );

Loading…
Cancel
Save