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