use of org.antlr.v4.runtime.Recognizer in project lucene-solr by apache.
the class JavascriptParserErrorStrategy method recoverInline.
/**
* Ensures the ANTLR parser will throw an exception after the first error
*
* @param recognizer the parser being used
* @return no actual return value
* @throws RecognitionException not used as a ParseException wrapped in a RuntimeException is thrown instead
*/
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
Token token = recognizer.getCurrentToken();
String message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + recognizer.getExpectedTokens().toString(recognizer.getVocabulary());
ParseException parseException = new ParseException(message, token.getStartIndex());
throw new RuntimeException(parseException);
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.
the class BaseBrowserTest method execHtmlPage.
public String execHtmlPage(String fileName, String input) throws Exception {
// 'file' protocol is not supported by Selenium drivers
// so we run an embedded Jetty server
ServerThread thread = new ServerThread(fileName);
thread.start();
try {
while (thread.server == null && thread.ex == null) Thread.sleep(10);
if (thread.ex != null)
throw thread.ex;
while (thread.server.isStarting()) Thread.sleep(10);
// despite all the above precautions, driver.get often fails if you don't give time to Jetty
Thread.sleep(400);
driver.get("http://localhost:" + httpPort + "/" + fileName);
driver.findElement(new ById("input")).clear();
driver.findElement(new ById("output")).clear();
driver.findElement(new ById("errors")).clear();
driver.navigate().refresh();
driver.findElement(new ById("input")).sendKeys(input);
driver.findElement(new ById("load")).click();
driver.findElement(new ById("submit")).click();
String errors = driver.findElement(new ById("errors")).getAttribute("value");
if (errors != null && errors.length() > 0) {
this.stderrDuringParse = errors;
System.err.print(errors);
}
String value = driver.findElement(new ById("output")).getAttribute("value");
// mimic stdout which adds a NL
if (value.length() > 0 && !value.endsWith("\n"))
value = value + "\n";
return value;
} catch (Exception e) {
System.err.println("can't exec recognizer");
e.printStackTrace(System.err);
} finally {
if (thread.server != null) {
thread.server.stop();
while (!thread.server.isStopped()) Thread.sleep(10);
// ensure the port is freed
Thread.sleep(100);
}
}
return null;
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.
the class BaseBrowserTest method writeLexerTestFile.
protected void writeLexerTestFile(String lexerName, boolean showDFA) {
String html = "<!DOCTYPE html>\r\n" + "<html>\r\n" + " <head>\r\n" + " <script src='lib/require.js'></script>\r\n" + " <script>\r\n" + " antlr4 = null;\r\n" + " listener = null;\r\n" + " " + lexerName + " = null;\r\n" + "\r\n" + " loadLexer = function() {\r\n" + " try {\r\n" + " antlr4 = require('antlr4/index');\r\n" + " " + lexerName + " = require('./parser/" + lexerName + "');\r\n" + " } catch (ex) {\r\n" + " document.getElementById('errors').value = ex.toString();\r\n" + " }\r\n" + " listener = function() {\r\n" + " antlr4.error.ErrorListener.call(this);\r\n" + " return this;\r\n" + " }\r\n" + " listener.prototype = Object.create(antlr4.error.ErrorListener.prototype);\r\n" + " listener.prototype.constructor = listener;\r\n" + " listener.prototype.syntaxError = function(recognizer, offendingSymbol, line, column, msg, e) {\r\n" + " document.getElementById('errors').value += 'line ' + line + ':' + column + ' ' + msg + '\\r\\n';\r\n" + " };\r\n" + " }\r\n" + "\r\n" + " test = function() {\r\n" + " document.getElementById('output').value = ''\r\n" + " var input = document.getElementById('input').value;\r\n" + " var chars = new antlr4.InputStream(input, true);\r\n" + " var lexer = new " + lexerName + "." + lexerName + "(chars);\r\n" + " lexer._listeners = [new listener()];\r\n" + " var stream = new antlr4.CommonTokenStream(lexer);\r\n" + " stream.fill();\r\n" + " for(var i=0; i<stream.tokens.length; i++) {\r\n" + " document.getElementById('output').value += stream.tokens[i].toString() + '\\r\\n';\r\n" + " }\n" + (showDFA ? " document.getElementById('output').value += lexer._interp.decisionToDFA[antlr4.Lexer.DEFAULT_MODE].toLexerString();\r\n" : "") + " };\r\n" + "\r\n" + " </script>\r\n" + " </head>\r\n" + " <body>\r\n" + " <textarea id='input'></textarea><br>\r\n" + " <button id='load' type='button' onclick='loadLexer()'>Load</button><br>\r\n" + " <button id='submit' type='button' onclick='test()'>Test</button><br>\r\n" + " <textarea id='output'></textarea><br>\r\n" + " <textarea id='errors'></textarea><br>\r\n" + " </body>\r\n" + "</html>\r\n";
writeFile(httpdir, "Test.html", html);
}
use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.
the class BaseNodeTest method execModule.
public String execModule(String fileName) {
String nodejsPath = locateNodeJS();
String runtimePath = locateRuntime();
String modulePath = new File(new File(tmpdir), fileName).getAbsolutePath();
String inputPath = new File(new File(tmpdir), "input").getAbsolutePath();
try {
ProcessBuilder builder = new ProcessBuilder(nodejsPath, modulePath, inputPath);
builder.environment().put("NODE_PATH", runtimePath + File.pathSeparator + tmpdir);
builder.directory(new File(tmpdir));
Process process = builder.start();
StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
stdoutVacuum.start();
stderrVacuum.start();
process.waitFor();
stdoutVacuum.join();
stderrVacuum.join();
String output = stdoutVacuum.toString();
if (output.length() == 0) {
output = null;
}
if (stderrVacuum.toString().length() > 0) {
this.stderrDuringParse = stderrVacuum.toString();
}
return output;
} catch (Exception e) {
System.err.println("can't exec recognizer");
e.printStackTrace(System.err);
}
return null;
}
use of org.antlr.v4.runtime.Recognizer in project compiler by boalang.
the class BoaCompiler method main.
public static void main(final String[] args) throws IOException {
CommandLine cl = processCommandLineOptions(args);
if (cl == null)
return;
final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
// get the name of the generated class
final String className = getGeneratedClass(cl);
// get the filename of the jar we will be writing
final String jarName;
if (cl.hasOption('o'))
jarName = cl.getOptionValue('o');
else
jarName = className + ".jar";
// make the output directory
File outputRoot = null;
if (cl.hasOption("cd")) {
outputRoot = new File(cl.getOptionValue("cd"));
} else {
outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
}
final File outputSrcDir = new File(outputRoot, "boa");
if (!outputSrcDir.mkdirs())
throw new IOException("unable to mkdir " + outputSrcDir);
// find custom libs to load
final List<URL> libs = new ArrayList<URL>();
if (cl.hasOption('l'))
for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL());
final File outputFile = new File(outputSrcDir, className + ".java");
final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
final List<String> jobnames = new ArrayList<String>();
final List<String> jobs = new ArrayList<String>();
final List<Integer> seeds = new ArrayList<Integer>();
boolean isSimple = true;
final List<Program> visitorPrograms = new ArrayList<Program>();
SymbolTable.initialize(libs);
final int maxVisitors;
if (cl.hasOption('v'))
maxVisitors = Integer.parseInt(cl.getOptionValue('v'));
else
maxVisitors = Integer.MAX_VALUE;
for (int i = 0; i < inputFiles.size(); i++) {
final File f = inputFiles.get(i);
try {
final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath()));
// use the whole input string to seed the RNG
seeds.add(lexer._input.getText(new Interval(0, lexer._input.size())).hashCode());
lexer.removeErrorListeners();
lexer.addErrorListener(new LexerErrorListener());
final CommonTokenStream tokens = new CommonTokenStream(lexer);
final BoaParser parser = new BoaParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
throw new ParseCancellationException(e);
}
});
final BoaErrorListener parserErrorListener = new ParserErrorListener();
final Start p = parse(tokens, parser, parserErrorListener);
if (cl.hasOption("ast"))
new ASTPrintingVisitor().start(p);
final String jobName = "" + i;
try {
if (!parserErrorListener.hasError) {
new TypeCheckingVisitor().start(p, new SymbolTable());
final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor();
simpleVisitor.start(p);
LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex"));
isSimple &= !simpleVisitor.isComplex();
new InheritedAttributeTransformer().start(p);
new LocalAggregationTransformer().start(p);
// also let jobs have own methods if visitor merging is disabled
if (!simpleVisitor.isComplex() || maxVisitors < 2 || inputFiles.size() == 1) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(jobName);
} else // if a job has visitors, fuse them all together into a single program
{
p.getProgram().jobName = jobName;
visitorPrograms.add(p.getProgram());
}
}
} catch (final TypeCheckException e) {
parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e);
}
} catch (final Exception e) {
System.err.print(f.getName() + ": compilation failed: ");
e.printStackTrace();
}
}
if (!visitorPrograms.isEmpty())
try {
for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(p.jobName);
}
} catch (final Exception e) {
System.err.println("error fusing visitors - falling back: " + e);
e.printStackTrace();
for (final Program p : visitorPrograms) {
new VisitorOptimizingTransformer().start(p);
if (cl.hasOption("pp"))
new PrettyPrintVisitor().start(p);
if (cl.hasOption("ast2"))
new ASTPrintingVisitor().start(p);
final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
cg.start(p);
jobs.add(cg.getCode());
jobnames.add(p.jobName);
}
}
if (jobs.size() == 0)
throw new RuntimeException("no files compiled without error");
final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
st.add("name", className);
st.add("numreducers", inputFiles.size());
st.add("jobs", jobs);
st.add("jobnames", jobnames);
st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024);
st.add("seeds", seeds);
if (DefaultProperties.localDataPath != null) {
st.add("isLocal", true);
}
o.write(st.render().getBytes());
} finally {
o.close();
}
compileGeneratedSrc(cl, jarName, outputRoot, outputFile);
}
Aggregations