简体   繁体   English

是/ a / g在{} / a / ga正则表达式还是分区?

[英]Is /a/g in {}/a/g a regex or a division?

I am writing a JavaCC-based parser for JavaScript ( ECMAScript 5.1 ) and have an interesting corner case with regular expressions. 我正在为JavaScriptECMAScript 5.1 )编写一个基于JavaCC的解析器,并且有一个有趣的角点案例和正则表达式。 Here it is. 这里是。

Consider the following statement: 请考虑以下声明:

{}/a/g

My question: From the specification point of view, should it be interpreted as 我的问题:从规范的角度来看,它应该被解释为

  • ObjectLiteral divided by Identifier a divided by Identifier b or ObjectLiteral除以Identifier a除以Identifier b
  • Block followed by RegularExpressionLiteral /a/g ? Block后跟RegularExpressionLiteral /a/g

For instance, Chrome interprets it as the latter and other parsers as well. 例如,Chrome将其解释为后者和其他解析器。

From the specification point of view, is /a/g in {}/a/g a regex or a division? 从规范的角度来看,是/a/g{}/a/g是正则表达式还是除法?

Here is what I think to be a relevant part of the specification: 以下是我认为是规范的相关部分

There are no syntactic grammar contexts where both a leading division or division-assignment, and a leading RegularExpressionLiteral are permitted. 没有语法语法上下文,其中允许前导除法或除法赋值和前导RegularExpressionLiteral。

Quoting another answer on the relevant question : 引用相关问题的 另一个答案

The division operator must follow an expression, and a regular expression literal can't follow an expression, so in all other cases you can safely assume you're looking at a regular expression literal. 除法运算符必须遵循表达式,并且正则表达式文字不能跟随表达式,因此在所有其他情况下,您可以安全地假设您正在查看正则表达式文字。

My understanding is that {} is an expression. 我的理解是{} 一种表达。 ObjectLiteral , even an empty one is a PrimaryExpression . ObjectLiteral ,即使是空的也是PrimaryExpression So the following /a/g must be a division, not a regular expression literal. 所以下面的/a/g必须是一个除法,而不是正则表达式文字。

Or am I wrong with this? 或者我错了吗?

No, {} is not an expression in this case. 不, {}在这种情况下不是表达式。 Quoting the relevant parts of the annotated standard: http://es5.github.io/#x12.4 "An ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block ". 引用带注释标准的相关部分: http//es5.github.io/#x12.4ExpressionStatement不能以开括号括号开头,因为这可能会使其与Block ”。 In your case, {}/a/g is a block, followed by an expression statement consisting of a regular expression literal. 在您的情况下, {}/a/g是一个块,后跟一个由正则表达式文字组成的表达式语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM