gpt4 book ai didi

javascript - 使用 Nearley.js 进行解析的友好错误报告

转载 作者:行者123 更新时间:2023-11-28 03:32:59 27 4
gpt4 key购买 nike

是否可以使用 Nearley.js 呈现用户友好的解析器错误?

const parser = new nearley.Parser((bracketexpr_grammar));
parse(): void{
try {
parser.feed(this._sql);
this._rawData = parser.results[0];
} catch(error){
console.log(error);
this._errors.push(error.offset);
}
}

我尝试过的:

  • error.offset:仅显示发生错误的行(不是我想要的)。
  • 错误:它给了我一个巨大的错误,例如:

.

Invalid syntax at line 1 col 1:

b4d455
^
Unexpected "b"

Instead of a "b", I was expecting to see one of the following:
A "#" based on:
csscolor → ● "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit
A "#" based on:
csscolor → ● "#" hexdigit hexdigit hexdigit
A "r" based on:
csscolor$string$1 → ● "r" "g" "b"
csscolor → ● csscolor$string$1 _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")"
A "h" based on:
csscolor$string$2 → ● "h" "s" "l"
csscolor → ● csscolor$string$2 _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")"
A "r" based on:
csscolor$string$3 → ● "r" "g" "b" "a"
csscolor → ● csscolor$string$3 _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")"
A "h" based on:
csscolor$string$4 → ● "h" "s" "l" "a"
csscolor → ● csscolor$string$4 _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")"

我想要一些更简单、更干净的东西,而不是那个巨大的错误:

Invalid syntax at line 1 col 1:

b4d455
^
Unexpected "b"

可以吗?

https://nearley.js.org/docs/parser#catching-errors

最佳答案

版本2.20.1在 Nearley 中,Error 对象有一个属性 token比可用于简化消息。在下面的示例中,我们使用 RegExp 来遍历 message错误的属性并将预期的标记添加到消息中。

RegExp 基于对 Nearley JS 错误消息的观察,正如您在上面的问题中看到的那样,有许多 A "<something>" based on: 的重复。模式(命名标记更改为 A <something> token based on: )

function parseFromFile(origin) {
try {
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
const source = fs.readFileSync(origin, 'utf8');
parser.feed(source);
let results = parser.results;

if (results.length > 1) throw new Error(`Language Design Error: Ambiguous Grammar! Generated ${results.length}) ASTs`);
if (results.length == 0) {
console.error("Unexpected end of Input error. Incomplete Egg program. Expected more input");
process.exit(1);
}
const ast = results[0];
return ast;
}
catch(e) {
let token = e.token;
let message = e.message;
let expected = message.match(/(?<=A ).*(?= based on:)/g).map(s => s.replace(/\s+token/i,''));
let newMessage = `Unexpected ${token.type} token "${token.value}" `+
`at line ${token.line} col ${token.col}.`;
if (expected && expected.length) newMessage += ` Tokens expected: ${[...new Set(expected)]}`;

throw new Error(newMessage)
}
}

当使用错误的输入执行时,消息会简化为:

➜  egg-oop-parser-solution git:(master) ✗ bin/eggc.js test/errors/unexpected-token.egg
Unexpected LCB token "{" at line 1 col 2. Tokens expected: "(","[",".",EOF

与错误管理相关的另一个想法是在语法中引入针对特定错误情况的生成规则以及处理错误的关联语义操作。有关更多信息,请参阅此 section

关于javascript - 使用 Nearley.js 进行解析的友好错误报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57999130/

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