繁体   English   中英

使用ANTLR解析JavaScript正则表达式

[英]Parsing JavaScript regex with ANTLR

我有一个ANTLR JavaScript语法(取自Internet),它似乎支持除正则表达式文字之外的所有内容。

正则表达式文字的问题在于你有两个规则,基本上:

multiplicativeExpression
    : unaryExpression (LT!* ('*' | '/' | '%')^ LT!* unaryExpression)*

regexLiteral
    : '/' RegexLiteralChar* '/'

规则RegexLiteralChar使用不同于正常表达式的词法规则(例如,双引号不会终止它)。

这意味着我需要以某种方式从我的解析器中改变某种词法分析器状态。 我怎样才能做到这一点? 它甚至可能吗?

看看Bart Kiers 评论中提到的语法,你可以看到这个评论,

定义这种语法面临的主要挑战是:

-1-与乘法表达式和正则表达式文字相关的DIV符号周围的歧义。 这是通过一些词法分析器驱动的魔法来解决的:门控语义谓词根据RegularExpressionsEnabled属性的值打开或关闭正则表达式的识别。 启用正则表达式时,它们优先于除法表达式。 是否启用正则表达式的决定是基于前一个令牌可被视为除法的左侧操作数的最后一个令牌的启发式算法。

...

areRegularExpressionsEnabled()函数定义为,

private final boolean areRegularExpressionsEnabled()
{
    if (last == null)
    {
        return true;
    }
    switch (last.getType())
    {
    // identifier
        case Identifier:
    // literals
        case NULL:
        case TRUE:
        case FALSE:
        case THIS:
        case OctalIntegerLiteral:
        case DecimalLiteral:
        case HexIntegerLiteral:
        case StringLiteral:
    // member access ending 
        case RBRACK:
    // function call or nested expression ending
        case RPAREN:
            return false;
    // otherwise OK
        default:
            return true;
    }
}

然后该函数用于RegularExpressionLiteral表达式,

RegularExpressionLiteral
    : { areRegularExpressionsEnabled() }?=> DIV RegularExpressionFirstChar RegularExpressionChar* DIV IdentifierPart*
    ;

暂无
暂无

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

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