use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class SpecialASN1Module method parseSpecialInternalAssignment.
/**
* Parses the special internal assignments to build their semantic
* representation.
*
* @param inputCode
* the code to parse.
* @param identifier
* the identifier for the assignment to be created.
*
* @return the parsed assignment.
*/
public static ASN1Assignment parseSpecialInternalAssignment(final String inputCode, final Identifier identifier) {
ASN1Assignment assignment = null;
final StringReader reader = new StringReader(inputCode);
final CharStream charStream = new UnbufferedCharStream(reader);
final Asn1Lexer lexer = new Asn1Lexer(charStream);
lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
final ASN1Listener lexerListener = new ASN1Listener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
final ModuleLevelTokenStreamTracker tracker = new ModuleLevelTokenStreamTracker(lexer);
tracker.discard(Asn1Lexer.WS);
tracker.discard(Asn1Lexer.MULTILINECOMMENT);
tracker.discard(Asn1Lexer.SINGLELINECOMMENT);
final Asn1Parser parser = new Asn1Parser(tracker);
parser.setBuildParseTree(false);
final ASN1Listener parserListener = new ASN1Listener(parser);
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
assignment = parser.pr_TITAN_special_Assignment(identifier).assignment;
if (!parser.getErrorStorage().isEmpty()) {
ErrorReporter.INTERNAL_ERROR(PARSINGFAILED);
for (SyntacticErrorStorage temp : parser.getErrorStorage()) {
ErrorReporter.logError(temp.message);
}
}
return assignment;
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class Definition method parseErrAttrSpecString.
private static ErroneousAttributeSpecification parseErrAttrSpecString(final AttributeSpecification aAttrSpec) {
String code = aAttrSpec.getSpecification();
if (code == null) {
return null;
}
final Location location = aAttrSpec.getLocation();
// code must be transformed, according to
// compiler2/ttcn3/charstring_la.l
// TODO
code = Ttcn3CharstringLexer.parseCharstringValue(code, location);
final Reader reader = new StringReader(code);
final CharStream charStream = new UnbufferedCharStream(reader);
final Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
// needs to be shifted by one because of the \" of the string
lexer.setCharPositionInLine(0);
// lexer and parser listener
final TitanListener parserListener = new TitanListener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(parserListener);
// 1. Previously it was UnbufferedTokenStream(lexer), but it was changed to BufferedTokenStream, because UnbufferedTokenStream seems to be unusable. It is an ANTLR 4 bug.
// Read this: https://groups.google.com/forum/#!topic/antlr-discussion/gsAu-6d3pKU
// pr_PatternChunk[StringBuilder builder, boolean[] uni]:
// $builder.append($v.text); <-- exception is thrown here: java.lang.UnsupportedOperationException: interval 85..85 not in token buffer window: 86..341
// 2. Changed from BufferedTokenStream to CommonTokenStream, otherwise tokens with "-> channel(HIDDEN)" are not filtered out in lexer.
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
final Ttcn3Reparser parser = new Ttcn3Reparser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
final IFile file = (IFile) location.getFile();
parser.setActualFile(file);
parser.setOffset(location.getOffset());
parser.setLine(location.getLine());
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER, location.getFile(), location.getOffset(), location.getEndOffset());
final Pr_ErroneousAttributeSpecContext root = parser.pr_ErroneousAttributeSpec();
ParserUtilities.logParseTree(root, parser);
final ErroneousAttributeSpecification returnValue = root.errAttrSpec;
final List<SyntacticErrorStorage> errors = parser.getErrors();
final List<TITANMarker> warnings = parser.getWarnings();
final List<TITANMarker> unsupportedConstructs = parser.getUnsupportedConstructs();
// add markers
if (errors != null) {
for (int i = 0; i < errors.size(); i++) {
final Location temp = new Location(location);
temp.setOffset(temp.getOffset());
ParserMarkerSupport.createOnTheFlySyntacticMarker(file, errors.get(i), IMarker.SEVERITY_ERROR, temp);
}
}
if (warnings != null) {
for (final TITANMarker marker : warnings) {
if (file.isAccessible()) {
final Location loc = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
loc.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
if (unsupportedConstructs != null) {
for (final TITANMarker marker : unsupportedConstructs) {
if (file.isAccessible()) {
final Location loc = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
loc.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
return returnValue;
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class TTCN3Analyzer method parse.
/**
* Parse TTCN-3 file using ANTLR v4
* @param aReader file to parse (cannot be null, closes aReader)
* @param aFileLength file length
* @param aEclipseFile Eclipse dependent resource file
*/
private void parse(final Reader aReader, final int aFileLength, final IFile aEclipseFile) {
CharStream charStream = new UnbufferedCharStream(aReader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setCommentTodo(true);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.initRootInterval(aFileLength);
TitanListener lexerListener = new TitanListener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
// 1. Previously it was UnbufferedTokenStream(lexer), but it was changed to BufferedTokenStream, because UnbufferedTokenStream seems to be unusable. It is an ANTLR 4 bug.
// Read this: https://groups.google.com/forum/#!topic/antlr-discussion/gsAu-6d3pKU
// pr_PatternChunk[StringBuilder builder, boolean[] uni]:
// $builder.append($v.text); <-- exception is thrown here: java.lang.UnsupportedOperationException: interval 85..85 not in token buffer window: 86..341
// 2. Changed from BufferedTokenStream to CommonTokenStream, otherwise tokens with "-> channel(HIDDEN)" are not filtered out in lexer.
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
Ttcn3Parser parser = new Ttcn3Parser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
PreprocessedTokenStream preprocessor = null;
if (aEclipseFile != null && GlobalParser.TTCNPP_EXTENSION.equals(aEclipseFile.getFileExtension())) {
lexer.setTTCNPP();
preprocessor = new PreprocessedTokenStream(lexer);
preprocessor.setActualFile(aEclipseFile);
if (aEclipseFile.getProject() != null) {
preprocessor.setMacros(PreprocessorSymbolsOptionsData.getTTCN3PreprocessorDefines(aEclipseFile.getProject()));
}
parser = new Ttcn3Parser(preprocessor);
ParserUtilities.setBuildParseTree(parser);
preprocessor.setActualLexer(lexer);
preprocessor.setParser(parser);
}
if (aEclipseFile != null) {
lexer.setActualFile(aEclipseFile);
parser.setActualFile(aEclipseFile);
parser.setProject(aEclipseFile.getProject());
}
// remove ConsoleErrorListener
parser.removeErrorListeners();
TitanListener parserListener = new TitanListener();
parser.addErrorListener(parserListener);
// This is added because of the following ANTLR 4 bug:
// Memory Leak in PredictionContextCache #499
// https://github.com/antlr/antlr4/issues/499
DFA[] decisionToDFA = parser.getInterpreter().decisionToDFA;
parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, new PredictionContextCache()));
// try SLL mode
try {
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
final ParseTree root = parser.pr_TTCN3File();
ParserUtilities.logParseTree(root, parser);
warnings = parser.getWarnings();
mErrorsStored = lexerListener.getErrorsStored();
mErrorsStored.addAll(parserListener.getErrorsStored());
} catch (RecognitionException e) {
// quit
}
if (!warnings.isEmpty() || !mErrorsStored.isEmpty()) {
// SLL mode might have failed, try LL mode
try {
CharStream charStream2 = new UnbufferedCharStream(aReader);
lexer.setInputStream(charStream2);
// lexer.reset();
parser.reset();
parserListener.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
final ParseTree root = parser.pr_TTCN3File();
ParserUtilities.logParseTree(root, parser);
warnings = parser.getWarnings();
mErrorsStored = lexerListener.getErrorsStored();
mErrorsStored.addAll(parserListener.getErrorsStored());
} catch (RecognitionException e) {
}
}
unsupportedConstructs = parser.getUnsupportedConstructs();
rootInterval = lexer.getRootInterval();
actualTtc3Module = parser.getModule();
if (preprocessor != null) {
// if the file was preprocessed
mErrorsStored.addAll(preprocessor.getErrorStorage());
warnings.addAll(preprocessor.getWarnings());
unsupportedConstructs.addAll(preprocessor.getUnsupportedConstructs());
if (actualTtc3Module != null) {
actualTtc3Module.setIncludedFiles(preprocessor.getIncludedFiles());
actualTtc3Module.setInactiveCodeLocations(preprocessor.getInactiveCodeLocations());
}
}
try {
aReader.close();
} catch (IOException e) {
}
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class PreprocessedTokenStream method processIncludeDirective.
/**
* Adds a new lexer to the lexer stack to read tokens from the included
* file
*
* @param fileName
* the file name paramtere of the #include directive
*/
private void processIncludeDirective(final PreprocessorDirective ppDirective) {
if (ppDirective.str == null || "".equals(ppDirective.str)) {
TITANMarker marker = new TITANMarker("File name was not provided", ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
unsupportedConstructs.add(marker);
return;
}
IFile includedFile = GlobalParser.getProjectSourceParser(actualFile.getProject()).getTTCN3IncludeFileByName(ppDirective.str);
if (includedFile == null) {
TITANMarker marker = new TITANMarker(MessageFormat.format("Included file `{0}'' could not be found", ppDirective.str), ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
unsupportedConstructs.add(marker);
return;
}
// check extension
if (!GlobalParser.TTCNIN_EXTENSION.equals(includedFile.getFileExtension())) {
TITANMarker marker = new TITANMarker(MessageFormat.format("File `{0}'' does not have the `{1}'' extension", ppDirective.str, GlobalParser.TTCNIN_EXTENSION), ppDirective.line, -1, -1, IMarker.SEVERITY_WARNING, IMarker.PRIORITY_NORMAL);
warnings.add(marker);
}
// check if the file is already loaded into an editor
String code = null;
if (EditorTracker.containsKey(includedFile)) {
List<ISemanticTITANEditor> editors = EditorTracker.getEditor(includedFile);
ISemanticTITANEditor editor = editors.get(0);
IDocument document = editor.getDocument();
code = document.get();
}
// create lexer and set it up
Reader reader = null;
CharStream charStream = null;
Ttcn3Lexer lexer = null;
int rootInt;
if (code != null) {
reader = new StringReader(code);
charStream = new UnbufferedCharStream(reader);
lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
rootInt = code.length();
} else {
try {
InputStreamReader temp = new InputStreamReader(includedFile.getContents());
if (!includedFile.getCharset().equals(temp.getEncoding())) {
try {
temp.close();
} catch (IOException e) {
ErrorReporter.logWarningExceptionStackTrace(e);
}
temp = new InputStreamReader(includedFile.getContents(), includedFile.getCharset());
}
reader = new BufferedReader(temp);
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
return;
} catch (UnsupportedEncodingException e) {
ErrorReporter.logExceptionStackTrace(e);
return;
}
charStream = new UnbufferedCharStream(reader);
lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexerListener = new TitanListener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
IFileStore store;
try {
store = EFS.getStore(includedFile.getLocationURI());
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace(e);
return;
}
IFileInfo fileInfo = store.fetchInfo();
rootInt = (int) fileInfo.getLength();
}
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.setTTCNPP();
lexer.initRootInterval(rootInt);
lexer.setActualFile(includedFile);
// add the lexer to the stack of lexers
tokenStreamStack.push(new TokenStreamData(lexer, includedFile, reader));
if (parser != null) {
parser.setActualFile(includedFile);
}
includedFiles.add(includedFile);
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project antlr4 by antlr.
the class BasePHPTest method writeParserTestFile.
protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
if (!parserStartRuleName.endsWith(")")) {
parserStartRuleName += "()";
}
ST outputFileST = new ST("\\<?php\n" + "\n" + "declare(strict_types=1);\n" + "\n" + "use Antlr\\Antlr4\\Runtime\\CommonTokenStream;\n" + "use Antlr\\Antlr4\\Runtime\\Error\\Listeners\\DiagnosticErrorListener;\n" + "use Antlr\\Antlr4\\Runtime\\Error\\Listeners\\ConsoleErrorListener;\n" + "use Antlr\\Antlr4\\Runtime\\InputStream;\n" + "use Antlr\\Antlr4\\Runtime\\ParserRuleContext;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ErrorNode;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeListener;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeWalker;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\RuleNode;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\TerminalNode;\n" + "\n" + "$runtime = \\getenv('RUNTIME');\n" + "\n" + "\\spl_autoload_register(function (string $class) use ($runtime) : void {\n" + " $file = \\str_replace('\\\\\\', \\DIRECTORY_SEPARATOR, \\str_replace('Antlr\\Antlr4\\Runtime\\\\\\', $runtime . '\\\\\\src\\\\\\', $class)) . '.php';\n" + "\n" + " if (\\file_exists($file)) {\n" + " require_once $file; \n" + " }\n" + "});\n" + "\n" + "final class TreeShapeListener implements ParseTreeListener {\n" + " public function visitTerminal(TerminalNode $node) : void {}\n" + " public function visitErrorNode(ErrorNode $node) : void {}\n" + " public function exitEveryRule(ParserRuleContext $ctx) : void {}\n" + "\n" + " public function enterEveryRule(ParserRuleContext $ctx) : void {\n" + " for ($i = 0, $count = $ctx->getChildCount(); $i \\< $count; $i++) {\n" + " $parent = $ctx->getChild($i)->getParent();\n" + "\n" + " if (!($parent instanceof RuleNode) || $parent->getRuleContext() !== $ctx) {\n" + " throw new RuntimeException('Invalid parse tree shape detected.');\n" + " }\n" + " }\n" + " }\n" + "}" + "\n" + "$input = InputStream::fromPath($argv[1]);\n" + "$lexer = new <lexerName>($input);\n" + "$lexer->addErrorListener(new ConsoleErrorListener());" + "$tokens = new CommonTokenStream($lexer);\n" + "<createParser>" + "$parser->addErrorListener(new ConsoleErrorListener());" + "$parser->setBuildParseTree(true);\n" + "$tree = $parser-><parserStartRuleName>;\n\n" + "ParseTreeWalker::default()->walk(new TreeShapeListener(), $tree);\n");
String stSource = "$parser = new <parserName>($tokens);\n";
if (debug) {
stSource += "$parser->addErrorListener(new DiagnosticErrorListener());\n";
}
if (trace) {
stSource += "$parser->setTrace(true);\n";
}
ST createParserST = new ST(stSource);
outputFileST.add("createParser", createParserST);
outputFileST.add("parserName", parserName);
outputFileST.add("lexerName", lexerName);
outputFileST.add("listenerName", listenerName);
outputFileST.add("visitorName", visitorName);
outputFileST.add("parserStartRuleName", parserStartRuleName);
writeFile(getTempDirPath(), "Test.php", outputFileST.render());
}
Aggregations