1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 13:29:56 +00:00

Add utility function for parsing macro parameter definitions

This commit is contained in:
jeremy@jermolene.com 2022-05-27 18:37:42 +01:00
parent 7fc65d0d1a
commit a2fbebf509
3 changed files with 39 additions and 28 deletions

View File

@ -123,6 +123,36 @@ exports.parseStringLiteral = function(source,pos) {
} }
}; };
/*
Returns an array of {name:} with an optional "default" property. Options include:
requireParenthesis: require the parameter definition to be wrapped in parenthesis
*/
exports.parseParameterDefinition = function(paramString,options) {
options = options || {};
if(options.requireParenthesis) {
var parenMatch = /^\s*\((.*)\)\s*$/g.exec(paramString);
if(!parenMatch) {
return [];
}
paramString = parenMatch[1];
}
var params = [],
reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg,
paramMatch = reParam.exec(paramString);
while(paramMatch) {
// Save the parameter details
var paramInfo = {name: paramMatch[1]},
defaultValue = paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5];
if(defaultValue !== undefined) {
paramInfo["default"] = defaultValue;
}
params.push(paramInfo);
// Look for the next parameter
paramMatch = reParam.exec(paramString);
}
return params;
};
exports.parseMacroParameters = function(node,source,pos) { exports.parseMacroParameters = function(node,source,pos) {
// Process parameters // Process parameters
var parameter = $tw.utils.parseMacroParameter(source,pos); var parameter = $tw.utils.parseMacroParameter(source,pos);

View File

@ -45,22 +45,9 @@ exports.parse = function() {
// Move past the macro name and parameters // Move past the macro name and parameters
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.matchRegExp.lastIndex;
// Parse the parameters // Parse the parameters
var paramString = this.match[4], var params = [];
params = [];
if(this.match[3]) { if(this.match[3]) {
var reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg, params = $tw.utils.parseParameterDefinition(this.match[4]);
paramMatch = reParam.exec(paramString);
while(paramMatch) {
// Save the parameter details
var paramInfo = {name: paramMatch[1]},
defaultValue = paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5];
if(defaultValue !== undefined) {
paramInfo["default"] = defaultValue;
}
params.push(paramInfo);
// Look for the next parameter
paramMatch = reParam.exec(paramString);
}
} }
// Is this a multiline definition? // Is this a multiline definition?
var reEnd; var reEnd;

View File

@ -36,20 +36,14 @@ exports.parse = function() {
// Move past the macro name and parameters // Move past the macro name and parameters
this.parser.pos = this.matchRegExp.lastIndex; this.parser.pos = this.matchRegExp.lastIndex;
// Parse the parameters // Parse the parameters
var paramString = this.match[1], var params = $tw.utils.parseParameterDefinition(this.match[1]);
attributes = Object.create(null), var attributes = Object.create(null),
orderedAttributes = [], orderedAttributes = [];
reParam = /\s*([^:),\s]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^,"'\s]+)))?/mg, $tw.utils.each(params,function(param) {
paramMatch = reParam.exec(paramString); var attribute = {name: param.name, type: "string", value: param["default"] || ""};
while(paramMatch) { attributes[param.name] = attribute;
// Save the parameter details
var name = paramMatch[1],
attribute = {name: name, type: "string", value: paramMatch[2] || paramMatch[3] || paramMatch[4] || paramMatch[5]};
attributes[name] = attribute;
orderedAttributes.push(attribute); orderedAttributes.push(attribute);
// Look for the next parameter });
paramMatch = reParam.exec(paramString);
}
// Save the macro definition // Save the macro definition
return [{ return [{
type: "parameters", type: "parameters",