Uncaught Error: Module name "antlr4/index" has not been loaded yet for context on require.js
Uncaught Error: Module name "antlr4/index" has not been loaded yet for context on require.js
我尝试在 javascript 上使用 antlr4,然后阅读 https://tomassetti.me/antlr-and-the-web/ 并进行编译,但出现错误。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="lib/require.js"></script>
<script type="text/javascript">
var antlr4 = require('antlr4/index');
var QueryLexer = require('gram/queryLexer');
var QueryParser = require('gram/queryParser');
document.getElementById("parse").addEventListener("click", function() {
var input = document.getElementById("code").value;
var chars = new antlr4.InputStream(input);
var lexer = new QueryLexer.queryLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new QueryParser.queryParser(tokens);
parser.buildParseTrees = true;
var tree = parser.query();
console.log("Parsed: "+ tree);
});
</script>
</head>
<body>
<div id="inputs">
<textarea id="code">
* play with antlr4
* write a tutorial
</textarea>
<br/>
<button id="parse">Parse</button>
</div>
</body>
</html>
错误可能会在 "var antlr4 = require('antlr4/index');" 上引起。
我从 http://www.antlr.org/download/index.html 下载了 antlr4 并放置了 "index.html" 的同一层。在 lib 目录中存在 "require.js".
index.js
exports.atn = require('./atn/index');
exports.codepointat = require('./polyfills/codepointat');
exports.dfa = require('./dfa/index');
exports.fromcodepoint = require('./polyfills/fromcodepoint');
exports.tree = require('./tree/index');
exports.error = require('./error/index');
exports.Token = require('./Token').Token;
exports.CharStreams = require('./CharStreams').CharStreams;
exports.CommonToken = require('./Token').CommonToken;
exports.InputStream = require('./InputStream').InputStream;
exports.FileStream = require('./FileStream').FileStream;
exports.CommonTokenStream = require('./CommonTokenStream').CommonTokenStream;
exports.Lexer = require('./Lexer').Lexer;
exports.Parser = require('./Parser').Parser;
var pc = require('./PredictionContext');
exports.PredictionContextCache = pc.PredictionContextCache;
exports.ParserRuleContext = require('./ParserRuleContext').ParserRuleContext;
exports.Interval = require('./IntervalSet').Interval;
exports.Utils = require('./Utils');
我觉得没有问题,因为require path('antlr4/index')没有错。
但是发生了错误。请给我一些想法。
您在问题中显示的代码不能按原样使用 RequireJS。您必须以不同的方式编写 require
调用,或者将所有 require
调用包装在 define
中,以便使用 RequireJS 提供的 CommonJS support。
但是本教程并未要求您使用 RequireJS。如果你去教程作者提供的github repo,你会看到:
Require.js was obtained from https://github.com/letorbi/smoothie/blob/master/standalone/require.js
你必须使用那个文件,它不是 RequireJS,但在它加载脚本的意义上类似于它,但在它似乎支持 CommonJS 模块格式的意义上与 RequireJS 不同——是,而 RequireJS 没有。
我尝试在 javascript 上使用 antlr4,然后阅读 https://tomassetti.me/antlr-and-the-web/ 并进行编译,但出现错误。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="lib/require.js"></script>
<script type="text/javascript">
var antlr4 = require('antlr4/index');
var QueryLexer = require('gram/queryLexer');
var QueryParser = require('gram/queryParser');
document.getElementById("parse").addEventListener("click", function() {
var input = document.getElementById("code").value;
var chars = new antlr4.InputStream(input);
var lexer = new QueryLexer.queryLexer(chars);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new QueryParser.queryParser(tokens);
parser.buildParseTrees = true;
var tree = parser.query();
console.log("Parsed: "+ tree);
});
</script>
</head>
<body>
<div id="inputs">
<textarea id="code">
* play with antlr4
* write a tutorial
</textarea>
<br/>
<button id="parse">Parse</button>
</div>
</body>
</html>
错误可能会在 "var antlr4 = require('antlr4/index');" 上引起。 我从 http://www.antlr.org/download/index.html 下载了 antlr4 并放置了 "index.html" 的同一层。在 lib 目录中存在 "require.js".
index.js
exports.atn = require('./atn/index');
exports.codepointat = require('./polyfills/codepointat');
exports.dfa = require('./dfa/index');
exports.fromcodepoint = require('./polyfills/fromcodepoint');
exports.tree = require('./tree/index');
exports.error = require('./error/index');
exports.Token = require('./Token').Token;
exports.CharStreams = require('./CharStreams').CharStreams;
exports.CommonToken = require('./Token').CommonToken;
exports.InputStream = require('./InputStream').InputStream;
exports.FileStream = require('./FileStream').FileStream;
exports.CommonTokenStream = require('./CommonTokenStream').CommonTokenStream;
exports.Lexer = require('./Lexer').Lexer;
exports.Parser = require('./Parser').Parser;
var pc = require('./PredictionContext');
exports.PredictionContextCache = pc.PredictionContextCache;
exports.ParserRuleContext = require('./ParserRuleContext').ParserRuleContext;
exports.Interval = require('./IntervalSet').Interval;
exports.Utils = require('./Utils');
我觉得没有问题,因为require path('antlr4/index')没有错。 但是发生了错误。请给我一些想法。
您在问题中显示的代码不能按原样使用 RequireJS。您必须以不同的方式编写 require
调用,或者将所有 require
调用包装在 define
中,以便使用 RequireJS 提供的 CommonJS support。
但是本教程并未要求您使用 RequireJS。如果你去教程作者提供的github repo,你会看到:
Require.js was obtained from https://github.com/letorbi/smoothie/blob/master/standalone/require.js
你必须使用那个文件,它不是 RequireJS,但在它加载脚本的意义上类似于它,但在它似乎支持 CommonJS 模块格式的意义上与 RequireJS 不同——是,而 RequireJS 没有。