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 = "";
52 private Gee.ArrayList<DocTag> private_params{
54 for (var i = 0; i < value.size; i++) {
55 //if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
56 // var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
57 // this._params[i] = new DocTag(
58 // "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
61 // this._params[i] = v[i];
63 this.private_params.add(v.get(i));
65 //this.params = this._params;
68 public Gee.ArrayList<DocTag> params {
70 return this.private_params;
76 private Gee.ArrayList<DocTag> augments ;
78 private Gee.ArrayList<DocTag> exceptions ;
80 private Gee.ArrayList<DocTag> inherits;
81 private Gee.ArrayList<DocTag> methods;
83 private Gee.ArrayList<DocTag> properties;
84 private Gee.ArrayList<DocTag> requires;
85 private Gee.ArrayList<DocTag> returns;
86 private Gee.ArrayList<DocTag> see ;
95 //$args : [], // original arguments used when constructing.
97 public string alias = "";
100 string classDesc = "";
102 string deprecated = "";
108 string isa = "OBJECT"; // OBJECT//FUNCTION
110 public bool isEvent = false;
111 public bool isConstant = false;
112 public bool isIgnored = false;
113 public bool isInner = false;
114 public bool isNamespace = false;
115 public bool isPrivate = false;
116 public bool isStatic = false;
118 string memberOf = "";
132 public void initArrays()
134 // only initialize arrays / objects..
141 this.exceptions = new Gee.ArrayList<DocTag>();
142 this.inherits = new Gee.ArrayList<DocTag>();
144 this.isa = "OBJECT"; // OBJECT//FUNCTION
145 this.methods = new Gee.ArrayList<DocTag>();
146 //this.private_params = new Gee.ArrayList<DocTag>();
147 this.properties = new Gee.ArrayList<DocTag>();
148 this.requires = new Gee.ArrayList<DocTag>();
149 this.returns = new Gee.ArrayList<DocTag>();
150 this.see = new Gee.ArrayList<DocTag>();
156 this.inheritsFrom = [];
157 this.childClasses = [];
159 this.comment = new DocComment();
160 this.comment.isUserComment = false;
165 Public Symbol.new_builtin(string name)
169 this.srcFile = DocParser.currentSourceFile;
170 this.setName( name );
171 this.alias = this.name;
172 this.isa = "CONSTRUCTOR";
173 this.comment = new DocComment("");
174 this.comment.isUserComment = false;
175 this.isNamespace = false;
177 this.isPrivate = false;
187 public Symbol.new_populate_with_args(
189 Gee.ArrayList<string> params, // fixme???
195 this.$args = arguments;
196 //println("Symbol created: " + isa + ":" + name);
198 this.alias = this.getName();
199 this.setParams(params);
200 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
201 this.comment = comment || new DocComment("")
203 this.srcFile = DocParser.currentSourceFile;
207 if (this.is("FILE") && !this.alias) { // this will never hapen???
208 this.alias = this.srcFile;
215 setTags : function() {
217 var authors = this.comment.getTag("author");
218 if (authors.length) {
219 this.author = authors.map(function($){return $.desc;}).join(", ");
223 assert("testing Symbol");
225 requires("../lib/JSDOC/DocComment.js");
226 requires("../frame/String.js");
227 requires("../lib/JSDOC/DocTag.js");
229 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
230 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
233 var mth = this.comment.getTag("method");
235 this.isa = "FUNCTION";
238 var descs = this.comment.getTag("desc");
240 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
244 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
245 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
249 if (this.is("FILE")) {
250 if (!this.alias) this.alias = this.srcFile;
252 var overviews = this.comment.getTag("overview");
253 if (overviews.length) {
254 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
259 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
260 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
264 var sinces = this.comment.getTag("since");
266 this.since = sinces.map(function($){return $.desc;}).join(", ");
270 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
271 assertEqual(sym.since, "1.01", "@since tag, description is found.");
275 if (this.comment.getTag("constant").length) {
276 this.isConstant = true;
281 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
282 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
286 var versions = this.comment.getTag("version");
287 if (versions.length) {
288 this.version = versions.map(function($){return $.desc;}).join(", ");
292 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
293 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
297 var deprecateds = this.comment.getTag("deprecated");
298 if (deprecateds.length) {
299 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
303 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
304 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
308 var examples = this.comment.getTag("example");
309 if (examples.length) {
310 this.example = examples[0];
314 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
315 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
319 var sees = this.comment.getTag("see");
321 var thisSee = this.see;
322 sees.map(function($){thisSee.push($.desc);});
326 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
327 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
331 var classes = this.comment.getTag("class");
332 if (classes.length) {
333 //print(JSON.stringify(this,null,4));
334 this.isa = "CONSTRUCTOR";
335 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
336 if (!this.classDesc) {
337 this.classDesc = this.desc;
344 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
345 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
346 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
350 var namespaces = this.comment.getTag("namespace");
351 if (namespaces.length) {
352 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
353 this.isNamespace = true;
357 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
358 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
362 var params = this.comment.getTag("param");
364 // user-defined params overwrite those with same name defined by the parser
365 var thisParams = this.getParams();
367 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
368 this.setParams(params);
370 else { // need to overlay these user-defined params on to existing parser-defined params
371 for (var i = 0, l = params.length; i < l; i++) {
373 if (params[i].type) thisParams[i].type = params[i].type;
374 thisParams[i].name = params[i].name;
375 thisParams[i].desc = params[i].desc;
376 thisParams[i].isOptional = params[i].isOptional;
377 thisParams[i].defaultValue = params[i].defaultValue;
379 else thisParams[i] = params[i];
385 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
386 assertEqual(sym.params.length, 1, "parser defined param is found.");
388 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
389 assertEqual(sym.params.length, 1, "user defined param is found.");
390 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
391 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
393 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
394 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
395 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
396 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
398 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
399 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
400 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
401 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
403 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
404 assertEqual(sym.params.length, 1, "user defined param with description is found.");
405 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
409 if (this.comment.getTag("constructor").length) {
410 this.isa = "CONSTRUCTOR";
414 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
415 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
419 if (this.comment.getTag("static").length) {
420 this.isStatic = true;
421 if (this.isa == "CONSTRUCTOR") {
422 this.isNamespace = true;
427 if (this.comment.getTag("singleton").length) {
428 this.isStatic = true;
429 //print('------------- got singleton ---------------' + this.isa);
430 //if (this.isa == "CONSTRUCTOR") {
431 // this.isNamespace = true;
438 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
439 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
440 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
444 if (this.comment.getTag("inner").length) {
446 this.isStatic = false;
450 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
451 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
452 assertEqual(sym.isInner, true, "@inner makes isInner true.");
456 if (this.comment.getTag("field").length) {
461 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
462 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
466 if (this.comment.getTag("function").length) {
467 this.isa = "FUNCTION";
471 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
472 // change a property to a function..
473 this.isa = "FUNCTION";
478 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
479 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
483 var events = this.comment.getTag("event");
485 this.isa = "FUNCTION";
490 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
491 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
492 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
496 var names = this.comment.getTag("name");
498 this.setName(names[0].desc);
506 var properties = this.comment.getTag("property");
507 if (properties.length) {
508 thisProperties = this.properties;
509 for (var i = 0; i < properties.length; i++) {
510 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
511 // TODO: shouldn't the following happen in the addProperty method of Symbol?
512 property.name = properties[i].name;
513 property.memberOf = this.alias;
514 if (properties[i].type) property.type = properties[i].type;
515 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
516 this.addProperty(property);
517 imports.Parser.Parser.addSymbol(property);
522 var conf = this.comment.getTag("cfg");
524 for (var i = 0; i < conf.length; i++) {
525 this.addConfig(conf[i]);
534 var returns = this.comment.getTag("return");
535 if (returns.length) { // there can be many return tags in a single doclet
536 this.returns = returns;
537 this.type = returns.map(function($){return $.type}).join(", ");
545 this.exceptions = this.comment.getTag("throws");
552 var requires = this.comment.getTag("requires");
553 if (requires.length) {
554 this.requires = requires.map(function($){return $.desc});
562 var types = this.comment.getTag("type");
564 this.type = types[0].desc; //multiple type tags are ignored
572 if (this.comment.getTag("private").length || this.isInner) {
573 this.isPrivate = true;
577 if (this.comment.getTag("ignore").length) {
578 this.isIgnored = true;
585 // @inherits ... as ...
586 var inherits = this.comment.getTag("inherits");
587 if (inherits.length) {
588 for (var i = 0; i < inherits.length; i++) {
589 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
590 var inAlias = RegExp.$1;
591 var inAs = RegExp.$2 || inAlias;
593 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
596 inAs = inAs.replace(/\.prototype\.?/g, "#");
597 inAs = inAs.replace(/^this\.?/, "#");
600 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
602 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
605 inAs = this.alias + joiner + inAs;
608 this.inherits.push({alias: inAlias, as: inAs});
617 this.augments = this.comment.getTag("augments");
620 if (this.comment.getTag("extends")) {
621 this.augments = this.comment.getTag("extends");
626 var defaults = this.comment.getTag("default");
627 if (defaults.length) {
628 if (this.is("OBJECT")) {
629 this.defaultValue = defaults[0].desc;
638 var memberOfs = this.comment.getTag("memberOf");
639 if (memberOfs.length) {
640 this.memberOf = memberOfs[0].desc;
641 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
642 this.name = this.name.split('.').pop();
643 this.name = this.name.split('#').pop();
644 this.name = this.memberOf + this.name;
645 this._name = this.name
646 this.alias = this.name;
654 if (this.comment.getTag("public").length) {
655 this.isPrivate = false;
663 is : function(what) {
664 return this.isa === what;
667 isBuiltin : function() {
668 return SymbolSet.isBuiltin(this.alias);
671 setType : function(/**String*/comment, /**Boolean*/overwrite) {
672 if (!overwrite && this.type) return;
673 var typeComment = DocComment.unwrapComment(comment);
674 this.type = typeComment;
677 inherit : function(symbol) {
678 if (!this.hasMember(symbol.name) && !symbol.isInner) {
679 if (symbol.is("FUNCTION"))
680 this.methods.push(symbol);
681 else if (symbol.is("OBJECT"))
682 this.properties.push(symbol);
686 hasMember : function(name) {
687 return (this.hasMethod(name) || this.hasProperty(name));
690 addMember : function(symbol) {
691 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
693 if (symbol.comment.getTag("cfg").length == 1) {
694 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
695 this.addConfig(symbol.comment.getTag("cfg")[0]);
699 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
700 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
703 hasMethod : function(name) {
704 var thisMethods = this.methods;
705 for (var i = 0, l = thisMethods.length; i < l; i++) {
706 if (thisMethods[i].name == name) return true;
707 if (thisMethods[i].alias == name) return true;
712 addMethod : function(symbol) {
713 var methodAlias = symbol.alias;
714 var thisMethods = this.methods;
715 for (var i = 0, l = thisMethods.length; i < l; i++) {
716 if (thisMethods[i].alias == methodAlias) {
717 thisMethods[i] = symbol; // overwriting previous method
721 thisMethods.push(symbol); // new method with this alias
724 hasProperty : function(name) {
725 var thisProperties = this.properties;
726 for (var i = 0, l = thisProperties.length; i < l; i++) {
727 if (thisProperties[i].name == name) return true;
728 if (thisProperties[i].alias == name) return true;
733 addProperty : function(symbol) {
734 var propertyAlias = symbol.alias;
735 var thisProperties = this.properties;
736 for (var i = 0, l = thisProperties.length; i < l; i++) {
737 if (thisProperties[i].alias == propertyAlias) {
738 thisProperties[i] = symbol; // overwriting previous property
743 thisProperties.push(symbol); // new property with this alias
746 addDocTag : function(docTag)
748 this.comment.tags.push(docTag);
749 if (docTag.title == 'cfg') {
750 this.addConfig(docTag);
755 addConfig : function(docTag)
757 if (typeof(docTag['memberOf']) == 'undefined') {
758 // remove prototype data...
759 //var a = this.alias.split('#')[0];
760 //docTag.memberOf = a;
761 docTag.memberOf = this.alias;
763 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
764 this.cfgs[docTag.name] = docTag;
768 configToArray: function()
771 for(var ci in this.cfgs) {
772 // dont show hidden!!
773 if (this.cfgs[ci].desc.match(/@hide/)) {
776 r.push(this.cfgs[ci]);
784 * Elements that are not serialized
788 '$args' // not needed AFAIK
791 Symbol.srcFile = ""; //running reference to the current file being parsed
794 Symbol.fromDump = function(t)
796 var ns = new Symbol();
798 if (typeof(ns[i]) == "undefined") {
799 println("ERR:no default for Symbol:"+ i);