/* * operation? prefixExpression */ private void parsePrefixExpression() { // System.out.println("pre at " + myBuilder.getTokenText()); if (at(AT)) { if (!parseLocalDeclaration()) { PsiBuilder.Marker expression = mark(); myJetParsing.parseAnnotations(DEFAULT); parsePrefixExpression(); expression.done(ANNOTATED_EXPRESSION); } } else { myBuilder.disableJoiningComplexTokens(); if (isAtLabelDefinitionOrMissingIdentifier()) { myBuilder.restoreJoiningComplexTokensState(); parseLabeledExpression(); } else if (atSet(Precedence.PREFIX.getOperations())) { PsiBuilder.Marker expression = mark(); parseOperationReference(); myBuilder.restoreJoiningComplexTokensState(); parsePrefixExpression(); expression.done(PREFIX_EXPRESSION); } else { myBuilder.restoreJoiningComplexTokensState(); parsePostfixExpression(); } } }
@Nullable private static PsiBuilder.Marker parseBinary( final PsiBuilder builder, final ExprType type, final TokenSet ops) { PsiBuilder.Marker left = parseExpression(builder, type); if (left == null) return null; while (true) { final IElementType tokenType = getGtTokenType(builder); if (tokenType == null || !ops.contains(tokenType)) break; final PsiBuilder.Marker binary = left.precede(); advanceGtToken(builder, tokenType); final PsiBuilder.Marker right = parseExpression(builder, type); if (right == null) { error(builder, JavaErrorMessages.message("expected.expression")); binary.done(JavaElementType.BINARY_EXPRESSION); return binary; } binary.done(JavaElementType.BINARY_EXPRESSION); left = binary; } return left; }
/* * if * : "if" "(" element ")" element SEMI? ("else" element)? * ; */ private void parseIf() { assert _at(IF_KEYWORD); PsiBuilder.Marker marker = mark(); advance(); // IF_KEYWORD parseCondition(); PsiBuilder.Marker thenBranch = mark(); if (!at(ELSE_KEYWORD) && !at(SEMICOLON)) { parseExpressionPreferringBlocks(); } if (at(SEMICOLON) && lookahead(1) == ELSE_KEYWORD) { advance(); // SEMICOLON } thenBranch.done(THEN); // lookahead for arrow is needed to prevent capturing of whenEntry like "else -> " if (at(ELSE_KEYWORD) && lookahead(1) != ARROW) { advance(); // ELSE_KEYWORD PsiBuilder.Marker elseBranch = mark(); if (!at(SEMICOLON)) { parseExpressionPreferringBlocks(); } elseBranch.done(ELSE); } marker.done(IF); }
/* * if * : "if" "(" element ")" element SEMI? ("else" element)? * ; */ private void parseIf() { assert _at(IF_KEYWORD); PsiBuilder.Marker marker = mark(); advance(); // IF_KEYWORD parseCondition(); PsiBuilder.Marker thenBranch = mark(); if (!at(ELSE_KEYWORD) && !at(SEMICOLON)) { parseExpressionPreferringBlocks(); } if (at(SEMICOLON) && lookahead(1) == ELSE_KEYWORD) { advance(); // SEMICOLON } thenBranch.done(THEN); if (at(ELSE_KEYWORD)) { advance(); // ELSE_KEYWORD PsiBuilder.Marker elseBranch = mark(); if (!at(SEMICOLON)) { parseExpressionPreferringBlocks(); } elseBranch.done(ELSE); } marker.done(IF); }
public static boolean parseMasonMethod( PsiBuilder b, int l, IElementType closeToken, IElementType statementTokenType) { boolean r = false; PsiBuilder.Marker methodMarker = b.mark(); b.advanceLexer(); PsiBuilder.Marker subMarker = b.mark(); if (PerlParserUtil.consumeToken(b, IDENTIFIER)) { subMarker.collapse(SUB); PerlParserImpl.method_signature(b, l); if (PerlParserUtil.consumeToken(b, MASON_TAG_CLOSER)) { PsiBuilder.Marker blockMarker = b.mark(); PerlParserImpl.block_content(b, l); if (b.getTokenType() == closeToken) { blockMarker.done(BLOCK); blockMarker.setCustomEdgeTokenBinders( WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER); b.advanceLexer(); methodMarker.done(statementTokenType); r = true; } } } if (!r) { methodMarker.rollbackTo(); } return r || recoverToGreedy(b, closeToken, "Error"); }
/* * whenCondition * : expression * : ("in" | "!in") expression * : ("is" | "!is") isRHS * ; */ private void parseWhenCondition() { PsiBuilder.Marker condition = mark(); myBuilder.disableNewlines(); if (at(IN_KEYWORD) || at(NOT_IN)) { PsiBuilder.Marker mark = mark(); advance(); // IN_KEYWORD or NOT_IN mark.done(OPERATION_REFERENCE); if (atSet(WHEN_CONDITION_RECOVERY_SET_WITH_ARROW)) { error("Expecting an element"); } else { parseExpression(); } condition.done(WHEN_CONDITION_IN_RANGE); } else if (at(IS_KEYWORD) || at(NOT_IS)) { advance(); // IS_KEYWORD or NOT_IS if (atSet(WHEN_CONDITION_RECOVERY_SET_WITH_ARROW)) { error("Expecting a type"); } else { myJetParsing.parseTypeRef(); } condition.done(WHEN_CONDITION_IS_PATTERN); } else { if (atSet(WHEN_CONDITION_RECOVERY_SET_WITH_ARROW)) { error("Expecting an expression, is-condition or in-condition"); } else { parseExpression(); } condition.done(WHEN_CONDITION_EXPRESSION); } myBuilder.restoreNewlinesState(); }
/* * operation? prefixExpression */ private void parsePrefixExpression() { // System.out.println("pre at " + myBuilder.getTokenText()); if (at(LBRACKET)) { if (!parseLocalDeclaration()) { PsiBuilder.Marker expression = mark(); myJetParsing.parseAnnotations(REGULAR_ANNOTATIONS_ONLY_WITH_BRACKETS); parsePrefixExpression(); expression.done(ANNOTATED_EXPRESSION); } } else { myBuilder.disableJoiningComplexTokens(); if (at(LABEL_IDENTIFIER)) { myBuilder.restoreJoiningComplexTokensState(); parseLabeledExpression(); } else if (atSet(Precedence.PREFIX.getOperations())) { PsiBuilder.Marker expression = mark(); parseOperationReference(); myBuilder.restoreJoiningComplexTokensState(); parsePrefixExpression(); expression.done(PREFIX_EXPRESSION); } else { myBuilder.restoreJoiningComplexTokensState(); parsePostfixExpression(); } } }
/* * typeConstraint * : attributes SimpleName ":" type * : attributes "class" "object" SimpleName ":" type * ; */ private void parseTypeConstraint() { PsiBuilder.Marker constraint = mark(); parseAnnotations(false); if (at(CLASS_KEYWORD)) { advance(); // CLASS_KEYWORD expect(OBJECT_KEYWORD, "Expecting 'object'", TYPE_REF_FIRST); } PsiBuilder.Marker reference = mark(); if (expect( IDENTIFIER, "Expecting type parameter name", TokenSet.orSet(TokenSet.create(COLON, COMMA), TYPE_REF_FIRST))) { reference.done(REFERENCE_EXPRESSION); } else { reference.drop(); } expect(COLON, "Expecting ':' before the upper bound", TYPE_REF_FIRST); parseTypeRef(); constraint.done(TYPE_CONSTRAINT); }
@Nullable public PsiBuilder.Marker parseConditional(final PsiBuilder builder) { final PsiBuilder.Marker condition = parseExpression(builder, ExprType.CONDITIONAL_OR); if (condition == null) return null; if (builder.getTokenType() != JavaTokenType.QUEST) return condition; final PsiBuilder.Marker ternary = condition.precede(); builder.advanceLexer(); final PsiBuilder.Marker truePart = parse(builder); if (truePart == null) { error(builder, JavaErrorMessages.message("expected.expression")); ternary.done(JavaElementType.CONDITIONAL_EXPRESSION); return ternary; } if (builder.getTokenType() != JavaTokenType.COLON) { error(builder, JavaErrorMessages.message("expected.colon")); ternary.done(JavaElementType.CONDITIONAL_EXPRESSION); return ternary; } builder.advanceLexer(); final PsiBuilder.Marker falsePart = parseConditional(builder); if (falsePart == null) { error(builder, JavaErrorMessages.message("expected.expression")); ternary.done(JavaElementType.CONDITIONAL_EXPRESSION); return ternary; } ternary.done(JavaElementType.CONDITIONAL_EXPRESSION); return ternary; }
public static void parse(PsiBuilder builder, GroovyParser parser) { PsiBuilder.Marker annArgs = builder.mark(); if (!ParserUtils.getToken(builder, mLPAREN)) { annArgs.done(ANNOTATION_ARGUMENTS); return; } if (ParserUtils.lookAhead(builder, mIDENT, mASSIGN)) { if (!parseAnnotationMemberValuePairs(builder, parser)) { annArgs.rollbackTo(); return; } } else { PsiBuilder.Marker pairMarker = builder.mark(); if (!parseAnnotationMemberValueInitializer(builder, parser)) { pairMarker.drop(); } else { pairMarker.done(ANNOTATION_MEMBER_VALUE_PAIR); } } ParserUtils.getToken(builder, mNLS); if (!ParserUtils.getToken(builder, mRPAREN)) { builder.error(GroovyBundle.message("rparen.expected")); } annArgs.done(ANNOTATION_ARGUMENTS); }
private void parseListLiteralExpression(final PsiBuilder builder, boolean isTargetExpression) { LOG.assertTrue(builder.getTokenType() == PyTokenTypes.LBRACKET); final PsiBuilder.Marker expr = builder.mark(); builder.advanceLexer(); if (builder.getTokenType() == PyTokenTypes.RBRACKET) { builder.advanceLexer(); expr.done(PyElementTypes.LIST_LITERAL_EXPRESSION); return; } if (!parseSingleExpression(isTargetExpression)) { builder.error(message("PARSE.expected.expression")); } if (builder.getTokenType() == PyTokenTypes.FOR_KEYWORD) { parseComprehension(expr, PyTokenTypes.RBRACKET, PyElementTypes.LIST_COMP_EXPRESSION); } else { while (builder.getTokenType() != PyTokenTypes.RBRACKET) { if (!matchToken(PyTokenTypes.COMMA)) { builder.error("expected ',' or ']'"); } if (atToken(PyTokenTypes.RBRACKET)) { break; } if (!parseSingleExpression(isTargetExpression)) { builder.error(message("PARSE.expected.expr.or.comma.or.bracket")); break; } } checkMatches(PyTokenTypes.RBRACKET, message("PARSE.expected.rbracket")); expr.done(PyElementTypes.LIST_LITERAL_EXPRESSION); } }
/* * SimpleName{,} */ private void parseFunctionLiteralShorthandParameterList() { PsiBuilder.Marker parameterList = mark(); while (!eof()) { PsiBuilder.Marker parameter = mark(); // int parameterNamePos = matchTokenStreamPredicate(new LastBefore(new // At(IDENTIFIER), new AtOffset(doubleArrowPos))); // createTruncatedBuilder(parameterNamePos).parseModifierList(MODIFIER_LIST, // false); expect(IDENTIFIER, "Expecting parameter name", TokenSet.create(ARROW)); parameter.done(VALUE_PARAMETER); if (at(COLON)) { PsiBuilder.Marker errorMarker = mark(); advance(); // COLON myJetParsing.parseTypeRef(); errorMarker.error( "To specify a type of a parameter or a return type, use the full notation: {(parameter : Type) : ReturnType -> ...}"); } else if (at(ARROW)) { break; } else if (at(COMMA)) { advance(); // COMMA } else { error("Expecting '->' or ','"); break; } } parameterList.done(VALUE_PARAMETER_LIST); }
private boolean parseSliceListTail( PsiBuilder.Marker exprStart, @Nullable PsiBuilder.Marker sliceOrTupleStart) { boolean inSlice = sliceOrTupleStart == null; while (atToken(PyTokenTypes.COMMA)) { nextToken(); PsiBuilder.Marker sliceItemStart = myBuilder.mark(); parseTestExpression(false, false); if (matchToken(PyTokenTypes.COLON)) { inSlice = true; parseTestExpression(false, false); if (matchToken(PyTokenTypes.COLON)) { parseTestExpression(false, false); } } sliceItemStart.done(PyElementTypes.SLICE_ITEM); if (!BRACKET_OR_COMMA.contains(myBuilder.getTokenType())) { myBuilder.error("']' or ',' expected"); break; } } checkMatches(PyTokenTypes.RBRACKET, message("PARSE.expected.rbracket")); if (inSlice) { if (sliceOrTupleStart != null) { sliceOrTupleStart.drop(); } exprStart.done(PyElementTypes.SLICE_EXPRESSION); } return inSlice; }
public void parseSliceEnd(PsiBuilder.Marker exprStart, PsiBuilder.Marker sliceItemStart) { myBuilder.advanceLexer(); if (atToken(PyTokenTypes.RBRACKET)) { PsiBuilder.Marker sliceMarker = myBuilder.mark(); sliceMarker.done(PyElementTypes.EMPTY_EXPRESSION); sliceItemStart.done(PyElementTypes.SLICE_ITEM); nextToken(); exprStart.done(PyElementTypes.SLICE_EXPRESSION); return; } else { if (atToken(PyTokenTypes.COLON)) { PsiBuilder.Marker sliceMarker = myBuilder.mark(); sliceMarker.done(PyElementTypes.EMPTY_EXPRESSION); } else { parseSingleExpression(false); } if (!BRACKET_COLON_COMMA.contains(myBuilder.getTokenType())) { myBuilder.error(message("PARSE.expected.colon.or.rbracket")); } if (matchToken(PyTokenTypes.COLON)) { parseSingleExpression(false); } sliceItemStart.done(PyElementTypes.SLICE_ITEM); if (!BRACKET_OR_COMMA.contains(myBuilder.getTokenType())) { myBuilder.error("']' or ',' expected"); } } parseSliceListTail(exprStart, null); }
/* * initializer * : attributes "this" valueArguments * : attributes constructorInvocation // type parameters may (must?) be omitted * ; */ private void parseInitializer() { PsiBuilder.Marker initializer = mark(); parseAnnotations(false); IElementType type; if (at(THIS_KEYWORD)) { PsiBuilder.Marker mark = mark(); advance(); // THIS_KEYWORD mark.done(THIS_CONSTRUCTOR_REFERENCE); type = THIS_CALL; } else if (atSet(TYPE_REF_FIRST)) { PsiBuilder.Marker reference = mark(); parseTypeRef(); reference.done(CONSTRUCTOR_CALLEE); type = DELEGATOR_SUPER_CALL; } else { errorWithRecovery( "Expecting constructor call (this(...)) or supertype initializer", TokenSet.create(LBRACE, COMMA)); initializer.drop(); return; } myExpressionParsing.parseValueArgumentList(); initializer.done(type); }
/* *preamble * : namespaceHeader? import* * ; */ private void parsePreamble(boolean imports) { /* * namespaceHeader * : modifiers "namespace" SimpleName{"."} SEMI? * ; */ PsiBuilder.Marker namespaceHeader = mark(); PsiBuilder.Marker firstEntry = mark(); parseModifierList(MODIFIER_LIST, true); if (at(PACKAGE_KEYWORD)) { advance(); // PACKAGE_KEYWORD parseNamespaceName(); if (at(LBRACE)) { // Because it's blocked namespace and it will be parsed as one of top level objects firstEntry.rollbackTo(); namespaceHeader.done(NAMESPACE_HEADER); return; } firstEntry.drop(); consumeIf(SEMICOLON); } else { firstEntry.rollbackTo(); } namespaceHeader.done(NAMESPACE_HEADER); if (imports) parseImportDirectives(); }
/* * callableReference * : (userType "?"*)? "::" SimpleName * ; */ private boolean parseDoubleColonExpression() { PsiBuilder.Marker expression = mark(); if (!at(COLONCOLON)) { PsiBuilder.Marker typeReference = mark(); myJetParsing.parseUserType(); typeReference = myJetParsing.parseNullableTypeSuffix(typeReference); typeReference.done(TYPE_REFERENCE); if (!at(COLONCOLON)) { expression.rollbackTo(); return false; } } advance(); // COLONCOLON if (at(CLASS_KEYWORD)) { advance(); // CLASS_KEYWORD expression.done(CLASS_LITERAL_EXPRESSION); } else { parseSimpleNameExpression(); expression.done(CALLABLE_REFERENCE_EXPRESSION); } return true; }
/* * "this" ("<" type ">")? label? */ private void parseSuperExpression() { assert _at(SUPER_KEYWORD); PsiBuilder.Marker mark = mark(); PsiBuilder.Marker superReference = mark(); advance(); // SUPER_KEYWORD superReference.done(REFERENCE_EXPRESSION); if (at(LT)) { // This may be "super < foo" or "super<foo>", thus the backtracking PsiBuilder.Marker supertype = mark(); myBuilder.disableNewlines(); advance(); // LT myJetParsing.parseTypeRef(); if (at(GT)) { advance(); // GT supertype.drop(); } else { supertype.rollbackTo(); } myBuilder.restoreNewlinesState(); } parseLabelOnTheSameLine(); mark.done(SUPER_EXPRESSION); }
/* * "object" (":" delegationSpecifier{","})? classBody // Cannot make class body optional: foo(object : F, A) */ public void parseObjectLiteral() { PsiBuilder.Marker literal = mark(); PsiBuilder.Marker declaration = mark(); myJetParsing.parseObject( NameParsingMode.PROHIBITED, false); // Body is not optional because of foo(object : A, B) declaration.done(OBJECT_DECLARATION); literal.done(OBJECT_LITERAL); }
public void parseArgumentList() { LOG.assertTrue(myBuilder.getTokenType() == PyTokenTypes.LPAR); final PsiBuilder.Marker arglist = myBuilder.mark(); myBuilder.advanceLexer(); PsiBuilder.Marker genexpr = myBuilder.mark(); int argNumber = 0; while (myBuilder.getTokenType() != PyTokenTypes.RPAR) { argNumber++; if (argNumber > 1) { if (argNumber == 2 && atToken(PyTokenTypes.FOR_KEYWORD) && genexpr != null) { parseComprehension(genexpr, null, PyElementTypes.GENERATOR_EXPRESSION); genexpr = null; continue; } else if (matchToken(PyTokenTypes.COMMA)) { if (atToken(PyTokenTypes.RPAR)) { break; } } else { myBuilder.error(message("PARSE.expected.comma.or.rpar")); break; } } if (myBuilder.getTokenType() == PyTokenTypes.MULT || myBuilder.getTokenType() == PyTokenTypes.EXP) { final PsiBuilder.Marker starArgMarker = myBuilder.mark(); myBuilder.advanceLexer(); if (!parseSingleExpression(false)) { myBuilder.error(message("PARSE.expected.expression")); } starArgMarker.done(PyElementTypes.STAR_ARGUMENT_EXPRESSION); } else { if (myBuilder.getTokenType() == PyTokenTypes.IDENTIFIER) { final PsiBuilder.Marker keywordArgMarker = myBuilder.mark(); myBuilder.advanceLexer(); if (myBuilder.getTokenType() == PyTokenTypes.EQ) { myBuilder.advanceLexer(); if (!parseSingleExpression(false)) { myBuilder.error(message("PARSE.expected.expression")); } keywordArgMarker.done(PyElementTypes.KEYWORD_ARGUMENT_EXPRESSION); continue; } keywordArgMarker.rollbackTo(); } if (!parseSingleExpression(false)) { myBuilder.error(message("PARSE.expected.expression")); break; } } } if (genexpr != null) { genexpr.drop(); } checkMatches(PyTokenTypes.RPAR, message("PARSE.expected.rpar")); arglist.done(PyElementTypes.ARGUMENT_LIST); }
private void parseFunctionLiteral(boolean preferBlock) { assert _at(LBRACE); PsiBuilder.Marker literalExpression = mark(); PsiBuilder.Marker literal = mark(); myBuilder.enableNewlines(); advance(); // LBRACE boolean paramsFound = false; if (at(ARROW)) { // { -> ...} mark().done(VALUE_PARAMETER_LIST); advance(); // ARROW paramsFound = true; } else { if (at(IDENTIFIER) || at(COLON)) { // Try to parse a simple name list followed by an ARROW // {a -> ...} // {a, b -> ...} PsiBuilder.Marker rollbackMarker = mark(); IElementType nextToken = lookahead(1); boolean preferParamsToExpressions = (nextToken == COMMA || nextToken == COLON); parseFunctionLiteralShorthandParameterList(); paramsFound = preferParamsToExpressions ? rollbackOrDrop(rollbackMarker, ARROW, "An -> is expected", RBRACE) : rollbackOrDropAt(rollbackMarker, ARROW); } } if (!paramsFound) { if (preferBlock) { literal.drop(); parseStatements(); expect(RBRACE, "Expecting '}'"); literalExpression.done(BLOCK); myBuilder.restoreNewlinesState(); return; } } PsiBuilder.Marker body = mark(); parseStatements(); body.done(BLOCK); expect(RBRACE, "Expecting '}'"); myBuilder.restoreNewlinesState(); literal.done(FUNCTION_LITERAL); literalExpression.done(LAMBDA_EXPRESSION); }
@Nullable private PsiBuilder.Marker parseUnary(final PsiBuilder builder) { final IElementType tokenType = builder.getTokenType(); if (PREFIX_OPS.contains(tokenType)) { final PsiBuilder.Marker unary = builder.mark(); builder.advanceLexer(); final PsiBuilder.Marker operand = parseUnary(builder); if (operand == null) { error(builder, JavaErrorMessages.message("expected.expression")); } unary.done(JavaElementType.PREFIX_EXPRESSION); return unary; } else if (tokenType == JavaTokenType.LPARENTH) { final PsiBuilder.Marker typeCast = builder.mark(); builder.advanceLexer(); ReferenceParser.TypeInfo typeInfo = myParser .getReferenceParser() .parseTypeInfo( builder, ReferenceParser.EAT_LAST_DOT | ReferenceParser.WILDCARD | ReferenceParser.CONJUNCTIONS | ReferenceParser.INCOMPLETE_ANNO); if (typeInfo == null || !expect(builder, JavaTokenType.RPARENTH)) { typeCast.rollbackTo(); return parsePostfix(builder); } if (PREF_ARITHMETIC_OPS.contains(builder.getTokenType()) && !typeInfo.isPrimitive) { typeCast.rollbackTo(); return parsePostfix(builder); } final PsiBuilder.Marker expr = parseUnary(builder); if (expr == null) { if (!typeInfo .isParameterized) { // cannot parse correct parenthesized expression after correct // parameterized type typeCast.rollbackTo(); return parsePostfix(builder); } else { error(builder, JavaErrorMessages.message("expected.expression")); } } typeCast.done(JavaElementType.TYPE_CAST_EXPRESSION); return typeCast; } else { return parsePostfix(builder); } }
/* * try * : "try" block catchBlock* finallyBlock? * ; * catchBlock * : "catch" "(" annotations SimpleName ":" userType ")" block * ; * * finallyBlock * : "finally" block * ; */ private void parseTry() { assert _at(TRY_KEYWORD); PsiBuilder.Marker tryExpression = mark(); advance(); // TRY_KEYWORD myJetParsing.parseBlock(); boolean catchOrFinally = false; while (at(CATCH_KEYWORD)) { catchOrFinally = true; PsiBuilder.Marker catchBlock = mark(); advance(); // CATCH_KEYWORD TokenSet recoverySet = TokenSet.create(LBRACE, FINALLY_KEYWORD, CATCH_KEYWORD); if (atSet(recoverySet)) { error("Expecting exception variable declaration"); } else { PsiBuilder.Marker parameters = mark(); expect(LPAR, "Expecting '('", recoverySet); if (!atSet(recoverySet)) { myJetParsing.parseValueParameter(/*typeRequired = */ true); expect(RPAR, "Expecting ')'", recoverySet); } else { error("Expecting exception variable declaration"); } parameters.done(VALUE_PARAMETER_LIST); } if (at(LBRACE)) { myJetParsing.parseBlock(); } else { error("Expecting a block: { ... }"); } catchBlock.done(CATCH); } if (at(FINALLY_KEYWORD)) { catchOrFinally = true; PsiBuilder.Marker finallyBlock = mark(); advance(); // FINALLY_KEYWORD myJetParsing.parseBlock(); finallyBlock.done(FINALLY); } if (!catchOrFinally) { error("Expecting 'catch' or 'finally'"); } tryExpression.done(TRY); }
/** * @param root The file currently being parsed * @param builder used to build the parse tree * @return parsed tree */ @NotNull @Override public ASTNode parse(IElementType root, PsiBuilder builder) { PsiBuilder.Marker rootMarker = builder.mark(); PsiBuilder.Marker wrapperMarker = builder.mark(); builder.setDebugMode(true); // Process all tokens parseTree(builder); wrapperMarker.done(SS_STATEMENTS); rootMarker.done(root); return builder.getTreeBuilt(); }
/* * "this" label? */ private void parseThisExpression() { assert _at(THIS_KEYWORD); PsiBuilder.Marker mark = mark(); PsiBuilder.Marker thisReference = mark(); advance(); // THIS_KEYWORD thisReference.done(REFERENCE_EXPRESSION); parseLabelOnTheSameLine(); mark.done(THIS_EXPRESSION); }
private static void close_frame_impl_( ErrorState state, Frame frame, PsiBuilder builder, PsiBuilder.Marker marker, IElementType elementType, boolean result, boolean pinned) { if (elementType != null && marker != null) { if ((frame.modifiers & _COLLAPSE_) != 0) { PsiBuilderImpl.ProductionMarker last = result || pinned ? (PsiBuilderImpl.ProductionMarker) builder.getLatestDoneMarker() : null; if (last != null && last.getStartIndex() == frame.position && state.typeExtends(last.getTokenType(), elementType)) { IElementType resultType = last.getTokenType(); ((PsiBuilder.Marker) last).drop(); marker.done(resultType); return; } } if (result || pinned) { if ((frame.modifiers & _UPPER_) != 0) { marker.drop(); frame.parentFrame.elementType = elementType; } else if ((frame.modifiers & _LEFT_INNER_) != 0 && frame.leftMarker != null) { marker.done(elementType); frame.leftMarker.precede().done(((LighterASTNode) frame.leftMarker).getTokenType()); frame.leftMarker.drop(); } else if ((frame.modifiers & _LEFT_) != 0 && frame.leftMarker != null) { marker.drop(); frame.leftMarker.precede().done(elementType); } else { if (frame.level == 0) builder.eof(); // skip whitespaces marker.done(elementType); } } else { close_marker_impl_(frame, marker, null, false); } } else if (result || pinned) { if (marker != null) marker.drop(); if ((frame.modifiers & _LEFT_INNER_) != 0 && frame.leftMarker != null) { frame.leftMarker.precede().done(((LighterASTNode) frame.leftMarker).getTokenType()); frame.leftMarker.drop(); } } else { close_marker_impl_(frame, marker, null, false); } }
/* * for * : "for" "(" annotations ("val" | "var")? (multipleVariableDeclarations | variableDeclarationEntry) "in" expression ")" expression * ; * * TODO: empty loop body (at the end of the block)? */ private void parseFor() { assert _at(FOR_KEYWORD); PsiBuilder.Marker loop = mark(); advance(); // FOR_KEYWORD if (expect(LPAR, "Expecting '(' to open a loop range", EXPRESSION_FIRST)) { myBuilder.disableNewlines(); if (!at(RPAR)) { PsiBuilder.Marker parameter = mark(); if (!at(IN_KEYWORD)) { myJetParsing.parseModifierList(DEFAULT, TokenSet.create(IN_KEYWORD, RPAR, COLON)); } if (at(VAL_KEYWORD) || at(VAR_KEYWORD)) advance(); // VAL_KEYWORD or VAR_KEYWORD if (at(LPAR)) { myJetParsing.parseMultiDeclarationName(TokenSet.create(IN_KEYWORD, LBRACE)); parameter.done(DESTRUCTURING_DECLARATION); } else { expect(IDENTIFIER, "Expecting a variable name", TokenSet.create(COLON, IN_KEYWORD)); if (at(COLON)) { advance(); // COLON myJetParsing.parseTypeRef(TokenSet.create(IN_KEYWORD)); } parameter.done(VALUE_PARAMETER); } if (expect(IN_KEYWORD, "Expecting 'in'", TokenSet.create(LPAR, LBRACE, RPAR))) { PsiBuilder.Marker range = mark(); parseExpression(); range.done(LOOP_RANGE); } } else { error("Expecting a variable name"); } expectNoAdvance(RPAR, "Expecting ')'"); myBuilder.restoreNewlinesState(); } parseControlStructureBody(); loop.done(FOR); }
public void done( @NotNull final PsiBuilder.Marker marker, @NotNull final IElementType elementType) { myIndents.remove(marker); myNewLines.remove(marker); marker.done(elementType); }
@Nullable private PsiBuilder.Marker parseLambdaExpression( final PsiBuilder builder, final boolean typed, @Nullable final PsiBuilder.Marker typeList) { final PsiBuilder.Marker start = typeList != null ? typeList.precede() : builder.mark(); myParser.getDeclarationParser().parseLambdaParameterList(builder, typed); if (!expect(builder, JavaTokenType.ARROW)) { start.rollbackTo(); return null; } final PsiBuilder.Marker body; if (builder.getTokenType() == JavaTokenType.LBRACE) { body = myParser.getStatementParser().parseCodeBlock(builder); } else { body = parse(builder); } if (body == null) { builder.error(JavaErrorMessages.message("expected.lbrace")); } start.done(JavaElementType.LAMBDA_EXPRESSION); return start; }
/* * whenEntry * // TODO : consider empty after -> * : whenCondition{","} "->" element SEMI * : "else" "->" element SEMI * ; */ private void parseWhenEntry() { PsiBuilder.Marker entry = mark(); if (at(ELSE_KEYWORD)) { advance(); // ELSE_KEYWORD if (!at(ARROW)) { errorUntil("Expecting '->'", TokenSet.create(ARROW, RBRACE, EOL_OR_SEMICOLON)); } if (at(ARROW)) { advance(); // ARROW if (atSet(WHEN_CONDITION_RECOVERY_SET)) { error("Expecting an element"); } else { parseExpressionPreferringBlocks(); } } else if (!atSet(WHEN_CONDITION_RECOVERY_SET)) { errorAndAdvance("Expecting '->'"); } } else { parseWhenEntryNotElse(); } entry.done(WHEN_ENTRY); consumeIf(SEMICOLON); }