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