JSDOC/TokenReader.js
authorAlan Knowles <alan@akbkhome.com>
Tue, 10 Aug 2010 02:55:27 +0000 (10:55 +0800)
committerAlan Knowles <alan@akbkhome.com>
Tue, 10 Aug 2010 02:55:27 +0000 (10:55 +0800)
JSDOC/TokenReader.js

index 41513a1..f9aa831 100644 (file)
@@ -14,17 +14,24 @@ Lang    = imports.Lang.Lang;
 TokenReader = XObject.define(
     function(o) {
         
-        this.keepDocs = true;
-        this.keepWhite = false;
-        this.keepComments = false;
-        this.sepIdents = false; // seperate '.' in identifiers..
         XObject.extend(this, o || {});
         
     },
     Object,
     {
+        /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
         collapseWhite : false, // only reduces white space...
-
+        /** @cfg {Boolean} keepDocs keep JSDOC comments **/
+        keepDocs : true,
+        /** @cfg {Boolean} keepWhite keep White space **/
+        keepWhite : false,
+        /** @cfg {Boolean} keepComments  keep all comments **/
+        keepComments : false,
+        /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
+        sepIdents : false,
+        /** @cfg {String} filename name of file being parsed. **/
+        filename : '',
+        
         /**
          * tokenize a stream
          * @return {Array} of tokens
@@ -82,29 +89,36 @@ TokenReader = XObject.define(
             if (found === "") {
                 return false;
             }
-            else {
-                var name;
-                if ((name = Lang.keyword(found))) {
-                    tokens.push(new Token(found, "KEYW", name, this.line));
-                    return true;
-                }
-                if (!this.sepIdents || found.indexOf('.') < 0 ) {
-                    tokens.push(new Token(found, "NAME", "NAME", this.line));
-                    return true;
-                }
-                var n = found.split('.');
-                var p = false;
-                var _this = this;
-                n.forEach(function(nm) {
-                    if (p) {
-                        tokens.push(new Token('.', "PUNC", "DOT", _this.line));
-                    }
-                    p=true;
-                    tokens.push(new Token(nm, "NAME", "NAME", _this.line));
-                });
-                return true;
+            
+            var name;
+            if ((name = Lang.keyword(found))) {
+                if (found == 'return' && tokens.lastSym().data == ')') {
+                    throw {
+                        name : "ArgumentError", 
+                        message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
+                    }   
+                
                 
+                tokens.push(new Token(found, "KEYW", name, this.line));
+                return true;
+            }
+            if (!this.sepIdents || found.indexOf('.') < 0 ) {
+                tokens.push(new Token(found, "NAME", "NAME", this.line));
+                return true;
             }
+            var n = found.split('.');
+            var p = false;
+            var _this = this;
+            n.forEach(function(nm) {
+                if (p) {
+                    tokens.push(new Token('.', "PUNC", "DOT", _this.line));
+                }
+                p=true;
+                tokens.push(new Token(nm, "NAME", "NAME", _this.line));
+            });
+            return true;
+                
+
         },
 
         /**
@@ -127,7 +141,7 @@ TokenReader = XObject.define(
                 //print(JSON.stringify(tokens.lastSym(), null,4));
                 throw {
                     name : "ArgumentError", 
-                    message: "Error - comma found before " + found + " on line " + this.line + "\n"
+                    message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
                 }   
             }