forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawSyntax.cpp
139 lines (119 loc) · 3.59 KB
/
RawSyntax.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//===--- RawSyntax.cpp - Swift Raw Syntax Implementation ------------------===//
//
// 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/RawSyntax.h"
#include "swift/Syntax/TokenSyntax.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using llvm::dyn_cast;
using namespace swift::syntax;
namespace {
void dumpSyntaxKind(llvm::raw_ostream &OS, const SyntaxKind Kind) {
switch (Kind) {
#define SYNTAX(Id, Parent) \
case SyntaxKind::Id: \
OS << #Id; \
break;
#define MISSING_SYNTAX(Id, Parent) SYNTAX(Id, Parent)
#define SYNTAX_COLLECTION(Id, Element) SYNTAX(Id, {})
#include "swift/Syntax/SyntaxKinds.def"
case SyntaxKind::Token: OS << "Token"; break;
}
}
}
void RawSyntax::print(llvm::raw_ostream &OS) const {
if (const auto Tok = dyn_cast<TokenSyntax>(this)) {
Tok->print(OS);
}
for (const auto &LE : Layout) {
LE->print(OS);
}
}
void RawSyntax::dump() const {
return RawSyntax::dump(llvm::errs(), /*Indent*/ 0);
}
void RawSyntax::dump(llvm::raw_ostream &OS, unsigned Indent) const {
auto indent = [&](unsigned Amount) {
for (decltype(Amount) i = 0; i < Amount; ++i) {
OS << ' ';
}
};
indent(Indent);
OS << '(';
dumpSyntaxKind(OS, Kind);
if (isMissing())
OS << " [missing] ";
OS << '\n';
for (auto LE = Layout.begin(); LE != Layout.end(); ++LE) {
if (LE != Layout.begin()) {
OS << '\n';
}
switch ((*LE)->Kind) {
case SyntaxKind::Token:
llvm::cast<TokenSyntax>(*LE)->dump(OS, Indent + 1);
break;
default:
(*LE)->dump(OS, Indent + 1);
break;
}
}
OS << ')';
}
bool RawSyntax::accumulateAbsolutePosition(
AbsolutePosition &Pos, const RawSyntax *UpToTargetNode) const {
auto Found = this == UpToTargetNode;
for (auto LE : Layout) {
switch (LE->Kind) {
case SyntaxKind::Token: {
auto Tok = llvm::cast<TokenSyntax>(LE);
for (auto Leader : Tok->LeadingTrivia) {
Leader.accumulateAbsolutePosition(Pos);
}
if (Found) {
return true;
}
Pos.addText(Tok->getText());
for (auto Trailer : Tok->TrailingTrivia) {
Trailer.accumulateAbsolutePosition(Pos);
}
break;
}
default:
if (Found)
return true;
LE->accumulateAbsolutePosition(Pos, UpToTargetNode);
break;
}
}
return false;
}
AbsolutePosition RawSyntax::getAbsolutePosition(RC<RawSyntax> Root) const {
AbsolutePosition Pos;
Root->accumulateAbsolutePosition(Pos, this);
return Pos;
}
void AbsolutePosition::printLineAndColumn(llvm::raw_ostream &OS) const {
OS << getLine() << ':' << getColumn();
}
void AbsolutePosition::dump(llvm::raw_ostream &OS) const {
OS << "(absolute_position ";
OS << "offset=" << getOffset() << " ";
OS << "line=" << getLine() << " ";
OS << "column=" << getColumn();
OS << ')';
}
swift::RC<RawSyntax>
RawSyntax::append(RC<RawSyntax> NewLayoutElement) const {
auto NewLayout = Layout;
NewLayout.push_back(NewLayoutElement);
return RawSyntax::make(Kind, NewLayout, SourcePresence::Present);
}