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

The following examples show how to use com.intellij.lang.PsiBuilder#lookAhead() . 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 File: RmlParser.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
private void parseStringValue(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    if (state.isCurrentContext(macroRaw)) {
        state.wrapWith(m_types.C_MACRO_RAW_BODY);
    } else if (state.isCurrentContext(raw)) {
        state.complete().add(mark(builder, rawBody, m_types.C_MACRO_RAW_BODY).complete()).advance().popEnd();
    } else if (state.isCurrentResolution(annotationName)) {
        state.popEndUntilStartScope();
    } else if (state.isCurrentResolution(maybeRecordUsage)) {
        IElementType nextToken = builder.lookAhead(1);
        if (m_types.COLON.equals(nextToken)) {
            state.updateCurrentContext(object).updateCurrentResolution(object).updateCurrentCompositeElementType(m_types.C_JS_OBJECT);
            state.add(markScope(builder, object, objectField, m_types.C_OBJECT_FIELD, m_types.STRING_VALUE));
        }
    } else if (state.isCurrentResolution(object)) {
        state.add(markScope(builder, object, objectField, m_types.C_OBJECT_FIELD, m_types.STRING_VALUE));
    }
}
 
Example 2
Source File: ParserUtil.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
public static boolean containsTokenInLookahead(PsiBuilder builder, IElementType token, int maxLookahead, boolean allowWhitespace) {
    int i = 0;

    while (i < maxLookahead) {
        IElementType current = allowWhitespace ? builder.lookAhead(i) : builder.rawLookup(i);
        if (current == null) {
            return false;
        }

        if (current == token) {
            return true;
        }

        i++;
    }

    return false;
}
 
Example 3
Source File: RTJSParser.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public RTJSParser(PsiBuilder builder) {
    super(JavaScriptSupportLoader.JAVASCRIPT_1_5, builder);
    myExpressionParser = new RTExpressionParser();
    myStatementParser = new StatementParser<RTJSParser>(this) {
        @Override
        protected void doParseStatement(boolean canHaveClasses) {
            final IElementType firstToken = builder.getTokenType();
            if (firstToken == JSTokenTypes.LBRACE) {
                parseExpressionStatement();
                checkForSemicolon();
                return;
            }
            if (isIdentifierToken(firstToken)) {
                final IElementType nextToken = builder.lookAhead(1);
                if (nextToken == JSTokenTypes.IN_KEYWORD) {
                    parseInStatement();
                    return;
                }
            }
            if (builder.getTokenType() == JSTokenTypes.LPAR) {
                if (parseInStatement()) {
                    return;
                }
            }
            super.doParseStatement(canHaveClasses);
        }

        private boolean parseInStatement() {
            PsiBuilder.Marker statement = builder.mark();
            if (!getExpressionParser().parseInExpression()) {
                statement.drop();
                return false;
            }
            statement.done(JSElementTypes.EXPRESSION_STATEMENT);
            return true;
        }
    };
}
 
Example 4
Source File: ParserUtil.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static boolean hasNextTokens(PsiBuilder builder, boolean enableWhitespace, IElementType... tokens) {
    for (int i = 0, tokensLength = tokens.length; i < tokensLength; i++) {
        IElementType lookAheadToken = enableWhitespace ? builder.rawLookup(i) : builder.lookAhead(i);
        if (lookAheadToken != tokens[i]) {
            return false;
        }
    }

    return true;
}
 
Example 5
Source File: RTJSParser.java    From react-templates-plugin with MIT License 5 votes vote down vote up
public RTJSParser(PsiBuilder builder) {
    super(JavaScriptSupportLoader.JAVASCRIPT_1_5, builder);
    myExpressionParser = new RTExpressionParser();
    myStatementParser = new StatementParser<RTJSParser>(this) {
        @Override
        protected void doParseStatement(boolean canHaveClasses) {
            final IElementType firstToken = builder.getTokenType();
            if (firstToken == JSTokenTypes.LBRACE) {
                parseExpressionStatement();
                checkForSemicolon();
                return;
            }
            if (isIdentifierToken(firstToken)) {
                final IElementType nextToken = builder.lookAhead(1);
                if (nextToken == JSTokenTypes.IN_KEYWORD) {
                    parseInStatement();
                    return;
                }
            }
            if (builder.getTokenType() == JSTokenTypes.LPAR) {
                if (parseInStatement()) {
                    return;
                }
            }
            super.doParseStatement(canHaveClasses);
        }

        private boolean parseInStatement() {
            PsiBuilder.Marker statement = builder.mark();
            if (!getExpressionParser().parseInExpression()) {
                statement.drop();
                return false;
            }
            statement.done(JSElementTypes.EXPRESSION_STATEMENT);
            return true;
        }
    };
}
 
