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 {
30 var n = Symbol.regex_global(value, value.length, 0, "")
31 n = Symbol.regex_prototype(n,n.length, 0, "#");
33 if (!n.has_suffix("#")) {
36 n = n.substring(0, n.length-1);
39 this.private_name = n;
45 get { return this.private_name; }
49 string defaultValue = "";
54 augments : [], // Doctag[]
55 exceptions : [], // Doctag[]
56 inherits : [], // Doctag[]
57 methods : [], // Symbol[]
59 _params : [], //Doctag[]
60 properties : [], //Doctag[]
61 requires : [], //Doctag[]
62 returns : [], //Doctag[]
72 //$args : [], // original arguments used when constructing.
74 public string alias = "";
77 string classDesc = "";
79 string deprecated = "";
85 string isa = "OBJECT"; // OBJECT//FUNCTION
87 public bool isEvent = false;
88 public bool isConstant = false;
89 public bool isIgnored = false;
90 public bool isInner = false;
91 public bool isNamespace = false;
92 public bool isPrivate = false;
93 public bool isStatic = false;
109 public void initArrays()
111 // only initialize arrays / objects..
118 this.exceptions = [];
121 this.isa = "OBJECT"; // OBJECT//FUNCTION
124 this.properties = [];
133 this.inheritsFrom = [];
134 this.childClasses = [];
136 this.comment = new DocComment();
137 this.comment.isUserComment = false;
142 Public Symbol.new_builtin(string name)
146 this.srcFile = DocParser.currentSourceFile;
147 this.setName( name );
148 this.alias = this.name;
149 this.isa = "CONSTRUCTOR";
150 this.comment = new DocComment("");
151 this.comment.isUserComment = false;
152 this.isNamespace = false;
154 this.isPrivate = false;
163 setParams :function(v) {
164 for (var i = 0, l = v.length; i < l; i++) {
165 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
166 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
167 this._params[i] = new DocTag(
168 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
171 this._params[i] = v[i];
174 this.params = this._params;
179 //__defineGetter__("params",
180 public getParams : function() {
185 public Symbol.new_populate_with_args(
187 Gee.ArrayList<string> params, // fixme???
193 this.$args = arguments;
194 //println("Symbol created: " + isa + ":" + name);
196 this.alias = this.getName();
197 this.setParams(params);
198 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
199 this.comment = comment || new DocComment("")
201 this.srcFile = DocParser.currentSourceFile;
205 if (this.is("FILE") && !this.alias) { // this will never hapen???
206 this.alias = this.srcFile;
213 setTags : function() {
215 var authors = this.comment.getTag("author");
216 if (authors.length) {
217 this.author = authors.map(function($){return $.desc;}).join(", ");
221 assert("testing Symbol");
223 requires("../lib/JSDOC/DocComment.js");
224 requires("../frame/String.js");
225 requires("../lib/JSDOC/DocTag.js");
227 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
228 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
231 var mth = this.comment.getTag("method");
233 this.isa = "FUNCTION";
236 var descs = this.comment.getTag("desc");
238 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
242 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
243 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
247 if (this.is("FILE")) {
248 if (!this.alias) this.alias = this.srcFile;
250 var overviews = this.comment.getTag("overview");
251 if (overviews.length) {
252 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
257 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
258 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
262 var sinces = this.comment.getTag("since");
264 this.since = sinces.map(function($){return $.desc;}).join(", ");
268 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
269 assertEqual(sym.since, "1.01", "@since tag, description is found.");
273 if (this.comment.getTag("constant").length) {
274 this.isConstant = true;
279 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
280 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
284 var versions = this.comment.getTag("version");
285 if (versions.length) {
286 this.version = versions.map(function($){return $.desc;}).join(", ");
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
291 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
295 var deprecateds = this.comment.getTag("deprecated");
296 if (deprecateds.length) {
297 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
301 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
302 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
306 var examples = this.comment.getTag("example");
307 if (examples.length) {
308 this.example = examples[0];
312 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
313 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
317 var sees = this.comment.getTag("see");
319 var thisSee = this.see;
320 sees.map(function($){thisSee.push($.desc);});
324 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
325 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
329 var classes = this.comment.getTag("class");
330 if (classes.length) {
331 //print(JSON.stringify(this,null,4));
332 this.isa = "CONSTRUCTOR";
333 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
334 if (!this.classDesc) {
335 this.classDesc = this.desc;
342 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
343 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
344 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
348 var namespaces = this.comment.getTag("namespace");
349 if (namespaces.length) {
350 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
351 this.isNamespace = true;
355 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
356 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
360 var params = this.comment.getTag("param");
362 // user-defined params overwrite those with same name defined by the parser
363 var thisParams = this.getParams();
365 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
366 this.setParams(params);
368 else { // need to overlay these user-defined params on to existing parser-defined params
369 for (var i = 0, l = params.length; i < l; i++) {
371 if (params[i].type) thisParams[i].type = params[i].type;
372 thisParams[i].name = params[i].name;
373 thisParams[i].desc = params[i].desc;
374 thisParams[i].isOptional = params[i].isOptional;
375 thisParams[i].defaultValue = params[i].defaultValue;
377 else thisParams[i] = params[i];
383 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
384 assertEqual(sym.params.length, 1, "parser defined param is found.");
386 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
387 assertEqual(sym.params.length, 1, "user defined param is found.");
388 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
389 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
391 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
392 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
393 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
394 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
396 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
397 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
398 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
399 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
401 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
402 assertEqual(sym.params.length, 1, "user defined param with description is found.");
403 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
407 if (this.comment.getTag("constructor").length) {
408 this.isa = "CONSTRUCTOR";
412 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
413 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
417 if (this.comment.getTag("static").length) {
418 this.isStatic = true;
419 if (this.isa == "CONSTRUCTOR") {
420 this.isNamespace = true;
425 if (this.comment.getTag("singleton").length) {
426 this.isStatic = true;
427 //print('------------- got singleton ---------------' + this.isa);
428 //if (this.isa == "CONSTRUCTOR") {
429 // this.isNamespace = true;
436 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
437 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
438 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
442 if (this.comment.getTag("inner").length) {
444 this.isStatic = false;
448 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
449 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
450 assertEqual(sym.isInner, true, "@inner makes isInner true.");
454 if (this.comment.getTag("field").length) {
459 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
460 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
464 if (this.comment.getTag("function").length) {
465 this.isa = "FUNCTION";
469 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
470 // change a property to a function..
471 this.isa = "FUNCTION";
476 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
477 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
481 var events = this.comment.getTag("event");
483 this.isa = "FUNCTION";
488 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
489 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
490 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
494 var names = this.comment.getTag("name");
496 this.setName(names[0].desc);
504 var properties = this.comment.getTag("property");
505 if (properties.length) {
506 thisProperties = this.properties;
507 for (var i = 0; i < properties.length; i++) {
508 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
509 // TODO: shouldn't the following happen in the addProperty method of Symbol?
510 property.name = properties[i].name;
511 property.memberOf = this.alias;
512 if (properties[i].type) property.type = properties[i].type;
513 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
514 this.addProperty(property);
515 imports.Parser.Parser.addSymbol(property);
520 var conf = this.comment.getTag("cfg");
522 for (var i = 0; i < conf.length; i++) {
523 this.addConfig(conf[i]);
532 var returns = this.comment.getTag("return");
533 if (returns.length) { // there can be many return tags in a single doclet
534 this.returns = returns;
535 this.type = returns.map(function($){return $.type}).join(", ");
543 this.exceptions = this.comment.getTag("throws");
550 var requires = this.comment.getTag("requires");
551 if (requires.length) {
552 this.requires = requires.map(function($){return $.desc});
560 var types = this.comment.getTag("type");
562 this.type = types[0].desc; //multiple type tags are ignored
570 if (this.comment.getTag("private").length || this.isInner) {
571 this.isPrivate = true;
575 if (this.comment.getTag("ignore").length) {
576 this.isIgnored = true;
583 // @inherits ... as ...
584 var inherits = this.comment.getTag("inherits");
585 if (inherits.length) {
586 for (var i = 0; i < inherits.length; i++) {
587 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
588 var inAlias = RegExp.$1;
589 var inAs = RegExp.$2 || inAlias;
591 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
594 inAs = inAs.replace(/\.prototype\.?/g, "#");
595 inAs = inAs.replace(/^this\.?/, "#");
598 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
600 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
603 inAs = this.alias + joiner + inAs;
606 this.inherits.push({alias: inAlias, as: inAs});
615 this.augments = this.comment.getTag("augments");
618 if (this.comment.getTag("extends")) {
619 this.augments = this.comment.getTag("extends");
624 var defaults = this.comment.getTag("default");
625 if (defaults.length) {
626 if (this.is("OBJECT")) {
627 this.defaultValue = defaults[0].desc;
636 var memberOfs = this.comment.getTag("memberOf");
637 if (memberOfs.length) {
638 this.memberOf = memberOfs[0].desc;
639 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
640 this.name = this.name.split('.').pop();
641 this.name = this.name.split('#').pop();
642 this.name = this.memberOf + this.name;
643 this._name = this.name
644 this.alias = this.name;
652 if (this.comment.getTag("public").length) {
653 this.isPrivate = false;
661 is : function(what) {
662 return this.isa === what;
665 isBuiltin : function() {
666 return SymbolSet.isBuiltin(this.alias);
669 setType : function(/**String*/comment, /**Boolean*/overwrite) {
670 if (!overwrite && this.type) return;
671 var typeComment = DocComment.unwrapComment(comment);
672 this.type = typeComment;
675 inherit : function(symbol) {
676 if (!this.hasMember(symbol.name) && !symbol.isInner) {
677 if (symbol.is("FUNCTION"))
678 this.methods.push(symbol);
679 else if (symbol.is("OBJECT"))
680 this.properties.push(symbol);
684 hasMember : function(name) {
685 return (this.hasMethod(name) || this.hasProperty(name));
688 addMember : function(symbol) {
689 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
691 if (symbol.comment.getTag("cfg").length == 1) {
692 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
693 this.addConfig(symbol.comment.getTag("cfg")[0]);
697 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
698 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
701 hasMethod : function(name) {
702 var thisMethods = this.methods;
703 for (var i = 0, l = thisMethods.length; i < l; i++) {
704 if (thisMethods[i].name == name) return true;
705 if (thisMethods[i].alias == name) return true;
710 addMethod : function(symbol) {
711 var methodAlias = symbol.alias;
712 var thisMethods = this.methods;
713 for (var i = 0, l = thisMethods.length; i < l; i++) {
714 if (thisMethods[i].alias == methodAlias) {
715 thisMethods[i] = symbol; // overwriting previous method
719 thisMethods.push(symbol); // new method with this alias
722 hasProperty : function(name) {
723 var thisProperties = this.properties;
724 for (var i = 0, l = thisProperties.length; i < l; i++) {
725 if (thisProperties[i].name == name) return true;
726 if (thisProperties[i].alias == name) return true;
731 addProperty : function(symbol) {
732 var propertyAlias = symbol.alias;
733 var thisProperties = this.properties;
734 for (var i = 0, l = thisProperties.length; i < l; i++) {
735 if (thisProperties[i].alias == propertyAlias) {
736 thisProperties[i] = symbol; // overwriting previous property
741 thisProperties.push(symbol); // new property with this alias
744 addDocTag : function(docTag)
746 this.comment.tags.push(docTag);
747 if (docTag.title == 'cfg') {
748 this.addConfig(docTag);
753 addConfig : function(docTag)
755 if (typeof(docTag['memberOf']) == 'undefined') {
756 // remove prototype data...
757 //var a = this.alias.split('#')[0];
758 //docTag.memberOf = a;
759 docTag.memberOf = this.alias;
761 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
762 this.cfgs[docTag.name] = docTag;
766 configToArray: function()
769 for(var ci in this.cfgs) {
770 // dont show hidden!!
771 if (this.cfgs[ci].desc.match(/@hide/)) {
774 r.push(this.cfgs[ci]);
782 * Elements that are not serialized
786 '$args' // not needed AFAIK
789 Symbol.srcFile = ""; //running reference to the current file being parsed
792 Symbol.fromDump = function(t)
794 var ns = new Symbol();
796 if (typeof(ns[i]) == "undefined") {
797 println("ERR:no default for Symbol:"+ i);