-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
lisp.js
220 lines (217 loc) · 6.02 KB
/
lisp.js
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
// @ts-nocheck
lisp.displayName = 'lisp'
lisp.aliases = ['elisp', 'emacs', 'emacs-lisp']
/** @type {import('../core.js').Syntax} */
export default function lisp(Prism) {
;(function (Prism) {
/**
* Functions to construct regular expressions
* e.g. (interactive ... or (interactive)
*
* @param {string} name
* @returns {RegExp}
*/
function simple_form(name) {
return RegExp(/(\()/.source + '(?:' + name + ')' + /(?=[\s\)])/.source)
}
/**
* booleans and numbers
*
* @param {string} pattern
* @returns {RegExp}
*/
function primitive(pattern) {
return RegExp(
/([\s([])/.source + '(?:' + pattern + ')' + /(?=[\s)])/.source
)
}
// Patterns in regular expressions
// Symbol name. See https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
// & and : are excluded as they are usually used for special purposes
var symbol = /(?!\d)[-+*/~!@$%^=<>{}\w]+/.source
// symbol starting with & used in function arguments
var marker = '&' + symbol
// Open parenthesis for look-behind
var par = '(\\()'
var endpar = '(?=\\))'
// End the pattern with look-ahead space
var space = '(?=\\s)'
var nestedPar =
/(?:[^()]|\((?:[^()]|\((?:[^()]|\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\))*\))*\))*/
.source
var language = {
// Three or four semicolons are considered a heading.
// See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html
heading: {
pattern: /;;;.*/,
alias: ['comment', 'title']
},
comment: /;.*/,
string: {
pattern: /"(?:[^"\\]|\\.)*"/,
greedy: true,
inside: {
argument: /[-A-Z]+(?=[.,\s])/,
symbol: RegExp('`' + symbol + "'")
}
},
'quoted-symbol': {
pattern: RegExp("#?'" + symbol),
alias: ['variable', 'symbol']
},
'lisp-property': {
pattern: RegExp(':' + symbol),
alias: 'property'
},
splice: {
pattern: RegExp(',@?' + symbol),
alias: ['symbol', 'variable']
},
keyword: [
{
pattern: RegExp(
par +
'(?:and|(?:cl-)?letf|cl-loop|cond|cons|error|if|(?:lexical-)?let\\*?|message|not|null|or|provide|require|setq|unless|use-package|when|while)' +
space
),
lookbehind: true
},
{
pattern: RegExp(
par +
'(?:append|by|collect|concat|do|finally|for|in|return)' +
space
),
lookbehind: true
}
],
declare: {
pattern: simple_form(/declare/.source),
lookbehind: true,
alias: 'keyword'
},
interactive: {
pattern: simple_form(/interactive/.source),
lookbehind: true,
alias: 'keyword'
},
boolean: {
pattern: primitive(/nil|t/.source),
lookbehind: true
},
number: {
pattern: primitive(/[-+]?\d+(?:\.\d*)?/.source),
lookbehind: true
},
defvar: {
pattern: RegExp(par + 'def(?:const|custom|group|var)\\s+' + symbol),
lookbehind: true,
inside: {
keyword: /^def[a-z]+/,
variable: RegExp(symbol)
}
},
defun: {
pattern: RegExp(
par +
/(?:cl-)?(?:defmacro|defun\*?)\s+/.source +
symbol +
/\s+\(/.source +
nestedPar +
/\)/.source
),
lookbehind: true,
greedy: true,
inside: {
keyword: /^(?:cl-)?def\S+/,
// See below, this property needs to be defined later so that it can
// reference the language object.
arguments: null,
function: {
pattern: RegExp('(^\\s)' + symbol),
lookbehind: true
},
punctuation: /[()]/
}
},
lambda: {
pattern: RegExp(
par +
'lambda\\s+\\(\\s*(?:&?' +
symbol +
'(?:\\s+&?' +
symbol +
')*\\s*)?\\)'
),
lookbehind: true,
greedy: true,
inside: {
keyword: /^lambda/,
// See below, this property needs to be defined later so that it can
// reference the language object.
arguments: null,
punctuation: /[()]/
}
},
car: {
pattern: RegExp(par + symbol),
lookbehind: true
},
punctuation: [
// open paren, brackets, and close paren
/(?:['`,]?\(|[)\[\]])/,
// cons
{
pattern: /(\s)\.(?=\s)/,
lookbehind: true
}
]
}
var arg = {
'lisp-marker': RegExp(marker),
varform: {
pattern: RegExp(
/\(/.source + symbol + /\s+(?=\S)/.source + nestedPar + /\)/.source
),
inside: language
},
argument: {
pattern: RegExp(/(^|[\s(])/.source + symbol),
lookbehind: true,
alias: 'variable'
},
rest: language
}
var forms = '\\S+(?:\\s+\\S+)*'
var arglist = {
pattern: RegExp(par + nestedPar + endpar),
lookbehind: true,
inside: {
'rest-vars': {
pattern: RegExp('&(?:body|rest)\\s+' + forms),
inside: arg
},
'other-marker-vars': {
pattern: RegExp('&(?:aux|optional)\\s+' + forms),
inside: arg
},
keys: {
pattern: RegExp('&key\\s+' + forms + '(?:\\s+&allow-other-keys)?'),
inside: arg
},
argument: {
pattern: RegExp(symbol),
alias: 'variable'
},
punctuation: /[()]/
}
}
language['lambda'].inside.arguments = arglist
language['defun'].inside.arguments = Prism.util.clone(arglist)
language['defun'].inside.arguments.inside.sublist = arglist
Prism.languages.lisp = language
Prism.languages.elisp = language
Prism.languages.emacs = language
Prism.languages['emacs-lisp'] = language
})(Prism)
}