4 @class Represents a symbol in the source code.
11 public class Symbol : Object
14 private static bool regex_init = false;
15 private GLib.Regex regex_global;
16 private GLib.Regex regex_prototype;
18 static void regexInit()
20 if (Symbol.regex_init = true) {
23 Symbol.regex_init = true;
24 Symbol.regex_global = new GLib.Regex("^_global_[.#-]");
25 Symbol.regex_prototype = new GLib.Regex("\\.prototype\\.?");
28 private string private_name = ""; // should not be directly accessed ??
30 private string set_name {
33 var n = Symbol.regex_global(value, value.length, 0, "")
34 n = Symbol.regex_prototype(n,n.length, 0, "#");
36 if (!n.has_suffix("#")) {
39 n = n.substring(0, n.length-1);
42 this.private_name = n;
47 get { return this.private_name; }
51 string defaultValue : "",
56 augments : [], // Doctag[]
57 exceptions : [], // Doctag[]
58 inherits : [], // Doctag[]
59 methods : [], // Symbol[]
61 _params : [], //Doctag[]
62 properties : [], //Doctag[]
63 requires : [], //Doctag[]
64 returns : [], //Doctag[]
74 //$args : [], // original arguments used when constructing.
76 public string alias = "";
79 string classDesc = "";
81 string deprecated = "";
87 string isa = "OBJECT"; // OBJECT//FUNCTION
89 public bool isEvent = false;
90 public bool isConstant = false;
91 public bool isIgnored = false;
92 public bool isInner = false;
93 public bool isNamespace = false;
94 public bool isPrivate = false;
95 public bool isStatic = false;
111 public void initArrays()
113 // only initialize arrays / objects..
120 this.exceptions = [];
123 this.isa = "OBJECT"; // OBJECT//FUNCTION
126 this.properties = [];
135 this.inheritsFrom = [];
136 this.childClasses = [];
138 this.comment = new DocComment();
139 this.comment.isUserComment = false;
144 Public Symbol.new_builtin(string name)
148 this.srcFile = DocParser.currentSourceFile;
150 this.alias = this.name;
151 this.isa = "CONSTRUCTOR";
152 this.comment = new DocComment("");
153 this.comment.isUserComment = false;
154 this.isNamespace = false;
156 this.isPrivate = false;
164 //__defineSetter__("name",
167 //__defineGetter__("name",
168 getName : function() { return this._name; },
170 //__defineSetter__("params",
171 setParams :function(v) {
172 for (var i = 0, l = v.length; i < l; i++) {
173 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
174 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
175 this._params[i] = new DocTag(
176 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
179 this._params[i] = v[i];
182 this.params = this._params;
187 //__defineGetter__("params",
188 public getParams : function() {
193 public Symbol.new_populate_with_args(
195 Gee.ArrayList<string> params, // fixme???
201 this.$args = arguments;
202 //println("Symbol created: " + isa + ":" + name);
204 this.alias = this.getName();
205 this.setParams(params);
206 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
207 this.comment = comment || new DocComment("")
209 this.srcFile = DocParser.currentSourceFile;
213 if (this.is("FILE") && !this.alias) { // this will never hapen???
214 this.alias = this.srcFile;
221 setTags : function() {
223 var authors = this.comment.getTag("author");
224 if (authors.length) {
225 this.author = authors.map(function($){return $.desc;}).join(", ");
229 assert("testing Symbol");
231 requires("../lib/JSDOC/DocComment.js");
232 requires("../frame/String.js");
233 requires("../lib/JSDOC/DocTag.js");
235 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
236 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
239 var mth = this.comment.getTag("method");
241 this.isa = "FUNCTION";
244 var descs = this.comment.getTag("desc");
246 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
250 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
251 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
255 if (this.is("FILE")) {
256 if (!this.alias) this.alias = this.srcFile;
258 var overviews = this.comment.getTag("overview");
259 if (overviews.length) {
260 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
265 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
266 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
270 var sinces = this.comment.getTag("since");
272 this.since = sinces.map(function($){return $.desc;}).join(", ");
276 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
277 assertEqual(sym.since, "1.01", "@since tag, description is found.");
281 if (this.comment.getTag("constant").length) {
282 this.isConstant = true;
287 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
288 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
292 var versions = this.comment.getTag("version");
293 if (versions.length) {
294 this.version = versions.map(function($){return $.desc;}).join(", ");
298 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
299 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
303 var deprecateds = this.comment.getTag("deprecated");
304 if (deprecateds.length) {
305 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
309 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
310 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
314 var examples = this.comment.getTag("example");
315 if (examples.length) {
316 this.example = examples[0];
320 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
321 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
325 var sees = this.comment.getTag("see");
327 var thisSee = this.see;
328 sees.map(function($){thisSee.push($.desc);});
332 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
333 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
337 var classes = this.comment.getTag("class");
338 if (classes.length) {
339 //print(JSON.stringify(this,null,4));
340 this.isa = "CONSTRUCTOR";
341 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
342 if (!this.classDesc) {
343 this.classDesc = this.desc;
350 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
351 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
352 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
356 var namespaces = this.comment.getTag("namespace");
357 if (namespaces.length) {
358 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
359 this.isNamespace = true;
363 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
364 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
368 var params = this.comment.getTag("param");
370 // user-defined params overwrite those with same name defined by the parser
371 var thisParams = this.getParams();
373 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
374 this.setParams(params);
376 else { // need to overlay these user-defined params on to existing parser-defined params
377 for (var i = 0, l = params.length; i < l; i++) {
379 if (params[i].type) thisParams[i].type = params[i].type;
380 thisParams[i].name = params[i].name;
381 thisParams[i].desc = params[i].desc;
382 thisParams[i].isOptional = params[i].isOptional;
383 thisParams[i].defaultValue = params[i].defaultValue;
385 else thisParams[i] = params[i];
391 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
392 assertEqual(sym.params.length, 1, "parser defined param is found.");
394 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
395 assertEqual(sym.params.length, 1, "user defined param is found.");
396 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
397 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
399 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
400 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
401 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
402 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
404 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
405 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
406 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
407 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
409 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
410 assertEqual(sym.params.length, 1, "user defined param with description is found.");
411 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
415 if (this.comment.getTag("constructor").length) {
416 this.isa = "CONSTRUCTOR";
420 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
421 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
425 if (this.comment.getTag("static").length) {
426 this.isStatic = true;
427 if (this.isa == "CONSTRUCTOR") {
428 this.isNamespace = true;
433 if (this.comment.getTag("singleton").length) {
434 this.isStatic = true;
435 //print('------------- got singleton ---------------' + this.isa);
436 //if (this.isa == "CONSTRUCTOR") {
437 // this.isNamespace = true;
444 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
445 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
446 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
450 if (this.comment.getTag("inner").length) {
452 this.isStatic = false;
456 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
457 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
458 assertEqual(sym.isInner, true, "@inner makes isInner true.");
462 if (this.comment.getTag("field").length) {
467 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
468 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
472 if (this.comment.getTag("function").length) {
473 this.isa = "FUNCTION";
477 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
478 // change a property to a function..
479 this.isa = "FUNCTION";
484 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
485 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
489 var events = this.comment.getTag("event");
491 this.isa = "FUNCTION";
496 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
497 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
498 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
502 var names = this.comment.getTag("name");
504 this.setName(names[0].desc);
512 var properties = this.comment.getTag("property");
513 if (properties.length) {
514 thisProperties = this.properties;
515 for (var i = 0; i < properties.length; i++) {
516 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
517 // TODO: shouldn't the following happen in the addProperty method of Symbol?
518 property.name = properties[i].name;
519 property.memberOf = this.alias;
520 if (properties[i].type) property.type = properties[i].type;
521 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
522 this.addProperty(property);
523 imports.Parser.Parser.addSymbol(property);
528 var conf = this.comment.getTag("cfg");
530 for (var i = 0; i < conf.length; i++) {
531 this.addConfig(conf[i]);
540 var returns = this.comment.getTag("return");
541 if (returns.length) { // there can be many return tags in a single doclet
542 this.returns = returns;
543 this.type = returns.map(function($){return $.type}).join(", ");
551 this.exceptions = this.comment.getTag("throws");
558 var requires = this.comment.getTag("requires");
559 if (requires.length) {
560 this.requires = requires.map(function($){return $.desc});
568 var types = this.comment.getTag("type");
570 this.type = types[0].desc; //multiple type tags are ignored
578 if (this.comment.getTag("private").length || this.isInner) {
579 this.isPrivate = true;
583 if (this.comment.getTag("ignore").length) {
584 this.isIgnored = true;
591 // @inherits ... as ...
592 var inherits = this.comment.getTag("inherits");
593 if (inherits.length) {
594 for (var i = 0; i < inherits.length; i++) {
595 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
596 var inAlias = RegExp.$1;
597 var inAs = RegExp.$2 || inAlias;
599 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
602 inAs = inAs.replace(/\.prototype\.?/g, "#");
603 inAs = inAs.replace(/^this\.?/, "#");
606 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
608 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
611 inAs = this.alias + joiner + inAs;
614 this.inherits.push({alias: inAlias, as: inAs});
623 this.augments = this.comment.getTag("augments");
626 if (this.comment.getTag("extends")) {
627 this.augments = this.comment.getTag("extends");
632 var defaults = this.comment.getTag("default");
633 if (defaults.length) {
634 if (this.is("OBJECT")) {
635 this.defaultValue = defaults[0].desc;
644 var memberOfs = this.comment.getTag("memberOf");
645 if (memberOfs.length) {
646 this.memberOf = memberOfs[0].desc;
647 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
648 this.name = this.name.split('.').pop();
649 this.name = this.name.split('#').pop();
650 this.name = this.memberOf + this.name;
651 this._name = this.name
652 this.alias = this.name;
660 if (this.comment.getTag("public").length) {
661 this.isPrivate = false;
669 is : function(what) {
670 return this.isa === what;
673 isBuiltin : function() {
674 return SymbolSet.isBuiltin(this.alias);
677 setType : function(/**String*/comment, /**Boolean*/overwrite) {
678 if (!overwrite && this.type) return;
679 var typeComment = DocComment.unwrapComment(comment);
680 this.type = typeComment;
683 inherit : function(symbol) {
684 if (!this.hasMember(symbol.name) && !symbol.isInner) {
685 if (symbol.is("FUNCTION"))
686 this.methods.push(symbol);
687 else if (symbol.is("OBJECT"))
688 this.properties.push(symbol);
692 hasMember : function(name) {
693 return (this.hasMethod(name) || this.hasProperty(name));
696 addMember : function(symbol) {
697 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
699 if (symbol.comment.getTag("cfg").length == 1) {
700 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
701 this.addConfig(symbol.comment.getTag("cfg")[0]);
705 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
706 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
709 hasMethod : function(name) {
710 var thisMethods = this.methods;
711 for (var i = 0, l = thisMethods.length; i < l; i++) {
712 if (thisMethods[i].name == name) return true;
713 if (thisMethods[i].alias == name) return true;
718 addMethod : function(symbol) {
719 var methodAlias = symbol.alias;
720 var thisMethods = this.methods;
721 for (var i = 0, l = thisMethods.length; i < l; i++) {
722 if (thisMethods[i].alias == methodAlias) {
723 thisMethods[i] = symbol; // overwriting previous method
727 thisMethods.push(symbol); // new method with this alias
730 hasProperty : function(name) {
731 var thisProperties = this.properties;
732 for (var i = 0, l = thisProperties.length; i < l; i++) {
733 if (thisProperties[i].name == name) return true;
734 if (thisProperties[i].alias == name) return true;
739 addProperty : function(symbol) {
740 var propertyAlias = symbol.alias;
741 var thisProperties = this.properties;
742 for (var i = 0, l = thisProperties.length; i < l; i++) {
743 if (thisProperties[i].alias == propertyAlias) {
744 thisProperties[i] = symbol; // overwriting previous property
749 thisProperties.push(symbol); // new property with this alias
752 addDocTag : function(docTag)
754 this.comment.tags.push(docTag);
755 if (docTag.title == 'cfg') {
756 this.addConfig(docTag);
761 addConfig : function(docTag)
763 if (typeof(docTag['memberOf']) == 'undefined') {
764 // remove prototype data...
765 //var a = this.alias.split('#')[0];
766 //docTag.memberOf = a;
767 docTag.memberOf = this.alias;
769 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
770 this.cfgs[docTag.name] = docTag;
774 configToArray: function()
777 for(var ci in this.cfgs) {
778 // dont show hidden!!
779 if (this.cfgs[ci].desc.match(/@hide/)) {
782 r.push(this.cfgs[ci]);
790 * Elements that are not serialized
794 '$args' // not needed AFAIK
797 Symbol.srcFile = ""; //running reference to the current file being parsed
800 Symbol.fromDump = function(t)
802 var ns = new Symbol();
804 if (typeof(ns[i]) == "undefined") {
805 println("ERR:no default for Symbol:"+ i);