JSDOC/DocTag.js
[gnome.introspection-doc-generator] / JSDOC / DocTag.js
1 //<script  type="text/javascript">
2  
3  
4 XObject = imports.XObject.XObject;
5
6 Options = imports.Options.Options;
7
8  
9 /**
10  * DocTag - represents a single A=b tag.
11  * @class DocTag
12  */
13  
14  
15 DocTag = XObject.define(
16
17 /**
18  * @constructor
19  * @arg {String} src
20  */
21
22     function(src) {
23         this.title        = "";
24         this.type         = "";
25         this.name         = "";
26         this.isOptional   = false;
27         this.defaultValue = "";
28         this.desc         = "";
29         if (typeof src != "undefined") {
30             this.parse(src);
31         }
32     }, 
33     Object,
34     {
35         
36         title: '',
37         type: '',
38         name : '',
39         isOptional : false,
40         defaultValue : '',
41         desc : '',
42         /**
43          * serialize..
44          */
45         toObject :function(t)
46         {
47             
48             var ret = { };
49             for (var i in this) {
50                 if (Symbol.hide.indexOf(i) > -1) {
51                     continue;
52                 }
53                 switch (typeof(this[i])) {
54                     case 'function':
55                        continue;
56                        continue;
57                         
58                     case 'string':
59                     case 'number':
60                     case 'boolean':                    
61                         ret[i] = this[i]; continue;
62                     default:
63                         print("unknown type:" + typeof(this[i]));
64                         Seed.quit();
65                    }
66             }
67             return ret;
68         },
69         
70
71
72         /**
73             Populate the properties of this from the given tag src.
74             @param {string} src
75          */
76         parse : function(src) {
77             if (typeof src != "string") throw "src must be a string not "+(typeof src);
78
79             try {
80                 src = this.nibbleTitle(src);
81                 //if (JSDOC.PluginManager) {
82                 //    JSDOC.PluginManager.run("onDocTagSynonym", this);
83                // }
84                 
85                 src = this.nibbleType(src);
86                 
87                 // only some tags are allowed to have names.
88                 if (this.title == "param" || this.title == "property" || this.title == "cfg") { // @config is deprecated
89                     src = this.nibbleName(src);
90                 }
91             }
92             catch(e) {
93                 if (Options.LOG) Options.LOG.warn(e);
94                 else throw e;
95             }
96             this.desc = src; // whatever is left
97             
98             // example tags need to have whitespace preserved
99             if (this.title != "example") this.desc = this.desc.trim();
100             
101             //if (JSDOC.PluginManager) {
102             //    JSDOC.PluginManager.run("onDocTag", this);
103             //}
104         },
105
106         /**
107             Automatically called when this is stringified.
108          */
109         toString : function() {
110             return this.desc;
111         },
112          
113
114         /**
115             Find and shift off the title of a tag.
116             @param {string} src
117             @return src
118          */
119         nibbleTitle : function(src) {
120             if (typeof src != "string") throw "src must be a string not "+(typeof src);
121             
122             var parts = src.match(/^\s*(\S+)(?:\s([\s\S]*))?$/);
123
124             if (parts && parts[1]) this.title = parts[1];
125             if (parts && parts[2]) src = parts[2];
126             else src = "";
127             
128             return src;
129         },
130          
131         /**
132             Find and shift off the type of a tag.
133             @requires frame/String.js
134             @param {string} src
135             @return src
136          */
137         nibbleType : function(src) 
138         {
139             if (typeof src != "string") throw "src must be a string not "+(typeof src);
140             
141             if (src.match(/^\s*\{/)) {
142                 var typeRange = this.balance(src,"{", "}");
143                 if (typeRange[1] == -1) {
144                     throw "Malformed comment tag ignored. Tag type requires an opening { and a closing }: "+src;
145                 }
146                 this.type = src.substring(typeRange[0]+1, typeRange[1]).trim();
147                 this.type = this.type.replace(/\s*,\s*/g, "|"); // multiples can be separated by , or |
148                 src = src.substring(typeRange[1]+1);
149             }
150             
151             return src;
152         },
153          
154
155         /**
156             Find and shift off the name of a tag.
157             @requires frame/String.js
158             @param {string} src
159             @return src
160          */
161         nibbleName : function(src) {
162             if (typeof src != "string") throw "src must be a string not "+(typeof src);
163             
164             src = src.trim();
165             
166             // is optional?
167             if (src.charAt(0) == "[") {
168                 var nameRange = this.balance(src,"[", "]");
169                 if (nameRange[1] == -1) {
170                     throw "Malformed comment tag ignored. Tag optional name requires an opening [ and a closing ]: "+src;
171                 }
172                 this.name = src.substring(nameRange[0]+1, nameRange[1]).trim();
173                 this.isOptional = true;
174                 
175                 src = src.substring(nameRange[1]+1);
176                 
177                 // has default value?
178                 var nameAndValue = this.name.split("=");
179                 if (nameAndValue.length) {
180                     this.name = nameAndValue.shift().trim();
181                     this.defaultValue = nameAndValue.join("=");
182                 }
183             }
184             else {
185                 var parts = src.match(/^(\S+)(?:\s([\s\S]*))?$/);
186                 if (parts) {
187                     if (parts[1]) this.name = parts[1];
188                     if (parts[2]) src = parts[2].trim();
189                     else src = "";
190                 }
191             }   
192
193             return src;
194         },
195         
196         balance : function(str, open, close) {
197             var i = 0;
198             while (str.charAt(i) != open) {
199                 if (i == str.length) return [-1, -1];
200                 i++;
201             }
202             
203             var j = i+1;
204             var balance = 1;
205             while (j < str.length) {
206                 if (str.charAt(j) == open) balance++;
207                 if (str.charAt(j) == close) balance--;
208                 if (balance == 0) break;
209                 j++;
210                 if (j == str.length) return [-1, -1];
211             }
212             
213             return [i, j];
214 }
215
216         
217         
218 });
219
220 // cached support?
221 DocTag.fromDump = function(t)
222 {
223     var ns = new DocTag();
224     for (var i in t) {
225         if (typeof(ns[i]) == "undefined") {
226             println("ERR:no default for DocTag:"+ i);
227         }
228        ns[i] = t[i];
229     }
230     return ns;
231 }