Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ZkHaider/SwiftySyntax
Swift wrapper around the Tree-Sitter lib, an incremental parser system for syntax.
https://github.com/ZkHaider/SwiftySyntax
Last synced: 14 days ago
JSON representation
Swift wrapper around the Tree-Sitter lib, an incremental parser system for syntax.
- Host: GitHub
- URL: https://github.com/ZkHaider/SwiftySyntax
- Owner: ZkHaider
- License: mit
- Created: 2019-08-25T17:57:02.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-02T02:59:03.000Z (almost 5 years ago)
- Last Synced: 2024-08-01T19:43:39.394Z (3 months ago)
- Language: C
- Size: 1.34 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftySyntax
Swift wrapper around the Tree-Sitter lib, an incremental parser system for syntax.## Installation
Just add `github "ZkHaider/SwiftySyntax" "master"` to your Cartfile and run `carthage`.
## Usage
Let's take a normal `UITextView` with some source code as an attributed string value, and set the text color to white:
```swift
let sourceCode = """
{
"counter": 0,
"images": [
{
"id": 10,
"url": "https://someimage.com/image/12"
}
]
}
"""let textView: UITextView = {
let view = UITextView(frame: .zero)
view.font = UIFont.systemFont(ofSize: 16.0)
view.textColor = .white
view.backgroundColor = .clear
return view
}()let mutableAttributedString = NSMutableAttributedString(string: self.sourceCode)
mutableAttributedString.addAttribute(
.foregroundColor,
value: UIColor.white,
range: NSRange(location: 0,
length: self.sourceCode.count)
)self._view.textView.attributedText = mutableAttributedString
```We get the following:
Now let's highlight the JSON syntax:
```swift
struct Colors {
static let matteRed: UIColor = {
return UIColor(red: 0.942, green: 0.566, blue: 0.498, alpha: 1.0)
}()
static let gold: UIColor = {
return UIColor(red: 0.839, green: 0.78, blue: 0.463, alpha: 1.0)
}()
}let sourceCode = """
{
"counter": 0,
"images": [
{
"id": 10,
"url": "https://someimage.com/image/12"
}
]
}
"""let textView: UITextView = {
let view = UITextView(frame: .zero)
view.font = UIFont.systemFont(ofSize: 16.0)
view.textColor = .white
view.backgroundColor = .clear
return view
}()let mutableAttributedString = NSMutableAttributedString(string: self.sourceCode)
mutableAttributedString.addAttribute(
.foregroundColor,
value: UIColor.white,
range: NSRange(location: 0,
length: self.sourceCode.count)
)for match in self.parser.matches(with: .pairCombined, on: tree.rootNode) {
for capture in match.captures {
switch capture.captureType {
case .pairKey:
let startByte = capture.node.startByte
let endByte = capture.node.endByte
let range = NSRange(location: startByte, length: endByte - startByte)
mutableAttributedString.addAttribute(
.foregroundColor,
value: Colors.matteRed,
range: range
)
case .pairString:
let startByte = capture.node.startByte
let endByte = capture.node.endByte
let range = NSRange(location: startByte, length: endByte - startByte)
mutableAttributedString.addAttribute(
.foregroundColor,
value: Colors.matteRed,
range: range
)
case .pairNumber:
let startByte = capture.node.startByte
let endByte = capture.node.endByte
let range = NSRange(location: startByte, length: endByte - startByte)
mutableAttributedString.addAttribute(
.foregroundColor,
value: Colors.gold,
range: range
)
case .pairArray:
continue
case .pairObject:
continue
default: continue
}
}
}self._view.textView.attributedText = mutableAttributedString
```Now we get this:
Lots more work to come. Python, PHP, Javascript, Rust, and even Swift grammars will be implemented soon.