/*
 * This file is part of yoob.js version 0.3
 * Available from https://github.com/catseye/yoob.js/
 * This file is in the public domain.  See http://unlicense.org/ for details.
 */
if (window.yoob === undefined) yoob = {};

/*
 * A simple S-expression parser.  Note that it produces yoob.Trees rather
 * than cons cells (pairs), so parsing "(a b c)" will yield a single yoob.Tree
 * node with three children.
 *
 * Note also that atomic symbols are restricted to alphanumeric characters.
 *
 * This module requires that you load yoob.Tree and yoob.Scanner first.
 *
 * This module is really intended more for you to copy and modify (and maybe
 * extend into a parser for a more complex language) than to use directly.
 */
yoob.SexpParser = function() {
  this.scanner = undefined;

  this.init = function(text) {
    this.scanner = new yoob.Scanner();
    this.scanner.init([
      ['paren',  "^(\\(|\\))"],
      ['atom',   "^([a-zA-Z]\\w*)"]
    ]);
    this.scanner.reset(text);
  };

  /*
   * SExp ::= Atom | "(" {SExpr} ")".
   */
  this.parse = function(text) {
    if (this.scanner.onType('atom')) {
      var x = this.scanner.token;
      this.scanner.scan();
      return (new yoob.Tree('atom')).setValue(x);
    } else if (this.scanner.consume('(')) {
      var children = []
      while (!this.scanner.on(')') && !this.scanner.onType('EOF')) {
        children.push(this.parse());
      }
      this.scanner.expect(')');
      return new yoob.Tree('list', children);
    } else {
      /* TODO: register some kind of error */
      this.scanner.scan();
    }
  };

};

@@ -42,9 +42,13 @@
 n 0.
-3
+9-PRE
 %0A * 
@@ -752,16 +752,18 @@
 ion() %7B%0A
+  
   this.s
@@ -785,16 +785,18 @@
 ned;%0A%0A  
+ 
+ 
 this.ini
@@ -812,32 +812,36 @@
 ion(text) %7B%0A    
+  
+  
 this.scanner = n
@@ -839,16 +839,17 @@
 anner = 
+(
 new yoob
@@ -862,34 +862,42 @@
 er()
-;%0A    this.scanner.init(
+).init(%7B%0A          table: 
 %5B%0A
+      
     
@@ -930,16 +930,22 @@
 ,%0A      
+     
+ 
 %5B'atom',
@@ -974,12 +974,32 @@
     
-%5D);%0A
+      %5D%0A        %7D);%0A    
     
@@ -1028,16 +1028,18 @@
 t);%0A
+  
   %7D;%0A%0A  
 /*%0A 
@@ -1034,19 +1034,23 @@
   %7D;%0A%0A  
+ 
+ 
 /*%0A
+  
    * SEx
@@ -1082,19 +1082,23 @@
 %22)%22.%0A   
+  
 */%0A
+  
   this.p
@@ -1117,24 +1117,28 @@
 ion(text) %7B%0A
+    
     if (this
@@ -1160,24 +1160,30 @@
 ('atom')) %7B%0A
+      
       var x 
@@ -1206,24 +1206,30 @@
 oken;%0A      
+ 
+     
 this.scanner
@@ -1233,24 +1233,30 @@
 ner.scan();%0A
+      
       return
@@ -1293,16 +1293,20 @@
 lue(x);%0A
+    
     %7D el
@@ -1337,24 +1337,30 @@
 ume('(')) %7B%0A
+      
       var ch
@@ -1377,16 +1377,22 @@
 %5D%0A      
+ 
+     
 while (!
@@ -1454,16 +1454,22 @@
         
+     
+ 
 children
@@ -1499,16 +1499,28 @@
     
-%7D%0A
+      %7D%0A    
       
+  
 this
@@ -1541,16 +1541,22 @@
 t(')');%0A
+      
       re
@@ -1593,24 +1593,28 @@
 ldren);%0A    
+  
+  
 %7D else %7B%0A   
@@ -1610,16 +1610,22 @@
  else %7B%0A
+      
       /*
@@ -1664,24 +1664,30 @@
 or */%0A      
+   
+   
 this.scanner
@@ -1699,19 +1699,24 @@
 ();%0A
+    
     %7D%0A
+  
   %7D;%0A
-%0A
 %7D;%0A

Update yoob.SexpParser to use recent yoob.Scanner.