我试图从第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));
}
我从SyntaxError: Unexpected end of input
获得了一个非常奇怪的if
。
然而,像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));
... }
答案 0 :(得分:1)
节点似乎在regexp中的"
字符有问题。
通过转义它们,函数可以正确解析。
function parseExpression(program) {
program = skipSpace(program);
var match, expr;
if ((match = /^\"([^\"]*)\"/.exec(program)))
expr = {type: "value", value: match[1]};
}
答案 1 :(得分:1)
你的代码没有错:
C:\test>node -v
v5.7.0
C:\test>node
> .load parser.js
> 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));
... }
undefined
>
如果它对您不起作用,我说它会影响您的版本的Node错误(您不能确切地说出哪一个),也许#5113 repl cannot handle single quote in regex, when within a function) :
我得到了一个&#39; SyntaxError:意外的输入结束&#39;当我尝试 在repl中输入以下内容
var someFunction = function(s) { s = s.replace(/'/g, ''); }
但是,
s = s.replace(/'/g, '')
本身在新行上是可以的。我 做错了什么?
该错误已在Node/5.7.0修复。
答案 2 :(得分:0)
尝试在Node v6.10.3上运行此代码时遇到了同样的问题。当我在正则表达式中转义引号时结束工作,正如其他人所说的那样。