Skip to content

Commit bf2794a

Browse files
Merge pull request #360 from SwiftFiddle/refactor
Align the latest SwiftSyntax changes
2 parents 440d688 + c0ac78f commit bf2794a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Resources/parsers/trunk/Sources/parser/SyntaxParser.swift

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
import SwiftSyntax
3+
import SwiftOperators
4+
import SwiftParser
5+
6+
struct SyntaxParser {
7+
static func parse(code: String, options: [String] = []) throws -> SyntaxResponse {
8+
let sourceFile = Parser.parse(source: code)
9+
10+
let syntax: Syntax
11+
if options.contains("fold") {
12+
syntax = OperatorTable.standardOperators.foldAll(sourceFile, errorHandler: { _ in })
13+
} else {
14+
syntax = Syntax(sourceFile)
15+
}
16+
17+
let visitor = TokenVisitor(
18+
locationConverter: SourceLocationConverter(fileName: "", tree: sourceFile),
19+
showMissingTokens: options.contains("showmissing")
20+
)
21+
_ = visitor.rewrite(syntax)
22+
23+
let html = "\(visitor.list.joined())"
24+
25+
let tree = visitor.tree
26+
let encoder = JSONEncoder()
27+
let json = String(decoding: try encoder.encode(tree), as: UTF8.self)
28+
29+
return SyntaxResponse(syntaxHTML: html, syntaxJSON: json, swiftVersion: "5.9")
30+
}
31+
}

0 commit comments

Comments
 (0)