2022-04-30 11:44:26 +00:00
|
|
|
/*\
|
2022-05-09 17:00:09 +00:00
|
|
|
title: $:/core/modules/parsers/wikiparser/rules/fnprocdef.js
|
2022-04-30 11:44:26 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: wikirule
|
|
|
|
|
2022-05-13 07:49:53 +00:00
|
|
|
Wiki pragma rule for function, procedure and widget definitions
|
2022-04-30 11:44:26 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
\function name(param:defaultvalue,param2:defaultvalue)
|
|
|
|
definition text
|
|
|
|
\end
|
2022-05-09 17:00:09 +00:00
|
|
|
|
|
|
|
\procedure name(param:defaultvalue,param2:defaultvalue)
|
|
|
|
definition text
|
|
|
|
\end
|
2022-05-13 07:49:53 +00:00
|
|
|
|
|
|
|
\widget $mywidget(param:defaultvalue,param2:defaultvalue)
|
|
|
|
definition text
|
|
|
|
\end
|
2022-04-30 11:44:26 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2022-05-09 17:00:09 +00:00
|
|
|
exports.name = "fnprocdef";
|
2022-04-30 11:44:26 +00:00
|
|
|
exports.types = {pragma: true};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Instantiate parse rule
|
|
|
|
*/
|
|
|
|
exports.init = function(parser) {
|
|
|
|
this.parser = parser;
|
|
|
|
// Regexp to match
|
2022-05-28 12:49:02 +00:00
|
|
|
this.matchRegExp = /^\\(\??)(function|procedure|widget)\s+([^(\s]+)(\(\s*([^)]*)\))?(\s*\r?\n)?/mg;
|
2022-04-30 11:44:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parse the most recent match
|
|
|
|
*/
|
|
|
|
exports.parse = function() {
|
|
|
|
// Move past the macro name and parameters
|
|
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
|
|
|
// Parse the parameters
|
2022-05-27 17:37:42 +00:00
|
|
|
var params = [];
|
2022-05-28 11:23:50 +00:00
|
|
|
if(this.match[4]) {
|
|
|
|
params = $tw.utils.parseParameterDefinition(this.match[5]);
|
2022-04-30 11:44:26 +00:00
|
|
|
}
|
|
|
|
// Is this a multiline definition?
|
|
|
|
var reEnd;
|
2022-05-28 11:23:50 +00:00
|
|
|
if(this.match[6]) {
|
2022-04-30 11:44:26 +00:00
|
|
|
// If so, the end of the body is marked with \end
|
|
|
|
reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg;
|
|
|
|
} else {
|
|
|
|
// Otherwise, the end of the definition is marked by the end of the line
|
|
|
|
reEnd = /($|\r?\n)/mg;
|
|
|
|
// Move past any whitespace
|
|
|
|
this.parser.pos = $tw.utils.skipWhiteSpace(this.parser.source,this.parser.pos);
|
|
|
|
}
|
|
|
|
// Find the end of the definition
|
|
|
|
reEnd.lastIndex = this.parser.pos;
|
|
|
|
var text,
|
|
|
|
endMatch = reEnd.exec(this.parser.source);
|
|
|
|
if(endMatch) {
|
|
|
|
text = this.parser.source.substring(this.parser.pos,endMatch.index);
|
|
|
|
this.parser.pos = endMatch.index + endMatch[0].length;
|
|
|
|
} else {
|
|
|
|
// We didn't find the end of the definition, so we'll make it blank
|
|
|
|
text = "";
|
|
|
|
}
|
|
|
|
// Save the macro definition
|
2022-05-09 17:00:09 +00:00
|
|
|
var parseTreeNodes = [{
|
2022-04-30 11:44:26 +00:00
|
|
|
type: "set",
|
2022-05-28 16:32:09 +00:00
|
|
|
attributes: {},
|
2022-04-30 11:44:26 +00:00
|
|
|
children: [],
|
2022-05-09 17:00:09 +00:00
|
|
|
params: params
|
2022-04-30 11:44:26 +00:00
|
|
|
}];
|
2022-05-28 16:32:09 +00:00
|
|
|
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"name",this.match[3]);
|
|
|
|
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"value",text);
|
2022-05-28 11:23:50 +00:00
|
|
|
if(this.match[2] === "function") {
|
2022-05-09 17:00:09 +00:00
|
|
|
parseTreeNodes[0].isFunctionDefinition = true;
|
2022-05-28 11:23:50 +00:00
|
|
|
} else if(this.match[2] === "procedure") {
|
2022-05-09 17:00:09 +00:00
|
|
|
parseTreeNodes[0].isProcedureDefinition = true;
|
2022-05-28 11:23:50 +00:00
|
|
|
} else if(this.match[2] === "widget") {
|
2022-05-13 07:49:53 +00:00
|
|
|
parseTreeNodes[0].isWidgetDefinition = true;
|
2022-05-09 17:00:09 +00:00
|
|
|
}
|
2022-05-23 14:30:33 +00:00
|
|
|
if(this.parser.configTrimWhiteSpace) {
|
|
|
|
parseTreeNodes[0].configTrimWhiteSpace = true;
|
|
|
|
}
|
2022-05-28 12:49:02 +00:00
|
|
|
if(this.match[1] === "?") {
|
2022-05-28 11:23:50 +00:00
|
|
|
$tw.utils.addAttributeToParseTreeNode(parseTreeNodes[0],"conditional","yes");
|
|
|
|
}
|
2022-05-09 17:00:09 +00:00
|
|
|
return parseTreeNodes;
|
2022-04-30 11:44:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|