Skip to content

libSyntax: parse codeblock syntax node. #12771

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

Merged
merged 4 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions include/swift/Syntax/SyntaxParsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#ifndef SWIFT_SYNTAX_PARSING_CONTEXT_H
#define SWIFT_SYNTAX_PARSING_CONTEXT_H

#include "swift/Syntax/Syntax.h"
#include "swift/Basic/SourceLoc.h"
#include "swift/Syntax/Syntax.h"

namespace swift {
class SourceLoc;
Expand All @@ -25,19 +26,28 @@ namespace syntax {
struct RawSyntax;
enum class SyntaxKind;

enum class SyntaxContextKind: uint8_t{
Expr,
Decl,
Stmt,
};

/// The handler for parser to generate libSyntax entities.
struct RawSyntaxInfo {
/// Start location of this syntax node.
SourceLoc StartLoc;

/// The number of tokens belong to the syntax node.
unsigned TokCount;
/// Start and end location of this syntax node.
SourceRange SyntaxRange;

/// The raw node.
RC<RawSyntax> RawNode;
RawSyntaxInfo(RC<RawSyntax> RawNode): RawNode(RawNode) {}
RawSyntaxInfo(SourceLoc StartLoc, RC<RawSyntax> RawNode):
RawSyntaxInfo(StartLoc, 1, RawNode) {}
RawSyntaxInfo(SourceLoc StartLoc, unsigned TokCount, RC<RawSyntax> RawNode);
SyntaxRange(StartLoc), RawNode(RawNode) {}
RawSyntaxInfo(SourceRange SyntaxRange, RC<RawSyntax> RawNode):
SyntaxRange(SyntaxRange), RawNode(RawNode) {}

bool isImplicit() const { return SyntaxRange.isInvalid(); }
SourceLoc getStartLoc() const { return SyntaxRange.Start; }
SourceLoc getEndLoc() const { return SyntaxRange.End; }

template <typename SyntaxNode>
SyntaxNode makeSyntax() const { return make<SyntaxNode>(RawNode); }
Expand All @@ -46,6 +56,7 @@ struct RawSyntaxInfo {
RC<RawSyntaxNode> getRaw() const {
return RC<RawSyntaxNode>(cast<RawSyntaxNode>(RawNode));
}
void brigeWithContext(SyntaxContextKind Kind);
};

enum class SyntaxParsingContextKind: uint8_t {
Expand Down Expand Up @@ -96,22 +107,28 @@ class SyntaxParsingContextRoot: public SyntaxParsingContext {
};
};

enum class SyntaxContextKind: uint8_t{
Expr,
Decl,
};

// The base class for contexts that are created from a parent context.
// The stack instance will set the context holder when the context
// is firstly created and reset the context holder to the parent when
// it's destructed.
class SyntaxParsingContextChild: public SyntaxParsingContext {
SyntaxParsingContext *Parent;
SyntaxParsingContext *&ContextHolder;
const SyntaxContextKind Kind;
Optional<SyntaxContextKind> Kind;
Optional<SyntaxKind> KnownSyntax;
void makeNodeWhole(SyntaxKind Kind);
SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
Optional<SyntaxContextKind> Kind,
Optional<SyntaxKind> KnownSyntax);
public:
SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
SyntaxContextKind Kind);
SyntaxContextKind Kind): SyntaxParsingContextChild(ContextHolder,
Kind, None) {}

SyntaxParsingContextChild(SyntaxParsingContext *&ContextHolder,
SyntaxKind KnownSyntax): SyntaxParsingContextChild(ContextHolder,
None, KnownSyntax) {};

~SyntaxParsingContextChild();
void makeNode(SyntaxKind Kind) override;
void addTokenSyntax(SourceLoc Loc) override;
Expand Down
16 changes: 12 additions & 4 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
#include "swift/Parse/Lexer.h"
#include "swift/Parse/CodeCompletionCallbacks.h"
#include "swift/Subsystems.h"
#include "swift/Syntax/TokenSyntax.h"
#include "swift/Syntax/SyntaxParsingContext.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SaveAndRestore.h"

using namespace swift;
using namespace swift::syntax;

/// isStartOfStmt - Return true if the current token starts a statement.
///
Expand Down Expand Up @@ -223,7 +226,7 @@ void Parser::consumeTopLevelDecl(ParserPosition BeginParserPosition,
ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
BraceItemListKind Kind,
BraceItemListKind ConditionalBlockKind) {

SyntaxParsingContextChild StmtListContext(SyntaxContext, SyntaxKind::StmtList);
bool IsTopLevel = (Kind == BraceItemListKind::TopLevelCode) ||
(Kind == BraceItemListKind::TopLevelLibrary);
bool isActiveConditionalBlock =
Expand Down Expand Up @@ -259,6 +262,7 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
Tok.isNot(tok::kw_sil_default_witness_table) &&
(isConditionalBlock ||
!isTerminatorForBraceItemListKind(Kind, Entries))) {
SyntaxParsingContextChild StmtContext(SyntaxContext, SyntaxContextKind::Stmt);
if (Kind == BraceItemListKind::TopLevelLibrary &&
skipExtraTopLevelRBraces())
continue;
Expand Down Expand Up @@ -489,7 +493,7 @@ static ParserResult<Stmt> recoverFromInvalidCase(Parser &P) {
}

ParserResult<Stmt> Parser::parseStmt() {

SyntaxParsingContextChild LocalContext(SyntaxContext, SyntaxContextKind::Stmt);
// Note that we're parsing a statement.
StructureMarkerRAII ParsingStmt(*this, Tok.getLoc(),
StructureMarkerKind::Statement);
Expand Down Expand Up @@ -586,15 +590,19 @@ ParserResult<BraceStmt> Parser::parseBraceItemList(Diag<> ID) {
if (Tok.isNot(tok::l_brace))
return nullptr;
}
SyntaxParsingContextChild LocalContext(SyntaxContext, SyntaxKind::CodeBlock);
SourceLoc LBLoc = consumeToken(tok::l_brace);
LocalContext.addTokenSyntax(LBLoc);

SmallVector<ASTNode, 16> Entries;
SourceLoc RBLoc;

ParserStatus Status = parseBraceItems(Entries, BraceItemListKind::Brace,
BraceItemListKind::Brace);
parseMatchingToken(tok::r_brace, RBLoc,
diag::expected_rbrace_in_brace_stmt, LBLoc);
if (!parseMatchingToken(tok::r_brace, RBLoc,
diag::expected_rbrace_in_brace_stmt, LBLoc)) {
LocalContext.addTokenSyntax(RBLoc);
}

return makeParserResult(Status,
BraceStmt::create(Context, LBLoc, Entries, RBLoc));
Expand Down
5 changes: 4 additions & 1 deletion lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ParseDelayedFunctionBodies : public ASTWalker {
SourceManager &SourceMgr = SF.getASTContext().SourceMgr;
unsigned BufferID = SourceMgr.findBufferContainingLoc(AFD->getLoc());
Parser TheParser(BufferID, SF, nullptr, &ParserState);

TheParser.SyntaxContext->disable();
std::unique_ptr<CodeCompletionCallbacks> CodeCompletion;
if (CodeCompletionFactory) {
CodeCompletion.reset(
Expand Down Expand Up @@ -102,6 +102,9 @@ static void parseDelayedDecl(
SourceMgr.findBufferContainingLoc(ParserState.getDelayedDeclLoc());
Parser TheParser(BufferID, SF, nullptr, &ParserState);

// Disable libSyntax creation in the delayed parsing.
TheParser.SyntaxContext->disable();

std::unique_ptr<CodeCompletionCallbacks> CodeCompletion;
if (CodeCompletionFactory) {
CodeCompletion.reset(
Expand Down
1 change: 1 addition & 0 deletions lib/Syntax/RawSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static bool isTrivialSyntaxKind(SyntaxKind Kind) {
case SyntaxKind::SourceFile:
case SyntaxKind::TopLevelCodeDecl:
case SyntaxKind::ExpressionStmt:
case SyntaxKind::DeclarationStmt:
return true;
default:
return false;
Expand Down
Loading