forked from microsoft/dpu-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keywordlist.py
46 lines (40 loc) · 1.56 KB
/
keywordlist.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
import os
import keyword
from functools import lru_cache
from typing import FrozenSet
__all__ = ['get_language_keywords']
def load_file(name: str) -> FrozenSet[str]:
with open(os.path.join(os.path.dirname(__file__), name)) as f:
return frozenset(l.strip() for l in f if len(l.strip()) > 0)
@lru_cache()
def get_language_keywords(language: str) -> FrozenSet[str]:
"""
Returns the keywords of a programming language.
There are some inconsistencies across languages wrt to
what is considered a keyword. For example, the true/false
literals are considered keywords in many languages. However,
we exclude them here for consistency. We also exclude special
functions-like keywords, such as `die()` in PHP.
"""
if language == 'c':
return load_file('c.txt')
elif language == 'cpp' or language == 'c++':
return load_file('cpp.txt')
elif language == 'csharp' or language == 'c#':
return load_file('csharp.txt')
elif language == 'go':
return load_file('go.txt')
elif language == 'java':
return load_file('java.txt')
elif language == 'javascript':
return load_file('javascript.txt')
elif language == 'php':
return load_file('php.txt')
elif language == 'python':
return frozenset(k for k in keyword.kwlist if k != 'True' and k != 'False')
elif language == 'ruby':
return load_file('ruby.txt')
elif language == 'typescript':
return load_file('typescript.txt')
else:
raise Exception('Language %s not supported yet' % language)