我试图让Nearley工作,但最终收到“ SyntaxError:意外令牌:”。
当我运行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$1", "symbols": [(lexer.has("space") ? {type: "space"} : space)], "postprocess": id},
{"name": "_$ebnf$1", "symbols": [], "postprocess": () => null},
{"name": "_", "symbols": ["_$ebnf$1"], "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'
}
任何帮助将不胜感激!
答案 0 :(得分:0)
因此,您生成Typescript:-o命令。 ts ,但是您的实用程序需要JavaScript。改为生成JavaScript:nearleyc command.ne -o command.js