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