Commit Graph

56 Commits

Author SHA1 Message Date
Harlan
9e2e97a879 Update SwiftSyntax README with example (#15548)
* Update SwiftSyntax README with example

* Add link and Xcode instructions to README
2018-03-29 16:28:49 -04:00
Harlan
bdfa6cd4d7 [SwiftSyntax] Add SyntaxRewriter.visitAny(_:) (#15212)
* [SwiftSyntax] Add SyntaxRewriter.visitAny(_:)

This function, when overridden, allows Syntax rewriters to perform custom dynamic visitation behavior. If a user wanted to, say, store a series of transformations accessible by metatype, they can override visitAny and do their own runtime dispatch.

If a non-nil result is returned from visitAny, the original specialized visitors are skipped.
2018-03-27 10:55:31 -04:00
Xi Ge
de34be29b2 SwiftSyntax: bridging absolute location with diagnostics location. (#15434)
We have two similar objects for source location. AbsoluteLocation
calculates the offset of a syntax node on the fly. SourceLocation is
designed to serialize a Swift syntax diagnostics to the driver. The only
difference is AbsoluteLocation doesn't contain source file name however
SourceLocation does. This patch bridges them by making AbsoluteLocation
a private member of SourceLocation. We also expect Swift syntax to
be file-name agnostic. The clients should keep track of the file name
when emitting diagnostics.
2018-03-23 15:36:22 -07:00
Xi Ge
123ccc7602 SwiftSyntax: add an API to check whether a syntax node has underlying source. (#15389) 2018-03-20 18:16:54 -07:00
Denis Morozov
2866f0e0a6 [SwiftSyntax] Add replace method to SyntaxCollections (#15331)
* [SwiftSyntax] Add replace method SyntaxCollections

* Change replaceSubrange method call to subscript setter call

* [SwiftSyntax] Add test for SyntaxCollections

* Fix mistakes in SyntaxCollections tests

* Use syntax's raw value instead of syntax itself in replacing method

* Update SyntaxCollections tests
2018-03-20 14:58:16 -04:00
Xi Ge
20bc656261 SwiftSyntax: make absolute position calculation independent of specific encoding. NFC (#15352) 2018-03-19 16:33:05 -07:00
swift-ci
7118b180ab Merge pull request #15288 from nkcsgexi/utf8-view-syntax 2018-03-15 19:27:45 -07:00
Xi Ge
4c1be6aabd SwiftSyntax: avoid creating c-string when accumulating text. NFC 2018-03-15 18:16:22 -07:00
Xi Ge
1c9f1c48ed SwiftSyntax: allow any Syntax nodes to access their leading/trailing trivia. (#15278)
Although libSyntax is designed in a way that trivia is attached to
tokens, we shouldn't restrict clients to access trivia only from a token.
An example can be doc-comment, which conceptually attaches to a declaration rather
than the start token of a declaration, like at-attributes.
2018-03-15 15:31:08 -07:00
Xi Ge
af868778b6 SwiftSyntax: add an API to teach Trivia to calculate byte size. (#15251)
This missing piece will allow clients to customize the size of
SyntaxNode better.
2018-03-14 17:14:52 -07:00
Rintaro Ishizaki
f23edf0c4b [SwiftSyntax] Align implementation of SyntaxBuilders to C++ API (#15232)
Use 'nil' for missing *optional* children.
2018-03-15 01:44:36 +09:00
Rintaro Ishizaki
ea5f93807f [Syntax] Add 'unknown' token to Token.py 2018-03-14 20:38:48 +09:00
Xi Ge
ee36dbeda3 [WIP] SwiftSyntax: Implement APIs for Syntax nodes to calculate absolute positions. (#15097)
Syntax nodes are designed as reusable blocks to construct Swift source
code. For this reason, we don't track absolute position in each node;
instead, the absolute position should be calculated on the fly when
needed. We recently found absolute positions are useful to bridge with
sourcekitd, which typically speaks in the language of line, column and
offset. Therefore, this patch tries to add a computed
property on SyntaxNode to get its absolute position.

To compute the absolute position of a SyntaxNode from scratch requires tree traversal.
However, getting the absolute position from these added APIs doesn't necessarily
mean we'll traverse since SyntaxData will actively cache the computed position.
Also, since we recursively compute the absolute position, all the position caches
for a SyntaxNode's previous siblings and ancestors will be populated as well during
the calculation of this SyntaxNode.
2018-03-12 15:42:46 -07:00
Denis Morozov
6608176e13 Use RawSyntax presence state properties in SwiftSyntax 2018-03-12 15:35:29 -07:00
Denis Morozov
568ae2deda Improve comments in Syntax.swift 2018-03-11 11:46:51 -07:00
Harlan
37a6f4c6ec [SwiftSyntax] Make traits conform to Syntax (#15112) 2018-03-10 09:39:35 -05:00
Harlan
e41a42b060 [SwiftSyntax] Ensure SyntaxRewriter respects nil children when rewriting (#15052) 2018-03-08 00:37:41 -05:00
Xi Ge
e61c8958fd SwiftSyntax: simply trivia implementation on the Swift side. 2018-03-07 16:19:09 -08:00
Harlan
d3f6b8743d [SwiftSyntax] Publicize DiagnosticEngine.addConsumer and add hasErrors property (#14996)
* [SwiftSyntax] Publicize DiagnosticEngine.addConsumer and add hasErrors property

* [Syntax] Make DiagnosticEngine.hasErrors public
2018-03-06 00:40:24 -05:00
Harlan
2a3d4cb598 Initial infrastructure for documenting SwiftSyntax API (#14701) 2018-02-26 13:43:11 -05:00
Xi Ge
1b81fcb2b6 SwiftSyntax: Add a trait for those statement nodes with code block as body. (#14726)
This patch also refactors SyntaxNodes code so that protocol conformances
are declared as extensions.
2018-02-19 18:49:07 -08:00
Xi Ge
2b61d4edbb SwiftSyntax: add a mechanism to define traits of syntax nodes to allow abstract access to popular child kinds. NFC (#14668)
Swift syntax APIs lack an abstract way of accessing children. The client has to
down-cast a syntax node to the leaf type to access any of its children. However,
some children are common among different syntax kinds, e.g.
DeclAttributeSyntax and DeclMembers. We should allow an abstract way to
access and modify them, so that clients can avoid logic duplication.

This patch adds a mechanism to define new traits and specify satisfied
traits in specific syntax nodes. A trait is a set of common children
and implemented in Swift as a protocol for syntax nodes to conform to.
As a proof-of-concept, we added two traits for now including DeclGroupSyntax
and BracedSyntax.

Resolves: SR-6931 and SR-6916
2018-02-15 16:41:20 -08:00
Xi Ge
50cde06cf0 Revert "SwiftSyntax: Teach SwiftSyntax to use SourceKitd to serialize syntax trees. (#14424)" 2018-02-06 23:20:42 -08:00
Xi Ge
871c9dac2a SwiftSyntax: Teach SwiftSyntax to use SourceKitd to serialize syntax trees. (#14424)
When using SwiftSyntax as a standalone tool, we invoke Swiftc
internally to get serialized syntax trees. This is not ideal for
several reasons: (1) we have to hard-code the relative path of swiftc
to invoke it; (2) we have to rely on standard input/output to pass the
tree across the process boundaries; and (3) we have to maintain two
different ways to get syntax tree (swiftc and sourcekitd).

This patch attempts to teach SwiftSyntax to use SourceKitd to get the
tree just like other clients. We first add a SourceKitd client library
written in Swift; and next teach SwiftSyntax to adopt this SourceKitd
client-side library. For platforms other than MacOS, we still use Swiftc
to get syntax trees. This client library also allows us to add 
SourceKitd tests in Swift.

This patch also re-enables several flaky tests.
2018-02-06 19:40:16 -08:00
Rintaro Ishizaki
5dc6b78457 [Syntax] Parse generic specialize expression 2018-02-06 15:05:59 +09:00
Xi Ge
dac918f870 SwiftSyntax: add pre and post visit function so that client can override a general visiting logic. SR-6902 (#14365) 2018-02-02 16:23:09 -08:00
Ben Cohen
9ee856f386 [stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342)
* Make Range conditionally a Collection

* Convert ClosedRange to conditionally a collection

* De-gyb Range/ClosedRange, refactoring some methods.

* Remove use of Countable{Closed}Range from stdlib

* Remove Countable use from Foundation

* Fix test errors and warnings resulting from Range/CountableRange collapse

* fix prespecialize test for new mangling

* Update CoreAudio use of CountableRange

* Update SwiftSyntax use of CountableRange

* Restore ClosedRange.Index: Hashable conformance

* Move fixed typechecker slowness test for array-of-ranges from slow to fast, yay

* Apply Doug's patch to loosen test to just check for error
2018-02-01 20:59:28 -08:00
Rintaro Ishizaki
fced748790 [Syntax] Represent missing optioanl nodes as nullptr (#14300)
Allocating RawSyntax/SyntaxData for missing optional node is a waste of
resource.
2018-01-31 19:24:00 +09:00
Xi Ge
1b5306158d [test] libSyntax: add a test to ensure the client side decodes syntax tree properly. NFC (#14203)
We need to expose a SwiftSyntax API to encode a source file into Json
format.
2018-01-26 16:53:20 -08:00
Harlan
95fc7d335e Change vars in SwiftSyntax nodes to lets (#14173) 2018-01-26 15:19:05 -08:00
Xi Ge
d13f83df14 SwiftSyntax: Factor out an API to decode serialized SourceFileSyntax. NFC (#14164)
Since our recent integration with SourceKit, clients may get a SourceFileSyntax
in a serialized form from SourceKit response.  We need an API in the
SwiftSyntax framework to decode this form. This'll be a more common
pattern to get a Swift side syntax tree than invoking swiftc internally.
2018-01-25 13:40:42 -08:00
Harlan
a339c82c1e [SwiftSyntax] Make Syntax nodes structs instead of classes (#14122)
* [Experiment] Make Syntax nodes structs instead of classes

* [Experiment] Add Hashable conformance to concrete types

* Fix pep8 violation

* Remove AnySyntax, explicitly specialize SyntaxCollection nodes

* Refine the comment for SyntaxCollection nodes
2018-01-24 23:17:25 -05:00
Rintaro Ishizaki
9c4d6f4893 Revert "[SwiftSyntax] Add debug print when the JSONDecoder fails to decode JSON (#14056)" (#14101)
This reverts commit 840166c3ea.
2018-01-24 10:01:43 +09:00
Rintaro Ishizaki
840166c3ea [SwiftSyntax] Add debug print when the JSONDecoder fails to decode JSON (#14056)
from the compiler's '-emit-syntax'.

Investigating: rdar://problem/36379512
2018-01-22 23:44:18 +09:00
Slava Pestov
162187c4fe Revert "SwiftSyntax: Fix resilient build"
This reverts commit bbb21c84ca.
2018-01-17 21:02:09 -08:00
Xi Ge
b85f6d9c58 libSyntax: add a C++ side read-only syntax visitor to facilitate verification. NFC (#13882) 2018-01-11 16:17:44 -08:00
Xi Ge
d41eedcbf6 SwiftSyntax: Add a read-only syntax visitor. (#13681)
Based on the feedbacks from our early adopters, separating syntax tree analysis
with transformation is a common pattern. Thus, we introduce a read-only
syntax tree visitor to help the analysis phase. This visitor never alters the
content of a tree being visited, in contrast to SyntaxRewriter which always does.
2018-01-02 16:52:30 -08:00
Xi Ge
d847bf5ddd SwiftSyntax: make Syntax node Hashable. (#13679)
SwiftSyntax: make Syntax node Hashable.

This gives Syntax node a unique identifier and makes them friendly
to Set and Dictionary.
2018-01-02 14:19:26 -08:00
Harlan
9185ef5610 [Syntax] Add #selector and #keyPath tokens (#13615)
* [Syntax] Add #selector and #keyPath tokens

* [Syntax][test] Add #selector and #keyPath to syntax parsing tests

* [Syntax] Don't fatalError on an unknown token kind

* Fix test build error
2017-12-29 12:35:08 -05:00
omochimetaru
3a3e89ba0c [Syntax] add TriviaKind::CarriageReturnLineFeed 2017-12-20 20:50:40 +09:00
Rintaro Ishizaki
2c06060165 [Syntax] Add CarriageReturn trivia kind
To distinguish '\r' from '\n'.
2017-12-19 09:24:34 +09:00
Harlan
a0512cd48a re-enable SwiftSyntax tests (#13399) 2017-12-14 23:39:16 -05:00
Rintaro Ishizaki
ef29650acd [Syntax] Parse: add support for TupleType and FunctionType
For now using SyntaxParsingContext.
2017-12-14 14:55:27 +09:00
Slava Pestov
bbb21c84ca SwiftSyntax: Fix resilient build
Address-only types do not support multiple case labels yet.
2017-12-12 21:10:59 -08:00
Harlan
2cc756414b [SwiftSyntax] Swift Diagnostics API (#11511)
This patch provides an API and Serialization for clang-style diagnostics
from within Swift. Libraries can use this API to pop their own custom
diagnostics that can be serialized to JSON.
2017-12-12 15:55:22 -05:00
Rintaro Ishizaki
2b1e316cf6 [Syntax] Add parsing hashbang (shebang) as a trivia.
Added GarbageText trivia kind for any skipped text.
2017-12-08 12:07:00 +09:00
Xi Ge
fec040d95e libSyntax: support generic parameter clause. (#13286)
This patch also performs minor refactoring to align syntax parsing
context with the right scope. We start to support the generic clauses
because they are necessary pieces to construct struct or
function syntax node.
2017-12-05 19:34:55 -08:00
Rintaro Ishizaki
e7cfae0ba9 [libSyntax] Support parsing type-identifier 2017-11-29 09:57:59 +09:00
Robert Widmann
cd64938e6b Provide an explicit iterator for SyntaxCollection (#13112)
The default IndexingIterator for a custom collection is not suitable for
subclasses that specialize the element type the way libSyntax does.
Overriding it with a more specific iterator, even with something like
covariant overrides, would not work because it the iterator is generic
over its base collection, not its element type.

Provide a custom iterator parameterized by the element type instead.
2017-11-28 15:23:35 -05:00
Harlan
f552f55969 Update SwiftSyntax's expectations now that -emit-syntax is emitting a SourceFileSyntax (#12860) 2017-11-10 15:48:13 -08:00