09ac12bc06bf1d9bf1604bc716b0b5eaf751fc19
[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                 if (m[1].match(/\|/)) {
101                     var opts = m[1].split(/\|/);
102                     this.optvalues = opts;
103                     
104                 }
105                 
106                 
107             }
108             
109             
110             this.desc = src; // whatever is left
111             
112             // example tags need to have whitespace preserved
113             if (this.title != "example") this.desc = this.desc.trim();
114             
115             //if (JSDOC.PluginManager) {
116             //    JSDOC.PluginManager.run("onDocTag", this);
117             //}
118         },
119
120         /**
121             Automatically called when this is stringified.
122          */
123         toString : function() {
124             return this.desc;
125         },
126          
127
128         /**
129             Find and shift off the title of a tag.
130             @param {string} src
131             @return src
132          */
133         nibbleTitle : function(src) {
134             if (typeof src != "string") throw "src must be a string not "+(typeof src);
135             
136             var parts = src.match(/^\s*(\S+)(?:\s([\s\S]*))?$/);
137
138             if (parts && parts[1]) this.title = parts[1];
139             if (parts && parts[2]) src = parts[2];
140             else src = "";
141             
142             return src;
143         },
144          
145         /**
146             Find and shift off the type of a tag.
147             @requires frame/String.js
148             @param {string} src
149             @return src
150          */
151         nibbleType : function(src) 
152         {
153             if (typeof src != "string") throw "src must be a string not "+(typeof src);
154             
155             if (src.match(/^\s*\{/)) {
156                 var typeRange = this.balance(src,"{", "}");
157                 if (typeRange[1] == -1) {
158                     throw "Malformed comment tag ignored. Tag type requires an opening { and a closing }: "+src;
159                 }
160                 this.type = src.substring(typeRange[0]+1, typeRange[1]).trim();
161                 this.type = this.type.replace(/\s*,\s*/g, "|"); // multiples can be separated by , or |
162                 src = src.substring(typeRange[1]+1);
163             }
164             
165             return src;
166         },
167          
168
169         /**
170             Find and shift off the name of a tag.
171             @requires frame/String.js
172             @param {string} src
173             @return src
174          */
175         nibbleName : function(src) {
176             if (typeof src != "string") throw "src must be a string not "+(typeof src);
177             
178             src = src.trim();
179             
180             // is optional?
181             if (src.charAt(0) == "[") {
182                 var nameRange = this.balance(src,"[", "]");
183                 if (nameRange[1] == -1) {
184                     throw "Malformed comment tag ignored. Tag optional name requires an opening [ and a closing ]: "+src;
185                 }
186                 this.name = src.substring(nameRange[0]+1, nameRange[1]).trim();
187                 this.isOptional = true;
188                 
189                 src = src.substring(nameRange[1]+1);
190                 
191                 // has default value?
192                 var nameAndValue = this.name.split("=");
193                 if (nameAndValue.length) {
194                     this.name = nameAndValue.shift().trim();
195                     this.defaultValue = nameAndValue.join("=");
196                 }
197             }
198             else {
199                 var parts = src.match(/^(\S+)(?:\s([\s\S]*))?$/);
200                 if (parts) {
201                     if (parts[1]) this.name = parts[1];
202                     if (parts[2]) src = parts[2].trim();
203                     else src = "";
204                 }
205             }   
206
207             return src;
208         },
209         
210         balance : function(str, open, close) {
211             var i = 0;
212             while (str.charAt(i) != open) {
213                 if (i == str.length) return [-1, -1];
214                 i++;
215             }
216             
217             var j = i+1;
218             var balance = 1;
219             while (j < str.length) {
220                 if (str.charAt(j) == open) balance++;
221                 if (str.charAt(j) == close) balance--;
222                 if (balance == 0) break;
223                 j++;
224                 if (j == str.length) return [-1, -1];
225             }
226             
227             return [i, j];
228 }
229
230         
231         
232 });
233
234 // cached support?
235 DocTag.fromDump = function(t)
236 {
237     var ns = new DocTag();
238     for (var i in t) {
239         if (typeof(ns[i]) == "undefined") {
240             println("ERR:no default for DocTag:"+ i);
241         }
242        ns[i] = t[i];
243     }
244     return ns;
245 }