How to fix 'SyntaxError: Unexpected token :'

How to fix 'SyntaxError: Unexpected token :'

我正在尝试让 nearley 工作,但我最终收到 'SyntaxError: Unexpected token :'。

当我 运行 nearley-test -i "help" command.js 时出现问题,我不完全确定它的原因是什么。由于我对 javascript 不太熟悉,而且这是一些旧的遗留代码,可能有些东西在 javascript/node?

的更高版本中已停止工作

错误:

➜  src git:(master) nearley-test -i "help" command.js
function id(d: any[]): any { return d[0]; }
             ^

SyntaxError: Unexpected token :
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)
    at Object.runInThisContext (vm.js:326:10)
    at Module._compile (internal/modules/cjs/loader.js:664:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)

代码:

// Generated automatically by nearley, version 2.19.0
// http://github.com/Hardmath123/nearley
// Bypasses TS6133. Allow declared but unused functions.
// @ts-ignore
function id(d: any[]): any { return d[0]; }
declare var tip: any;
declare var withdraw: any;
declare var deposit: any;
declare var balance: any;
declare var help: any;
declare var eth: any;
declare var address: any;
declare var ens: any;
declare var username: any;
declare var number: any;
declare var any: any;
declare var space: any;

import { CommandType } from './parser'
import * as moo from 'moo'

const lexer = moo.compile({
  space: / +/,
  ens: /[0-9a-zA-Z-]+\.eth/,
  address: /0x[0-9a-fA-F]{40}/,
  username: /@[0-9a-zA-Z_]{1,15}/,
  number: /(?:[1-9][0-9]*|0)(?:\.[0-9]+)?/,
  eth: /[eE]ther|ETH|[eE]th/,
  tip: /[tT]ip/,
  withdraw: /[wW]ithdraw/,
  deposit: /[dD]eposit/,
  balance: /[bB]alance/,
  help: /[hH]elp/,
  any: /.+/
})

interface NearleyToken {  value: any;
  [key: string]: any;
};

interface NearleyLexer {
  reset: (chunk: string, info: any) => void;
  next: () => NearleyToken | undefined;
  save: () => any;
  formatError: (token: NearleyToken) => string;
  has: (tokenType: string) => boolean;
};

interface NearleyRule {
  name: string;
  symbols: NearleySymbol[];
  postprocess?: (d: any[], loc?: number, reject?: {}) => any;
};

type NearleySymbol = string | { literal: any } | { test: (token: any) => boolean };

interface Grammar {
  Lexer: NearleyLexer | undefined;
  ParserRules: NearleyRule[];
  ParserStart: string;
};

const grammar: Grammar = {
  Lexer: lexer,
  ParserRules: [
    {"name": "Main", "symbols": ["AnyCommand", "_"], "postprocess": d => d[0]},
    {"name": "Main", "symbols": ["AnyCommand", "__", "Any"], "postprocess": d => d[0]},
    {"name": "AnyCommand", "symbols": ["TipCommand"], "postprocess": id},
    {"name": "AnyCommand", "symbols": ["WithdrawCommand"], "postprocess": id},
    {"name": "AnyCommand", "symbols": ["BalanceCommand"], "postprocess": id},
    {"name": "AnyCommand", "symbols": ["DepositCommand"], "postprocess": id},
    {"name": "AnyCommand", "symbols": ["HelpCommand"], "postprocess": id},
    {"name": "TipCommand", "symbols": ["_", (lexer.has("tip") ? {type: "tip"} : tip), "__", "Username"], "postprocess": d => ({ type: CommandType.TIP, username: d[3] })},
    {"name": "TipCommand", "symbols": ["TipCommand", "_", "Amount"], "postprocess": d => Object.assign(d[0], d[2])},
    {"name": "WithdrawCommand", "symbols": ["_", (lexer.has("withdraw") ? {type: "withdraw"} : withdraw), "__", "Amount", "__", "AddressOrENS"], "postprocess": d => Object.assign({ type: CommandType.WITHDRAW, address: d[5] }, d[3])},
    {"name": "DepositCommand", "symbols": ["_", (lexer.has("deposit") ? {type: "deposit"} : deposit)], "postprocess": d => ({ type: CommandType.DEPOSIT })},
    {"name": "BalanceCommand", "symbols": ["_", (lexer.has("balance") ? {type: "balance"} : balance)], "postprocess": d => ({ type: CommandType.BALANCE })},
    {"name": "HelpCommand", "symbols": ["_", (lexer.has("help") ? {type: "help"} : help)], "postprocess": d => ({ type: CommandType.HELP })},
    {"name": "Amount", "symbols": ["Number", "_", "Symbol"], "postprocess": d => ({ amount: d[0], symbol: d[2] })},
    {"name": "Symbol", "symbols": [(lexer.has("eth") ? {type: "eth"} : eth)], "postprocess": d => 'ETH'},
    {"name": "AddressOrENS", "symbols": ["Address"], "postprocess": id},
    {"name": "AddressOrENS", "symbols": ["ENS"], "postprocess": id},
    {"name": "Address", "symbols": [(lexer.has("address") ? {type: "address"} : address)], "postprocess": d => d[0].value},
    {"name": "ENS", "symbols": [(lexer.has("ens") ? {type: "ens"} : ens)], "postprocess": d => d[0].value},
    {"name": "Username", "symbols": [(lexer.has("username") ? {type: "username"} : username)], "postprocess": d => d[0].value.slice(1)},
    {"name": "Number", "symbols": [(lexer.has("number") ? {type: "number"} : number)], "postprocess": d => parseFloat(d[0].value)},
    {"name": "Any", "symbols": [(lexer.has("any") ? {type: "any"} : any)], "postprocess": d => d[0].value},
    {"name": "_$ebnf", "symbols": [(lexer.has("space") ? {type: "space"} : space)], "postprocess": id},
    {"name": "_$ebnf", "symbols": [], "postprocess": () => null},
    {"name": "_", "symbols": ["_$ebnf"], "postprocess": d => null},
    {"name": "__", "symbols": [(lexer.has("space") ? {type: "space"} : space)], "postprocess": d => null}
  ],
  ParserStart: "Main",
};

export default grammar;

引用的 CommandType 是:

export enum CommandType {
  TIP = 'tip',
  WITHDRAW = 'withdraw',
  DEPOSIT = 'deposit',
  BALANCE = 'balance',
  HELP = 'help'
}

如有任何帮助,我们将不胜感激!

因此,您生成了 Typescript: -o 命令。ts,但是您的实用程序需要 JavaScript。改为生成 JavaScript:nearleyc command.ne -o command.js