我试图将我的JavaScript ANTLR4解析器与基于CodeMirror的文本编辑器集成。我调查了如何集成解析器并编写以下代码:
// Parser
var antlr4 = require('antlr4/index');
// load nodejs compatible require
var ace_require = require;
require = undefined;
var Honey = { 'requirePath': [] }; // walk up to js folder, see Honey docs
importScripts("lib/require.js");
var antlr4_require = require;
require = ace_require;
// load antlr4 and myLanguage
var antlr4, Lexer ,Parser;
try {
require = antlr4_require;
antlr4 = require('antlr4/index');
Lexer = require('generated-parser/SparkS_v2Lexer');
Parser = require('generated-parser/SparkS_v2Parser');
} finally {
require = ace_require;
}
// class for gathering errors and posting them to the editor
var AnnotatingErrorListener = function(annotations) {
antlr4.error.ErrorListener.call(this);
this.annotations = annotations;
return this;
};
AnnotatingErrorListener.prototype = Object.create(antlr4.error.ErrorListener.prototype);
AnnotatingErrorListener.prototype.constructor = AnnotatingErrorListener;
AnnotatingErrorListener.prototype.syntaxError = function(recognizer, offendingSymbol, line, column, msg, e) {
this.annotations.push({
row: line - 1,
column: column,
text: msg,
type: "error"
});
};
var validate = function(input) {
var stream = new antlr4.InputStream(input);
var lexer = new Lexer.SparkS_v2Lexer(stream);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new Parser.SparkS_v2Parser(tokens);
var annotations = [];
var listener = new AnnotatingErrorListener(annotations)
parser.removeErrorListeners();
parser.addErrorListener(listener);
parser.parseMyRule();
return annotations;
};
editor.on("keyup", function() {
console.log(validate(editor.getValue()));
})
要加载解析器,我使用" require.js"基于this教程的Torben Haase蜂蜜项目库。但是,我为每个"所需的"得到了以下和其他XML解析错误。文件:
Uncaught Error: Honey: unable to load 0:antlr4/index (0 )
XML Parsing Error: not well-formed Location: file:///home/enes/Desktop/git/text_editor_mirror/antlr4/index.js Line Number 1, Column 1:
ReferenceError: importScripts is not defined
那么,我该如何解决这些错误?
提前致谢...