Search in sources :

Example 1 with ConsoleErrorListener

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;
}
Also used : Asn1Parser(org.eclipse.titan.designer.parsers.asn1parser.Asn1Parser) Reference(org.eclipse.titan.designer.AST.Reference) Asn1Lexer(org.eclipse.titan.designer.parsers.asn1parser.Asn1Lexer) StringReader(java.io.StringReader) ASN1Listener(org.eclipse.titan.designer.parsers.asn1parser.ASN1Listener) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) ModuleLevelTokenStreamTracker(org.eclipse.titan.designer.parsers.asn1parser.ModuleLevelTokenStreamTracker) TokenWithIndexAndSubTokensFactory(org.eclipse.titan.designer.parsers.asn1parser.TokenWithIndexAndSubTokensFactory) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 2 with ConsoleErrorListener

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) {
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 3 with ConsoleErrorListener

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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) Reader(java.io.Reader) StringReader(java.io.StringReader) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) TITANMarker(org.eclipse.titan.common.parsers.TITANMarker) StringReader(java.io.StringReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 4 with ConsoleErrorListener

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();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TemplateContext(jetbrick.template.parser.grammer.JetTemplateParser.TemplateContext) Code(jetbrick.template.parser.code.Code) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 5 with ConsoleErrorListener

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) {
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IFileStore(org.eclipse.core.filesystem.IFileStore) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)9 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)8 StringReader (java.io.StringReader)7 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 Reader (java.io.Reader)5 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)5 TitanListener (org.eclipse.titan.common.parsers.TitanListener)5 IOException (java.io.IOException)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)3 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 IFileInfo (org.eclipse.core.filesystem.IFileInfo)2 IFileStore (org.eclipse.core.filesystem.IFileStore)2 IFile (org.eclipse.core.resources.IFile)2 CoreException (org.eclipse.core.runtime.CoreException)2 SyntacticErrorStorage (org.eclipse.titan.common.parsers.SyntacticErrorStorage)2 ASN1Listener (org.eclipse.titan.designer.parsers.asn1parser.ASN1Listener)2 Asn1Lexer (org.eclipse.titan.designer.parsers.asn1parser.Asn1Lexer)2