Skip to content

[Syntax] Add Trivia C++ unit tests #7722

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 1 commit into from
Feb 23, 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
59 changes: 46 additions & 13 deletions include/swift/Syntax/Trivia.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ enum class TriviaKind {

/// A backtick '`' character, used to escape identifiers.
Backtick,

/// A semicolon ';' character, used for delimiting statements.
Semicolon
};

/// A contiguous stretch of a single kind of trivia. The constiuent part of
Expand Down Expand Up @@ -187,11 +184,6 @@ struct TriviaPiece {
return TriviaPiece {TriviaKind::Backtick, 1, OwnedString{}};
}

/// Return a piece of trivia for some number of semicolons in a row.
static TriviaPiece semicolon(unsigned Count) {
return TriviaPiece {TriviaKind::Semicolon, Count, OwnedString{}};
}

void accumulateAbsolutePosition(AbsolutePosition &Pos) const;

/// Print a debug representation of this trivia piece to the provided output
Expand All @@ -200,6 +192,16 @@ struct TriviaPiece {

/// Print this piece of trivia to the provided output stream.
void print(llvm::raw_ostream &OS) const;

bool operator==(const TriviaPiece &Other) const {
return Kind == Other.Kind &&
Count == Other.Count &&
Text.str().compare(Other.Text.str()) == 0;
}

bool operator!=(const TriviaPiece &Other) const {
return !(*this == Other);
}
};

using TriviaList = std::deque<TriviaPiece>;
Expand Down Expand Up @@ -233,20 +235,23 @@ struct Trivia {
///
/// Precondition: !empty()
const TriviaPiece &front() const {
assert(!empty());
return Pieces.front();
}

/// Return a reference to the last piece.
///
/// Precondition: !empty()
const TriviaPiece &back() const {
assert(!empty());
return Pieces.back();
}

/// Remove the last piece from the Trivia collection.
///
/// Precondition: !empty()
void pop_back() {
assert(!empty());
Pieces.pop_back();
}

Expand Down Expand Up @@ -283,41 +288,74 @@ struct Trivia {
return find(Kind) != end();
}

bool operator==(const Trivia &Other) const {
if (Pieces.size() != Other.size()) {
return false;
}

for (size_t i = 0; i < Pieces.size(); ++i) {
if (Pieces[i] != Other.Pieces[i]) {
return false;
}
}

return true;
}

bool operator!=(const Trivia &Other) const {
return !(*this == Other);
}

/// Return a collection of trivia of some number of space characters in a row.
static Trivia spaces(unsigned Count) {
if (Count == 0) {
return {};
}
return {{ TriviaPiece {TriviaKind::Space, Count, OwnedString{}} }};
}

/// Return a collection of trivia of some number of tab characters in a row.
static Trivia tabs(unsigned Count) {
if (Count == 0) {
return {};
}
return {{ TriviaPiece {TriviaKind::Tab, Count, OwnedString{}} }};
}

/// Return a collection of trivia of some number of newline characters
// in a row.
static Trivia newlines(unsigned Count) {
if (Count == 0) {
return {};
}
return {{ TriviaPiece {TriviaKind::Newline, Count, OwnedString{}} }};
}

/// Return a collection of trivia with a single line of ('//')
// developer comment.
static Trivia lineComment(const OwnedString Text) {
assert(Text.str().startswith("//"));
return {{ TriviaPiece {TriviaKind::LineComment, 1, Text} }};
}

/// Return a collection of trivia with a block comment ('/* ... */')
static Trivia blockComment(const OwnedString Text) {
assert(Text.str().startswith("/*"));
assert(Text.str().endswith("*/"));
return {{ TriviaPiece {TriviaKind::BlockComment, 1, Text} }};
}

/// Return a collection of trivia with a single line of ('///') doc comment.
static Trivia docLineComment(const OwnedString Text) {
assert(Text.str().startswith("///"));
return {{ TriviaPiece {TriviaKind::DocLineComment, 1, Text} }};
}

/// Return a collection of trivia with a documentation block
// comment ('/** ... */')
static Trivia docBlockComment(const OwnedString Text) {
assert(Text.str().startswith("/**"));
assert(Text.str().endswith("*/"));
return {{ TriviaPiece {TriviaKind::DocBlockComment, 1, Text} }};
}

Expand All @@ -326,11 +364,6 @@ struct Trivia {
static Trivia backtick() {
return {{ TriviaPiece {TriviaKind::Backtick, 1, OwnedString{}} }};
}

/// Return a piece of trivia for some number of semicolons in a row.
static Trivia semicolon() {
return {{ TriviaPiece {TriviaKind::Semicolon, 1, OwnedString{}} }};
}
};
}
}
Expand Down
6 changes: 0 additions & 6 deletions lib/Syntax/Trivia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ void TriviaPiece::dump(llvm::raw_ostream &OS, unsigned Indent) const {
case TriviaKind::Backtick:
OS << "backtick " << Count;
break;
case TriviaKind::Semicolon:
OS << "semicolon " << Count;
break;
}
OS << ')';
}
Expand All @@ -100,7 +97,6 @@ void TriviaPiece::accumulateAbsolutePosition(AbsolutePosition &Pos) const {
break;
case TriviaKind::Space:
case TriviaKind::Backtick:
case TriviaKind::Semicolon:
case TriviaKind::Tab:
case TriviaKind::VerticalTab:
case TriviaKind::Formfeed:
Expand Down Expand Up @@ -135,8 +131,6 @@ void TriviaPiece::print(llvm::raw_ostream &OS) const {
case TriviaKind::Backtick:
printRepeated(OS, '`', Count);
break;
case TriviaKind::Semicolon:
printRepeated(OS, ';', Count);
}
}

Expand Down
1 change: 1 addition & 0 deletions unittests/Syntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_swift_unittest(SwiftSyntaxTests
RawSyntaxTests.cpp
StmtSyntaxTests.cpp
ThreadSafeCachingTests.cpp
TriviaTests.cpp
TypeSyntaxTests.cpp)

target_link_libraries(SwiftSyntaxTests
Expand Down
Loading