JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / Parser.js
1 //<script type="text/javascript">
2
3 const Walker2      = imports.Walker2.Walker2;
4 const Symbol      = imports.Symbol.Symbol;
5 const SymbolSet      = imports.SymbolSet.SymbolSet;
6 const DocComment  = imports.DocComment.DocComment;
7 const Options     = imports.Options.Options;
8 /**
9  * Parser is a static  instance..
10  * 
11  * 
12  */
13  
14  
15 const Parser = {
16         conf: { 
17         loaded: false 
18     },
19     
20     walker : false, // will be JSDOC.Walker()
21     symbols : false, //will be JSDOC.SymbolSet()
22     
23     filesSymbols : { },
24     
25     /** 
26     * global init once 
27     * 
28     */
29          
30     init: function() {
31         if (this.conf.loaded) {
32             return;
33         }
34         //print("init parser conf!?");
35         this.conf = {
36             loaded : true,
37             //ignoreCode:                 Options.n,
38             ignoreAnonymous:           true, // factory: true
39             treatUnderscoredAsPrivate: true, // factory: true
40             explain:                   false // factory: false
41         };
42          
43                 this.symbols = new  SymbolSet();
44                 //this.walker = new JSDOC.Walker();
45         //JSDOC.Parser.filesSymbols = {};
46         },
47
48
49
50     /**
51      * Parse a token stream.
52      * @param {JSDOC.TokenStream} token stream
53      * @param {String} filename 
54          
55      */
56     
57     
58     parse : function(ts, srcFile) 
59     {
60         this.init();
61         
62         
63         // not a nice way to set stuff...
64         
65         Symbol.srcFile = (srcFile || "");
66         DocComment.shared = ""; // shared comments don't cross file boundaries
67         
68        
69         
70         
71         
72         this.filesSymbols[Symbol.srcFile] = new SymbolSet();
73         
74         //Options.LOG.inform("Parser - run walker");
75         this.walker = new  Walker2(ts);
76         this.walker.buildSymbolTree();
77         
78         
79         
80         //this.walker.walk(ts); // adds to our symbols
81        // throw "done sym tree";
82         //Options.LOG.inform("Parser - checking symbols");
83         // filter symbols by option
84         for (p in this.symbols._index) {
85             var symbol = this.symbols.getSymbol(p);
86             
87            // print(JSON.stringify(symbol, null,4));
88             
89             if (!symbol) continue;
90             
91             if (symbol.isPrivate) {
92                 this.symbols.deleteSymbol(symbol.alias);
93                 continue;
94             }
95             
96             if (symbol.is("FILE") || symbol.is("GLOBAL")) {
97                 continue;
98             }
99             //else if (!Options.a && !symbol.comment.isUserComment) {
100                 //print("Deleting Symbols (no a / user comment): " + symbol.alias);
101                 //this.symbols.deleteSymbol(symbol.alias);
102                 //this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
103             //}
104             
105             if (/#$/.test(symbol.alias)) { // we don't document prototypes - this should not happen..
106                 // rename the symbol ??
107                 /*if (!this.symbols.getSymbol(symbol.alias.substring(0,symbol.alias.length-1))) {
108                     // rename it..
109                     print("Renaming Symbol (got  a #): " + symbol.alias);
110                     var n = '' + symbol.alias;
111                     this.symbols.renameSymbol( n ,n.substring(0,n-1));
112                     this.filesSymbols[Symbol.srcFile].renameSymbol( n ,n.substring(0,n-1));
113                     continue;
114                 }
115                 */
116                 print("Deleting Symbols (got  a #): " + symbol.alias);
117                 
118                 this.symbols.deleteSymbol(symbol.alias);
119                 this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
120             
121             }
122         }
123         //print(prettyDump(toQDump(this.filesSymbols[Symbol.srcFile]._index,'{','}')));
124         //print("AfterParse: " + this.symbols.keys().toSource().split(",").join(",\n   "));
125         return this.symbols.toArray();
126     },
127
128         
129         addSymbol: function(symbol) 
130     {
131         //print("PARSER addSYMBOL : " + symbol.alias);
132         
133                 // if a symbol alias is documented more than once the last one with the user docs wins
134                 if (this.symbols.hasSymbol(symbol.alias)) {
135                         var oldSymbol = this.symbols.getSymbol(symbol.alias);
136             
137                         if (oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
138                                 if (symbol.comment.isUserComment) { // old and new are both documented
139                                         Options.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
140                                 }
141                                 else { // old is documented but new isn't
142                                         return;
143                                 }
144                         }
145                 }
146                 
147                 // we don't document anonymous things
148                 if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
149
150                 // uderscored things may be treated as if they were marked private, this cascades
151                 if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
152                         symbol.isPrivate = true;
153                 }
154                 
155                 // -p flag is required to document private things
156                 if ((symbol.isInner || symbol.isPrivate) && !Options.p) return;
157                 
158                 // ignored things are not documented, this doesn't cascade
159                 if (symbol.isIgnored) return;
160         // add it to the file's list... (for dumping later..)
161         if (Symbol.srcFile) {
162             this.filesSymbols[Symbol.srcFile].addSymbol(symbol);
163         }
164                 
165                 this.symbols.addSymbol(symbol);
166         },
167         
168         addBuiltin: function(name) {
169   
170                 var builtin = new Symbol(name, [], "CONSTRUCTOR", new DocComment(""));
171                 builtin.isNamespace = false;
172                 builtin.srcFile = "";
173                 builtin.isPrivate = false;
174         this.addSymbol(builtin);
175                 return builtin;
176         },
177         
178                 
179         finish: function() {
180                 this.symbols.relate();          
181                 
182                 // make a litle report about what was found
183                 if (this.conf.explain) {
184                         var symbols = this.symbols.toArray();
185                         var srcFile = "";
186                         for (var i = 0, l = symbols.length; i < l; i++) {
187                                 var symbol = symbols[i];
188                                 if (srcFile != symbol.srcFile) {
189                                         srcFile = symbol.srcFile;
190                                         print("\n"+srcFile+"\n-------------------");
191                                 }
192                                 print(i+":\n  alias => "+symbol.alias + "\n  name => "+symbol.name+ "\n  isa => "+symbol.isa + "\n  memberOf => " + symbol.memberOf + "\n  isStatic => " + symbol.isStatic + ",  isInner => " + symbol.isInner);
193                         }
194                         print("-------------------\n");
195                 }
196         },
197     /**
198      * return symbols so they can be serialized.
199      */
200     symbolsToObject : function(srcFile)
201     {
202         //this.filesSymbols[srcFile] is a symbolset..
203         return this.filesSymbols[srcFile];
204         
205             //    Parser.filesSymbols[srcFile]._index
206     }
207
208 }