Skip to content

Commit d65a491

Browse files
Merge pull request #18 from mattmassicotte/feature/python
python
2 parents 91534b1 + 87ca209 commit d65a491

File tree

19 files changed

+231
-8
lines changed

19 files changed

+231
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The `tree_sitter.xcframework` binary comes with:
1010
- [tree-sitter-php](https://github.com/tree-sitter/tree-sitter-php)
1111
- [tree-sitter-markdown](https://github.com/ikatyang/tree-sitter-markdown)
1212
- [tree-sitter-java](https://github.com/tree-sitter/tree-sitter-java)
13+
- [tree-sitter-python](https://github.com/tree-sitter/tree-sitter-python)
1314

1415
This is a [work-in-progress](https://github.com/tree-sitter/tree-sitter/issues/1488). But, if the parser you'd like to use doesn't have a Makefile, let me know and I'll help get it set up.
1516

Sources/tree_sitter_language_resources/LanguageResource.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum LanguageResource: CaseIterable, Hashable {
88
case json
99
case markdown
1010
case php
11+
case python
1112
case ruby
1213
case swift
1314

@@ -25,6 +26,8 @@ public enum LanguageResource: CaseIterable, Hashable {
2526
return "markdown"
2627
case .php:
2728
return "php"
29+
case .python:
30+
return "python"
2831
case .ruby:
2932
return "ruby"
3033
case .swift:
@@ -46,6 +49,8 @@ public enum LanguageResource: CaseIterable, Hashable {
4649
return tree_sitter_markdown()
4750
case .php:
4851
return tree_sitter_php()
52+
case .python:
53+
return tree_sitter_python()
4954
case .ruby:
5055
return tree_sitter_ruby()
5156
case .swift:
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
; Identifier naming conventions
2+
3+
((identifier) @constructor
4+
(#match? @constructor "^[A-Z]"))
5+
6+
((identifier) @constant
7+
(#match? @constant "^[A-Z][A-Z_]*$"))
8+
9+
; Builtin functions
10+
11+
((call
12+
function: (identifier) @function.builtin)
13+
(#match?
14+
@function.builtin
15+
"^(abs|all|any|ascii|bin|bool|breakpoint|bytearray|bytes|callable|chr|classmethod|compile|complex|delattr|dict|dir|divmod|enumerate|eval|exec|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|vars|zip|__import__)$"))
16+
17+
; Function calls
18+
19+
(decorator) @function
20+
21+
(call
22+
function: (attribute attribute: (identifier) @function.method))
23+
(call
24+
function: (identifier) @function)
25+
26+
; Function definitions
27+
28+
(function_definition
29+
name: (identifier) @function)
30+
31+
(identifier) @variable
32+
(attribute attribute: (identifier) @property)
33+
(type (identifier) @type)
34+
35+
; Literals
36+
37+
[
38+
(none)
39+
(true)
40+
(false)
41+
] @constant.builtin
42+
43+
[
44+
(integer)
45+
(float)
46+
] @number
47+
48+
(comment) @comment
49+
(string) @string
50+
(escape_sequence) @escape
51+
52+
(interpolation
53+
"{" @punctuation.special
54+
"}" @punctuation.special) @embedded
55+
56+
[
57+
"-"
58+
"-="
59+
"!="
60+
"*"
61+
"**"
62+
"**="
63+
"*="
64+
"/"
65+
"//"
66+
"//="
67+
"/="
68+
"&"
69+
"%"
70+
"%="
71+
"^"
72+
"+"
73+
"->"
74+
"+="
75+
"<"
76+
"<<"
77+
"<="
78+
"<>"
79+
"="
80+
":="
81+
"=="
82+
">"
83+
">="
84+
">>"
85+
"|"
86+
"~"
87+
"and"
88+
"in"
89+
"is"
90+
"not"
91+
"or"
92+
] @operator
93+
94+
[
95+
"as"
96+
"assert"
97+
"async"
98+
"await"
99+
"break"
100+
"class"
101+
"continue"
102+
"def"
103+
"del"
104+
"elif"
105+
"else"
106+
"except"
107+
"exec"
108+
"finally"
109+
"for"
110+
"from"
111+
"global"
112+
"if"
113+
"import"
114+
"lambda"
115+
"nonlocal"
116+
"pass"
117+
"print"
118+
"raise"
119+
"return"
120+
"try"
121+
"while"
122+
"with"
123+
"yield"
124+
"match"
125+
"case"
126+
] @keyword
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(class_definition
2+
name: (identifier) @name) @definition.class
3+
4+
(function_definition
5+
name: (identifier) @name) @definition.function
6+
7+
(call
8+
function: [
9+
(identifier) @name
10+
(attribute
11+
attribute: (identifier) @name)
12+
]) @reference.call

scripts/build-tree-sitter.sh

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ pushd tree-sitter-java
131131
popd
132132
build_parser "java"
133133

134+
git clone --depth 1 https://github.com/tree-sitter/tree-sitter-python.git
135+
pushd tree-sitter-python
136+
gh pr checkout 162
137+
popd
138+
build_parser "python"
139+
134140
# now, build the frameworks
135141

136142
pushd $TMP_BUILD_DIR/build/macos
@@ -144,7 +150,8 @@ libtool -static -o libtree-sitter.a \
144150
lib/libtree-sitter-json.a \
145151
lib/libtree-sitter-php.a \
146152
lib/libtree-sitter-markdown.a \
147-
lib/libtree-sitter-java.a
153+
lib/libtree-sitter-java.a \
154+
lib/libtree-sitter-python.a
148155

149156
mkdir -p tree_sitter.framework/Versions/A/{Headers,Modules,Resources}
150157
cp -f libtree-sitter.a tree_sitter.framework/Versions/A/tree_sitter
@@ -177,7 +184,8 @@ libtool -static -o libtree-sitter.a \
177184
lib/libtree-sitter-json.a \
178185
lib/libtree-sitter-php.a \
179186
lib/libtree-sitter-markdown.a \
180-
lib/libtree-sitter-java.a
187+
lib/libtree-sitter-java.a \
188+
lib/libtree-sitter-python.a
181189

182190
mkdir -p tree_sitter.framework/{Headers,Modules}
183191
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter
@@ -199,7 +207,8 @@ libtool -static -o libtree-sitter.a \
199207
lib/libtree-sitter-json.a \
200208
lib/libtree-sitter-php.a \
201209
lib/libtree-sitter-markdown.a \
202-
lib/libtree-sitter-java.a
210+
lib/libtree-sitter-java.a \
211+
lib/libtree-sitter-python.a
203212

204213
mkdir -p tree_sitter.framework/{Headers,Modules}
205214
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter
@@ -221,7 +230,8 @@ libtool -static -o libtree-sitter.a \
221230
lib/libtree-sitter-json.a \
222231
lib/libtree-sitter-php.a \
223232
lib/libtree-sitter-markdown.a \
224-
lib/libtree-sitter-java.a
233+
lib/libtree-sitter-java.a \
234+
lib/libtree-sitter-python.a
225235

226236
mkdir -p tree_sitter.framework/{Headers,Modules}
227237
cp -f libtree-sitter.a tree_sitter.framework/tree_sitter

shim/tree_sitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#include <tree_sitter/tree-sitter-json.h>
99
#include <tree_sitter/markdown.h>
1010
#include <tree_sitter/php.h>
11+
#include <tree_sitter/python.h>

tree_sitter.xcframework/Info.plist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<array>
77
<dict>
88
<key>LibraryIdentifier</key>
9-
<string>ios-arm64_x86_64-maccatalyst</string>
9+
<string>ios-arm64_x86_64-simulator</string>
1010
<key>LibraryPath</key>
1111
<string>tree_sitter.framework</string>
1212
<key>SupportedArchitectures</key>
@@ -17,7 +17,7 @@
1717
<key>SupportedPlatform</key>
1818
<string>ios</string>
1919
<key>SupportedPlatformVariant</key>
20-
<string>maccatalyst</string>
20+
<string>simulator</string>
2121
</dict>
2222
<dict>
2323
<key>LibraryIdentifier</key>
@@ -46,7 +46,7 @@
4646
</dict>
4747
<dict>
4848
<key>LibraryIdentifier</key>
49-
<string>ios-arm64_x86_64-simulator</string>
49+
<string>ios-arm64_x86_64-maccatalyst</string>
5050
<key>LibraryPath</key>
5151
<string>tree_sitter.framework</string>
5252
<key>SupportedArchitectures</key>
@@ -57,7 +57,7 @@
5757
<key>SupportedPlatform</key>
5858
<string>ios</string>
5959
<key>SupportedPlatformVariant</key>
60-
<string>simulator</string>
60+
<string>maccatalyst</string>
6161
</dict>
6262
</array>
6363
<key>CFBundlePackageType</key>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_PYTHON_H_
2+
#define TREE_SITTER_PYTHON_H_
3+
4+
#include <tree_sitter/parser.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
extern TSLanguage *tree_sitter_python();
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_PYTHON_H_

tree_sitter.xcframework/ios-arm64/tree_sitter.framework/Headers/tree_sitter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
#include <tree_sitter/tree-sitter-json.h>
99
#include <tree_sitter/markdown.h>
1010
#include <tree_sitter/php.h>
11+
#include <tree_sitter/python.h>
Binary file not shown.

0 commit comments

Comments
 (0)