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