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