1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-01 09:43:16 +00:00

Allow brackets to be omitted for function definitions with no parameters

This commit is contained in:
jeremy@jermolene.com 2022-04-30 13:06:28 +01:00
parent c9bd1b5274
commit 99750d78b5
2 changed files with 21 additions and 5 deletions

View File

@ -27,7 +27,7 @@ Instantiate parse rule
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
this.matchRegExp = /^\\function\s+([^(\s]+)\(\s*([^)]*)\)(\s*\r?\n)?/mg;
this.matchRegExp = /^\\function\s+([^(\s]+)(\(\s*([^)]*)\))?(\s*\r?\n)?/mg;
};
/*
@ -37,9 +37,9 @@ exports.parse = function() {
// Move past the macro name and parameters
this.parser.pos = this.matchRegExp.lastIndex;
// Parse the parameters
var paramString = this.match[2],
var paramString = this.match[3],
params = [];
if(paramString !== "") {
if(this.match[2]) {
var reParam = /\s*([A-Za-z0-9\-_]+)(?:\s*:\s*(?:"""([\s\S]*?)"""|"([^"]*)"|'([^']*)'|([^"'\s]+)))?/mg,
paramMatch = reParam.exec(paramString);
while(paramMatch) {
@ -56,7 +56,7 @@ exports.parse = function() {
}
// Is this a multiline definition?
var reEnd;
if(this.match[3]) {
if(this.match[4]) {
// If so, the end of the body is marked with \end
reEnd = /(\r?\n\\end[^\S\n\r]*(?:$|\r?\n))/mg;
} else {

View File

@ -119,7 +119,23 @@ describe("WikiText parser tests", function() {
);
});
it("should parse function definitions", function() {
it("should parse function definitions with no parameters", function() {
expect(parse("\\function myMacro\nnothing\n\\end\n")).toEqual(
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ ], isFunctionDefinition : true } ]
);
});
it("should parse single line function definitions with no parameters", function() {
expect(parse("\\function myMacro nothing\n")).toEqual(
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ ], isFunctionDefinition : true } ]
);
});
it("should parse function definitions with parameters", function() {
expect(parse("\\function myMacro(one,two,three,four:elephant)\nnothing\n\\end\n")).toEqual(
[ { type : 'set', attributes : { name : { type : 'string', value : 'myMacro' }, value : { type : 'string', value : 'nothing' } }, children : [ ], variableParams : [ { name: 'one' }, { name: 'two' }, { name: 'three' }, { name: 'four', default: 'elephant' } ], isFunctionDefinition : true } ]