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