-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[libSyntax] Syntax coloring using libSyntax #16636
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
Closed
Closed
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
275821a
[incrParse] Add a stable id to the syntax nodes
ahoppen 9e0195b
[libSyntax] Add syntax coloring based on the syntax tree
ahoppen 3505596
[libSyntax] Disable caching of token nodes
ahoppen 6833540
[libSyntax] Classify unterminated string literals as string literals
ahoppen 6b03383
[libSyntax] Classify the opening parenthesis of a string interpolatio…
ahoppen 4670478
[libSyntax] Enable tests for libSyntax based syntax coloring
ahoppen 82f7e20
[libSyntax] Add syntax coloring based on the syntax tree to SourceKit
ahoppen 7c9368e
[libSyntax] Enable incremental parsing for syntax tree based syntax c…
ahoppen 5b596b9
[libSyntax] Allow syntax cache reuse info to be passed back via Sourc…
ahoppen 7b3580d
[libSyntax] Copy the text of a syntax node
ahoppen cabb5d5
[libSyntax] Add a json::Output to print a syntax tree without nodes
ahoppen 21b438b
[incrParse] Test utility: Compare syntax trees without IDs
ahoppen a2ff54d
[incrParse] Add validation of incremental parsing
ahoppen 7eb0ec8
[incrParse] Refactor logging of syntax reuse regions
ahoppen 07e9b96
[libSyntax] Adjust some failing tests
ahoppen bce813a
[incrParse] Perform a full reparse of the file if needed for formatting
ahoppen 7691715
[incrParse] Add test case for nested initializers
ahoppen bb7919f
[libSyntax] Adjust SyntaxClassifier for visitable SyntaxCollections
ahoppen a3a84f8
[libSyntax] By default use the old syntax classifier
ahoppen a3fb15b
Fix test case for OwnedSyntax because it now by default copies the st…
ahoppen c9f215c
[libSyntax] Teach SyntaxToSyntaxMapConverter to convert trivia
ahoppen 0500e65
[libSyntax] Classify identifier tokens as identifiers if no other inf…
ahoppen e7fe537
[libSyntax] Classify pound directives as build config keywords
ahoppen 4b506b2
[libSyntax] Add test variants for building the syntax map via libSyntax
ahoppen 8b037fc
[SourceKit] Move all options into a common options struct
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
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,114 @@ | ||
%{ | ||
# -*- 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/AST/Identifier.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; | ||
}; | ||
|
||
std::map<unsigned, SyntaxClassification> ClassifiedTokens; | ||
/// The top classification of this stack determines the color of identifiers | ||
std::stack<ContextStackEntry, llvm::SmallVector<ContextStackEntry, 4>> ContextStack; | ||
|
||
template<typename T> | ||
void visit(T Node, SyntaxClassification Classification, | ||
bool ForceClassification) { | ||
ContextStack.push({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, 4>>(); | ||
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
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.
It's still kind of weird that the serialization util knows syntax tree :( . Can we define a 32-bit field in
Output
whose meaning can be interpreted while serializing? For instance, we can use the first bit to indicate whethershouldIncludeNodeIds
.