use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class ASN1ReferenceParser method parseReference.
private Reference parseReference(final IFile file, final String input, final int line, final int offset) {
Reference reference = null;
StringReader reader = new StringReader(input);
CharStream charStream = new UnbufferedCharStream(reader);
Asn1Lexer lexer = new Asn1Lexer(charStream);
lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
ASN1Listener lexerListener = new ASN1Listener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
ModuleLevelTokenStreamTracker tracker = new ModuleLevelTokenStreamTracker(lexer);
tracker.discard(Asn1Lexer.WS);
tracker.discard(Asn1Lexer.MULTILINECOMMENT);
tracker.discard(Asn1Lexer.SINGLELINECOMMENT);
Asn1Parser parser = new Asn1Parser(tracker);
parser.setProject(file.getProject());
parser.setActualFile(file);
parser.setLine(line);
parser.setOffset(offset);
parser.setBuildParseTree(false);
ASN1Listener parserListener = new ASN1Listener();
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
reference = parser.pr_parseReference().reference;
return reference;
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class ASN1Analyzer method parse.
@Override
public void parse(final IFile file, final String code) throws FileNotFoundException {
Reader reader = null;
if (code != null) {
reader = new StringReader(code);
} else if (file != null) {
try {
reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
} catch (Exception e) {
ErrorReporter.logExceptionStackTrace("Could not get the contents of `" + file.getName() + "'", e);
return;
}
} else {
return;
}
final CharStream charStream = new UnbufferedCharStream(reader);
final Asn1Lexer lexer = new Asn1Lexer(charStream);
lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
lexer.setActualFile(file);
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);
tracker.setActualFile(file);
final Asn1Parser parser = new Asn1Parser(tracker);
parser.setProject(file.getProject());
parser.setActualFile(file);
parser.setBuildParseTree(false);
parserListener = new ASN1Listener(parser);
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
parser.pr_ASN1ModuleDefinition();
actualAsn1Module = parser.getModule();
warnings = parser.getWarnings();
unsupportedConstructs = parser.getUnsupportedConstructs();
try {
reader.close();
} catch (IOException e) {
}
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReparseUpdater method parse.
public int parse(final ITTCN3ReparseBase userDefined) {
if (modificationStartOffset == modificationEndOffset + shift) {
return 0;
}
// double wideparsing = System.nanoTime();
mErrors = null;
warnings = null;
Iterator<TITANMarker> iterator = unsupportedConstructs.iterator();
while (iterator.hasNext()) {
TITANMarker marker = iterator.next();
if ((marker.getOffset() > modificationStartOffset && marker.getOffset() <= modificationEndOffset) || (marker.getEndOffset() > modificationStartOffset && marker.getEndOffset() <= modificationEndOffset)) {
iterator.remove();
}
}
MarkerHandler.markAllOnTheFlyMarkersForRemoval(file, modificationStartOffset, modificationEndOffset + shift);
if (code == null) {
return Integer.MAX_VALUE;
}
int line = getLineOfOffset(code, modificationStartOffset);
String substring;
if (code.length() <= modificationEndOffset + shift) {
substring = code.substring(modificationStartOffset);
} else {
substring = code.substring(modificationStartOffset, modificationEndOffset + shift);
}
Reader reader = new StringReader(substring);
CharStream charStream = new UnbufferedCharStream(reader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
// lexer and parser listener
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);
Ttcn3Reparser parser = new Ttcn3Reparser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
lexer.setActualFile(file);
parser.setActualFile(file);
parser.setProject(file.getProject());
parser.setOffset(modificationStartOffset);
parser.setLine(line + 1);
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
userDefined.reparse(parser);
mErrors = parserListener.getErrorsStored();
warnings = parser.getWarnings();
unsupportedConstructs.addAll(parser.getUnsupportedConstructs());
int result = measureIntervallDamage();
if (!parser.isErrorListEmpty()) {
++result;
}
return result;
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project jetbrick-template-1x by subchen.
the class JetTemplate method generateJavaSource.
private String generateJavaSource(Resource resource) {
char[] source = resource.getSource();
ANTLRInputStream is = new ANTLRInputStream(source, source.length);
// set source file name, it will be displayed in error report.
is.name = resource.getAbsolutePath();
JetTemplateLexer lexer = new JetTemplateLexer(is);
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(JetTemplateErrorListener.getInstance());
JetTemplateParser parser = new JetTemplateParser(new CommonTokenStream(lexer));
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(JetTemplateErrorListener.getInstance());
parser.setErrorHandler(new JetTemplateErrorStrategy());
TemplateContext templateParseTree = parser.template();
JetTemplateCodeVisitor visitor = new JetTemplateCodeVisitor(engine, engine.getVariableResolver(), engine.getSecurityManager(), parser, resource);
Code code = templateParseTree.accept(visitor);
return code.toString();
}
use of org.antlr.v4.runtime.ConsoleErrorListener in project titan.EclipsePlug-ins by eclipse.
the class CfgAnalyzer method directParse.
/**
* Parses the provided elements.
* If the contents of an editor are to be parsed, than the file parameter is only used to report the errors to.
*
* @param file the file to parse
* @param fileName the name of the file, to refer to.
* @param code the contents of an editor, or null.
*/
public void directParse(final IFile file, final String fileName, final String code) {
final Reader reader;
final int fileLength;
if (null != code) {
reader = new StringReader(code);
fileLength = code.length();
} else if (null != file) {
try {
reader = new BufferedReader(new InputStreamReader(file.getContents(), StandardCharsets.UTF8));
final IFileStore store = EFS.getStore(file.getLocationURI());
final IFileInfo fileInfo = store.fetchInfo();
fileLength = (int) fileInfo.getLength();
} catch (CoreException e) {
ErrorReporter.logExceptionStackTrace("Could not get the contents of `" + fileName + "'", e);
return;
}
} else {
ErrorReporter.INTERNAL_ERROR("CfgAnalyzer.directParse(): nothing to parse");
return;
}
final CharStream charStream = new UnbufferedCharStream(reader);
final CfgLexer lexer = new CfgLexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.initRootInterval(fileLength);
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);
final CfgParser parser = new CfgParser(tokenStream);
parser.setActualFile(file);
// parser tree is built by default
parserListener = new TitanListener();
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
final ParserRuleContext parseTreeRoot = parser.pr_ConfigFile();
mCfgParseResult = parser.getCfgParseResult();
// manually add the result parse tree, and its corresponding token stream,
// because they logically belong to here
mCfgParseResult.setParseTreeRoot(parseTreeRoot);
mCfgParseResult.setTokens(tokenStream.getTokens());
// fill handlers
moduleParametersHandler = parser.getModuleParametersHandler();
testportParametersHandler = parser.getTestportParametersHandler();
componentSectionHandler = parser.getComponentSectionHandler();
groupSectionHandler = parser.getGroupSectionHandler();
mcSectionHandler = parser.getMcSectionHandler();
externalCommandsSectionHandler = parser.getExternalCommandsSectionHandler();
executeSectionHandler = parser.getExecuteSectionHandler();
includeSectionHandler = parser.getIncludeSectionHandler();
orderedIncludeSectionHandler = parser.getOrderedIncludeSectionHandler();
defineSectionHandler = parser.getDefineSectionHandler();
loggingSectionHandler = parser.getLoggingSectionHandler();
rootInterval = lexer.getRootInterval();
try {
reader.close();
} catch (IOException e) {
}
}
Aggregations