Files
swift-mirror/lib/Syntax/StmtSyntax.cpp
David Farler 7ee42994c8 Start the Syntax library and optional full token lexing
Add an option to the lexer to go back and get a list of "full"
tokens, which include their leading and trailing trivia, which
we can index into from SourceLocs in the current AST.

This starts the Syntax sublibrary, which will support structured
editing APIs. Some skeleton support and basic implementations are
in place for types and generics in the grammar. Yes, it's slightly
redundant with what we have right now. lib/AST conflates syntax
and semantics in the same place(s); this is a first step in changing
that to separate the two concepts for clarity and also to get closer
to incremental parsing and type-checking. The goal is to eventually
extract all of the syntactic information from lib/AST and change that
to be more of a semantic/symbolic model.

Stub out a Semantics manager. This ought to eventually be used as a hub
for encapsulating lazily computed semantic information for syntax nodes.
For the time being, it can serve as a temporary place for mapping from
Syntax nodes to semantically full lib/AST nodes.

This is still in a molten state - don't get too close, wear appropriate
proximity suits, etc.
2017-02-17 12:57:04 -08:00

154 lines
5.4 KiB
C++

//===--- StmtSyntax.cpp - Swift Statement Syntax Implementation -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Syntax/TokenSyntax.h"
#include "swift/Syntax/StmtSyntax.h"
using namespace swift;
using namespace swift::syntax;
StmtSyntax::StmtSyntax(RC<SyntaxData> Root, StmtSyntaxData *Data)
: Syntax(Root, Data) {}
#pragma mark fallthrough-statement Data
FallthroughStmtSyntaxData::FallthroughStmtSyntaxData(RC<RawSyntax> Raw,
const SyntaxData *Parent,
CursorIndex IndexInParent)
: StmtSyntaxData(Raw, Parent, IndexInParent) {}
RC<FallthroughStmtSyntaxData>
FallthroughStmtSyntaxData::make(RC<RawSyntax> Raw,
const SyntaxData *Parent,
CursorIndex IndexInParent) {
return RC<FallthroughStmtSyntaxData> {
new FallthroughStmtSyntaxData { Raw, Parent, IndexInParent }
};
}
RC<FallthroughStmtSyntaxData> FallthroughStmtSyntaxData::makeBlank() {
return make(RawSyntax::make(SyntaxKind::FallthroughStmt,
{
TokenSyntax::missingToken(tok::kw_fallthrough, "fallthrough"),
},
SourcePresence::Present));
}
#pragma mark fallthrough-statement API
FallthroughStmtSyntax::FallthroughStmtSyntax(const RC<SyntaxData> Root,
FallthroughStmtSyntaxData *Data)
: StmtSyntax(Root, Data) {}
FallthroughStmtSyntax
FallthroughStmtSyntax::make(RC<RawSyntax> Raw, const SyntaxData *Parent,
CursorIndex IndexInParent) {
assert(Raw->Layout.size() == 1);
syntax_assert_child_token_text(Raw,
Cursor::FallthroughKeyword,
tok::kw_fallthrough, "fallthrough");
auto Data = FallthroughStmtSyntaxData::make(Raw, Parent, IndexInParent);
return FallthroughStmtSyntax {
Data, Data.get(),
};
}
CodeBlockStmtSyntaxData::CodeBlockStmtSyntaxData(RC<RawSyntax> Raw)
: StmtSyntaxData(Raw) {
assert(Raw->Kind == SyntaxKind::CodeBlockStmt);
syntax_assert_child_token_text(Raw, CodeBlockStmtSyntax::Cursor::LeftBrace,
tok::l_brace, "{");
syntax_assert_child_kind(Raw, CodeBlockStmtSyntax::Cursor::Elements,
SyntaxKind::StmtList);
syntax_assert_child_token_text(Raw, CodeBlockStmtSyntax::Cursor::RightBrace,
tok::r_brace, "}");
}
RC<CodeBlockStmtSyntaxData>
CodeBlockStmtSyntaxData::make(RC<RawSyntax> Raw) {
return RC<CodeBlockStmtSyntaxData> {
new CodeBlockStmtSyntaxData { Raw }
};
}
RC<CodeBlockStmtSyntaxData>
CodeBlockStmtSyntaxData::makeBlank() {
return make(RawSyntax::make(SyntaxKind::CodeBlockStmt,
{
TokenSyntax::missingToken(tok::l_brace, "{"),
RawSyntax::missing(SyntaxKind::StmtList),
TokenSyntax::missingToken(tok::r_brace, "}"),
},
SourcePresence::Present));
}
#pragma mark code-block API
CodeBlockStmtSyntax::CodeBlockStmtSyntax(const RC<SyntaxData> Root,
CodeBlockStmtSyntaxData *Data)
: StmtSyntax(Root, Data) {}
#pragma mark statements Data
StmtListSyntaxData::StmtListSyntaxData(RC<RawSyntax> Raw,
const SyntaxData *Parent,
CursorIndex IndexInParent)
: StmtSyntaxData(Raw, Parent, IndexInParent) {
assert(Raw->Kind == SyntaxKind::StmtList);
}
RC<StmtListSyntaxData> StmtListSyntaxData::make(RC<RawSyntax> Raw,
const SyntaxData *Parent,
CursorIndex IndexInParent) {
return RC<StmtListSyntaxData> {
new StmtListSyntaxData { Raw, Parent, IndexInParent }
};
}
RC<StmtListSyntaxData> StmtListSyntaxData::makeBlank() {
return make(RawSyntax::make(SyntaxKind::StmtList,
{},
SourcePresence::Present));
}
#pragma mark statements API
StmtListSyntax::StmtListSyntax(const RC<SyntaxData> Root,
const StmtListSyntaxData *Data)
: Syntax(Root, Data) {}
StmtListSyntax
StmtListSyntax::withAddedStatement(Syntax AdditionalStatement) const {
auto Layout = getRaw()->Layout;
Layout.push_back(AdditionalStatement.getRaw());
auto NewRaw = RawSyntax::make(SyntaxKind::StmtList, Layout,
getRaw()->Presence);
return Data->replaceSelf<StmtListSyntax>(NewRaw);
}
#pragma mark statements Builder
StmtListSyntaxBuilder &
StmtListSyntaxBuilder::addStatement(Syntax Statement) {
StmtListLayout.push_back(Statement.getRaw());
return *this;
}
StmtListSyntax StmtListSyntaxBuilder::build() const {
auto Raw = RawSyntax::make(SyntaxKind::StmtList, StmtListLayout,
SourcePresence::Present);
auto Data = StmtListSyntaxData::make(Raw);
return StmtListSyntax { Data, Data.get() };
}