Java Code Examples for com.intellij.lang.PsiBuilder#getCurrentOffset()

The following examples show how to use com.intellij.lang.PsiBuilder#getCurrentOffset() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: reasonml-idea-plugin   File: ParserScope.java    License: MIT License 5 votes vote down vote up
private ParserScope(@NotNull PsiBuilder builder, ParserScopeEnum context, ParserScopeEnum resolution, IElementType compositeElementType,
                    ORTokenElementType scopeTokenElementType) {
    m_builder = builder;
    m_mark = builder.mark();
    m_offset = builder.getCurrentOffset();
    m_context = context;
    m_resolution = resolution;
    m_compositeElementType = compositeElementType;
    m_scopeTokenElementType = scopeTokenElementType;
}
 
Example 2
/**
 * @param exact if true then positions should be equal;
 *              else delegate should be behind, not including exactly all foreign (skipped) or whitespace tokens
 */
private void synchronizePositions(boolean exact) {
    final PsiBuilder delegate = getDelegate();

    if (myLexPosition >= myShrunkSequenceSize || delegate.eof()) {
        myLexPosition = myShrunkSequenceSize;
        while (!delegate.eof()) {
            delegate.advanceLexer();
        }
        return;
    }

    if (delegate.getCurrentOffset() > myShrunkSequence.get(myLexPosition).realStart) {
        LOG.debug("delegate is ahead of my builder!");
        return;
    }

    final int keepUpPosition = getKeepUpPosition(exact);

    while (!delegate.eof()) {
        final int delegatePosition = delegate.getCurrentOffset();

        if (delegatePosition < keepUpPosition) {
            delegate.advanceLexer();
        } else {
            break;
        }
    }
}
 
Example 3
/**
 * External rule used to determine if the current lexer position is
 * in an indent. Semantically, this returns true unless the previous
 * character was a newline and the current one is not whitespace.
 * Via recoverWhile, this rule will be used to consume input until
 * it reaches an unindented token, presumably a top-level element
 * of some sort, and resume at that time. It's quite convenient to just
 * resume parsing once we encounter a new top-level element.
 */
public static boolean inIndentRecover(@NotNull PsiBuilder builder, int level) {
    if (builder.eof()) return false;
    final int offset = builder.getCurrentOffset();
    if (offset == 0) return false;
    final CharSequence text = builder.getOriginalText();
    return !(
        text.charAt(offset - 1) == '\n'
            && !Character.isWhitespace(text.charAt(offset)));
}
 
Example 4
/**
 * Called when the parser gets confused from layout rules.
 *
 * Increases the debt of rbraces in the token remapper in
 * HaskellParserWrapper. When the remapper is in debt it will swallow
 * (=remap them to TokenType.WHITE_SPACE) synthetic rbraces until no debt
 * remains.
 */
public static boolean stateHackMess(@NotNull PsiBuilder builder,  int level) {
    if (!(builder instanceof Builder)) return false;
    PsiParser wrapper = ((Builder) builder).parser;
    if (!(wrapper instanceof HaskellParserWrapper)) return false;

    // IElementType tok = builder.getTokenType();
    int offs = builder.getCurrentOffset();
    int line = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs);
    int lineStart = StringUtil.lineColToOffset(builder.getOriginalText(), line, 1);
    Pair<Integer, Integer> p = ((HaskellParserWrapper) wrapper).debtPoints.get(offs);
    if (p != null && p.getSecond() == 0 && !((HaskellParserWrapper) wrapper).regressed) {
        ((HaskellParserWrapper) wrapper).maxRbraceDebt = ((HaskellParserWrapper) wrapper).debtPoints.get(offs).getFirst();
        ((HaskellParserWrapper) wrapper).rbraceDebt = ((HaskellParserWrapper) wrapper).rbraceDebt - ((HaskellParserWrapper) wrapper).maxRbraceDebt;
        ((HaskellParserWrapper) wrapper).debtPoints.put(offs, Pair.create(((HaskellParserWrapper) wrapper).maxRbraceDebt, ((HaskellParserWrapper) wrapper).maxRbraceDebt));
    } else if (((HaskellParserWrapper) wrapper).maxRbraceDebt == -1) {
        int numOpen = findBraces(((HaskellParserWrapper) wrapper).lexer.openBraces, offs, line, lineStart);
        ((HaskellParserWrapper) wrapper).maxRbraceDebt = numOpen;
    }
    // System.out.println("Confused at: " + offs + " line " + line + " on token " + tok
    //        + " regressed: " + ((HaskellParserWrapper) wrapper).regressed + " max: "
    //        + ((HaskellParserWrapper) wrapper).maxRbraceDebt);

    boolean ret = ((HaskellParserWrapper) wrapper).increaseRbraceDebt(builder.getCurrentOffset());
    if (((HaskellParserWrapper) wrapper).maxRbraceDebt == 0) ((HaskellParserWrapper) wrapper).maxRbraceDebt = -1;
    return ret;
}
 
Example 5
public static boolean indented(@NotNull PsiBuilder builder, int level, Parser geq) {
    if (!(builder instanceof Builder)) return false;
    PsiParser wrapper = ((Builder) builder).parser;
    if (!(wrapper instanceof HaskellParserWrapper)) return false;
    if (builder.eof()) return false;

    // IElementType currtok = builder.getTokenType();
    Pair<Integer, IElementType> prevtok = previousElem(builder);
    if (prevtok == null) return true;

    int offs = builder.getCurrentOffset();
    int line = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs);
    int prevline = StringUtil.offsetToLineNumber(builder.getOriginalText(), offs + prevtok.getFirst());
    if (prevline == line) return true;

    int thisLineStart = StringUtil.lineColToOffset(builder.getOriginalText(), line, 0);
    int prevLineStart = StringUtil.lineColToOffset(builder.getOriginalText(), prevline, 0);
    // CharSequence lineStuff = builder.getOriginalText().subSequence(thisLineStart, offs);
    CharSequence prevLineStuff = builder.getOriginalText().subSequence(prevLineStart, thisLineStart - 1);
    int indentation = indentationLevel(prevLineStuff);
    int myindentation = offs - thisLineStart;
    // String tokName = builder.getTokenText();

    boolean geqVal = geq.parse(builder, level);
    if (geqVal && myindentation >= indentation ||
            !geqVal && myindentation > indentation) return true;
    return false;
}
 
Example 6
/**
 * HACK! This is pure copy-pasta from {@link com.intellij.lang.parser.GeneratedParserUtilBase}
 * We are abusing the static import of this class in {@link com.haskforce.parser.HaskellParser}
 * to "override" the {@link GeneratedParserUtilBase#recursion_guard_(com.intellij.lang.PsiBuilder, int, java.lang.String)}
 * static method.
 *
 * The problem is that, for large files, our parser may do a lot of backtracking, and this
 * helps us to prevent seemingly endless loops while parsing. Instead of using the default
 * max recursion level of 1000, we reduce this to 10.
 *
 * Note that changing this value WILL change the parse tree as it breaks
 * the red cuts in the parser.
 *
 * The real solution is to rewrite the parser, but this should provide some amount of
 * life support for the current parser.
 */

public static boolean recursion_guard_(PsiBuilder builder, int level, String funcName) {
    if (level > MAX_RECURSION_LEVEL) {
        final String msg =
            "Maximum recursion level "
                + "(" + MAX_RECURSION_LEVEL + ") "
                + "reached in '" + funcName + "' "
                + "at offset " + builder.getCurrentOffset();
        if (THROW_ON_MAX_RECURSION) throw new RuntimeException(msg);
        builder.mark().error(msg);
        return false;
    }
    return true;
}