Repository navigation
Expand file tree
/
Copy pathlangs.py
More file actions
182 lines (159 loc) · 4.92 KB
/
Copy pathlangs.py
File metadata and controls
182 lines (159 loc) · 4.92 KB
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
# Doc : Description
# lists every supported language and casts each file extension
# to a general comment family
from dataclasses import dataclass
# Doc code : MultCommentPair
# start and close delimiters of a multiline comment pair
@dataclass
class MultCommentPair:
start: str | None
close: str | None
#Doc end
#Doc: Logic of the dictionaries
# the main idea is that we have dictionaries that point
# to the way comments start for the programming languages
# and appoint a leader (the standard they follow) to access those
# we do the same for multiline comments and then we have 2 more
# dictionaries to convert file extensions to the appropriate
# comment family the language follows so they can get access
# to the syntax of the comments for example:
# .cpp, .c, .rs map to the c-family comments (c key) and
# we use the key to access the comment syntax c maps to // or /* */
# Doc : comment families and conversion tables
# single_comments holds the keys that lead to the right syntax for single line comments
# mult_comments does the same but for multiline comments
# conv_table_single converts different filetypes to comment families for single line comments
# conv_table_mult does the same as conv_table_single but for multi line comments
# some languages map to different multiline comments and single line comments or don't have them
# NOTE: these are the major single line comment families we support
single_comments = {
"c": "//",
"python": "#",
"lua": "--",
"assembly": ";",
"visual_basic": "'",
"latex": "%",
}
# NOTE: these are the major multi line comment families we support
mult_comments = {
"c": MultCommentPair(start="/*", close="*/"),
"python": MultCommentPair(start='"""', close='"""'),
"lua": MultCommentPair(start="--[[", close="]]"),
"haskell": MultCommentPair(start="{-", close="-}"),
"html": MultCommentPair(start="<!--", close="-->"),
"pascal": MultCommentPair(start="(*", close="*)"),
"matlab": MultCommentPair(start="%{", close="}%"),
}
# NOTE: the dictionary we use to convert a file to a file type that we use as a key for single line comments
conv_table_single = {
# c family
**dict.fromkeys(
[
".c",
".h",
".cpp",
".cc",
".cxx",
".hpp",
".hh",
".cs",
".java",
".js",
".jsx",
".ts",
".tsx",
".go",
".rs",
".d",
".swift",
".kt",
".kts",
".php",
".dart",
".scala",
".pas",
".pp",
".inc",
".mm",
".fs",
".zig",
],
"c",
),
# python family
**dict.fromkeys([".py", ".pyw", ".pyi", ".erlang"], "python"),
# lua family
**dict.fromkeys([".lua", ".hs"], "lua"),
# asm family
**dict.fromkeys([".asm", ".s"], "assembly"),
# visual basic
**dict.fromkeys([".vb", ".vbs", ".bas"], "visual_basic"),
# latex
**dict.fromkeys([".tex", ".sty", ".cls", ".m"], "latex"),
}
# NOTE: the dictionary we use to convert a file to a file type that we use as a key for multi line comments
conv_table_mult = {
**dict.fromkeys(
[
".c",
".h",
".cpp",
".cc",
".cxx",
".hpp",
".hh",
".cs",
".java",
".js",
".jsx",
".ts",
".tsx",
".go",
".rs",
".d",
".swift",
".kt",
".kts",
".php",
".dart",
".scala",
".mm",
],
"c",
),
# python
**dict.fromkeys([".py", ".pyw", ".pyi"], "python"),
# lua
**dict.fromkeys([".lua"], "lua"),
# haskell
**dict.fromkeys([".hs"], "haskell"),
# html
**dict.fromkeys([".html", ".htm", ".xhtml", ".xml"], "html"),
# pascal
**dict.fromkeys([".pas", ".pp", ".inc", ".fs"], "pascal"),
# matlab has different multiline comments
".m": "matlab",
}
# Doc : CommentFam
# the single and multiline comment families of a language
# used in GetCommentFamily to return the comment syntax
# to parser.py so that it can use regex operations to parse
# the files
@dataclass
class CommentFam:
single_line: str | None
mult_pair: MultCommentPair | None
# Doc code : GetCommentFamily
# looks up the comment families of a file extension in the
# conversion tables, can return None, None for unknown extensions
def GetCommentFamily(type: str) -> CommentFam:
single = None
mult = None
fam_single = conv_table_single.get(type)
if fam_single is not None:
single = single_comments.get(fam_single)
fam_mult = conv_table_mult.get(type)
if fam_mult is not None:
mult = mult_comments.get(fam_mult)
return CommentFam(single, mult)
# Doc end