use of org.antlr.v4.runtime.Recognizer in project antlr4 by antlr.
the class BaseBrowserTest method writeParserTestFile.
protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) {
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" + " TreeShapeListener = null;\r\n" + " " + lexerName + " = null;\r\n" + " " + parserName + " = null;\r\n" + " " + listenerName + " = null;\r\n" + " " + visitorName + " = null;\r\n" + " printer = function() {\r\n" + " this.println = function(s) { document.getElementById('output').value += s + '\\n'; }\r\n" + " this.print = function(s) { document.getElementById('output').value += s; }\r\n" + " return this;\r\n" + " };\r\n" + "\r\n" + " loadParser = function() {\r\n" + " try {\r\n" + " antlr4 = require('antlr4/index');\r\n" + " " + lexerName + " = require('./parser/" + lexerName + "');\n" + " " + parserName + " = require('./parser/" + parserName + "');\n" + " " + listenerName + " = require('./parser/" + listenerName + "');\n" + " " + visitorName + " = require('./parser/" + visitorName + "');\n" + " } catch (ex) {\r\n" + " document.getElementById('errors').value = ex.toString();\r\n" + " }\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" + " TreeShapeListener = function() {\r\n" + " antlr4.tree.ParseTreeListener.call(this);\r\n" + " return this;\r\n" + " };\r\n" + "\r\n" + " TreeShapeListener.prototype = Object.create(antlr4.tree.ParseTreeListener.prototype);\r\n" + " TreeShapeListener.prototype.constructor = TreeShapeListener;\r\n" + "\r\n" + " TreeShapeListener.prototype.enterEveryRule = function(ctx) {\r\n" + " for(var i=0;i<ctx.getChildCount; i++) {\r\n" + " var child = ctx.getChild(i);\r\n" + " var parent = child.parentCtx;\r\n" + " if(parent.getRuleContext() !== ctx || !(parent instanceof antlr4.tree.RuleNode)) {\r\n" + " throw 'Invalid parse tree shape detected.';\r\n" + " }\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 stream = new antlr4.InputStream(input, true);\n" + " var lexer = new " + lexerName + "." + lexerName + "(stream);\n" + " lexer._listeners = [new listener()];\r\n" + " var tokens = new antlr4.CommonTokenStream(lexer);\n" + " var parser = new " + parserName + "." + parserName + "(tokens);\n" + " parser._listeners.push(new listener());\n" + (debug ? " parser._listeners.push(new antlr4.error.DiagnosticErrorListener());\n" : "") + " parser.buildParseTrees = true;\n" + " parser.printer = new printer();\n" + " var tree = parser." + parserStartRuleName + "();\n" + " antlr4.tree.ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\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='loadParser()'>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 lucene-solr by apache.
the class JavascriptParserErrorStrategy method recover.
/**
* Ensures the ANTLR parser will throw an exception after the first error
*
* @param recognizer the parser being used
* @param re the original exception from the parser
*/
@Override
public void recover(Parser recognizer, RecognitionException re) {
Token token = re.getOffendingToken();
String message;
if (token == null) {
message = "error " + getTokenErrorDisplay(token);
} else if (re instanceof InputMismatchException) {
message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + re.getExpectedTokens().toString(recognizer.getVocabulary());
} else if (re instanceof NoViableAltException) {
if (token.getType() == JavascriptParser.EOF) {
message = "unexpected end of expression";
} else {
message = "invalid sequence of tokens near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
}
} else {
message = " unexpected token near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
}
ParseException parseException = new ParseException(message, token.getStartIndex());
parseException.initCause(re);
throw new RuntimeException(parseException);
}
use of org.antlr.v4.runtime.Recognizer in project compiler by boalang.
the class ParserErrorListener method syntaxError.
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
final Token offendingToken = (Token) offendingSymbol;
error("parser", ((CommonTokenStream) recognizer.getInputStream()).getTokenSource(), offendingSymbol, line, charPositionInLine, offendingToken.getStopIndex() - offendingToken.getStartIndex() + 1, msg, e);
}
use of org.antlr.v4.runtime.Recognizer in project compiler by boalang.
the class BoaCompiler method parseOnly.
public static void parseOnly(final String[] args) throws IOException {
final CommandLine cl = processParseCommandLineOptions(args);
if (cl == null)
return;
final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
// 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());
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()));
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);
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"));
}
} 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() + ": parsing failed: ");
e.printStackTrace();
}
}
}
use of org.antlr.v4.runtime.Recognizer in project latexdraw by arnobl.
the class CodeInserter method initialize.
@Override
public void initialize(final URL location, final ResourceBundle resources) {
label.setText(LangTool.INSTANCE.getBundle().getString("LaTeXDrawFrame.16"));
// Collecting errors from the parser.
final ANTLRErrorListener errorListener = new BaseErrorListener() {
@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
// NON-NLS
errorLog.setText(errorLog.getText() + "Syntax error: " + msg + LSystem.EOL);
}
};
final PSTLatexdrawListener listener = new PSTLatexdrawListener() {
@Override
public void exitUnknowncmds(final PSTParser.UnknowncmdsContext ctx) {
// NON-NLS
errorLog.setText(errorLog.getText() + "Unknown command: " + ctx.LATEXCMD().getSymbol().getText() + LSystem.EOL);
}
@Override
public void enterUnknownParamSetting(final PSTParser.UnknownParamSettingContext ctx) {
// NON-NLS
errorLog.setText(errorLog.getText() + "Unknown parameter: " + ctx.name.getText() + LSystem.EOL);
}
@Override
public void visitErrorNode(final ErrorNode node) {
// NON-NLS
errorLog.setText(errorLog.getText() + "Error: " + node.getText() + LSystem.EOL);
}
@Override
public void exitText(final PSTParser.TextContext ctx) {
super.exitText(ctx);
if (ctx.getText().startsWith("\\")) {
// NON-NLS
errorLog.setText(errorLog.getText() + "Bad command: '" + ctx.getText() + "'?" + LSystem.EOL);
}
}
};
listener.log.addHandler(new Handler() {
@Override
public void publish(final LogRecord record) {
errorLog.setText(errorLog.getText() + record.getMessage() + LSystem.EOL);
}
@Override
public void flush() {
}
@Override
public void close() {
}
});
// On each text change, the code is parsed and errors reported.
text.textProperty().addListener((observable, oldValue, newValue) -> {
errorLog.setText("");
final PSTLexer lexer = new PSTLexer(CharStreams.fromString(newValue));
lexer.addErrorListener(errorListener);
final PSTParser parser = new PSTParser(new CommonTokenStream(lexer));
parser.addParseListener(listener);
parser.addErrorListener(errorListener);
parser.pstCode(new PSTContext());
});
}
Aggregations