javascript - 同时分配和检查并有一个很好的语法错误

标签 javascript node.js

我正在尝试从第 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/

相关文章:

node.js - Node imap 尝试更新标志 : Error: Command received in Invalid state.

javascript - d3.json 未执行

javascript - 没有得到我期望的计算结果

node.js - Node.js 中的 Firebase Admin SDK 全局应用初始化

node.js - 无法解析配置的 Swagger 路由器处理程序 : movie_get

node.js - React App 在主要的 js 和 css 上给出 404

javascript - 函数为每次交互运行额外的时间

javascript - 使用 HTML5 视频及时移动覆盖内容

javascript - 创建元素节点、设置其属性并附加元素?

node.js - 如何在没有命令行的情况下运行 Nodejs 服务器