我正在尝试从第 11 章的 Eloquent Javascript 书中重现语言解析器。参见 here .
当我尝试这样做时:
function parseExpression(program) {
program = skipSpace(program);
var match, expr;
if (match = /^"([^"]*)"/.exec(program))
expr = {type: "value", value: match[1]};
else if (match = /^\d+\b/.exec(program))
expr = {type: "value", value: Number(match[0])};
else if (match = /^[^\s(),"]+/.exec(program))
expr = {type: "word", name: match[0]};
else
throw new SyntaxError("Unexpected syntax: " + program);
return parseApply(expr, program.slice(match[0].length));
}
我从 if
得到一个非常奇怪的 SyntaxError: Unexpected end of input
。
但是,像 if (str = 'my string')
这样的虚拟赋值可以完美地工作。
正则表达式是否会干扰该行为?
编辑:我尝试使用控制台进行调试。这是我得到的:
$ node
> .load parser.js
> function parseExpression(program) {
... program = skipSpace(program);
... var match, expr;
... if ((match = /^"([^"]*)"/.exec(program)))
SyntaxError: Unexpected end of input
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:225:25)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:884:20)
> expr = {type: "value", value: match[1]};
ReferenceError: match is not defined
at repl:1:31
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:884:20)
> else if (match = /^\d+\b/.exec(program))
... expr = {type: "value", value: Number(match[0])};
... else if (match = /^[^\s(),"]+/.exec(program))
SyntaxError: Unexpected token else
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:225:25)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:884:20)
> expr = {type: "word", name: match[0]};
ReferenceError: match is not defined
at repl:1:29
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:884:20)
> else
... throw new SyntaxError("Unexpected syntax: " + program);
... return parseApply(expr, program.slice(match[0].length));
... }
最佳答案
Node 似乎对正则表达式中的 "
字符有问题。
通过转义它们,函数可以正确解析。
function parseExpression(program) {
program = skipSpace(program);
var match, expr;
if ((match = /^\"([^\"]*)\"/.exec(program)))
expr = {type: "value", value: match[1]};
}
关于javascript - 同时分配和检查并有一个很好的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841979/