src/jsdoc/DocParser.vala
[roojspacker] / src / jsdoc / DocParser.vala
1
2 namespace JSDOC
3 {
4
5         public class DocParser : Object 
6         {
7                 // options - should they bee in PackerRun?
8                 static bool ignoreAnonymous =            true; 
9                 static bool treatUnderscoredAsPrivate = true;
10                 static bool explain=             false;
11  
12                 
13                 static bool has_init = false;
14                 static DocWalker walker ;
15             static SymbolSet symbols ;
16             
17             public static string currentSourceFile;
18     
19             static Gee.HashMap<string,SymbolSet> filesSymbols;
20                 
21                 // no CTOR.. it's mostly static!!
22
23                 private static void initStatic()
24                 {
25                         if (DocParser.has_init) {
26                                 return ;
27                         }
28                         DocParser.symbols = new  SymbolSet();
29                         DocParser.filesSymbols = new  Gee.HashMap<string,SymbolSet>();
30                         
31                         DocParser.has_init = true;
32                 
33            }
34                 
35                 
36                 public static void parse(TokenStream ts, string srcFile) 
37                 {
38                     
39                     DocParser.currentSourceFile = srcFile;
40                     // not a nice way to set stuff...
41                    
42                     DocComment.shared = ""; // shared comments don't cross file boundaries
43                     
44                    
45                     this.filesSymbols.set(srcFile, new SymbolSet());
46                     
47                     //Options.LOG.inform("Parser - run walker");
48                     this.walker = new  Walker2(ts);
49                     this.walker.buildSymbolTree();
50                     
51                     
52                     
53                     //this.walker.walk(ts); // adds to our symbols
54                    // throw "done sym tree";
55                     //Options.LOG.inform("Parser - checking symbols");
56                     // filter symbols by option
57                     for (p in DocParser.symbols._index) {
58                         var symbol = DocParser.symbols.getSymbol(p);
59                         
60                        // print(JSON.stringify(symbol, null,4));
61                         
62                         if (!symbol) continue;
63                         
64                         if (symbol.isPrivate) {
65                             DocParser.symbols.deleteSymbol(symbol.alias);
66                             DocParser.filesSymbols.get(srcFile).deleteSymbol(symbol.alias);
67                             continue;
68                         }
69                         
70                         if (symbol.is("FILE") || symbol.is("GLOBAL")) {
71                             continue;
72                         }
73                        
74                         
75                         if (symbol.alias.substring(symbol.alias-1) = "#")) { // we don't document prototypes - this should not happen..
76                             
77                             print("Deleting Symbols (alias ends in #): " + symbol.alias);
78                             
79                             DocParser.symbols.deleteSymbol(symbol.alias);
80                             DocParser.filesSymbols.get(srcFile).deleteSymbol(symbol.alias);
81                         
82                         }
83                     }
84                     //print(prettyDump(toQDump(this.filesSymbols[Symbol.srcFile]._index,'{','}')));
85                     //print("AfterParse: " + this.symbols.keys().toSource().split(",").join(",\n   "));
86                     return; //this.symbols.toArray();
87                 }
88
89         
90                 public static void addSymbol(Symbol symbol) 
91                 {
92                     //print("PARSER addSYMBOL : " + symbol.alias);
93                     
94                         // if a symbol alias is documented more than once the last one with the user docs wins
95                         if (DocParser.symbols.hasSymbol(symbol.alias)) {
96                                 var oldSymbol = DocParser.symbols.getSymbol(symbol.alias);
97                         
98                                 if (oldSymbol.comment.isUserComment && !oldSymbol.comment.hasTags) {
99                                         if (symbol.comment.isUserComment) { // old and new are both documented
100                                                 GLib.debug("The symbol '%s' is documented more than once.",symbol.alias);
101                                                 // we use the new one???
102                                         } else { // old is documented but new isn't
103                                                 return;
104                                         }
105                                 }
106                         }
107                 
108                         // we don't document anonymous things
109                         if (DocParser.ignoreAnonymous && symbol.name.index_of("$anonymous\b") > -1) {
110                                  return;
111                         }
112
113                         // uderscored things may be treated as if they were marked private, this cascades
114                         if (DocParser.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
115                                 symbol.isPrivate = true;
116                         }
117                 
118                         // -p flag is required to document private things
119                         if ((symbol.isInner || symbol.isPrivate) && !PackerRun.opt_doc_include_private) {
120                                  return;
121                         }
122                 
123                         // ignored things are not documented, this doesn't cascade
124                         if (symbol.isIgnored) {
125                                 return;
126                         }
127                     // add it to the file's list... (for dumping later..)
128                     if (Symbol.srcFile != null) {
129                         DocParser.filesSymbols.get(Symbol.srcFile).addSymbol(symbol);
130                     }
131                 
132                         DocParser.symbols.addSymbol(symbol);
133                 }
134         
135                 public static Symbol addBuiltin(string name) 
136                 {
137                         var builtin = new Symbol.new_builtin(name);
138                     DocParser.addSymbol(builtin);
139                         return builtin;
140                 }
141         
142                 
143                 public static  void finish() {
144                         this.symbols.relate();          
145                 
146                         // make a litle report about what was found
147                         /*
148                         if (this.conf.explain) {
149                                 var symbols = this.symbols.toArray();
150                                 var srcFile = "";
151                                 for (var i = 0, l = symbols.length; i < l; i++) {
152                                         var symbol = symbols[i];
153                                         if (srcFile != symbol.srcFile) {
154                                                 srcFile = symbol.srcFile;
155                                                 print("\n"+srcFile+"\n-------------------");
156                                         }
157                                         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);
158                                 }
159                                 print("-------------------\n");
160                         }
161                         */
162         },
163     /**
164      * return symbols so they can be serialized.
165      */
166     symbolsToObject : function(srcFile)
167     {
168         //this.filesSymbols[srcFile] is a symbolset..
169         return this.filesSymbols[srcFile];
170         
171             //    Parser.filesSymbols[srcFile]._index
172     }
173
174 }