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 _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;
48 get { return this.private_name; }
52 string defaultValue : "",
57 augments : [], // Doctag[]
58 exceptions : [], // Doctag[]
59 inherits : [], // Doctag[]
60 methods : [], // Symbol[]
62 _params : [], //Doctag[]
63 properties : [], //Doctag[]
64 requires : [], //Doctag[]
65 returns : [], //Doctag[]
75 //$args : [], // original arguments used when constructing.
77 public string alias = "";
80 string classDesc = "";
82 string deprecated = "";
88 string isa = "OBJECT"; // OBJECT//FUNCTION
90 public bool isEvent = false;
91 public bool isConstant = false;
92 public bool isIgnored = false;
93 public bool isInner = false;
94 public bool isNamespace = false;
95 public bool isPrivate = false;
96 public bool isStatic = false;
112 public void initArrays()
114 // only initialize arrays / objects..
121 this.exceptions = [];
124 this.isa = "OBJECT"; // OBJECT//FUNCTION
127 this.properties = [];
136 this.inheritsFrom = [];
137 this.childClasses = [];
139 this.comment = new DocComment();
140 this.comment.isUserComment = false;
145 Public Symbol.new_builtin(string name)
149 this.srcFile = DocParser.currentSourceFile;
151 this.alias = this.name;
152 this.isa = "CONSTRUCTOR";
153 this.comment = new DocComment("");
154 this.comment.isUserComment = false;
155 this.isNamespace = false;
157 this.isPrivate = false;
165 //__defineSetter__("name",
168 //__defineGetter__("name",
169 getName : function() { return this._name; },
171 //__defineSetter__("params",
172 setParams :function(v) {
173 for (var i = 0, l = v.length; i < l; i++) {
174 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
175 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
176 this._params[i] = new DocTag(
177 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
180 this._params[i] = v[i];
183 this.params = this._params;
188 //__defineGetter__("params",
189 public getParams : function() {
194 public Symbol.new_populate_with_args(
196 Gee.ArrayList<string> params, // fixme???
202 this.$args = arguments;
203 //println("Symbol created: " + isa + ":" + name);
205 this.alias = this.getName();
206 this.setParams(params);
207 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
208 this.comment = comment || new DocComment("")
210 this.srcFile = DocParser.currentSourceFile;
214 if (this.is("FILE") && !this.alias) { // this will never hapen???
215 this.alias = this.srcFile;
222 setTags : function() {
224 var authors = this.comment.getTag("author");
225 if (authors.length) {
226 this.author = authors.map(function($){return $.desc;}).join(", ");
230 assert("testing Symbol");
232 requires("../lib/JSDOC/DocComment.js");
233 requires("../frame/String.js");
234 requires("../lib/JSDOC/DocTag.js");
236 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
237 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
240 var mth = this.comment.getTag("method");
242 this.isa = "FUNCTION";
245 var descs = this.comment.getTag("desc");
247 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
251 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
252 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
256 if (this.is("FILE")) {
257 if (!this.alias) this.alias = this.srcFile;
259 var overviews = this.comment.getTag("overview");
260 if (overviews.length) {
261 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
266 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
267 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
271 var sinces = this.comment.getTag("since");
273 this.since = sinces.map(function($){return $.desc;}).join(", ");
277 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
278 assertEqual(sym.since, "1.01", "@since tag, description is found.");
282 if (this.comment.getTag("constant").length) {
283 this.isConstant = true;
288 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
289 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
293 var versions = this.comment.getTag("version");
294 if (versions.length) {
295 this.version = versions.map(function($){return $.desc;}).join(", ");
299 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
300 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
304 var deprecateds = this.comment.getTag("deprecated");
305 if (deprecateds.length) {
306 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
310 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
311 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
315 var examples = this.comment.getTag("example");
316 if (examples.length) {
317 this.example = examples[0];
321 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
322 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
326 var sees = this.comment.getTag("see");
328 var thisSee = this.see;
329 sees.map(function($){thisSee.push($.desc);});
333 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
334 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
338 var classes = this.comment.getTag("class");
339 if (classes.length) {
340 //print(JSON.stringify(this,null,4));
341 this.isa = "CONSTRUCTOR";
342 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
343 if (!this.classDesc) {
344 this.classDesc = this.desc;
351 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
352 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
353 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
357 var namespaces = this.comment.getTag("namespace");
358 if (namespaces.length) {
359 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
360 this.isNamespace = true;
364 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
365 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
369 var params = this.comment.getTag("param");
371 // user-defined params overwrite those with same name defined by the parser
372 var thisParams = this.getParams();
374 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
375 this.setParams(params);
377 else { // need to overlay these user-defined params on to existing parser-defined params
378 for (var i = 0, l = params.length; i < l; i++) {
380 if (params[i].type) thisParams[i].type = params[i].type;
381 thisParams[i].name = params[i].name;
382 thisParams[i].desc = params[i].desc;
383 thisParams[i].isOptional = params[i].isOptional;
384 thisParams[i].defaultValue = params[i].defaultValue;
386 else thisParams[i] = params[i];
392 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
393 assertEqual(sym.params.length, 1, "parser defined param is found.");
395 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
396 assertEqual(sym.params.length, 1, "user defined param is found.");
397 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
398 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
400 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
401 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
402 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
403 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
405 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
406 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
407 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
408 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
410 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
411 assertEqual(sym.params.length, 1, "user defined param with description is found.");
412 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
416 if (this.comment.getTag("constructor").length) {
417 this.isa = "CONSTRUCTOR";
421 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
422 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
426 if (this.comment.getTag("static").length) {
427 this.isStatic = true;
428 if (this.isa == "CONSTRUCTOR") {
429 this.isNamespace = true;
434 if (this.comment.getTag("singleton").length) {
435 this.isStatic = true;
436 //print('------------- got singleton ---------------' + this.isa);
437 //if (this.isa == "CONSTRUCTOR") {
438 // this.isNamespace = true;
445 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
446 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
447 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
451 if (this.comment.getTag("inner").length) {
453 this.isStatic = false;
457 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
458 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
459 assertEqual(sym.isInner, true, "@inner makes isInner true.");
463 if (this.comment.getTag("field").length) {
468 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
469 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
473 if (this.comment.getTag("function").length) {
474 this.isa = "FUNCTION";
478 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
479 // change a property to a function..
480 this.isa = "FUNCTION";
485 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
486 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
490 var events = this.comment.getTag("event");
492 this.isa = "FUNCTION";
497 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
498 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
499 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
503 var names = this.comment.getTag("name");
505 this.setName(names[0].desc);
513 var properties = this.comment.getTag("property");
514 if (properties.length) {
515 thisProperties = this.properties;
516 for (var i = 0; i < properties.length; i++) {
517 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
518 // TODO: shouldn't the following happen in the addProperty method of Symbol?
519 property.name = properties[i].name;
520 property.memberOf = this.alias;
521 if (properties[i].type) property.type = properties[i].type;
522 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
523 this.addProperty(property);
524 imports.Parser.Parser.addSymbol(property);
529 var conf = this.comment.getTag("cfg");
531 for (var i = 0; i < conf.length; i++) {
532 this.addConfig(conf[i]);
541 var returns = this.comment.getTag("return");
542 if (returns.length) { // there can be many return tags in a single doclet
543 this.returns = returns;
544 this.type = returns.map(function($){return $.type}).join(", ");
552 this.exceptions = this.comment.getTag("throws");
559 var requires = this.comment.getTag("requires");
560 if (requires.length) {
561 this.requires = requires.map(function($){return $.desc});
569 var types = this.comment.getTag("type");
571 this.type = types[0].desc; //multiple type tags are ignored
579 if (this.comment.getTag("private").length || this.isInner) {
580 this.isPrivate = true;
584 if (this.comment.getTag("ignore").length) {
585 this.isIgnored = true;
592 // @inherits ... as ...
593 var inherits = this.comment.getTag("inherits");
594 if (inherits.length) {
595 for (var i = 0; i < inherits.length; i++) {
596 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
597 var inAlias = RegExp.$1;
598 var inAs = RegExp.$2 || inAlias;
600 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
603 inAs = inAs.replace(/\.prototype\.?/g, "#");
604 inAs = inAs.replace(/^this\.?/, "#");
607 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
609 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
612 inAs = this.alias + joiner + inAs;
615 this.inherits.push({alias: inAlias, as: inAs});
624 this.augments = this.comment.getTag("augments");
627 if (this.comment.getTag("extends")) {
628 this.augments = this.comment.getTag("extends");
633 var defaults = this.comment.getTag("default");
634 if (defaults.length) {
635 if (this.is("OBJECT")) {
636 this.defaultValue = defaults[0].desc;
645 var memberOfs = this.comment.getTag("memberOf");
646 if (memberOfs.length) {
647 this.memberOf = memberOfs[0].desc;
648 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
649 this.name = this.name.split('.').pop();
650 this.name = this.name.split('#').pop();
651 this.name = this.memberOf + this.name;
652 this._name = this.name
653 this.alias = this.name;
661 if (this.comment.getTag("public").length) {
662 this.isPrivate = false;
670 is : function(what) {
671 return this.isa === what;
674 isBuiltin : function() {
675 return SymbolSet.isBuiltin(this.alias);
678 setType : function(/**String*/comment, /**Boolean*/overwrite) {
679 if (!overwrite && this.type) return;
680 var typeComment = DocComment.unwrapComment(comment);
681 this.type = typeComment;
684 inherit : function(symbol) {
685 if (!this.hasMember(symbol.name) && !symbol.isInner) {
686 if (symbol.is("FUNCTION"))
687 this.methods.push(symbol);
688 else if (symbol.is("OBJECT"))
689 this.properties.push(symbol);
693 hasMember : function(name) {
694 return (this.hasMethod(name) || this.hasProperty(name));
697 addMember : function(symbol) {
698 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
700 if (symbol.comment.getTag("cfg").length == 1) {
701 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
702 this.addConfig(symbol.comment.getTag("cfg")[0]);
706 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
707 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
710 hasMethod : function(name) {
711 var thisMethods = this.methods;
712 for (var i = 0, l = thisMethods.length; i < l; i++) {
713 if (thisMethods[i].name == name) return true;
714 if (thisMethods[i].alias == name) return true;
719 addMethod : function(symbol) {
720 var methodAlias = symbol.alias;
721 var thisMethods = this.methods;
722 for (var i = 0, l = thisMethods.length; i < l; i++) {
723 if (thisMethods[i].alias == methodAlias) {
724 thisMethods[i] = symbol; // overwriting previous method
728 thisMethods.push(symbol); // new method with this alias
731 hasProperty : function(name) {
732 var thisProperties = this.properties;
733 for (var i = 0, l = thisProperties.length; i < l; i++) {
734 if (thisProperties[i].name == name) return true;
735 if (thisProperties[i].alias == name) return true;
740 addProperty : function(symbol) {
741 var propertyAlias = symbol.alias;
742 var thisProperties = this.properties;
743 for (var i = 0, l = thisProperties.length; i < l; i++) {
744 if (thisProperties[i].alias == propertyAlias) {
745 thisProperties[i] = symbol; // overwriting previous property
750 thisProperties.push(symbol); // new property with this alias
753 addDocTag : function(docTag)
755 this.comment.tags.push(docTag);
756 if (docTag.title == 'cfg') {
757 this.addConfig(docTag);
762 addConfig : function(docTag)
764 if (typeof(docTag['memberOf']) == 'undefined') {
765 // remove prototype data...
766 //var a = this.alias.split('#')[0];
767 //docTag.memberOf = a;
768 docTag.memberOf = this.alias;
770 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
771 this.cfgs[docTag.name] = docTag;
775 configToArray: function()
778 for(var ci in this.cfgs) {
779 // dont show hidden!!
780 if (this.cfgs[ci].desc.match(/@hide/)) {
783 r.push(this.cfgs[ci]);
791 * Elements that are not serialized
795 '$args' // not needed AFAIK
798 Symbol.srcFile = ""; //running reference to the current file being parsed
801 Symbol.fromDump = function(t)
803 var ns = new Symbol();
805 if (typeof(ns[i]) == "undefined") {
806 println("ERR:no default for Symbol:"+ i);