Refactored macro parameter parsing

So that it happens during parsing, not compilation. This will enable us
to do the dependency tracking during parsing, and not wait until
compilation time
print-window-tiddler
Jeremy Ruston 2012-01-06 18:43:36 +00:00
rodzic 86bf495dec
commit 3d507c3bab
3 zmienionych plików z 50 dodań i 37 usunięć

Wyświetl plik

@ -314,15 +314,18 @@ store.renderTiddler("text/html",templateTitle,tiddlerTitle)
WikiStore.prototype.renderTiddler = function(type,title,asTitle) {
var tiddler = this.getTiddler(title),
fn = this.compileTiddler(title,type);
if(asTitle) {
var asTiddler = this.getTiddler(asTitle);
return fn(asTiddler,this,utils);
} else {
if(!tiddler.renditions[type]) {
tiddler.renditions[type] = fn(tiddler,this,utils);
if(tiddler) {
if(asTitle) {
var asTiddler = this.getTiddler(asTitle);
return fn(asTiddler,this,utils);
} else {
if(!tiddler.renditions[type]) {
tiddler.renditions[type] = fn(tiddler,this,utils);
}
return tiddler.renditions[type];
}
return tiddler.renditions[type];
}
return null;
};
WikiStore.prototype.installMacros = function() {

Wyświetl plik

@ -76,32 +76,10 @@ WikiTextParseTree.prototype.pushString = function(s) {
};
WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
var me = this,
macro = this.store.macros[name];
var macro = this.store.macros[name],
p,
n;
if(macro) {
var args = new ArgParser(params,{defaultName: "anon"}),
paramsProps = {};
var insertParam = function(name,arg) {
if(arg.evaluated) {
paramsProps[name] = me.store.jsParser.parse(arg.string).tree.elements[0];
} else {
paramsProps[name] = {type: "StringLiteral", value: arg.string};
}
};
for(var m in macro.params) {
var param = macro.params[m];
if("byPos" in param && args.byPos[param.byPos]) {
insertParam(m,args.byPos[param.byPos].v);
} else if("byName" in param) {
var arg = args.getValueByName(m);
if(!arg && param.byName === "default") {
arg = args.getValueByName("anon");
}
if(arg) {
insertParam(m,arg);
}
}
}
var macroCall = {
type: "FunctionCall",
name: {
@ -115,11 +93,16 @@ WikiTextParseTree.prototype.compileMacroCall = function(type,name,params) {
}]
};
macroCall.name.elements = macro.code[type].tree.elements;
for(m in paramsProps) {
for(p in params) {
if(params[p].type === "string") {
n = {type: "StringLiteral", value: params[p].value};
} else {
n = this.store.jsParser.parse(params[p].value).tree.elements[0];
}
macroCall["arguments"][0].properties.push({
type: "PropertyAssignment",
name: m,
value: paramsProps[m]
name: p,
value: n
});
}
this.output.push(macroCall);

Wyświetl plik

@ -7,7 +7,8 @@ title: js/WikiTextRules.js
/*jslint node: true */
"use strict";
var util = require("util");
var ArgParser = require("./ArgParser.js").ArgParser,
util = require("util");
var textPrimitives = {
upperLetter: "[A-Z\u00c0-\u00de\u0150\u0170]",
@ -107,6 +108,32 @@ var enclosedTextHelper = function(w) {
}
};
var parseMacroCall = function(w,name,paramString) {
var macro = w.store.macros[name],
params = {};
if(macro) {
var args = new ArgParser(paramString,{defaultName: "anon"}),
insertParam = function(name,arg) {
params[name] = {type: arg.evaluated ? "eval" : "string", value: arg.string};
};
for(var m in macro.params) {
var param = macro.params[m];
if("byPos" in param && args.byPos[param.byPos]) {
insertParam(m,args.byPos[param.byPos].v);
} else if("byName" in param) {
var arg = args.getValueByName(m);
if(!arg && param.byName === "default") {
arg = args.getValueByName("anon");
}
if(arg) {
insertParam(m,arg);
}
}
}
}
return {type: "macro", name: name, params: params};
};
var rules = [
{
name: "table",
@ -410,7 +437,7 @@ var rules = [
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart && lookaheadMatch[1]) {
w.nextMatch = this.lookaheadRegExp.lastIndex;
w.output.push({type: "macro", name: lookaheadMatch[1], params: lookaheadMatch[2]});
w.output.push(parseMacroCall(w,lookaheadMatch[1],lookaheadMatch[2]));
}
}
},