Skip to content
Permalink
Browse files
Lisp: Improved defun (#3130)
  • Loading branch information
RunDevelopment committed Oct 5, 2021
1 parent 4dde2e2 commit e8f84a6c85a4c3c6c007654503c5d650858807c5
Showing with 323 additions and 50 deletions.
  1. +35 −35 components/prism-lisp.js
  2. +1 −1 components/prism-lisp.min.js
  3. +280 −10 tests/languages/lisp/defun_feature.test
  4. +7 −4 tests/languages/lisp/punctuation_feature.test
@@ -1,27 +1,37 @@
(function (Prism) {
// Functions to construct regular expressions
// simple form
// e.g. (interactive ... or (interactive)
/**
* Functions to construct regular expressions
* e.g. (interactive ... or (interactive)
*
* @param {string} name
* @returns {RegExp}
*/
function simple_form(name) {
return RegExp('(\\()' + name + '(?=[\\s\\)])');
return RegExp(/(\()/.source + '(?:' + name + ')' + /(?=[\s\)])/.source);
}
// booleans and numbers
/**
* booleans and numbers
*
* @param {string} pattern
* @returns {RegExp}
*/
function primitive(pattern) {
return RegExp('([\\s([])' + pattern + '(?=[\\s)])');
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 = '[-+*/~!@$%^=<>{}\\w]+';
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.
@@ -68,21 +78,21 @@
},
],
declare: {
pattern: simple_form('declare'),
pattern: simple_form(/declare/.source),
lookbehind: true,
alias: 'keyword'
},
interactive: {
pattern: simple_form('interactive'),
pattern: simple_form(/interactive/.source),
lookbehind: true,
alias: 'keyword'
},
boolean: {
pattern: primitive('(?:t|nil)'),
pattern: primitive(/nil|t/.source),
lookbehind: true
},
number: {
pattern: primitive('[-+]?\\d+(?:\\.\\d*)?'),
pattern: primitive(/[-+]?\d+(?:\.\d*)?/.source),
lookbehind: true
},
defvar: {
@@ -94,13 +104,9 @@
}
},
defun: {
pattern: RegExp(
par +
'(?:cl-)?(?:defmacro|defun\\*?)\\s+' +
symbol +
'\\s+\\([\\s\\S]*?\\)'
),
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
@@ -116,6 +122,7 @@
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
@@ -141,29 +148,22 @@

var arg = {
'lisp-marker': RegExp(marker),
rest: {
argument: {
pattern: RegExp(symbol),
alias: 'variable'
},
varform: {
pattern: RegExp(par + symbol + '\\s+\\S[\\s\\S]*' + endpar),
lookbehind: true,
inside: {
string: language.string,
boolean: language.boolean,
number: language.number,
symbol: language.symbol,
punctuation: /[()]/
}
}
}
'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 + '[\\s\\S]*' + endpar),
pattern: RegExp(par + nestedPar + endpar),
lookbehind: true,
inside: {
'rest-vars': {

Some generated files are not rendered by default. Learn more.

0 comments on commit e8f84a6

Please sign in to comment.