[FIXED] pretty printing, sort of. it's pretty enough.

This commit is contained in:
Sami Samhuri 2009-12-05 13:31:45 -08:00
parent 31acf28b38
commit 7d8e3cc2a3

View file

@ -90,7 +90,6 @@ EL.Parser.prototype.parse = function() {
} }
this.expressions = exprs; this.expressions = exprs;
this.prettyPrint(exprs); this.prettyPrint(exprs);
return exprs;
}; };
EL.Parser.prototype.parseUntil = function(regex, initial, next) { EL.Parser.prototype.parseUntil = function(regex, initial, next) {
@ -165,10 +164,8 @@ EL.Parser.prototype.parseExpression = function() {
else if (c == "'") { else if (c == "'") {
print("QUOTE"); print("QUOTE");
this.consumeChar(); this.consumeChar();
var expr = this.parseExpression(), value = ['sexp', [['symbol', 'quote']]];
quote = [['symbol', 'quote']]; value[1].push(this.parseExpression());
quote.push(expr);
value = ['sexp', quote];
} }
else if (c == '"') { else if (c == '"') {
print("STRING"); print("STRING");
@ -186,7 +183,7 @@ EL.Parser.prototype.parseExpression = function() {
return value; return value;
}; };
EL.Parser.prototype.prettyPrint = function(x, indent, key) { EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
var typeOf = function(value) { var typeOf = function(value) {
var s = typeof value; var s = typeof value;
if (s === 'object') { if (s === 'object') {
@ -207,64 +204,90 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key) {
indent = 0; indent = 0;
} }
/* string to print at the end, the only way to print 2 strings in
* succession without a newline is to buffer them
*/
var buffer = "";
var printB = function(s, newline) {
buffer += s;
if (newline) buffer += "\n";
};
var dumpBuffer = function() {
if (buffer.length > 0)
print(buffer);
buffer = "";
};
var space = ""; var space = "";
for (var j = 0; j < indent; j++) { // for (var j = 0; j < indent; j++) {
space += " "; // space += " ";
} // }
switch (typeOf(x)) { switch (typeOf(x)) {
case 'object': case 'object':
print("PP:OBJECT");
if (key) { if (key) {
print(space + key + ': {'); printB(space + key + ': {');
} }
else { else {
print(space + '{'); printB(space + '{');
} }
for (var a in x) { for (var a in x) {
this.prettyPrint(x[a], 1+indent, a); printB(this.prettyPrint(x[a], 1+indent, a, true));
} }
print(space + "},"); printB(space + "}");
break; break;
case 'string': case 'string':
print("PP:STRING");
if (key) { if (key) {
print(space + key + ': "' + x + '",'); printB(space + key + ': "' + x + '"');
} }
else { else {
print(space + '"' + x + '",'); printB(space + '"' + x + '"');
} }
return buffer;
break; break;
case 'array': case 'array':
print("PP:ARRAY");
if (key) { if (key) {
print(space + key + ': ['); printB(space + key + ': [');
} }
else { else {
print(space + '['); printB(space + '[');
} }
var n = x.length, i = 0; var n = x.length, i = 0;
while (i < n) { while (i < n) {
this.prettyPrint(x[i++], 1+indent); if (i > 0) printB(', ');
printB(this.prettyPrint(x[i++], 1+indent, undefined, true));
} }
print(space + '],'); printB(space + ']');
break; break;
case 'null': case 'null':
print("PP:NULL");
if (key) { if (key) {
print(space + key + ': (null),'); printB(space + key + ': (null)');
} }
else { else {
print(space + '(null),'); printB(space + '(null)');
} }
return buffer;
break; break;
default: default:
print("PP:UNKNOWN");
if (key) { if (key) {
print(space + key + ": " + x + ','); printB(space + key + ": " + x);
} }
else { else {
print(space + x + ','); printB(space + x);
} }
return buffer;
break; break;
} }
var s = buffer;
if (!noprint) dumpBuffer();
return s;
}; };