-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTrivia.cpp.gyb
155 lines (138 loc) · 3.79 KB
/
Trivia.cpp.gyb
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
%{
# -*- mode: C++ -*-
from gyb_syntax_support import *
from gyb_syntax_support.Trivia import TRIVIAS
# Ignore the following admonition; it applies to the resulting .h file only
}%
//// Automatically Generated From Trivia.cpp.gyb.
//// Do Not Edit Directly!
//===--- Trivia.cpp - Swift Syntax Trivia 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/Trivia.h"
using namespace swift;
using namespace swift::syntax;
TriviaPiece TriviaPiece::fromText(TriviaKind kind, StringRef text) {
switch (kind) {
% for trivia in TRIVIAS:
case TriviaKind::${trivia.name}:
% if trivia.is_collection():
assert(text.size() % ${trivia.characters_len()} == 0);
return TriviaPiece(kind, text.size()/${trivia.characters_len()});
% else:
return TriviaPiece(kind, OwnedString::makeRefCounted(text));
% end
% end
}
llvm_unreachable("Unhandled TriviaKind in switch");
}
void TriviaPiece::dump(llvm::raw_ostream &OS, unsigned Indent) const {
for (decltype(Count) i = 0; i < Indent; ++i)
OS << ' ';
OS << "(trivia ";
switch (Kind) {
% for trivia in TRIVIAS:
case TriviaKind::${trivia.name}:
OS << "${trivia.lower_name} ";
% if trivia.is_collection():
OS << Count;
% else:
OS.write_escaped(Text.str());
% end
break;
% end
}
OS << ')';
}
bool syntax::isCommentTriviaKind(TriviaKind Kind) {
switch (Kind) {
% for trivia in TRIVIAS:
case TriviaKind::${trivia.name}:
% if trivia.is_comment:
return true;
% else:
return false;
% end
% end
}
llvm_unreachable("unknown kind");
}
bool TriviaPiece::trySquash(const TriviaPiece &Next) {
if (Kind != Next.Kind) { return false; }
switch (Kind) {
% for trivia in TRIVIAS:
case TriviaKind::${trivia.name}:
% if trivia.is_collection():
Count+= Next.Count;
return true;
% else:
return false;
% end
% end
}
llvm_unreachable("unknown kind");
}
void TriviaPiece::print(llvm::raw_ostream &OS) const {
switch (Kind) {
% for trivia in TRIVIAS:
case TriviaKind::${trivia.name}:
% if trivia.is_collection():
% joined = ''.join(trivia.characters)
for (unsigned i = 0; i < Count; i ++)
OS << "${joined}";
% else:
OS << Text.str();
% end
return;
% end
}
}
#pragma mark - Trivia collection
void Trivia::appendOrSquash(const TriviaPiece &Next) {
if (Pieces.size() > 0) {
TriviaPiece &last = Pieces.back();
if (last.trySquash(Next)) {
return;
}
}
push_back(Next);
}
Trivia Trivia::appending(const Trivia &Other) const {
auto NewPieces = Pieces;
std::copy(Other.begin(), Other.end(), std::back_inserter(NewPieces));
return { NewPieces };
}
void Trivia::dump(llvm::raw_ostream &OS, unsigned Indent) const {
for (const auto &Piece : Pieces) {
Piece.dump(OS, Indent);
}
}
void Trivia::dump() const {
dump(llvm::errs());
}
void Trivia::print(llvm::raw_ostream &OS) const {
for (const auto &Piece : Pieces) {
Piece.print(OS);
}
}
TriviaList::const_iterator Trivia::find(const TriviaKind DesiredKind) const {
return std::find_if(Pieces.begin(), Pieces.end(),
[=](const TriviaPiece &Piece) -> bool {
return Piece.getKind() == DesiredKind;
});
}
Trivia Trivia::operator+(const Trivia &Other) const {
auto NewPieces = Pieces;
std::copy(Other.Pieces.begin(), Other.Pieces.end(),
std::back_inserter(NewPieces));
return { NewPieces };
}