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