-
Notifications
You must be signed in to change notification settings - Fork 108
/
lang.go
366 lines (328 loc) · 7.58 KB
/
lang.go
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-License-Identifier: MIT
// Package lang 管理各类语言提取注释代码块规则的定义
package lang
import "fmt"
// 所有支持的语言模型定义
var langs = []*Language{
{
DisplayName: "C#",
ID: "c#",
Exts: []string{".cs"},
blocks: cStyle,
},
{
DisplayName: "C/C++",
ID: "c++",
Exts: []string{".h", ".c", ".cpp", ".cxx", ".hpp"},
blocks: cStyle,
},
{
DisplayName: "D",
ID: "d",
Exts: []string{".d"},
blocks: cStyle,
},
{
DisplayName: "Dart",
ID: "dart",
Exts: []string{".dart"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newString("r'", "'", ``),
newString("r\"", "\"", ``),
newString("'''", "'''", ``),
newString(`"""`, `"""`, ``),
newSingleComment(`///`),
newSwiftNestMCommentBlock("/**", "*/", "*"),
newCStyleSingleComment(),
newCStyleMultipleComment(),
},
},
{
DisplayName: "Erlang",
ID: "erlang",
Exts: []string{".erl", ".hrl"},
blocks: []blocker{
newCStyleString(),
newSingleComment("%"),
},
},
{
DisplayName: "Go",
ID: "go",
Exts: []string{".go"},
blocks: []blocker{
newCStyleString(),
newString("`", "`", ""),
newCStyleChar(),
newCStyleSingleComment(),
newCStyleMultipleComment(),
},
},
{
DisplayName: "Groovy",
ID: "groovy",
Exts: []string{".groovy"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newString("'''", "'''", `\`),
newCStyleSingleComment(),
newCStyleMultipleComment(),
},
},
{
DisplayName: "Java",
ID: "java",
Exts: []string{".java"},
blocks: cStyle,
},
{
DisplayName: "JavaScript",
ID: "javascript",
Exts: []string{".js"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newString("`", "`", `\`),
newCStyleSingleComment(),
newCStyleMultipleComment(),
// NOTE: js 中若出现 /*abc/.test() 应该是先优先注释的。放最后,优先匹配 // 和 /*
newString("/", "/", `\`),
},
},
{
DisplayName: "Julia",
ID: "julia",
Exts: []string{".jl"},
blocks: []blocker{
newString("'", "'", `\`),
newString(`r"`, `"`, ""),
newString(`b"`, `"`, ""),
newString(`v"`, `"`, ""),
newString(`raw"`, `"`, ""),
newString(`"""`, `"""`, ``),
newString(`"`, `"`, `\`),
newMultipleComment("#=", "=#", "#="),
newSingleComment("#"),
},
},
{
DisplayName: "Kotlin",
ID: "kotlin",
Exts: []string{".kt"},
blocks: cStyle,
},
{
DisplayName: "Lisp/Clojure",
ID: "lisp",
// .ss,.scm ==> scheme
// .clj ==> Clojure
Exts: []string{".lisp", ".lsp", ".l", ".ss", ".scm", ".clj"},
blocks: []blocker{
newString(`"`, `"`, `\`), // #"" 为正则表达式
newSingleComment(";;;;"),
newSingleComment(";;;"),
newSingleComment(";;"),
newSingleComment(";"),
},
},
{
DisplayName: "Lua",
ID: "lua",
Exts: []string{".lua"},
blocks: []blocker{
newString("'", "'", `\`),
newString("\"", "\"", `\`),
newString("[[", "]]", ``),
newMultipleComment("--[====[", "]====]", "-="),
newMultipleComment("--[[", "]]", "-="),
newSingleComment("--"), // 放在 --[[ 之后,否则会把 --[[ 当作 -- 解析
},
},
{
DisplayName: "Nim",
ID: "nim",
Exts: []string{".nim"},
blocks: []blocker{
newString("'", "'", "\\"),
newString(`"`, `"`, "\\"),
newNimRawString(),
newNimMultipleString(),
newSwiftNestMCommentBlock("##[", "]##", "#"),
newSwiftNestMCommentBlock("#[", "]#", "#"),
newSingleComment("#"),
},
},
{
DisplayName: "Pascal/Delphi",
ID: "pascal",
Exts: []string{".pas", ".pp"},
blocks: []blocker{
newPascalStringBlock('\''),
newPascalStringBlock('"'),
newMultipleComment("{", "}", ""),
newMultipleComment("(*", "*)", "*"),
},
},
{
DisplayName: "Perl",
ID: "perl",
Exts: []string{".perl", ".prl", ".pl"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newSingleComment("#"),
newRubyMultipleComment("=pod", "=cut", ""),
},
},
{
DisplayName: "PHP",
ID: "php",
Exts: []string{".php"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newPHPDocBlock(),
newCStyleSingleComment(),
newSingleComment("#"),
newCStyleMultipleComment(),
},
},
{
DisplayName: "Python",
ID: "python",
Exts: []string{".py"},
blocks: []blocker{
newMultipleComment(`"""`, `"""`, ""),
newMultipleComment("'''", "'''", ""),
newCStyleString(),
newSingleComment(`#`),
},
},
{
DisplayName: "Ruby",
ID: "ruby",
Exts: []string{".rb"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newSingleComment(`#`),
newRubyMultipleComment("=begin", "=end", ""),
},
},
{
DisplayName: "Rust",
ID: "rust",
Exts: []string{".rs"},
blocks: cStyle,
},
{
DisplayName: "Scala",
ID: "scala",
Exts: []string{".scala"},
blocks: cStyle,
},
{
DisplayName: "Swift",
ID: "swift",
Exts: []string{".swift"},
blocks: []blocker{
newString(`"""`, `"""`, `\`),
newString(`#"`, `"#`, ""),
newCStyleString(),
newCStyleChar(),
newCStyleSingleComment(),
newSwiftNestMCommentBlock("/*", "*/", "*"),
},
},
{
DisplayName: "TypeScript",
ID: "typescript",
Exts: []string{".ts"},
blocks: []blocker{
newCStyleString(),
newString("'", "'", `\`),
newString("`", "`", `\`),
newCStyleSingleComment(),
newCStyleMultipleComment(),
// NOTE: js 中若出现 /*abc/.test() 应该是先优先注释的。放最后,优先匹配 // 和 /*
newString("/", "/", `\`),
},
},
{
DisplayName: "Zig",
ID: "zig",
Exts: []string{".zig"},
blocks: []blocker{
newCStyleString(),
newCStyleChar(),
newSingleComment("///"), // 需要在 // 之前定义
newSingleComment("//!"), // 需要在 // 之前定义
newCStyleSingleComment(),
},
},
}
var cStyle = []blocker{
newCStyleString(),
newCStyleChar(),
newSingleComment("///"), // 需要在 // 之前定义
newCStyleSingleComment(),
newCStyleMultipleComment(),
}
// 处理 "XXX\""
func newCStyleString() blocker {
return newString(`"`, `"`, `\`)
}
// 处理 '"'
func newCStyleChar() blocker {
return newString(`'`, `'`, "")
}
// 处理 // xxx
func newCStyleSingleComment() blocker {
return newSingleComment(`//`)
}
// 处理 /* */
func newCStyleMultipleComment() blocker {
return newMultipleComment(`/*`, `*/`, "*")
}
// Language 语言模块的定义
type Language struct {
DisplayName string // 显示友好的名称
ID string // 语言唯一名称,一律小写
blocks []blocker // 注释块的解析规则定义
Exts []string // 扩展名列表,必须以 . 开头且小写
}
// Get 获取指定语言的定义信息
//
// 若不存在,则返回 nil
func Get(id string) *Language {
for _, lang := range langs {
if lang.ID == id {
return lang
}
}
return nil
}
// GetByExt 根据扩展名获取语言定义信息
//
// ext 必须以 . 作为开头
// 若不存在,则返回 nil
func GetByExt(ext string) *Language {
if len(ext) == 0 || ext[0] != '.' {
panic(fmt.Sprintf("参数 ext 的值 [%s] 不能为空,且必须以 . 作为开头", ext))
}
for _, lang := range langs {
for _, e := range lang.Exts {
if e == ext {
return lang
}
}
}
return nil
}
// Langs 返回所有支持的语言
func Langs() []*Language {
return langs
}