Example 6
Source File: LatteParserUtil.java    From intellij-latte with MIT License 5 votes vote down vote up
public static boolean isNamespace(PsiBuilder builder, int level) {
	PsiBuilder.Marker marker = builder.mark();

	boolean result = false;

	IElementType type = builder.getTokenType();
	IElementType nextToken = builder.lookAhead(1);
	if (type == LatteTypes.T_PHP_NAMESPACE_REFERENCE && nextToken == LatteTypes.T_PHP_NAMESPACE_RESOLUTION) {
		result = true;
	}

	marker.rollbackTo();

	return result;
}
 
Example 7
Source File: RmlParser.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
private void parseLBrace(@NotNull PsiBuilder builder, @NotNull ParserState state) {
    if (state.previousElementType1 == m_types.DOT && state.previousElementType2 == m_types.UIDENT) {
        // Local open a js object
        // Xxx.|>{<| .. }
        state.add(markScope(builder, localOpen, localObjectOpen, m_types.C_LOCAL_OPEN, m_types.LPAREN).complete());
    }

    if (state.isCurrentResolution(typeNamedEq)) {
        boolean isJsObject = builder.lookAhead(1) == m_types.DOT;
        state.add(markScope(builder, isJsObject ? jsObject : recordBinding, isJsObject ? m_types.C_JS_OBJECT : m_types.C_RECORD_EXPR, m_types.LBRACE));
    } else if (state.isCurrentResolution(tryBody)) {
        // A try expression
        //   try (..) |>{<| .. }
        state.updateCurrentResolution(tryBodyWith).
                add(markScope(builder, state.currentContext(), tryBodyWith, m_types.C_TRY_HANDLERS, m_types.LBRACE));
    } else if (state.isCurrentResolution(moduleNamedEq) || state.isCurrentResolution(moduleNamedSignature)) {
        state.add(markScope(builder, moduleBinding, m_types.C_SCOPED_EXPR, m_types.LBRACE));
    } else if (state.isCurrentResolution(letNamedEq)) {
        state.add(markScope(builder, maybeRecord, maybeRecordUsage, m_types.C_SCOPED_EXPR, m_types.LBRACE));
    } else if (state.isCurrentResolution(ifThenStatement)) {
        state.add(markScope(builder, scope, brace, m_types.C_SCOPED_EXPR, m_types.LBRACE));
    } else if (state.isCurrentResolution(clazzNamedEq)) {
        state.add(markScope(builder, clazzBodyScope, m_types.C_SCOPED_EXPR, m_types.LBRACE));
    } else if (state.isCurrentResolution(switchBinaryCondition)) {
        boolean isSwitch = state.popEndUntilContext(switch_).isCurrentResolution(switch_);
        state.add(markScope(builder, isSwitch ? switchBody : brace, m_types.C_SCOPED_EXPR, isSwitch ? m_types.SWITCH : m_types.LBRACE));
    } else if (state.isCurrentResolution(jsxTagPropertyEqValue)) {
        // A scoped property
        state.updateScopeToken(m_types.LBRACE);
    } else if (state.isCurrentResolution(functorParamColon)) {
        state.updateCurrentResolution(functorParamColonSignature).
                add(markScope(builder, state.currentContext(), functorParamColonSignature, m_types.C_SIG_EXPR, m_types.LBRACE).complete());
    } else if (state.isCurrentContext(functorDeclaration) && state.previousElementType1 == m_types.ARROW) {
        // Functor implementation
        //    module M = (..) => |>{<| .. }
        state.add(markScope(builder, functorBinding, functorBinding, m_types.C_FUNCTOR_BINDING, m_types.LBRACE).complete());
    } else {
        // it might be a js object
        IElementType nextElement = builder.lookAhead(1);
        if (state.isCurrentResolution(signatureItem) && nextElement == m_types.DOT) {
            // js object detected (in definition)
            // let x: |>{<|. ___ }
            state.add(markScope(builder, state.currentContext(), jsObject, m_types.C_JS_OBJECT, m_types.LBRACE));
        } else if (nextElement == m_types.STRING_VALUE || nextElement == m_types.DOT) {
            // js object detected (in usage)
            // |>{<| "x" ___ }
            state.add(markScope(builder, state.currentContext(), jsObject, m_types.C_JS_OBJECT, m_types.LBRACE)).
                    advance().
                    add(mark(builder, state.currentContext(), objectField, m_types.C_OBJECT_FIELD));
        } else {
            state.add(markScope(builder, scope, brace, m_types.C_SCOPED_EXPR, m_types.LBRACE));
        }
    }
}