Search in sources :

Example 16 with CharStream

use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.

the class TTCN3ReferenceAnalyzer method parse.

/**
 * @return the parsed reference or null if the text can not form a reference
 */
public Reference parse(final IFile file, final String code, final boolean reportErrors, final int aLine, final int aOffset) {
    Reference reference = null;
    Reader reader = new StringReader(code);
    CharStream charStream = new UnbufferedCharStream(reader);
    Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    lexer.initRootInterval(code.length());
    lexer.removeErrorListeners();
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    Ttcn3Parser parser = new Ttcn3Parser(tokenStream);
    ParserUtilities.setBuildParseTree(parser);
    lexer.setActualFile(file);
    parser.setActualFile(file);
    parser.setProject(file.getProject());
    parser.setLine(aLine);
    parser.setOffset(aOffset);
    parser.removeErrorListeners();
    final Pr_UnifiedReferenceParserContext root = parser.pr_UnifiedReferenceParser();
    ParserUtilities.logParseTree(root, parser);
    reference = root.reference;
    return reference;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) Pr_UnifiedReferenceParserContext(org.eclipse.titan.designer.parsers.ttcn3parser.Ttcn3Parser.Pr_UnifiedReferenceParserContext) Reference(org.eclipse.titan.designer.AST.Reference) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 17 with CharStream

use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.

the class TTCN3ReparseUpdater method startsWithFollow.

/**
 * Checks if the first TTCN-3 lexical token in the substring, that covers the possibly changed interval of the document belongs to a given list of
 * expected tokens or not.
 *
 * @param followSet the possible tokens that can follow the element before the suspected
 *
 * @return true if the first lexical token is part of the followset, false otherwise
 */
public boolean startsWithFollow(final List<Integer> followSet) {
    if (followSet == null || followSet.isEmpty()) {
        return false;
    }
    if (code == null) {
        return false;
    }
    int line = getLineOfOffset(code, modificationStartOffset);
    int column = getPositionInLine(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.setLine(line + 1);
    lexer.setCharPositionInLine(column);
    lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
    Token token = lexer.nextToken();
    if (token == null) {
        return false;
    }
    return followSet.contains(token.getType());
}
Also used : CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Token(org.antlr.v4.runtime.Token) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 18 with CharStream

use of org.antlr.v4.runtime.CharStream 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 19 with CharStream

use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.

the class TTCN3ReparseUpdater method endsWithToken.

/**
 * Checks if the last TTCN-3 lexical token in the substring, that covers the possibly changed interval of the document belongs to a given list of
 * expected tokens or not.
 *
 * @param followSet the possible tokens that can prepend the element before the suspected
 *
 * @return true if the first lexical token is part of the followset, false otherwise
 */
public boolean endsWithToken(final List<Integer> followSet) {
    if (followSet == null || followSet.isEmpty()) {
        return false;
    }
    if (code == null) {
        return false;
    }
    int line = getLineOfOffset(code, modificationStartOffset);
    int column = getPositionInLine(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.setLine(line + 1);
    lexer.setCharPositionInLine(column);
    lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
    Token token = lexer.nextToken();
    if (token == null) {
        return false;
    }
    return followSet.contains(token.getType());
}
Also used : CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Token(org.antlr.v4.runtime.Token) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 20 with CharStream

use of org.antlr.v4.runtime.CharStream 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)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14