JSDOC/Parser.js
[gnome.introspection-doc-generator] / JSDOC / Parser.js
1 //<script type="text/javascript">
2
3 Walker2      = imports.Walker2.Walker2;
4 Symbol      = imports.Symbol.Symbol;
5 SymbolSet      = imports.SymbolSet.SymbolSet;
6 DocComment  = imports.DocComment.DocComment;
7 Options     = imports.BuildDocs.Options;
8 /**
9  * Parser is a static  instance..
10  * 
11  * 
12  */
13  
14  
15 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         this.walker = new  Walker2(ts);
75         this.walker.buildSymbolTree()
76         //this.walker.walk(ts); // adds to our symbols
77        // throw "done sym tree";
78         
79         // filter symbols by option
80         for (p in this.symbols._index) {
81             var symbol = this.symbols.getSymbol(p);
82             
83             if (!symbol) continue;
84             
85             if (symbol.is("FILE") || symbol.is("GLOBAL")) {
86                 continue;
87             }
88             //else if (!Options.a && !symbol.comment.isUserComment) {
89                 //print("Deleting Symbols (no a / user comment): " + symbol.alias);
90                 //this.symbols.deleteSymbol(symbol.alias);
91                 //this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
92             //}
93             
94             if (/#$/.test(symbol.alias)) { // we don't document prototypes - this should not happen..
95                 // rename the symbol ??
96                 /*if (!this.symbols.getSymbol(symbol.alias.substring(0,symbol.alias.length-1))) {
97                     // rename it..
98                     print("Renaming Symbol (got  a #): " + symbol.alias);
99                     var n = '' + symbol.alias;
100                     this.symbols.renameSymbol( n ,n.substring(0,n-1));
101                     this.filesSymbols[Symbol.srcFile].renameSymbol( n ,n.substring(0,n-1));
102                     continue;
103                 }
104                 */
105                 print("Deleting Symbols (got  a #): " + symbol.alias);
106                 
107                 this.symbols.deleteSymbol(symbol.alias);
108                 this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
109             
110             }
111         }
112         //print(prettyDump(toQDump(this.filesSymbols[Symbol.srcFile]._index,'{','}')));
113         //print("AfterParse: " + this.symbols.keys().toSource().split(",").join(",\n   "));
114         return this.symbols.toArray();
115     },
116
117         
118         addSymbol: function(symbol) 
119     {
120          print("PARSER addSYMBOL : " + symbol.alias);
121         
122                 // if a symbol alias is documented more than once the last one with the user docs wins
123                 if (this.symbols.hasSymbol(symbol.alias)) {
124                         var oldSymbol = this.symbols.getSymbol(symbol.alias);
125             
126                         if (oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
127                                 if (symbol.comment.isUserComment) { // old and new are both documented
128                                         Options.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
129                                 }
130                                 else { // old is documented but new isn't
131                                         return;
132                                 }
133                         }
134                 }
135                 
136                 // we don't document anonymous things
137                 if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
138
139                 // uderscored things may be treated as if they were marked private, this cascades
140                 if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
141                         symbol.isPrivate = true;
142                 }
143                 
144                 // -p flag is required to document private things
145                 if ((symbol.isInner || symbol.isPrivate) && !Options.p) return;
146                 
147                 // ignored things are not documented, this doesn't cascade
148                 if (symbol.isIgnored) return;
149         // add it to the file's list... (for dumping later..)
150         if (Symbol.srcFile) {
151             this.filesSymbols[Symbol.srcFile].addSymbol(symbol);
152         }
153                 
154                 this.symbols.addSymbol(symbol);
155         },
156         
157         addBuiltin: function(name) {
158   
159                 var builtin = new Symbol(name, [], "CONSTRUCTOR", new DocComment(""));
160                 builtin.isNamespace = false;
161                 builtin.srcFile = "";
162                 builtin.isPrivate = false;
163         this.addSymbol(builtin);
164                 return builtin;
165         },
166         
167                 
168         finish: function() {
169                 this.symbols.relate();          
170                 
171                 // make a litle report about what was found
172                 if (this.conf.explain) {
173                         var symbols = this.symbols.toArray();
174                         var srcFile = "";
175                         for (var i = 0, l = symbols.length; i < l; i++) {
176                                 var symbol = symbols[i];
177                                 if (srcFile != symbol.srcFile) {
178                                         srcFile = symbol.srcFile;
179                                         print("\n"+srcFile+"\n-------------------");
180                                 }
181                                 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);
182                         }
183                         print("-------------------\n");
184                 }
185         }
186     
187     
188     
189
190 }