-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[libSyntax] Syntax colouring based on the syntax tree #17621
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
ahoppen
merged 8 commits into
swiftlang:master
from
ahoppen:002-sytnax-tree-based-coloring
Jul 17, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dfad6f7
[Basic] Extract isEditorPlaceholder from Identifier to standalone fun…
ahoppen 9d59cd2
[incrParse] Add a stable id to the syntax nodes
ahoppen 4516873
[incrParse] Test utility: Compare syntax trees without IDs
ahoppen 03a7042
[libSyntax] Disable caching of token nodes
ahoppen 8430eff
[libSyntax] Add syntax coloring based on the syntax tree
ahoppen 5c22761
[libSyntax] Enable tests for libSyntax based syntax coloring
ahoppen c8a3957
[SourceKit] Add option to force the SyntaxMap to be generated via the…
ahoppen 6bc1b5a
[libSyntax] Add test variants for building the syntax map via libSyntax
ahoppen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
%{ | ||
# -*- mode: C++ -*- | ||
from gyb_syntax_support import * | ||
NODE_MAP = create_node_map() | ||
# Ignore the following admonition; it applies to the resulting .h file only | ||
}% | ||
//// Automatically Generated From SyntaxClassifier.h.gyb. | ||
//// Do Not Edit Directly! | ||
//===----------- SyntaxClassifier.h - SyntaxClassifier definitions --------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2018 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_SYNTAX_CLASSIFIER_H | ||
#define SWIFT_SYNTAX_CLASSIFIER_H | ||
|
||
#include "swift/Syntax/SyntaxVisitor.h" | ||
#include <stack> | ||
|
||
namespace swift { | ||
namespace syntax { | ||
|
||
|
||
/// A classification that determines which color a token should be colored in | ||
/// for syntax coloring. | ||
enum class SyntaxClassification { | ||
None, | ||
Keyword, | ||
Identifier, | ||
DollarIdentifier, | ||
IntegerLiteral, | ||
FloatingLiteral, | ||
StringLiteral, | ||
/// Marks the parens for a string interpolation. | ||
StringInterpolationAnchor, | ||
TypeIdentifier, | ||
/// #if/#else/#endif occurrence. | ||
BuildConfigKeyword, | ||
/// An identifier in a #if condition. | ||
BuildConfigId, | ||
/// #-keywords like #warning, #sourceLocation | ||
PoundDirectiveKeyword, | ||
/// Any occurrence of '@<attribute-name>' anywhere. | ||
Attribute, | ||
/// An editor placeholder string <#like this#>. | ||
EditorPlaceholder, | ||
ObjectLiteral | ||
}; | ||
|
||
|
||
class SyntaxClassifier: public SyntaxVisitor { | ||
struct ContextStackEntry { | ||
/// The classification all identifiers shall inherit | ||
SyntaxClassification Classification; | ||
/// If set to \c true, all tokens will be forced to receive the above | ||
/// classification, overriding their context-free classification | ||
bool ForceClassification; | ||
|
||
ContextStackEntry(SyntaxClassification Classification, | ||
bool ForceClassification) | ||
: Classification(Classification), | ||
ForceClassification(ForceClassification) {} | ||
}; | ||
|
||
std::map<unsigned, SyntaxClassification> ClassifiedTokens; | ||
/// The top classification of this stack determines the color of identifiers | ||
std::stack<ContextStackEntry, llvm::SmallVector<ContextStackEntry, 16>> ContextStack; | ||
|
||
template<typename T> | ||
void visit(T Node, SyntaxClassification Classification, | ||
bool ForceClassification) { | ||
ContextStack.emplace(Classification, ForceClassification); | ||
visit(Node); | ||
ContextStack.pop(); | ||
} | ||
|
||
template<typename T> | ||
void visit(llvm::Optional<T> OptNode) { | ||
if (OptNode.hasValue()) { | ||
static_cast<SyntaxVisitor *>(this)->visit(OptNode.getValue()); | ||
} | ||
} | ||
|
||
virtual void visit(TokenSyntax TokenNode) override; | ||
|
||
virtual void visit(Syntax Node) override { | ||
SyntaxVisitor::visit(Node); | ||
} | ||
|
||
% for node in SYNTAX_NODES: | ||
% if is_visitable(node): | ||
virtual void visit(${node.name} Node) override; | ||
% end | ||
% end | ||
|
||
public: | ||
std::map<unsigned, SyntaxClassification> classify(Syntax Node) { | ||
// Clean up the environment | ||
ContextStack = std::stack<ContextStackEntry, llvm::SmallVector<ContextStackEntry, 16>>(); | ||
ContextStack.push({SyntaxClassification::None, false}); | ||
ClassifiedTokens.clear(); | ||
|
||
Node.accept(*this); | ||
|
||
return ClassifiedTokens; | ||
} | ||
}; | ||
} // namespace syntax | ||
} // namespace swift | ||
|
||
#endif // SWIFT_SYNTAX_CLASSIFIER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,10 +67,19 @@ static void dumpTokenKind(llvm::raw_ostream &OS, tok Kind) { | |
|
||
} // end of anonymous namespace | ||
|
||
unsigned RawSyntax::NextFreeNodeId = 1; | ||
|
||
RawSyntax::RawSyntax(SyntaxKind Kind, ArrayRef<RC<RawSyntax>> Layout, | ||
SourcePresence Presence, bool ManualMemory) { | ||
SourcePresence Presence, bool ManualMemory, | ||
llvm::Optional<unsigned> NodeId) { | ||
assert(Kind != SyntaxKind::Token && | ||
"'token' syntax node must be constructed with dedicated constructor"); | ||
if (NodeId.hasValue()) { | ||
this->NodeId = NodeId.getValue(); | ||
NextFreeNodeId = std::max(this->NodeId + 1, NextFreeNodeId); | ||
} else { | ||
this->NodeId = NextFreeNodeId++; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the above comment (Receive static unsigned claimNodeId(unsigned NodeId) {
if (!NodeId)
return NextFreeNodeId++;
NextFreeNodeId = std::max(NodeId + 1, NextFreeNodeId);
return NodeId;
} then call it from here and token constructor. this->NodeId = claimNodeId(NodeId); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above. |
||
Bits.Common.Kind = unsigned(Kind); | ||
Bits.Common.Presence = unsigned(Presence); | ||
Bits.Common.ManualMemory = unsigned(ManualMemory); | ||
|
@@ -92,7 +101,14 @@ RawSyntax::RawSyntax(SyntaxKind Kind, ArrayRef<RC<RawSyntax>> Layout, | |
RawSyntax::RawSyntax(tok TokKind, OwnedString Text, | ||
ArrayRef<TriviaPiece> LeadingTrivia, | ||
ArrayRef<TriviaPiece> TrailingTrivia, | ||
SourcePresence Presence, bool ManualMemory) { | ||
SourcePresence Presence, bool ManualMemory, | ||
llvm::Optional<unsigned> NodeId) { | ||
if (NodeId.hasValue()) { | ||
this->NodeId = NodeId.getValue(); | ||
NextFreeNodeId = std::max(this->NodeId + 1, NextFreeNodeId); | ||
} else { | ||
this->NodeId = NextFreeNodeId++; | ||
} | ||
Bits.Common.Kind = unsigned(SyntaxKind::Token); | ||
Bits.Common.Presence = unsigned(Presence); | ||
Bits.Common.ManualMemory = unsigned(ManualMemory); | ||
|
@@ -126,25 +142,28 @@ RawSyntax::~RawSyntax() { | |
} | ||
|
||
RC<RawSyntax> RawSyntax::make(SyntaxKind Kind, ArrayRef<RC<RawSyntax>> Layout, | ||
SourcePresence Presence, SyntaxArena *Arena) { | ||
SourcePresence Presence, SyntaxArena *Arena, | ||
llvm::Optional<unsigned> NodeId) { | ||
auto size = totalSizeToAlloc<RC<RawSyntax>, OwnedString, TriviaPiece>( | ||
Layout.size(), 0, 0); | ||
void *data = Arena ? Arena->AllocateRawSyntax(size, alignof(RawSyntax)) | ||
: ::operator new(size); | ||
return RC<RawSyntax>(new (data) | ||
RawSyntax(Kind, Layout, Presence, bool(Arena))); | ||
return RC<RawSyntax>( | ||
new (data) RawSyntax(Kind, Layout, Presence, bool(Arena), NodeId)); | ||
} | ||
|
||
RC<RawSyntax> RawSyntax::make(tok TokKind, OwnedString Text, | ||
ArrayRef<TriviaPiece> LeadingTrivia, | ||
ArrayRef<TriviaPiece> TrailingTrivia, | ||
SourcePresence Presence, SyntaxArena *Arena) { | ||
SourcePresence Presence, SyntaxArena *Arena, | ||
llvm::Optional<unsigned> NodeId) { | ||
auto size = totalSizeToAlloc<RC<RawSyntax>, OwnedString, TriviaPiece>( | ||
0, 1, LeadingTrivia.size() + TrailingTrivia.size()); | ||
void *data = Arena ? Arena->AllocateRawSyntax(size, alignof(RawSyntax)) | ||
: ::operator new(size); | ||
return RC<RawSyntax>(new (data) RawSyntax( | ||
TokKind, Text, LeadingTrivia, TrailingTrivia, Presence, bool(Arena))); | ||
return RC<RawSyntax>(new (data) RawSyntax(TokKind, Text, LeadingTrivia, | ||
TrailingTrivia, Presence, | ||
bool(Arena), NodeId)); | ||
} | ||
|
||
RC<RawSyntax> RawSyntax::append(RC<RawSyntax> NewLayoutElement) const { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
NodeId
should be always greater than0
, I think this can beunsigned NodeId = 0
just like token version. Am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially used 0 to indicate that the NodeId should be picked automatically, but changed it to
Optional<unsigned>
because of @nkcsgexi's comment here: #16636 (comment). I'm fine with either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
0
as default is too un-readable. @nkcsgexi WDYT?Either way, if there's no specific reason, please be consistent between token and layout.