mirror of
https://github.com/samsonjs/elisp.js.git
synced 2026-04-27 15:07:47 +00:00
Removed debug prints and renamed *Sexp to *List.
This commit is contained in:
parent
7d8e3cc2a3
commit
f273ad2b74
1 changed files with 60 additions and 22 deletions
80
el/parse.js
80
el/parse.js
|
|
@ -30,6 +30,11 @@ var EL = function(){};
|
||||||
/* EL.nil is implicitly undefined */
|
/* EL.nil is implicitly undefined */
|
||||||
EL.t = true;
|
EL.t = true;
|
||||||
|
|
||||||
|
EL.parse = function(input) {
|
||||||
|
var parser = new EL.Parser(input);
|
||||||
|
parser.parse();
|
||||||
|
};
|
||||||
|
|
||||||
EL.Parser = function(data) {
|
EL.Parser = function(data) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
};
|
};
|
||||||
|
|
@ -89,7 +94,9 @@ EL.Parser.prototype.parse = function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.expressions = exprs;
|
this.expressions = exprs;
|
||||||
|
print('');
|
||||||
this.prettyPrint(exprs);
|
this.prettyPrint(exprs);
|
||||||
|
print('');
|
||||||
};
|
};
|
||||||
|
|
||||||
EL.Parser.prototype.parseUntil = function(regex, initial, next) {
|
EL.Parser.prototype.parseUntil = function(regex, initial, next) {
|
||||||
|
|
@ -103,15 +110,27 @@ EL.Parser.prototype.parseUntil = function(regex, initial, next) {
|
||||||
return token;
|
return token;
|
||||||
};
|
};
|
||||||
|
|
||||||
EL.Parser.prototype.parseSexp = function() {
|
EL.Parser.prototype.parseList = function() {
|
||||||
var sexp = [],
|
var list = [],
|
||||||
token;
|
expr;
|
||||||
// consume initial paren '('
|
// consume initial paren '('
|
||||||
this.consumeChar();
|
this.consumeChar();
|
||||||
while ((expr = this.parseExpression()) && expr != ')') {
|
while ((expr = this.parseExpression()) && expr != ')') {
|
||||||
sexp.push(expr);
|
list.push(expr);
|
||||||
}
|
}
|
||||||
return sexp;
|
return list;
|
||||||
|
};
|
||||||
|
|
||||||
|
EL.Parser.prototype.parseCons = function() {
|
||||||
|
var cons = [],
|
||||||
|
expr;
|
||||||
|
// consume initial paren '('
|
||||||
|
this.consumeChar();
|
||||||
|
cons.push(this.parseExpression());
|
||||||
|
// ignore .
|
||||||
|
this.parseExpression();
|
||||||
|
cons.push(this.parseExpression());
|
||||||
|
return cons;
|
||||||
};
|
};
|
||||||
|
|
||||||
EL.Parser.prototype.parseString = function() {
|
EL.Parser.prototype.parseString = function() {
|
||||||
|
|
@ -150,33 +169,47 @@ EL.Parser.prototype.lookingAtNumber = function() {
|
||||||
return (match != null);
|
return (match != null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
EL.Parser.prototype.lookingAtCons = function() {
|
||||||
|
var orig_pos = this.pos,
|
||||||
|
_ = this.consumeChar(),
|
||||||
|
_ = this.parseExpression(),
|
||||||
|
cdr = this.parseExpression();
|
||||||
|
this.pos = orig_pos; // rewind, like it never happened.
|
||||||
|
return cdr != null && cdr[0] == 'symbol' && cdr[1] == '.';
|
||||||
|
};
|
||||||
|
|
||||||
EL.Parser.prototype.parseExpression = function() {
|
EL.Parser.prototype.parseExpression = function() {
|
||||||
var value,
|
var value,
|
||||||
c = this.peek();
|
c = this.peek();
|
||||||
if (c == '(') {
|
if (c == '(' && this.lookingAtCons()) {
|
||||||
print("SEXP(");
|
// print("CONS(");
|
||||||
value = ['sexp', this.parseSexp()];
|
value = ['cons', this.parseCons()];
|
||||||
|
}
|
||||||
|
else if (c == '(') {
|
||||||
|
// print("LIST(");
|
||||||
|
var list = this.parseList();
|
||||||
|
value = (list.length > 0) ? ['list', list] : ['symbol', 'nil'];
|
||||||
}
|
}
|
||||||
else if (c == ')') {
|
else if (c == ')') {
|
||||||
print(")");
|
// print(")");
|
||||||
return this.consumeChar();
|
return this.consumeChar();
|
||||||
}
|
}
|
||||||
else if (c == "'") {
|
else if (c == "'") {
|
||||||
print("QUOTE");
|
// print("QUOTE");
|
||||||
this.consumeChar();
|
this.consumeChar();
|
||||||
value = ['sexp', [['symbol', 'quote']]];
|
value = ['list', [['symbol', 'quote']]];
|
||||||
value[1].push(this.parseExpression());
|
value[1].push(this.parseExpression());
|
||||||
}
|
}
|
||||||
else if (c == '"') {
|
else if (c == '"') {
|
||||||
print("STRING");
|
// print("STRING");
|
||||||
value = ['string', this.parseString()];
|
value = ['string', this.parseString()];
|
||||||
}
|
}
|
||||||
else if (this.lookingAtNumber()) {
|
else if (this.lookingAtNumber()) {
|
||||||
print("NUMBER");
|
// print("NUMBER");
|
||||||
value = ['number', this.parseNumber()];
|
value = ['number', this.parseNumber()];
|
||||||
}
|
}
|
||||||
else if (c) {
|
else if (c) {
|
||||||
print("SYMBOL");
|
// print("SYMBOL");
|
||||||
value = ['symbol', this.parseSymbol()];
|
value = ['symbol', this.parseSymbol()];
|
||||||
}
|
}
|
||||||
this.consumeWhitespace();
|
this.consumeWhitespace();
|
||||||
|
|
@ -212,9 +245,19 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
buffer += s;
|
buffer += s;
|
||||||
if (newline) buffer += "\n";
|
if (newline) buffer += "\n";
|
||||||
};
|
};
|
||||||
var dumpBuffer = function() {
|
var dumpBuffer = function(b) {
|
||||||
if (buffer.length > 0)
|
if (b === undefined) b = buffer;
|
||||||
|
if (buffer.length <= 72) {
|
||||||
print(buffer);
|
print(buffer);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var split = 72;
|
||||||
|
var c;
|
||||||
|
while ((c = b[split]) && c.match(/[a-zA-Z0-9"'\[\]_-]/)) --split;
|
||||||
|
print(b.substring(0, split));
|
||||||
|
var next_chunk = b.substring(split, b.length);
|
||||||
|
if (next_chunk) dumpBuffer(next_chunk);
|
||||||
|
}
|
||||||
buffer = "";
|
buffer = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -225,7 +268,6 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
|
|
||||||
switch (typeOf(x)) {
|
switch (typeOf(x)) {
|
||||||
case 'object':
|
case 'object':
|
||||||
print("PP:OBJECT");
|
|
||||||
if (key) {
|
if (key) {
|
||||||
printB(space + key + ': {');
|
printB(space + key + ': {');
|
||||||
}
|
}
|
||||||
|
|
@ -239,7 +281,6 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'string':
|
case 'string':
|
||||||
print("PP:STRING");
|
|
||||||
if (key) {
|
if (key) {
|
||||||
printB(space + key + ': "' + x + '"');
|
printB(space + key + ': "' + x + '"');
|
||||||
}
|
}
|
||||||
|
|
@ -250,7 +291,6 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'array':
|
case 'array':
|
||||||
print("PP:ARRAY");
|
|
||||||
if (key) {
|
if (key) {
|
||||||
printB(space + key + ': [');
|
printB(space + key + ': [');
|
||||||
}
|
}
|
||||||
|
|
@ -266,7 +306,6 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'null':
|
case 'null':
|
||||||
print("PP:NULL");
|
|
||||||
if (key) {
|
if (key) {
|
||||||
printB(space + key + ': (null)');
|
printB(space + key + ': (null)');
|
||||||
}
|
}
|
||||||
|
|
@ -277,7 +316,6 @@ EL.Parser.prototype.prettyPrint = function(x, indent, key, noprint) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
print("PP:UNKNOWN");
|
|
||||||
if (key) {
|
if (key) {
|
||||||
printB(space + key + ": " + x);
|
printB(space + key + ": " + x);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue