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