-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
236 lines (217 loc) · 4.96 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from importlib import import_module
from typing import Callable, Literal, Union, cast
from tree_sitter import Language, Parser
from tree_sitter_c_sharp import language as c_sharp_language
from tree_sitter_embedded_template import language as embedded_template_language
from tree_sitter_php import language_php as php_language
from tree_sitter_typescript import language_tsx, language_typescript
from tree_sitter_xml import language_dtd as dtd_language
from tree_sitter_xml import language_xml as xml_language
from tree_sitter_yaml import language as yaml_language
InstalledBindings = Literal[
"csharp",
"dtd",
"embeddedtemplate",
"php",
"tsx",
"typescript",
"xml",
"yaml",
]
SupportedLanguage = Union[
Literal[
"actionscript",
"ada",
"agda",
"arduino",
"asm",
"astro",
"bash",
"beancount",
"bibtex",
"bicep",
"bitbake",
"c",
"cairo",
"capnp",
"chatito",
"clarity",
"clojure",
"cmake",
"comment",
"commonlisp",
"cpon",
"cpp",
"css",
"csv",
"cuda",
"d",
"dart",
"dockerfile",
"doxygen",
"elisp",
"elixir",
"elm",
"erlang",
"fennel",
"firrtl",
"fish",
"fortran",
"func",
"gdscript",
"gitattributes",
"gitcommit",
"gitignore",
"gleam",
"glsl",
"gn",
"go",
"gomod",
"gosum",
"groovy",
"gstlaunch",
"hack",
"hare",
"haskell",
"haxe",
"hcl",
"heex",
"hlsl",
"html",
"hyprlang",
"ispc",
"janet",
"java",
"javascript",
"jsdoc",
"json",
"jsonnet",
"julia",
"kconfig",
"kdl",
"kotlin",
"latex",
"linkerscript",
"llvm",
"lua",
"luadoc",
"luap",
"luau",
"magik",
"make",
"markdown",
"matlab",
"mermaid",
"meson",
"ninja",
"nix",
"nqc",
"objc",
"odin",
"org",
"pascal",
"pem",
"perl",
"pgn",
"po",
"pony",
"powershell",
"printf",
"prisma",
"properties",
"psv",
"puppet",
"purescript",
"pymanifest",
"python",
"qmldir",
"qmljs",
"query",
"r",
"racket",
"re2c",
"readline",
"requirements",
"ron",
"rst",
"ruby",
"rust",
"scala",
"scheme",
"scss",
"smali",
"smithy",
"solidity",
"sql",
"squirrel",
"starlark",
"svelte",
"swift",
"tablegen",
"tcl",
"terraform",
"test",
"thrift",
"toml",
"tsv",
"twig",
"typst",
"udev",
"ungrammar",
"uxntal",
"v",
"verilog",
"vhdl",
"vim",
"vue",
"wgsl",
"xcompose",
"yuck",
"zig",
],
InstalledBindings,
]
installed_bindings_map: dict[InstalledBindings, Callable[[], int]] = {
"csharp": c_sharp_language,
"dtd": dtd_language,
"embeddedtemplate": embedded_template_language,
"php": php_language,
"tsx": language_tsx,
"typescript": language_typescript,
"xml": xml_language,
"yaml": yaml_language,
}
def get_binding(language_name: SupportedLanguage) -> int:
"""Get the binding for the given language name.
Args:
language_name: The name of the language.
Raises:
LookupError: If the language is not found.
Returns:
int: The binding for the language.
"""
if language_name in installed_bindings_map:
return installed_bindings_map[cast(InstalledBindings, language_name)]()
try:
module = import_module(name=f".bindings.{language_name}", package=__package__)
return cast(int, module.language())
except ModuleNotFoundError as e:
raise LookupError(f"Language not found: {language_name}") from e
def get_language(language_name: SupportedLanguage) -> Language:
"""Get the language with the given name.
Args:
language_name: The name of the language.
Returns:
Language: The language as a tree-sitter Language instance.
"""
binding = get_binding(language_name)
return Language(binding)
def get_parser(language_name: SupportedLanguage) -> Parser:
"""Get a parser for the given language name.
Args:
language_name: The name of the language.
Returns:
Parser: The parser for the language as a tree-sitter Parser instance.
"""
return Parser(get_language(language_name=language_name))
__all__ = ["get_language", "get_parser", "SupportedLanguage"]