未捕获错误:模块名称" antlr4 / index"尚未加载require.js上下文

时间:2017-08-14 16:42:26

标签: javascript requirejs antlr4

我尝试在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>

错误可能导致&#34; var antlr4 = require(&#39; antlr4 / index&#39;);&#34;。 我从http://www.antlr.org/download/index.html下载了antlr4并将#34; index.html&#34;放在同一层。在lib目录中存在&#34; require.js&#34;。

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');

我认为没有问题,因为需要路径(&#39; antlr4 / index&#39;)没有错。 但是发生了错误。请给我一些想法。

1 个答案:

答案 0 :(得分:0)

您在问题中显示的代码无法与RequireJS一样正常工作。您必须以不同方式编写require个调用,或者将require所有define个调用包含在var OutputStreamCallback = Java.type("org.apache.nifi.processor.io.OutputStreamCallback"); var StandardCharsets = Java.type("java.nio.charset.StandardCharsets"); Date.prototype.isValid = function () { return (Object.prototype.toString.call(this) === "[object Date]") && !isNaN(this.getTime()); }; var flowFile = session.get(); if (flowFile != null) var userId= flowFile.getAttribute('userId') //equals 1 var parameter=flowFile.getAttribute('parameter')//equals5 var i=0; while(parameter<i) { var flowFile = session.get(); if (flowFile != null) flowFile = session.putAttribute(flowFile, 'filename', userId + '_' + parameter); flowFile = session.write(flowFile, new OutputStreamCallback(function (outputStream) { outputStream.write(0) })); session.transfer(flowFile, REL_SUCCESS) userId++; parameter--; i++; } 中,以便使用RequireJS提供的CommonJS support

本教程并未要求您使用RequireJS 。如果你转到教程编写者提供的github repo,你会看到:

  

Require.js来自https://github.com/letorbi/smoothie/blob/master/standalone/require.js

你必须使用那个不是RequireJS的文件,但是它也可以加载脚本,但它与RequireJS不同,因为它似乎支持CommonJS模块格式, RequireJS没有。