From 68c734cf593dd23d746c6739f42c3b0739fed2a5 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Fri, 19 Oct 2012 11:58:55 +0200 Subject: [PATCH] Better indentation, also for FQ output --- cql.js | 108 ++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/cql.js b/cql.js index 0a1cc75..2a04c67 100644 --- a/cql.js +++ b/cql.js @@ -1,7 +1,9 @@ -function indent(n) { +var EXPORTED_SYMBOLS = ["CQLParser"]; + +function indent(n, c) { var s = ""; for (var i = 0; i < n; i++) - s = s + " "; + s = s + c; return s; } // CQLModifier @@ -16,16 +18,16 @@ CQLModifier.prototype = { return this.name + this.relation + this.value; }, - toXCQL: function (n) { - var s = indent(n+1) + "\n"; - s = s + indent(n+2) + "" + this.name + "\n"; + toXCQL: function (n, c) { + var s = indent(n+1, c) + "\n"; + s = s + indent(n+2, c) + "" + this.name + "\n"; if (this.relation != null) - s = s + indent(n+2) + s = s + indent(n+2, c) + "" + this.relation + "\n"; if (this.value != null) - s = s + indent(n+2) + s = s + indent(n+2, c) + "" + this.value +"\n"; - s = s + indent(n+1) + "\n"; + s = s + indent(n+1, c) + "\n"; return s; }, @@ -65,38 +67,38 @@ CQLSearchClause.prototype = { '"' + this.term + '"'; }, - toXCQL: function (n) { - var s = indent(n) + "\n"; + toXCQL: function (n, c) { + var s = indent(n, c) + "\n"; if (this.fielduri.length > 0) { - s = s + indent(n+1) + "\n" + - indent(n+2) + "\n" + - indent(n+3) + "" + this.fielduri + + s = s + indent(n+1, c) + "\n" + + indent(n+2, c) + "\n" + + indent(n+3, c) + "" + this.fielduri + "\n" + - indent(n+2) + "\n" + - indent(n+1) + "\n"; + indent(n+2, c) + "\n" + + indent(n+1, c) + "\n"; } - s = s + indent(n+1) + "" + this.field + "\n"; - s = s + indent(n+1) + "\n"; + s = s + indent(n+1, c) + "" + this.field + "\n"; + s = s + indent(n+1, c) + "\n"; if (this.relationuri.length > 0) { - s = s + indent(n+2) + + s = s + indent(n+2, c) + "" + this.relationuri + "\n"; } - s = s + indent(n+2) + "" + this.relation + "\n"; + s = s + indent(n+2, c) + "" + this.relation + "\n"; if (this.modifiers.length > 0) { - s = s + indent(n+2) + "\n"; + s = s + indent(n+2, c) + "\n"; for (var i = 0; i < this.modifiers.length; i++) - s = s + this.modifiers[i].toXCQL(n+2); - s = s + indent(n+2) + "\n"; + s = s + this.modifiers[i].toXCQL(n+2, c); + s = s + indent(n+2, c) + "\n"; } - s = s + indent(n+1) + "\n"; - s = s + indent(n+1) + "" + this.term + "\n"; - s = s + indent(n) + "\n"; + s = s + indent(n+1, c) + "\n"; + s = s + indent(n+1, c) + "" + this.term + "\n"; + s = s + indent(n, c) + "\n"; return s; }, toFQ: function () { - var s = '{ "term": "'+this.term+'"'; + var s = '{"term": "'+this.term+'"'; if (this.field.length > 0 && this.field != 'cql.serverChoice') s+= ', "field": "'+this.field+'"'; if (this.relation.length > 0 && this.relation != 'scr') @@ -109,7 +111,7 @@ CQLSearchClause.prototype = { continue; s += ', ' + this.modifiers[i].toFQ(); } - s += ' }'; + s += '}'; return s; }, @@ -153,34 +155,35 @@ CQLBoolean.prototype = { (this.modifiers.length > 0 ? '/' + this.modifiers.join('/') : '') + ' ' + (this.right.op ? '(' + this.right + ')' : this.right);; }, - toXCQL: function (n) { - var s = indent(n) + "\n"; - s = s + indent(n+1) + "\n" + - indent(n+2) + "" + this.op + "\n"; + toXCQL: function (n, c) { + var s = indent(n, c) + "\n"; + s = s + indent(n+1, c) + "\n" + + indent(n+2, c) + "" + this.op + "\n"; if (this.modifiers.length > 0) { - s = s + indent(n+2) + "\n"; + s = s + indent(n+2, c) + "\n"; for (var i = 0; i < this.modifiers.length; i++) - s = s + this.modifiers[i].toXCQL(n+2); - s = s + indent(n+2) + "\n"; + s = s + this.modifiers[i].toXCQL(n+2, c); + s = s + indent(n+2, c) + "\n"; } - s = s + indent(n+1) + "\n"; - s = s + indent(n+1) + "\n" + - this.left.toXCQL(n+2) + indent(n+1) + "\n"; + s = s + indent(n+1, c) + "\n"; + s = s + indent(n+1, c) + "\n" + + this.left.toXCQL(n+2, c) + indent(n+1, c) + "\n"; - s = s + indent(n+1) + "\n" + - this.right.toXCQL(n+2) + indent(n+1) + "\n"; - s = s + indent(n) + "\n"; + s = s + indent(n+1, c) + "\n" + + this.right.toXCQL(n+2, c) + indent(n+1, c) + "\n"; + s = s + indent(n, c) + "\n"; return s; }, - toFQ: function () { - var s = ' { "op": "'+this.op+'"'; + toFQ: function (n, c, nl) { + var s = '{"op": "'+this.op+'"'; //proximity modifiers for (var i = 0; i < this.modifiers.length; i++) s += ', ' + this.modifiers[i].toFQ(); - s += ', "s1": '+this.left.toFQ(); - s += ', "s2": '+this.right.toFQ(); - s += ' }' + s += ','+nl+indent(n, c)+' "s1": '+this.left.toFQ(n+1, c, nl); + s += ','+nl+indent(n, c)+' "s2": '+this.right.toFQ(n+1, c, nl); + var fill = n && c ? ' ' : ''; + s += nl+indent(n-1, c)+fill+'}'; return s; } @@ -246,7 +249,9 @@ CQLParser.prototype = { node.field = fq.hasOwnProperty('field') ? fq.field : 'cql.serverChoice'; node.relation = fq.hasOwnProperty('relation') - ? node._remapRelation(fq.relation) : 'scr'; + ? node._remapRelation(fq.relation) : + //HACK: if the field is set, assume '=' rather than scr + (fq.hasOwnProperty('field') ? '=' : 'scr'); //include all other members as modifiers node.relationuri = ''; node.fielduri = ''; @@ -264,11 +269,14 @@ CQLParser.prototype = { } throw new Error('Unknow node type; '+JSON.stringify(fq)); }, - toXCQL: function () { - return this.tree.toXCQL(); + toXCQL: function (c) { + c = typeof c == "undefined" ? ' ' : c; + return this.tree.toXCQL(0, c); }, - toFQ: function () { - return this.tree.toFQ(); + toFQ: function (c, nl) { + c = typeof c == "undefined" ? ' ' : c; + nl = typeof nl == "undefined" ? '\n' : c; + return this.tree.toFQ(0, c, nl); }, toString: function () { return this.tree.toString(); -- 1.7.10.4