reset to working status
[gnome.introspection-doc-generator] / JSDOC / DocTag.js
1 //<script  type="text/javascript">
2  
3  
4 XObject = imports.XObject.XObject;
5
6 Options = imports.BuildDocs.Options;
7
8  
9 /**
10  * DocTag - represents a single A=b tag.
11  * @class DocTag
12  */
13  
14  
15 DocTag = XObject.define(
16     function(src) {
17         this.title        = "";
18         this.type         = "";
19         this.name         = "";
20         this.isOptional   = false;
21         this.defaultValue = "";
22         this.desc         = "";
23         if (typeof src != "undefined") {
24             this.parse(src);
25         }
26     }, 
27     Object,
28     {
29         
30         title: '',
31         type: '',
32         name : '',
33         isOptional : false,
34         defaultValue : '',
35         desc : '',
36         /* qdump needed for cahcing?
37         toQDump  :function(t)
38         {
39             return JSDOC.toQDump(t, 'JSDOC.DocTag.fromDump({', '})', new JSDOC.DocTag());
40         } ,
41         */
42
43
44         /**
45             Populate the properties of this from the given tag src.
46             @param {string} src
47          */
48         parse : function(src) {
49             if (typeof src != "string") throw "src must be a string not "+(typeof src);
50
51             try {
52                 src = this.nibbleTitle(src);
53                 //if (JSDOC.PluginManager) {
54                 //    JSDOC.PluginManager.run("onDocTagSynonym", this);
55                // }
56                 
57                 src = this.nibbleType(src);
58                 
59                 // only some tags are allowed to have names.
60                 if (this.title == "param" || this.title == "property" || this.title == "cfg") { // @config is deprecated
61                     src = this.nibbleName(src);
62                 }
63             }
64             catch(e) {
65                 if (Options.LOG) Options.warn(e);
66                 else throw e;
67             }
68             this.desc = src; // whatever is left
69             
70             // example tags need to have whitespace preserved
71             if (this.title != "example") this.desc = this.desc.trim();
72             
73             //if (JSDOC.PluginManager) {
74             //    JSDOC.PluginManager.run("onDocTag", this);
75             //}
76         },
77
78         /**
79             Automatically called when this is stringified.
80          */
81         toString : function() {
82             return this.desc;
83         },
84          
85
86         /**
87             Find and shift off the title of a tag.
88             @param {string} src
89             @return src
90          */
91         nibbleTitle : function(src) {
92             if (typeof src != "string") throw "src must be a string not "+(typeof src);
93             
94             var parts = src.match(/^\s*(\S+)(?:\s([\s\S]*))?$/);
95
96             if (parts && parts[1]) this.title = parts[1];
97             if (parts && parts[2]) src = parts[2];
98             else src = "";
99             
100             return src;
101         },
102          
103         /**
104             Find and shift off the type of a tag.
105             @requires frame/String.js
106             @param {string} src
107             @return src
108          */
109         nibbleType : function(src) 
110         {
111             if (typeof src != "string") throw "src must be a string not "+(typeof src);
112             
113             if (src.match(/^\s*\{/)) {
114                 var typeRange = src.balance("{", "}");
115                 if (typeRange[1] == -1) {
116                     throw "Malformed comment tag ignored. Tag type requires an opening { and a closing }: "+src;
117                 }
118                 this.type = src.substring(typeRange[0]+1, typeRange[1]).trim();
119                 this.type = this.type.replace(/\s*,\s*/g, "|"); // multiples can be separated by , or |
120                 src = src.substring(typeRange[1]+1);
121             }
122             
123             return src;
124         },
125          
126
127         /**
128             Find and shift off the name of a tag.
129             @requires frame/String.js
130             @param {string} src
131             @return src
132          */
133         nibbleName : function(src) {
134             if (typeof src != "string") throw "src must be a string not "+(typeof src);
135             
136             src = src.trim();
137             
138             // is optional?
139             if (src.charAt(0) == "[") {
140                 var nameRange = src.balance("[", "]");
141                 if (nameRange[1] == -1) {
142                     throw "Malformed comment tag ignored. Tag optional name requires an opening [ and a closing ]: "+src;
143                 }
144                 this.name = src.substring(nameRange[0]+1, nameRange[1]).trim();
145                 this.isOptional = true;
146                 
147                 src = src.substring(nameRange[1]+1);
148                 
149                 // has default value?
150                 var nameAndValue = this.name.split("=");
151                 if (nameAndValue.length) {
152                     this.name = nameAndValue.shift().trim();
153                     this.defaultValue = nameAndValue.join("=");
154                 }
155             }
156             else {
157                 var parts = src.match(/^(\S+)(?:\s([\s\S]*))?$/);
158                 if (parts) {
159                     if (parts[1]) this.name = parts[1];
160                     if (parts[2]) src = parts[2].trim();
161                     else src = "";
162                 }
163             }   
164
165             return src;
166         }
167 });
168
169 // cached support?
170 DocTag.fromDump = function(t)
171 {
172     var ns = new DocTag();
173     for (var i in t) {
174         if (typeof(ns[i]) == "undefined") {
175             println("ERR:no default for DocTag:"+ i);
176         }
177        ns[i] = t[i];
178     }
179     return ns;
180 }