@@ -13,8 +13,8 @@ The emitted JSON tree preserves the AST's named structure. Every node has a
1313` kind ` and a ` range ` with ` start ` /` end ` positions (UTF-8 ` offset ` plus 1-based
1414` line ` /` column ` ). Beyond that:
1515
16- - ** Tokens** carry ` text ` , ` tokenKind ` , and ` leadingTrivia ` / ` trailingTrivia `
17- arrays of ` { kind, text } ` pieces.
16+ - ** Tokens** carry ` text ` , ` tokenKind ` , and — only when non-empty —
17+ ` leadingTrivia ` / ` trailingTrivia ` arrays of ` { kind, text } ` pieces.
1818- ** Layout nodes** (e.g. ` functionDecl ` ) embed their children directly as
1919 members keyed by the child's name in the parent (` name ` , ` signature ` ,
2020 ` body ` , …), alongside ` kind ` /` range ` . Absent optional children are omitted.
@@ -49,9 +49,7 @@ abbreviated here as `…`):
4949 " kind" : " token" ,
5050 " text" : " let" ,
5151 " tokenKind" : " keyword(SwiftSyntax.Keyword.let)" ,
52- " range" : …,
53- " leadingTrivia" : [],
54- " trailingTrivia" : []
52+ " range" : …
5553 },
5654 " bindings" : [
5755 {
@@ -60,12 +58,12 @@ abbreviated here as `…`):
6058 " pattern" : {
6159 " kind" : " identifierPattern" ,
6260 " range" : …,
63- " identifier" : { " kind" : " token" , " text" : " x" , " tokenKind" : " identifier(\" x\" )" , " range" : …, " leadingTrivia " : [], " trailingTrivia " : [] }
61+ " identifier" : { " kind" : " token" , " text" : " x" , " tokenKind" : " identifier(\" x\" )" , " range" : … }
6462 },
6563 " initializer" : {
6664 " kind" : " initializerClause" ,
6765 " range" : …,
68- " equal" : { " kind" : " token" , " text" : " =" , " tokenKind" : " equal" , " range" : …, " leadingTrivia " : [], " trailingTrivia " : [] },
66+ " equal" : { " kind" : " token" , " text" : " =" , " tokenKind" : " equal" , " range" : … },
6967 " value" : {
7068 " kind" : " integerLiteralExpr" ,
7169 " range" : …,
@@ -74,7 +72,6 @@ abbreviated here as `…`):
7472 " text" : " 1" ,
7573 " tokenKind" : " integerLiteral(\" 1\" )" ,
7674 " range" : …,
77- " leadingTrivia" : [],
7875 " trailingTrivia" : [ { " kind" : " lineComment" , " text" : " // c" } ]
7976 }
8077 }
@@ -84,14 +81,15 @@ abbreviated here as `…`):
8481 }
8582 }
8683 ],
87- " endOfFileToken" : { " kind" : " token" , " text" : " " , " tokenKind" : " endOfFile" , " range" : …, " leadingTrivia " : [], " trailingTrivia " : [] }
84+ " endOfFileToken" : { " kind" : " token" , " text" : " " , " tokenKind" : " endOfFile" , " range" : … }
8885}
8986```
9087
9188Note how ` statements ` , ` bindings ` , ` attributes ` , and ` modifiers ` are plain
9289arrays (their collection nodes are elided), layout children such as
9390` bindingSpecifier ` and ` initializer ` are embedded by name, and the ` // c `
94- comment rides along as ` trailingTrivia ` on the token it follows.
91+ comment rides along as ` trailingTrivia ` on the token it follows. Tokens without
92+ trivia (most of them) simply omit the ` leadingTrivia ` /` trailingTrivia ` keys.
9593
9694## Prerequisites
9795
@@ -171,10 +169,29 @@ CLI (reads a file argument or stdin, prints the syntax tree as JSON):
171169echo ' let x = 1' | cargo run --bin swift-syntax-parse
172170```
173171
172+ ## Converting to a yeast AST
173+
174+ For use in the CodeQL extractor, the JSON tree can be converted into a
175+ [ ` yeast::Ast ` ] ( ../../shared/yeast ) — the in-memory format the extractor's
176+ rewrite rules operate on — via [ ` yeast_adapter::json_to_ast ` ] ( src/yeast_adapter.rs ) :
177+
178+ ``` rust
179+ let json = swift_syntax_rs :: parse_to_json (" let x = 1" )? ;
180+ let ast = swift_syntax_rs :: yeast_adapter :: json_to_ast (& json )? ;
181+ ```
182+
183+ The adapter mirrors tree-sitter's node model, which is what yeast expects:
184+ layout nodes and varying tokens (identifiers, literals, operators) become
185+ ** named** nodes; fixed tokens (keywords, punctuation) become ** anonymous**
186+ nodes keyed by their text. It preserves swift-syntax's own kind/field names —
187+ aligning them with the tree-sitter-swift schema so the existing rewrite rules
188+ fire is a separate, later step.
189+
174190## Layout
175191
176192- ` swift/ ` — Swift package exposing the ` ssr_parse_json ` / ` ssr_string_free ` C ABI.
177193- ` build.rs ` — builds the Swift package and emits link/rpath flags (local ` cargo ` only).
178194- ` BUILD.bazel ` — Bazel targets for the hermetic CI build (swift_library + rust targets).
179195- ` src/lib.rs ` — safe Rust bindings (` parse_to_json ` ).
196+ - ` src/yeast_adapter.rs ` — converts the JSON tree into a ` yeast::Ast ` .
180197- ` src/main.rs ` — demo CLI.
0 commit comments