Skip to content

Commit e8f84a6

Browse files
Lisp: Improved defun (#3130)
1 parent 4dde2e2 commit e8f84a6

File tree

4 files changed

+323
-50
lines changed

4 files changed

+323
-50
lines changed

components/prism-lisp.js

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
(function (Prism) {
2-
// Functions to construct regular expressions
3-
// simple form
4-
// e.g. (interactive ... or (interactive)
2+
/**
3+
* Functions to construct regular expressions
4+
* e.g. (interactive ... or (interactive)
5+
*
6+
* @param {string} name
7+
* @returns {RegExp}
8+
*/
59
function simple_form(name) {
6-
return RegExp('(\\()' + name + '(?=[\\s\\)])');
10+
return RegExp(/(\()/.source + '(?:' + name + ')' + /(?=[\s\)])/.source);
711
}
8-
// booleans and numbers
12+
/**
13+
* booleans and numbers
14+
*
15+
* @param {string} pattern
16+
* @returns {RegExp}
17+
*/
918
function primitive(pattern) {
10-
return RegExp('([\\s([])' + pattern + '(?=[\\s)])');
19+
return RegExp(/([\s([])/.source + '(?:' + pattern + ')' + /(?=[\s)])/.source);
1120
}
1221

1322
// Patterns in regular expressions
1423

1524
// Symbol name. See https://www.gnu.org/software/emacs/manual/html_node/elisp/Symbol-Type.html
1625
// & and : are excluded as they are usually used for special purposes
17-
var symbol = '[-+*/~!@$%^=<>{}\\w]+';
26+
var symbol = /(?!\d)[-+*/~!@$%^=<>{}\w]+/.source;
1827
// symbol starting with & used in function arguments
1928
var marker = '&' + symbol;
2029
// Open parenthesis for look-behind
2130
var par = '(\\()';
2231
var endpar = '(?=\\))';
2332
// End the pattern with look-ahead space
2433
var space = '(?=\\s)';
34+
var nestedPar = /(?:[^()]|\((?:[^()]|\((?:[^()]|\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\))*\))*\))*/.source;
2535

2636
var language = {
2737
// Three or four semicolons are considered a heading.
@@ -68,21 +78,21 @@
6878
},
6979
],
7080
declare: {
71-
pattern: simple_form('declare'),
81+
pattern: simple_form(/declare/.source),
7282
lookbehind: true,
7383
alias: 'keyword'
7484
},
7585
interactive: {
76-
pattern: simple_form('interactive'),
86+
pattern: simple_form(/interactive/.source),
7787
lookbehind: true,
7888
alias: 'keyword'
7989
},
8090
boolean: {
81-
pattern: primitive('(?:t|nil)'),
91+
pattern: primitive(/nil|t/.source),
8292
lookbehind: true
8393
},
8494
number: {
85-
pattern: primitive('[-+]?\\d+(?:\\.\\d*)?'),
95+
pattern: primitive(/[-+]?\d+(?:\.\d*)?/.source),
8696
lookbehind: true
8797
},
8898
defvar: {
@@ -94,13 +104,9 @@
94104
}
95105
},
96106
defun: {
97-
pattern: RegExp(
98-
par +
99-
'(?:cl-)?(?:defmacro|defun\\*?)\\s+' +
100-
symbol +
101-
'\\s+\\([\\s\\S]*?\\)'
102-
),
107+
pattern: RegExp(par + /(?:cl-)?(?:defmacro|defun\*?)\s+/.source + symbol + /\s+\(/.source + nestedPar + /\)/.source),
103108
lookbehind: true,
109+
greedy: true,
104110
inside: {
105111
keyword: /^(?:cl-)?def\S+/,
106112
// See below, this property needs to be defined later so that it can
@@ -116,6 +122,7 @@
116122
lambda: {
117123
pattern: RegExp(par + 'lambda\\s+\\(\\s*(?:&?' + symbol + '(?:\\s+&?' + symbol + ')*\\s*)?\\)'),
118124
lookbehind: true,
125+
greedy: true,
119126
inside: {
120127
keyword: /^lambda/,
121128
// See below, this property needs to be defined later so that it can
@@ -141,29 +148,22 @@
141148

142149
var arg = {
143150
'lisp-marker': RegExp(marker),
144-
rest: {
145-
argument: {
146-
pattern: RegExp(symbol),
147-
alias: 'variable'
148-
},
149-
varform: {
150-
pattern: RegExp(par + symbol + '\\s+\\S[\\s\\S]*' + endpar),
151-
lookbehind: true,
152-
inside: {
153-
string: language.string,
154-
boolean: language.boolean,
155-
number: language.number,
156-
symbol: language.symbol,
157-
punctuation: /[()]/
158-
}
159-
}
160-
}
151+
'varform': {
152+
pattern: RegExp(/\(/.source + symbol + /\s+(?=\S)/.source + nestedPar + /\)/.source),
153+
inside: language
154+
},
155+
'argument': {
156+
pattern: RegExp(/(^|[\s(])/.source + symbol),
157+
lookbehind: true,
158+
alias: 'variable'
159+
},
160+
rest: language
161161
};
162162

163163
var forms = '\\S+(?:\\s+\\S+)*';
164164

165165
var arglist = {
166-
pattern: RegExp(par + '[\\s\\S]*' + endpar),
166+
pattern: RegExp(par + nestedPar + endpar),
167167
lookbehind: true,
168168
inside: {
169169
'rest-vars': {

components/prism-lisp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)