Parsing and Evaluating a Meta-Language

With this functions I can parse/evaluate a simple meta-language that accepts commands on this format:


The parsing engine automatically replace each command with its result (“” by default) over the evaluated string. Is of course possible, as well, to perform custom operations before to return the result.

//Metalanguage evaluation
//Syntax: [@<funcName>(<funcParam>)] or [@<funcName>]
function evaluateMetalanguage(html) {
    var mlRegExp = /\[\@(\w+)(\((.*?(?=\)\]))\))?\]/i;
    var mlMatch = mlRegExp.exec(html);
    while (mlMatch != null) {
        var match = mlMatch[0];
        var funcName = mlMatch[1];
        var funcParam = mlMatch.length > 3 ? mlMatch[3] : null;
        html = evaluateMLFunction(html, match, funcName, funcParam);
        mlMatch = mlRegExp.exec(html);
    return html;
function evaluateMLFunction(html, match, funcName, funcParam) {
    var result = "";
    switch (funcName.toLowerCase()) {
        case "function1":
            result = "...";
        case "function2":
    return html.replace(match, result);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s