Example usage for org.antlr.v4.runtime.misc IntervalSet contains

List of usage examples for org.antlr.v4.runtime.misc IntervalSet contains

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.misc IntervalSet contains.

Prototype

@Override
public boolean contains(int el) 

Source Link

Usage

From source file:com.nextbreakpoint.nextfractal.contextfree.compiler.CompilerErrorStrategy.java

License:Open Source License

private String generateErrorMessage(String message, Parser recognizer) {
    StringBuilder builder = new StringBuilder();
    builder.append(message);//w  ww .java  2s .c o m
    IntervalSet tokens = recognizer.getExpectedTokens();
    boolean first = true;
    for (Entry<String, Integer> entry : recognizer.getTokenTypeMap().entrySet()) {
        if (tokens.contains(entry.getValue())) {
            if (first) {
                first = false;
                if (message.length() > 0 && !message.endsWith(".")) {
                    builder.append(". ");
                }
                builder.append("Expected tokens: ");
            } else {
                builder.append(", ");
            }
            builder.append(entry.getKey());
        }
    }
    return builder.toString();
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This method implements the single-token insertion inline error recovery
 * strategy. It is called by {@link #recoverInline} if the single-token
 * deletion strategy fails to recover from the mismatched input. If this
 * method returns {@code true}, {@code recognizer} will be in error recovery
 * mode.//from  w ww.  j  a  v a 2s  .c o m
 *
 * <p>
 * This method determines whether or not single-token insertion is viable by
 * checking if the {@code LA(1)} input symbol could be successfully matched
 * if it were instead the {@code LA(2)} symbol. If this method returns
 * {@code true}, the caller is responsible for creating and inserting a
 * token with the correct type to produce this behavior.
 * </p>
 *
 * @param recognizer
 *            the parser instance
 * @return {@code true} if single-token insertion is a viable recovery
 *         strategy for the current mismatched input, otherwise
 *         {@code false}
 */
protected boolean singleTokenInsertion(@NotNull Parser recognizer) {
    int currentSymbolType = recognizer.getInputStream().LA(1);
    // if current token is consistent with what could come after current
    // ATN state, then we know we're missing a token; error recovery
    // is free to conjure up and insert the missing token
    ATNState currentState = recognizer.getInterpreter().atn.states.get(recognizer.getState());
    ATNState next = currentState.transition(0).target;
    ATN atn = recognizer.getInterpreter().atn;
    IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer.getContext());
    // System.out.println("LT(2) set="+expectingAtLL2.toString(recognizer.getTokenNames()));
    if (expectingAtLL2.contains(currentSymbolType)) {
        reportMissingToken(recognizer);
        return true;
    }
    return false;
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/**
 * This method implements the single-token deletion inline error recovery
 * strategy. It is called by {@link #recoverInline} to attempt to recover
 * from mismatched input. If this method returns null, the parser and error
 * handler state will not have changed. If this method returns non-null,
 * {@code recognizer} will <em>not</em> be in error recovery mode since the
 * returned token was a successful match.
 *
 * <p>//from   w w w. ja  va 2  s .co  m
 * If the single-token deletion is successful, this method calls
 * {@link #reportUnwantedToken} to report the error, followed by
 * {@link Parser#consume} to actually "delete" the extraneous token. Then,
 * before returning {@link #reportMatch} is called to signal a successful
 * match.
 * </p>
 *
 * @param recognizer
 *            the parser instance
 * @return the successfully matched {@link Token} instance if single-token
 *         deletion successfully recovers from the mismatched input,
 *         otherwise {@code null}
 */
@Nullable
protected Token singleTokenDeletion(@NotNull Parser recognizer) {
    int nextTokenType = recognizer.getInputStream().LA(2);
    IntervalSet expecting = getExpectedTokens(recognizer);
    if (expecting.contains(nextTokenType)) {
        reportUnwantedToken(recognizer);
        /*
         * System.err.println("recoverFromMismatchedToken deleting "+
         * ((TokenStream)recognizer.getInputStream()).LT(1)+
         * " since "+((TokenStream)recognizer.getInputStream()).LT(2)+
         * " is what we want");
         */
        recognizer.consume(); // simply delete extra token
        // we want to return the token we're actually matching
        Token matchedSymbol = recognizer.getCurrentToken();
        reportMatch(recognizer); // we know current token is correct
        return matchedSymbol;
    }
    return null;
}

From source file:com.sample.JavaErrorStrategy.java

License:BSD License

/** Consume tokens until one matches the given token set. */
protected void consumeUntil(@NotNull Parser recognizer, @NotNull IntervalSet set) {
    // System.err.println("consumeUntil("+set.toString(recognizer.getTokenNames())+")");
    int ttype = recognizer.getInputStream().LA(1);
    while (ttype != Token.EOF && !set.contains(ttype)) {
        // System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
        // recognizer.getInputStream().consume();
        recognizer.consume();//from  ww w .  j a va 2  s . c om
        ttype = recognizer.getInputStream().LA(1);
    }
}

From source file:org.tvl.goworks.editor.go.completion.GoCompletionQuery.java

License:Open Source License

public static RuleContext getTopContext(Parser parser, RuleContext context, IntervalSet values,
        boolean checkTop) {
    if (checkTop && context instanceof ParserRuleContext) {
        if (values.contains(context.getRuleIndex())) {
            return context;
        }//from w  ww  .  ja  v  a 2  s  . c  o  m
    }

    if (context.isEmpty()) {
        return null;
    }

    if (values.contains(parser.getATN().states.get(context.invokingState).ruleIndex)) {
        return context.parent;
    }

    return getTopContext(parser, context.parent, values, false);
}

From source file:org.tvl.goworks.editor.go.completion.GoCompletionQuery.java

License:Open Source License

@CheckForNull
public static ParseTree getTopContext(@NonNull ParseTree context, @NonNull IntervalSet values,
        boolean checkTop) {
    if (checkTop && context instanceof RuleNode
            && values.contains(((RuleNode) context).getRuleContext().getRuleIndex())) {
        return context;
    }/*w  w w. j a  v a2  s.c om*/

    ParseTree parent = context.getParent();
    if (parent == null) {
        return null;
    }

    return getTopContext(parent, values, true);
}

From source file:org.tvl.goworks.editor.go.highlighter.GoHighlighterLexer.java

License:Open Source License

private static Transition patchTransition(Transition transition, IntervalSet digitChars,
        IntervalSet letterChars) {//w  w  w  .  j ava 2 s .  c o  m
    switch (transition.getSerializationType()) {
    case Transition.ATOM:
    case Transition.RANGE:
    case Transition.SET:
        break;

    default:
        return null;
    }

    IntervalSet label = transition.label();
    if (label == null) {
        return null;
    }

    IntervalSet updated = null;
    if (label.contains(unicodeDigitPlaceholder)) {
        updated = new IntervalSet(label);
        updated.addAll(digitChars);
        if (updated.size() == digitChars.size()) {
            updated = digitChars;
        }
    }

    if (label.contains(unicodeLetterPlaceholder)) {
        if (updated != null) {
            updated.addAll(label);
        } else {
            updated = new IntervalSet(label);
        }

        updated.addAll(letterChars);
        if (updated.size() == letterChars.size()) {
            updated = letterChars;
        }
    }

    if (updated == null) {
        return null;
    }

    return new SetTransition(transition.target, updated);
}

From source file:org.tvl.goworks.editor.go.semantics.TreeCorrectionParserATNSimulator.java

License:Open Source License

@Override
public ATNState getReachableTarget(ATNConfig source, Transition trans, int ttype) {
    if (trans instanceof RuleTransition) {
        IntervalSet suppressed = getSuppressedSet(startIndex);
        if (suppressed.contains(((RuleTransition) trans).ruleIndex)) {
            return null;
        }//w  w  w .j a v a 2s.  com
    }

    return super.getReachableTarget(source, trans, ttype);
}