JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / TokenReader.vala
index 4ec980b..9f2c12d 100644 (file)
@@ -1,15 +1,15 @@
 //<script type="text/javascript">
 
  
-
-
+// test code
 //const Token   = imports.Token.Token;
 //const Lang    = imports.Lang.Lang;
 
 /**
        @class Search a {@link JSDOC.TextStream} for language tokens.
 */
-
 namespace JSDOC {
 
     public class TokenArray: Object {
@@ -21,17 +21,17 @@ namespace JSDOC {
         
         public TokenArray()
         {
-            this.items = new Gee.ArrayList<Token>();
+            this.tokens = new Gee.ArrayList<Token>();
         }
         
         public Token? last() {
-            if (this.tokens > 0) {
-                return this.tokens[this.tokens.length-1];
+            if (this.tokens.size > 0) {
+                return this.tokens.get(this.tokens.size-1);
             }
             return null;
         }
         public Token? lastSym () {
-            for (var i = this.tokens.length-1; i >= 0; i--) {
+            for (var i = this.tokens.size-1; i >= 0; i--) {
                 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM")))  {
                     return this.tokens.get(i);
                 }
@@ -43,18 +43,25 @@ namespace JSDOC {
         }
         public Token? pop ()
         {
-            if (this.size > 0) {
-                return this.tokens.remove_at(this.size-1);
+            if (this.tokens.size > 0) {
+                return this.tokens.remove_at(this.tokens.size-1);
             }
             return null;
         }
         
-        public Token get(int i) {
+           public new Token get(int i) {
             return this.tokens.get(i);
         }
+        public void dump()
+        {
+               foreach(var token in this.tokens) {
+                       print(token.asString() +"\n");
+               }
+        }
+        
     }
 
-    errordomain TokenReader_Error {
+    public errordomain TokenReader_Error {
             ArgumentError
     }
     
@@ -101,10 +108,10 @@ namespace JSDOC {
             this.line =1;
             var tokens = new TokenArray();
            
-            bool eof;
+         
             while (!stream.lookEOF()) {
                 
-                
+
                 if (this.read_mlcomment(stream, tokens)) continue;
                 if (this.read_slcomment(stream, tokens)) continue;
                 if (this.read_dbquote(stream, tokens))   continue;
@@ -138,20 +145,20 @@ namespace JSDOC {
          */
         public int findPuncToken(TokenArray tokens, string data, int n)
         {
-            n = n || tokens.length -1;
+            n = n > 0 ? n :  tokens.length -1;
             var stack = 0;
             while (n > -1) {
                 
-                if (!stack && tokens.get(n).data == data) {
+                if (stack < 1 && tokens.get(n).data == data) {
                     return n;
                 }
                 
-                if (tokens.get(n).data  == ')' || tokens.get(n).data  == '}') {
+                if (tokens.get(n).data  == ")" || tokens.get(n).data  == "}") {
                     stack++;
                     n--;
                     continue;
                 }
-                if (stack && (tokens.get(n).data  == '{' || tokens.get(n).data  == '(')) {
+                if (stack > 0 && (tokens.get(n).data  == "{" || tokens.get(n).data  == "(")) {
                     stack--;
                     n--;
                     continue;
@@ -170,7 +177,7 @@ namespace JSDOC {
          * @arg {Number} offset where to start..
          * @return {Token} the token
          */
-        public Token lastSym(TokenArray tokens, int n)
+        public Token? lastSym(TokenArray tokens, int n)
         {
             for (var i = n-1; i >= 0; i--) {
                 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
@@ -185,10 +192,10 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_word (TokenStream stream, TokenArray tokens)
+        public bool read_word (TextStream stream, TokenArray tokens)
         {
             string found = "";
-            while (!stream.lookEOF() && Lang.isWordChar(stream.look())) {
+            while (!stream.lookEOF() && Lang.isWordChar(stream.look().to_string())) {
                 found += stream.next();
             }
             
@@ -203,12 +210,12 @@ namespace JSDOC {
                 var ls = tokens.lastSym();
                 if (found == "return" && ls != null && ls.data == ")") {
                     //Seed.print('@' + tokens.length);
-                    var n = this.findPuncToken(tokens, ")");
+                    var n = this.findPuncToken(tokens, ")", 0);
                     //Seed.print(')@' + n);
                     n = this.findPuncToken(tokens, "(", n-1);
                     //Seed.print('(@' + n);
                     
-                    var lt = this.lastSym(tokens, n);
+                    //var lt = this.lastSym(tokens, n);
                     /*
                     //print(JSON.stringify(lt));
                     if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
@@ -227,15 +234,15 @@ namespace JSDOC {
                 return true;
             }
             
-            if (!this.sepIdents || found.indexOf('.') < 0 ) {
+            if (!this.sepIdents || found.index_of(".") < 0 ) {
                 tokens.push(new Token(found, "NAME", "NAME", this.line));
                 return true;
             }
-            var n = found.split('.');
+            var n = found.split(".");
             var p = false;
             foreach (unowned string nm in n) {
                 if (p) {
-                    tokens.push(new Token('.', "PUNC", "DOT", this.line));
+                    tokens.push(new Token(".", "PUNC", "DOT", this.line));
                 }
                 p=true;
                 tokens.push(new Token(nm, "NAME", "NAME", this.line));
@@ -248,11 +255,16 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_punc (TokenStream stream, TokenArray tokens)
+        public bool read_punc (TextStream stream, TokenArray tokens) throws TokenReader_Error
         {
             string found = "";
-            var name;
-            while (!stream.lookEOF() && Lang.punc(found + stream.look()).length > 0) {
+            
+            while (!stream.lookEOF()) {
+                       var ns = stream.look().to_string();
+
+                   if (null == Lang.punc(found + ns )) {
+                               break;
+                       }
                 found += stream.next();
             }
             
@@ -267,10 +279,10 @@ namespace JSDOC {
                 //print("Error - comma found before " + found);
                 //print(JSON.stringify(tokens.lastSym(), null,4));
                 if (this.ignoreBadGrammer) {
-                    print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
+                    print("\n" + this.filename + ":" + this.line.to_string() + " Error - comma found before " + found);
                 } else {
                     throw new TokenReader_Error.ArgumentError(
-                                this.filename + ":" + this.line + "  comma found before " + found
+                                this.filename + ":" + this.line.to_string() + "  comma found before " + found
                   
                     );
                      
@@ -285,11 +297,11 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_space  (TokenStream stream, TokenArray tokens)
+        public bool read_space  (TextStream stream, TokenArray tokens)
         {
             var found = "";
             
-            while (!stream.lookEOF() && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
+            while (!stream.lookEOF() && Lang.isSpaceC(  stream.look()) && !Lang.isNewlineC(stream.look())) {
                 found += stream.next();
             }
             
@@ -312,11 +324,11 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_newline  (TokenStream stream, TokenArray tokens)
+        public bool read_newline  (TextStream stream, TokenArray tokens)
         {
             var found = "";
             var line = this.line;
-            while (!stream.lookEOF() && Lang.isNewline(stream.look())) {
+            while (!stream.lookEOF() && Lang.isNewlineC(stream.look())) {
                 this.line++;
                 found += stream.next();
             }
@@ -348,18 +360,18 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_mlcomment  (TokenStream stream, TokenArray tokens)
+        public bool read_mlcomment  (TextStream stream, TokenArray tokens)
         {
-            if (stream.look() != "/") {
+            if (stream.look() != '/') {
                 return false;
             }
-            if (stream.look(1) != "*") {
+            if (stream.look(1) != '*') {
                 return false;
             }
             var found = stream.next(2);
-            var c = '';
+            string  c = "";
             var line = this.line;
-            while (!stream.lookEOF() && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
+            while (!stream.lookEOF() && !(stream.look(-1) == '/' && stream.look(-2) == '*')) {
                 c = stream.next();
                 if (c == "\n") {
                     this.line++;
@@ -369,7 +381,7 @@ namespace JSDOC {
             
             // to start doclet we allow /** or /*** but not /**/ or /****
             //if (found.length /^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) {
-            if (this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != "/") {
+            if (this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != '/') {
                 tokens.push(new Token(found, "COMM", "JSDOC", this.line));
             } else if (this.keepComments) {
                 tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
@@ -381,21 +393,25 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-         public bool read_slcomment  (TokenStream stream, TokenArray tokens)
+         public bool read_slcomment  (TextStream stream, TokenArray tokens)
          {
             var found = "";
             if (
-                (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
+                (stream.look() == '/' && stream.look(1) == '/' && (""!=(found=stream.next(2))))
                 || 
-                (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
+                (stream.look() == '<' && stream.look(1) == '!' && stream.look(2) == '-' && stream.look(3) == '-' && (""!=(found=stream.next(4))))
             ) {
                 var line = this.line;
-                while (!stream.lookEOF() && !Lang.isNewline(stream.look())) {
+                while (!stream.lookEOF()) {
+                                       //print(stream.look().to_string());
+                       if ( Lang.isNewline(stream.look().to_string())) {
+                               break;
+                       }
                     found += stream.next();
                 }
-                //if (!stream.lookEOF()) { // what? << eat the EOL?
+                if (!stream.lookEOF()) { // lookinng for end  of line... if we got it, then do not eat the character..
                     found += stream.next();
-                //}
+                }
                 if (this.keepComments) {
                     tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
                 }
@@ -408,20 +424,20 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_dbquote  (TokenStream stream, TokenArray tokens)
+        public bool read_dbquote  (TextStream stream, TokenArray tokens)
         {
-            if (stream.look() != "\"") {
+            if (stream.look() != '"') {
                 return false;
             }
                 // find terminator
             var str = stream.next();
             
             while (!stream.lookEOF()) {
-                if (stream.look() == "\\") {
-                    if (Lang.isNewline(stream.look(1))) {
+                if (stream.look() == '\\') {
+                    if (Lang.isNewline(stream.look(1).to_string())) {
                         do {
                             stream.next();
-                        } while (!stream.lookEOF() && Lang.isNewline(stream.look()));
+                        } while (!stream.lookEOF() && Lang.isNewline(stream.look().to_string()));
                         str += "\\\n";
                     }
                     else {
@@ -429,7 +445,7 @@ namespace JSDOC {
                     }
                     continue;
                 }
-                if (stream.look() == "\"") {
+                if (stream.look() == '"') {
                     str += stream.next();
                     tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
                     return true;
@@ -444,20 +460,20 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_snquote  (TokenStream stream, TokenArray tokens)
+        public bool read_snquote  (TextStream stream, TokenArray tokens)
         {
-            if (stream.look() != "'") {
+            if (stream.look() != '\'') {
                 return false;
             }
             // find terminator
             var str = stream.next();
             
-            while (!stream.look().eof) {
-                if (stream.look() == "\\") { // escape sequence
+            while (!stream.lookEOF()) {
+                if (stream.look() == '\\') { // escape sequence
                     str += stream.next(2);
                     continue;
                 }
-                if (stream.look() == "'") {
+                if (stream.look() == '\'') {
                     str += stream.next();
                     tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
                     return true;
@@ -472,19 +488,19 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_numb  (TokenStream stream, TokenArray tokens)
+        public bool read_numb  (TextStream stream, TokenArray tokens)
         {
-            if (stream.look() === "0" && stream.look(1) == "x") {
+            if (stream.look() == '0' && stream.look(1) == 'x') {
                 return this.read_hex(stream, tokens);
             }
             
             var found = "";
             
-            while (!stream.lookEOF() && Lang.isNumber(found+stream.look())){
+            while (!stream.lookEOF() && Lang.isNumber(found+stream.look().to_string())){
                 found += stream.next();
             }
             
-            if (found === "") {
+            if (found == "") {
                 return false;
             }
             if (GLib.Regex.match_simple("^0[0-7]", found)) {
@@ -499,12 +515,12 @@ namespace JSDOC {
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_hex  (TokenStream stream, TokenArray tokens)
+        public bool read_hex  (TextStream stream, TokenArray tokens)
         {
             var found = stream.next(2);
             
             while (!stream.lookEOF()) {
-                if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
+                if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look().to_string())) { // done
                     tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
                     return true;
                 }
@@ -513,18 +529,18 @@ namespace JSDOC {
                
             }
             return false;
-        },
+        }
 
         /**
             @returns {Boolean} Was the token found?
          */
-        public bool read_regx (TokenStream stream, TokenArray tokens)
+        public bool read_regx (TextStream stream, TokenArray tokens)
         {
-            Token last;
-            if (stream.look() != "/") {
+              
+            if (stream.look() != '/') {
                 return false;
             }
-            var last = tokens.lastSym();
+            var  last = tokens.lastSym();
             if (
                 (last == null)
                 || 
@@ -538,14 +554,14 @@ namespace JSDOC {
                 var regex = stream.next();
                 
                 while (!stream.lookEOF()) {
-                    if (stream.look() == "\\") { // escape sequence
+                    if (stream.look() == '\\') { // escape sequence
                         regex += stream.next(2);
                         continue;
                     }
-                    if (stream.look() == "/") {
+                    if (stream.look() == '/') {
                         regex += stream.next();
                         
-                        while (GLib.Regex.match_simple("[gmi]", stream.look()) {
+                        while (GLib.Regex.match_simple("[gmi]", stream.look().to_string())) {
                             regex += stream.next();
                         }