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.Options.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         //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                 continue;
93             }
94             
95             if (symbol.is("FILE") || symbol.is("GLOBAL")) {
96                 continue;
97             }
98             //else if (!Options.a && !symbol.comment.isUserComment) {
99                 //print("Deleting Symbols (no a / user comment): " + symbol.alias);
100                 //this.symbols.deleteSymbol(symbol.alias);
101                 //this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
102             //}
103             
104             if (/#$/.test(symbol.alias)) { // we don't document prototypes - this should not happen..
105                 // rename the symbol ??
106                 /*if (!this.symbols.getSymbol(symbol.alias.substring(0,symbol.alias.length-1))) {
107                     // rename it..
108                     print("Renaming Symbol (got  a #): " + symbol.alias);
109                     var n = '' + symbol.alias;
110                     this.symbols.renameSymbol( n ,n.substring(0,n-1));
111                     this.filesSymbols[Symbol.srcFile].renameSymbol( n ,n.substring(0,n-1));
112                     continue;
113                 }
114                 */
115                 print("Deleting Symbols (got  a #): " + symbol.alias);
116                 
117                 this.symbols.deleteSymbol(symbol.alias);
118                 this.filesSymbols[Symbol.srcFile].deleteSymbol(symbol.alias);
119             
120             }
121         }
122         //print(prettyDump(toQDump(this.filesSymbols[Symbol.srcFile]._index,'{','}')));
123         //print("AfterParse: " + this.symbols.keys().toSource().split(",").join(",\n   "));
124         return this.symbols.toArray();
125     },
126
127         
128         addSymbol: function(symbol) 
129     {
130          //print("PARSER addSYMBOL : " + symbol.alias);
131         
132                 // if a symbol alias is documented more than once the last one with the user docs wins
133                 if (this.symbols.hasSymbol(symbol.alias)) {
134                         var oldSymbol = this.symbols.getSymbol(symbol.alias);
135             
136                         if (oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
137                                 if (symbol.comment.isUserComment) { // old and new are both documented
138                                         Options.LOG.warn("The symbol '"+symbol.alias+"' is documented more than once.");
139                                 }
140                                 else { // old is documented but new isn't
141                                         return;
142                                 }
143                         }
144                 }
145                 
146                 // we don't document anonymous things
147                 if (this.conf.ignoreAnonymous && symbol.name.match(/\$anonymous\b/)) return;
148
149                 // uderscored things may be treated as if they were marked private, this cascades
150                 if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
151                         symbol.isPrivate = true;
152                 }
153                 
154                 // -p flag is required to document private things
155                 if ((symbol.isInner || symbol.isPrivate) && !Options.p) return;
156                 
157                 // ignored things are not documented, this doesn't cascade
158                 if (symbol.isIgnored) return;
159         // add it to the file's list... (for dumping later..)
160         if (Symbol.srcFile) {
161             this.filesSymbols[Symbol.srcFile].addSymbol(symbol);
162         }
163                 
164                 this.symbols.addSymbol(symbol);
165         },
166         
167         addBuiltin: function(name) {
168   
169                 var builtin = new Symbol(name, [], "CONSTRUCTOR", new DocComment(""));
170                 builtin.isNamespace = false;
171                 builtin.srcFile = "";
172                 builtin.isPrivate = false;
173         this.addSymbol(builtin);
174                 return builtin;
175         },
176         
177                 
178         finish: function() {
179                 this.symbols.relate();          
180                 
181                 // make a litle report about what was found
182                 if (this.conf.explain) {
183                         var symbols = this.symbols.toArray();
184                         var srcFile = "";
185                         for (var i = 0, l = symbols.length; i < l; i++) {
186                                 var symbol = symbols[i];
187                                 if (srcFile != symbol.srcFile) {
188                                         srcFile = symbol.srcFile;
189                                         print("\n"+srcFile+"\n-------------------");
190                                 }
191                                 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);
192                         }
193                         print("-------------------\n");
194                 }
195         }
196     
197     
198     
199
200 }