Search in sources :

Example 1 with TitanListener

use of org.eclipse.titan.common.parsers.TitanListener 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 2 with TitanListener

use of org.eclipse.titan.common.parsers.TitanListener in project titan.EclipsePlug-ins by eclipse.

the class VariantAttributeAnalyzer method parse.

public void parse(final RawAST rawAST, final AttributeSpecification specification, final int lengthMultiplier, final AtomicBoolean raw_found) {
    VariantAttributeLexer lexer;
    Location location = specification.getLocation();
    StringReader reader = new StringReader(specification.getSpecification());
    CharStream charStream = new UnbufferedCharStream(reader);
    lexer = new VariantAttributeLexer(charStream);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    TitanListener lexerListener = new TitanListener();
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    VariantAttributeParser parser = new VariantAttributeParser(tokenStream);
    parser.setBuildParseTree(false);
    TitanListener parserListener = new TitanListener();
    parser.removeErrorListeners();
    parser.addErrorListener(parserListener);
    parser.setActualFile((IFile) location.getFile());
    parser.setLine(location.getLine());
    parser.setOffset(location.getOffset() + 1);
    MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER, location.getFile(), location.getOffset(), location.getEndOffset());
    parser.setRawAST(rawAST);
    parser.setLengthMultiplier(lengthMultiplier);
    parser.pr_AttribSpec();
    if (!lexerListener.getErrorsStored().isEmpty()) {
        for (int i = 0; i < lexerListener.getErrorsStored().size(); i++) {
            Location temp = new Location(location);
            temp.setOffset(temp.getOffset() + 1);
            ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), lexerListener.getErrorsStored().get(i), IMarker.SEVERITY_ERROR, temp);
        }
    }
    if (!parserListener.getErrorsStored().isEmpty()) {
        for (int i = 0; i < parserListener.getErrorsStored().size(); i++) {
            Location temp = new Location(location);
            temp.setOffset(temp.getOffset() + 1);
            ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), parserListener.getErrorsStored().get(i), IMarker.SEVERITY_ERROR, temp);
        }
    }
    if (!raw_found.get()) {
        raw_found.set(parser.getRawFound());
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) StringReader(java.io.StringReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) Location(org.eclipse.titan.designer.AST.Location)

Example 3 with TitanListener

use of org.eclipse.titan.common.parsers.TitanListener in project titan.EclipsePlug-ins by eclipse.

the class ExtensionAttributeAnalyzer method parse.

public void parse(final AttributeSpecification specification) {
    ExtensionAttributeLexer lexer;
    Location location = specification.getLocation();
    StringReader reader = new StringReader(specification.getSpecification());
    CharStream charStream = new UnbufferedCharStream(reader);
    lexer = new ExtensionAttributeLexer(charStream);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    TitanListener lexerListener = new TitanListener();
    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);
    ExtensionAttributeParser parser = new ExtensionAttributeParser(tokenStream);
    parser.setBuildParseTree(false);
    TitanListener parserListener = new TitanListener();
    parser.removeErrorListeners();
    parser.addErrorListener(parserListener);
    parser.setActualFile((IFile) location.getFile());
    parser.setLine(location.getLine());
    parser.setOffset(location.getOffset() + 1);
    MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER, location.getFile(), location.getOffset(), location.getEndOffset());
    attributes = null;
    attributes = parser.pr_ExtensionAttributeRoot().list;
    if (!lexerListener.getErrorsStored().isEmpty()) {
        String reportLevel = Platform.getPreferencesService().getString(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.REPORTERRORSINEXTENSIONSYNTAX, GeneralConstants.WARNING, null);
        int errorLevel;
        if (GeneralConstants.ERROR.equals(reportLevel)) {
            errorLevel = IMarker.SEVERITY_ERROR;
        } else if (GeneralConstants.WARNING.equals(reportLevel)) {
            errorLevel = IMarker.SEVERITY_WARNING;
        } else {
            return;
        }
        for (int i = 0; i < lexerListener.getErrorsStored().size(); i++) {
            Location temp = new Location(location);
            temp.setOffset(temp.getOffset() + 1);
            ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), lexerListener.getErrorsStored().get(i), errorLevel, temp);
        }
    }
    if (!parserListener.getErrorsStored().isEmpty()) {
        String reportLevel = Platform.getPreferencesService().getString(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.REPORTERRORSINEXTENSIONSYNTAX, GeneralConstants.WARNING, null);
        int errorLevel;
        if (GeneralConstants.ERROR.equals(reportLevel)) {
            errorLevel = IMarker.SEVERITY_ERROR;
        } else if (GeneralConstants.WARNING.equals(reportLevel)) {
            errorLevel = IMarker.SEVERITY_WARNING;
        } else {
            return;
        }
        for (int i = 0; i < parserListener.getErrorsStored().size(); i++) {
            Location temp = new Location(location);
            temp.setOffset(temp.getOffset() + 1);
            ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), parserListener.getErrorsStored().get(i), errorLevel, temp);
        }
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) StringReader(java.io.StringReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) Location(org.eclipse.titan.designer.AST.Location)

Example 4 with TitanListener

use of org.eclipse.titan.common.parsers.TitanListener 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)

Example 5 with TitanListener

use of org.eclipse.titan.common.parsers.TitanListener 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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) IFile(org.eclipse.core.resources.IFile) ErroneousAttributeSpecification(org.eclipse.titan.designer.AST.TTCN3.attributes.ErroneousAttributeSpecification) Ttcn3Reparser(org.eclipse.titan.designer.parsers.ttcn3parser.Ttcn3Reparser) Ttcn3Lexer(org.eclipse.titan.designer.parsers.ttcn3parser.Ttcn3Lexer) Reader(java.io.Reader) StringReader(java.io.StringReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) TITANMarker(org.eclipse.titan.common.parsers.TITANMarker) Pr_ErroneousAttributeSpecContext(org.eclipse.titan.designer.parsers.ttcn3parser.Ttcn3Reparser.Pr_ErroneousAttributeSpecContext) SyntacticErrorStorage(org.eclipse.titan.common.parsers.SyntacticErrorStorage) StringReader(java.io.StringReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)7 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)7 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)7 TitanListener (org.eclipse.titan.common.parsers.TitanListener)7 StringReader (java.io.StringReader)6 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)6 Reader (java.io.Reader)4 IOException (java.io.IOException)3 TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)3 Location (org.eclipse.titan.designer.AST.Location)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)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 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ParserATNSimulator (org.antlr.v4.runtime.atn.ParserATNSimulator)1