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
This commit is contained in:
Jeremy Ruston
2012-01-06 18:43:36 +00:00
parent 86bf495dec
commit 3d507c3bab
3 changed files with 50 additions and 37 deletions

View File

@@ -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]));
}
}
},