forked from caixw/apidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.go
184 lines (159 loc) · 5.05 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
// Copyright 2016 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package input
import (
"sort"
"strings"
)
// 所有支持的语言模型定义
//
// NOTE: 应该保持键名为非大写,按字母顺序排列,方便查找。
// langs 应该和 langExts 保持一一对应关系。
var langs = map[string][]blocker{
// C#
"c#": cStyle,
// c/c++
"c++": cStyle,
// d
"d": cStyle,
// erlang
"erlang": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeSComment, Begin: `%`},
},
// go
"go": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "`", End: "`"},
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
},
// groovy
"groovy": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "'", End: "'", Escape: `\`},
&block{Type: blockTypeString, Begin: "'''", End: "'''", Escape: `\`},
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
},
// java
"java": cStyle,
// javascript
"javascript": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "'", End: "'", Escape: `\`},
&block{Type: blockTypeString, Begin: "`", End: "`", Escape: `\`},
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
// NOTE: js 中若出现 /*abc/.test() 应该是先优先注释的。放最后,优先匹配 // 和 /*
&block{Type: blockTypeString, Begin: "/", End: "/", Escape: `\`}, // 正则表达式
},
// pascal/delphi
"pascal": {
newPascalStringBlock('\''),
newPascalStringBlock('"'),
&block{Type: blockTypeMComment, Begin: "{", End: "}"},
&block{Type: blockTypeMComment, Begin: "(*", End: "*)"},
},
// perl
"perl": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "'", End: "'", Escape: `\`},
&block{Type: blockTypeSComment, Begin: `#`},
&block{Type: blockTypeMComment, Begin: "\n=pod\n", End: "\n=cut\n"},
},
// python
"python": {
&block{Type: blockTypeMComment, Begin: `"""`, End: `"""`},
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeSComment, Begin: `#`},
},
// php
"php": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "'", End: "'", Escape: `\`},
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
},
// ruby
"ruby": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeString, Begin: "'", End: "'", Escape: `\`},
&block{Type: blockTypeSComment, Begin: `#`},
&block{Type: blockTypeMComment, Begin: "\n=begin\n", End: "\n=end\n"},
},
// rust
"rust": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeSComment, Begin: `///`}, // 需要在 // 之前定义
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
},
// scala
"scala": cStyle,
// swift
"swift": {
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeSComment, Begin: `//`},
newSwiftNestMCommentBlock("/*", "*/"),
},
}
var cStyle = []blocker{
&block{Type: blockTypeString, Begin: `"`, End: `"`, Escape: `\`},
&block{Type: blockTypeSComment, Begin: `//`},
&block{Type: blockTypeMComment, Begin: `/*`, End: `*/`},
}
// 各语言默认支持的文件扩展名。
//
// NOTE: 键名与 langs 中的键一一对应。
// 键值为可能的文件扩展名列表,应该使用非大写状态。
// `go test` 会作大量名称是否规范的检测。
var langExts = map[string][]string{
"c#": {".cs"},
"c++": {".h", ".c", ".cpp", ".cxx", ".hpp"},
"d": {".d"},
"erlang": {".erl", ".hrl"},
"go": {".go"},
"groovy": {".groovy"},
"java": {".java"},
"javascript": {".js"},
"pascal": {".pas", ".pp"},
"perl": {".perl", ".prl", ".pl"},
"php": {".php"},
"python": {".py"},
"ruby": {".rb"},
"rust": {".rs"},
"scala": {".scala"},
"swift": {".swift"},
}
// Languages 返回所有支持的语言
func Languages() []string {
ret := make([]string, 0, len(langs))
for l := range langs {
ret = append(ret, l)
}
sort.SliceStable(ret, func(i, j int) bool {
return ret[i] > ret[j]
})
return ret
}
// 根据扩展名获取其对应的语言名称。
// 若返回空值,则表示没有找到对应的。
func getLangByExt(ext string) string {
ext = strings.ToLower(ext)
for lang, exts := range langExts {
for _, elem := range exts {
if elem == ext {
return lang
}
}
}
return ""
}
// 是否支持该语言
func langIsSupported(lang string) bool {
// 由测试函数保证 langs 和 langExts 拥有相同的键名。
_, found := langs[lang]
return found
}