-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SyntaxParse] Parse PoundAssertStmt #27460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,18 @@ | |
using namespace swift; | ||
using namespace swift::syntax; | ||
|
||
ParsedSyntaxResult<ParsedExprSyntax> Parser::parseExpressionSyntax(Diag<> ID) { | ||
SourceLoc ExprLoc = Tok.getLoc(); | ||
SyntaxParsingContext ExprParsingContext(SyntaxContext, SyntaxContextKind::Expr); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: 80 columns |
||
ExprParsingContext.setTransparent(); | ||
ParserResult<Expr> Result = parseExpr(ID); | ||
if (auto ParsedExpr = ExprParsingContext.popIf<ParsedExprSyntax>()) { | ||
Generator.addExpr(Result.getPtrOrNull(), ExprLoc); | ||
return makeParsedResult(std::move(*ParsedExpr), Result.getStatus()); | ||
} | ||
return Result.getStatus(); | ||
} | ||
|
||
/// parseExpr | ||
/// | ||
/// expr: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ | |
#include "swift/Parse/Lexer.h" | ||
#include "swift/Parse/Parser.h" | ||
#include "swift/Parse/SyntaxParsingContext.h" | ||
#include "swift/Parse/ParsedSyntaxBuilders.h" | ||
#include "swift/Parse/ParsedSyntaxRecorder.h" | ||
#include "swift/Subsystems.h" | ||
#include "swift/Syntax/TokenSyntax.h" | ||
#include "llvm/ADT/PointerUnion.h" | ||
|
@@ -2529,49 +2531,76 @@ ParserResult<CaseStmt> Parser::parseStmtCase(bool IsActive) { | |
/// stmt-pound-assert: | ||
/// '#assert' '(' expr (',' string_literal)? ')' | ||
ParserResult<Stmt> Parser::parseStmtPoundAssert() { | ||
SyntaxContext->setCreateSyntax(SyntaxKind::PoundAssertStmt); | ||
auto leadingLoc = leadingTriviaLoc(); | ||
auto parsed = parseStmtPoundAssertSyntax(); | ||
SyntaxContext->addSyntax(parsed.get()); | ||
auto poundAssertSyntax = SyntaxContext->topNode<PoundAssertStmtSyntax>(); | ||
auto poundAssert = Generator.generate(poundAssertSyntax, leadingLoc); | ||
return makeParserResult(parsed.getStatus(), poundAssert); | ||
} | ||
|
||
SourceLoc startLoc = consumeToken(tok::pound_assert); | ||
SourceLoc endLoc; | ||
ParsedSyntaxResult<ParsedPoundAssertStmtSyntax> | ||
Parser::parseStmtPoundAssertSyntax() { | ||
ParsedPoundAssertStmtSyntaxBuilder builder(*SyntaxContext); | ||
ParserStatus status; | ||
|
||
if (Tok.isNot(tok::l_paren)) { | ||
diagnose(Tok, diag::pound_assert_expected_lparen); | ||
return makeParserError(); | ||
SourceLoc startLoc = Tok.getLoc(); | ||
builder.usePoundAssert(consumeTokenSyntax(tok::pound_assert)); | ||
if (!Tok.isFollowingLParen()) { | ||
diagnose(Tok.getLoc(), diag::pound_assert_expected_lparen); | ||
status.setIsParseError(); | ||
builder.useCondition(ParsedSyntaxRecorder::makeUnknownExpr({}, *SyntaxContext)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 80 columns. |
||
return makeParsedResult(builder.build(), status); | ||
} | ||
SourceLoc LBLoc = consumeToken(tok::l_paren); | ||
SourceLoc lParenLoc = Tok.getLoc(); | ||
builder.useLeftParen(consumeTokenSyntax(tok::l_paren)); | ||
|
||
auto conditionExprResult = parseExpr(diag::pound_assert_expected_expression); | ||
if (conditionExprResult.isParseError()) | ||
return ParserStatus(conditionExprResult); | ||
auto conditionExprResult = parseExpressionSyntax(diag::pound_assert_expected_expression); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 80 columns |
||
status |= conditionExprResult.getStatus(); | ||
builder.useCondition(conditionExprResult.isNull() | ||
? ParsedSyntaxRecorder::makeUnknownExpr({}, *SyntaxContext) | ||
: conditionExprResult.get()); | ||
|
||
if (auto Comma = consumeTokenSyntaxIf(tok::comma)) { | ||
builder.useComma(std::move(*Comma)); | ||
|
||
StringRef message; | ||
if (consumeIf(tok::comma)) { | ||
if (Tok.isNot(tok::string_literal)) { | ||
diagnose(Tok.getLoc(), diag::pound_assert_expected_string_literal); | ||
return makeParserError(); | ||
status.setIsParseError(); | ||
return makeParsedResult(builder.build(), status); | ||
} | ||
|
||
auto messageOpt = getStringLiteralIfNotInterpolated(Tok.getLoc(), | ||
"'#assert' message"); | ||
consumeToken(); | ||
if (!messageOpt) | ||
return makeParserError(); | ||
|
||
message = *messageOpt; | ||
auto MessageLoc = Tok.getLoc(); | ||
// FIXME: Support extended escaping string literal. | ||
if (Tok.getCustomDelimiterLen()) { | ||
diagnose(MessageLoc, diag::forbidden_extended_escaping_string, "'#assert' message"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
status.setIsParseError(); | ||
} else { | ||
SmallVector<Lexer::StringSegment, 1> Segments; | ||
L->getStringLiteralSegments(Tok, Segments); | ||
if (Segments.size() != 1 || | ||
Segments.front().Kind == Lexer::StringSegment::Expr) { | ||
diagnose(MessageLoc, diag::forbidden_interpolated_string, "'#assert' message"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
status.setIsParseError(); | ||
} | ||
} | ||
builder.useMessage(consumeTokenSyntax(tok::string_literal)); | ||
} | ||
|
||
if (parseMatchingToken(tok::r_paren, endLoc, | ||
diag::pound_assert_expected_rparen, LBLoc)) { | ||
return makeParserError(); | ||
SourceLoc EndLoc; | ||
if (auto rParen = parseMatchingTokenSyntax(tok::r_paren, EndLoc, | ||
diag::pound_assert_expected_rparen, | ||
lParenLoc)) { | ||
builder.useRightParen(std::move(*rParen)); | ||
} else { | ||
status.setIsParseError(); | ||
} | ||
|
||
// We check this after consuming everything, so that the SyntaxContext | ||
// understands this statement even when the feature is disabled. | ||
if (!Context.LangOpts.EnableExperimentalStaticAssert) { | ||
diagnose(startLoc, diag::pound_assert_disabled); | ||
return makeParserError(); | ||
status.setIsParseError(); | ||
} | ||
|
||
return makeParserResult<Stmt>(new (Context) PoundAssertStmt( | ||
SourceRange(startLoc, endLoc), conditionExprResult.get(), message)); | ||
return makeParsedResult(builder.build(), status); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,12 @@ llvm::Optional<Syntax> Syntax::getChild(const size_t N) const { | |
return Syntax {Root, ChildData.get()}; | ||
} | ||
|
||
Optional<Syntax> Syntax::getPreviousNode() const { | ||
if (auto prev = getData().getPreviousNode()) | ||
return TokenSyntax(Root, prev.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should be |
||
return None; | ||
} | ||
|
||
Optional<TokenSyntax> Syntax::getFirstToken() const { | ||
if (auto tok = getData().getFirstToken()) | ||
return TokenSyntax(Root, tok.get()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we have
generate(ExprSyntax)
, this block should be like