gpt4 book ai didi

javascript - 使用 PEG.js 匹配包含 underscope 的字符串文字

转载 作者:行者123 更新时间:2023-11-29 23:45:48 24 4
gpt4 key购买 nike

我正在继续学习 PEG.js,但仍停留在下一期。

PEG.js 生成的解析器无法匹配包含下划线的字符串:

CONFIG += stl_off

成功解析没有它们的字符串:

CONFIG += static

(这是一个内置变量CONFIG“appending-assignment”语句,rvalue是一组有限的字符串列表)

我做错了什么?


语法:

Start =
Statement* {return env; }

Statement
= Comment
/ GenericAssignmentStatementT

GenericAssignmentStatementT = Whitespace* GenericAssignmentStatement Whitespace*
GenericAssignmentStatement
// TEMPLATE
= TemplateAssignmentStatement
// CONFIG
/ ConfigAssignmentStatement
/ ConfigAppendingAssignmentStatement

// -------------------------------------------------------------------------------------------------

// # Single-line comment
Comment "Comment string" = Whitespace* "#" rvalue:$(!LineBreak .)* LineBreak+ {
return "#" + rvalue;
}

// -------------------------------------------------------------------------------------------------

// TEMPLATE = app|lib|subdirs|aux|vcapp|vclib
SystemTemplateVariable = "TEMPLATE"
SystemTemplateVariableValue = "app" / "lib" / "subdirs" / "aux" / "vcapp" / "vclib"
TemplateAssignmentStatement = lvalue:SystemTemplateVariable AssignmentOperator rvalue:SystemTemplateVariableValue Whitespace* LineBreak* {
if (!env.qmakeVars)
env.qmakeVars = {};
env.qmakeVars[lvalue] = rvalue;
return {name:"TEMPLATE", op:"=", value:rvalue};
}

// -------------------------------------------------------------------------------------------------

// CONFIG = release|debug|debug_and_release|debug_and_release_target
SystemConfigVariable = "CONFIG"
SystemConfigVariableValue = "release" / "debug" / "debug_and_release" / "debug_and_release_target"

ConfigAssignmentStatement = lvalue:SystemConfigVariable AssignmentOperator rvalue:SystemConfigVariableValue? Whitespace* LineBreak* {
if (!env.qmakeVars)
env.qmakeVars = {};
env.qmakeVars[lvalue] = [rvalue];
return {name:"CONFIG", op:"=", value:rvalue};
}

ConfigAppendingAssignmentStatement = lvalue:SystemConfigVariable
AppendingAssignmentOperator rvalue:SystemConfigVariableValue Whitespace* LineBreak* {
if (!env.qmakeVars)
env.qmakeVars = {};
if (!env.qmakeVars[lvalue])
env.qmakeVars[lvalue] = [];
env.qmakeVars[lvalue].push(rvalue);
return {name:"CONFIG", op:"+=", value:rvalue};
}

// Assignment operators
AssignmentOperator = Whitespace* "=" Whitespace*
AppendingAssignmentOperator = Whitespace* "+=" Whitespace*

// Delimeters
LineBreak = [\r\n] {
return "LB";
}

Whitespace = [ \t] {
return "WS";
}

测试输入:

TEMPLATE = app
CONFIG += debug_and_release

测试 PEG.js 输出:

Line 2, column 16: Expected "CONFIG", "TEMPLATE", Comment string, [ \t], [\r\n], or end of input but "_" found.

最佳答案

PEG 从左到右评估 SystemConfigVariableValue,因此 debug_and_release 实际上将与 release 文字匹配(之后解析器会混淆) .

如果您先声明更具体的字面量,它会起作用:

SystemConfigVariableValue = "debug_and_release_target" / "debug_and_release" / "release" / "debug"

关于javascript - 使用 PEG.js 匹配包含 underscope 的字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44226562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com