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_string_name = "";
29 private string private_name {
31 var n = Symbol.regex_global(value, value.length, 0, "");
32 n = Symbol.regex_prototype(n,n.length, 0, "#");
34 if (!n.has_suffix("#")) {
37 n = n.substring(0, n.length-1);
40 this.private_string_name = n;
46 get { return this.private_string_name; }
50 string defaultValue = "";
52 private Gee.ArrayList<DocTag> private_doctag_params;
54 private Gee.ArrayList<DocTag> private_params{
56 for (var i = 0; i < value.size; i++) {
58 this.private_doctag_params.add(v.get(i));
60 //this.params = this._params;
64 Gee.ArrayList<string> private_string_params{
66 for (var i = 0; i < value.size; i++) {
68 //var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
69 this.private_doctag_params.add( new DocTag(value.get(i)));
71 //"param"+((ty)?" {"+ty+"}":"")+" "+v.get(i).name);
77 public Gee.ArrayList<DocTag> params {
79 return this.private_doctag_params;
84 private Gee.ArrayList<DocTag> augments ;
86 private Gee.ArrayList<DocTag> exceptions ;
88 private Gee.ArrayList<DocTag> inherits;
89 private Gee.ArrayList<DocTag> methods;
91 private Gee.ArrayList<DocTag> properties;
92 private Gee.ArrayList<DocTag> requires;
93 private Gee.ArrayList<DocTag> returns;
94 private Gee.ArrayList<DocTag> see ;
103 //$args : [], // original arguments used when constructing.
105 public string alias = "";
108 string classDesc = "";
110 string deprecated = "";
116 string isa = "OBJECT"; // OBJECT//FUNCTION
118 public bool isEvent = false;
119 public bool isConstant = false;
120 public bool isIgnored = false;
121 public bool isInner = false;
122 public bool isNamespace = false;
123 public bool isPrivate = false;
124 public bool isStatic = false;
126 string memberOf = "";
140 public void initArrays()
142 // only initialize arrays / objects..
149 this.exceptions = new Gee.ArrayList<DocTag>();
150 this.inherits = new Gee.ArrayList<DocTag>();
152 this.isa = "OBJECT"; // OBJECT//FUNCTION
153 this.methods = new Gee.ArrayList<DocTag>();
154 //this.private_params = new Gee.ArrayList<DocTag>();
155 this.properties = new Gee.ArrayList<DocTag>();
156 this.requires = new Gee.ArrayList<DocTag>();
157 this.returns = new Gee.ArrayList<DocTag>();
158 this.see = new Gee.ArrayList<DocTag>();
164 //this.inheritsFrom = [];
165 //this.childClasses = [];
167 this.comment = new DocComment();
168 this.comment.isUserComment = false;
173 Public Symbol.new_builtin(string name)
177 this.srcFile = DocParser.currentSourceFile;
178 this.prviate_name = name ;
179 this.alias = this.name;
180 this.isa = "CONSTRUCTOR";
181 this.comment = new DocComment("");
182 this.comment.isUserComment = false;
183 this.isNamespace = false;
185 this.isPrivate = false;
195 public Symbol.new_populate_with_args(
197 Gee.ArrayList<string> params, // fixme???
203 // this.$args = arguments;
204 //println("Symbol created: " + isa + ":" + name);
205 this.private_name = name;
206 this.alias = this.getName();
207 this.private_string_params = params;
208 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
209 this.comment = new DocComment(comment);
211 this.srcFile = DocParser.currentSourceFile;
215 if (this.is("FILE") && !this.alias) { // this will never hapen???
216 this.alias = this.srcFile;
219 this.tagsFromComment();
223 void tagsFromComment() {
225 var authors = this.comment.getTag("author");
227 // turns author into a string....
229 foreach(var a in authors) {
230 this.author += (this.author == "") ? "": ", ";
231 this.author += a.desc;
236 assert("testing Symbol");
238 requires("../lib/JSDOC/DocComment.js");
239 requires("../frame/String.js");
240 requires("../lib/JSDOC/DocTag.js");
242 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
243 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
246 var mth = this.comment.getTag("method");
248 this.isa = "FUNCTION";
251 var descs = this.comment.getTag("desc");
254 foreach(var d in desc) {
255 this.desc = this.desc == "" ? "": "\n";
262 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
263 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
267 if (this.is("FILE")) {
268 if (!this.alias) this.alias = this.srcFile;
270 var overviews = this.comment.getTag("overview");
271 if (overviews.length) {
272 foreach(var d in this.overviews) {
273 this.desc = this.desc == "" ? "": "\n";
280 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
281 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
285 var sinces = this.comment.getTag("since");
287 this.since = sinces.map(function($){return $.desc;}).join(", ");
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
292 assertEqual(sym.since, "1.01", "@since tag, description is found.");
296 if (this.comment.getTag("constant").length) {
297 this.isConstant = true;
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
303 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
307 var versions = this.comment.getTag("version");
308 if (versions.length) {
309 this.version = versions.map(function($){return $.desc;}).join(", ");
313 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
314 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
318 var deprecateds = this.comment.getTag("deprecated");
319 if (deprecateds.length) {
320 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
324 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
325 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
329 var examples = this.comment.getTag("example");
330 if (examples.length) {
331 this.example = examples[0];
335 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
336 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
340 var sees = this.comment.getTag("see");
342 var thisSee = this.see;
343 sees.map(function($){thisSee.push($.desc);});
347 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
348 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
352 var classes = this.comment.getTag("class");
353 if (classes.length) {
354 //print(JSON.stringify(this,null,4));
355 this.isa = "CONSTRUCTOR";
356 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
357 if (!this.classDesc) {
358 this.classDesc = this.desc;
365 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
366 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
367 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
371 var namespaces = this.comment.getTag("namespace");
372 if (namespaces.length) {
373 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
374 this.isNamespace = true;
378 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
379 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
383 var params = this.comment.getTag("param");
385 // user-defined params overwrite those with same name defined by the parser
386 var thisParams = this.getParams();
388 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
389 this.setParams(params);
391 else { // need to overlay these user-defined params on to existing parser-defined params
392 for (var i = 0, l = params.length; i < l; i++) {
394 if (params[i].type) thisParams[i].type = params[i].type;
395 thisParams[i].name = params[i].name;
396 thisParams[i].desc = params[i].desc;
397 thisParams[i].isOptional = params[i].isOptional;
398 thisParams[i].defaultValue = params[i].defaultValue;
400 else thisParams[i] = params[i];
406 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
407 assertEqual(sym.params.length, 1, "parser defined param is found.");
409 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
410 assertEqual(sym.params.length, 1, "user defined param is found.");
411 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
412 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
414 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
415 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
416 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
417 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
419 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
420 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
421 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
422 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
424 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
425 assertEqual(sym.params.length, 1, "user defined param with description is found.");
426 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
430 if (this.comment.getTag("constructor").length) {
431 this.isa = "CONSTRUCTOR";
435 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
436 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
440 if (this.comment.getTag("static").length) {
441 this.isStatic = true;
442 if (this.isa == "CONSTRUCTOR") {
443 this.isNamespace = true;
448 if (this.comment.getTag("singleton").length) {
449 this.isStatic = true;
450 //print('------------- got singleton ---------------' + this.isa);
451 //if (this.isa == "CONSTRUCTOR") {
452 // this.isNamespace = true;
459 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
460 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
461 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
465 if (this.comment.getTag("inner").length) {
467 this.isStatic = false;
471 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
472 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
473 assertEqual(sym.isInner, true, "@inner makes isInner true.");
477 if (this.comment.getTag("field").length) {
482 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
483 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
487 if (this.comment.getTag("function").length) {
488 this.isa = "FUNCTION";
492 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
493 // change a property to a function..
494 this.isa = "FUNCTION";
499 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
500 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
504 var events = this.comment.getTag("event");
506 this.isa = "FUNCTION";
511 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
512 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
513 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
517 var names = this.comment.getTag("name");
519 this.setName(names[0].desc);
527 var properties = this.comment.getTag("property");
528 if (properties.length) {
529 thisProperties = this.properties;
530 for (var i = 0; i < properties.length; i++) {
531 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
532 // TODO: shouldn't the following happen in the addProperty method of Symbol?
533 property.name = properties[i].name;
534 property.memberOf = this.alias;
535 if (properties[i].type) property.type = properties[i].type;
536 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
537 this.addProperty(property);
538 imports.Parser.Parser.addSymbol(property);
543 var conf = this.comment.getTag("cfg");
545 for (var i = 0; i < conf.length; i++) {
546 this.addConfig(conf[i]);
555 var returns = this.comment.getTag("return");
556 if (returns.length) { // there can be many return tags in a single doclet
557 this.returns = returns;
558 this.type = returns.map(function($){return $.type}).join(", ");
566 this.exceptions = this.comment.getTag("throws");
573 var requires = this.comment.getTag("requires");
574 if (requires.length) {
575 this.requires = requires.map(function($){return $.desc});
583 var types = this.comment.getTag("type");
585 this.type = types[0].desc; //multiple type tags are ignored
593 if (this.comment.getTag("private").length || this.isInner) {
594 this.isPrivate = true;
598 if (this.comment.getTag("ignore").length) {
599 this.isIgnored = true;
606 // @inherits ... as ...
607 var inherits = this.comment.getTag("inherits");
608 if (inherits.length) {
609 for (var i = 0; i < inherits.length; i++) {
610 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
611 var inAlias = RegExp.$1;
612 var inAs = RegExp.$2 || inAlias;
614 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
617 inAs = inAs.replace(/\.prototype\.?/g, "#");
618 inAs = inAs.replace(/^this\.?/, "#");
621 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
623 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
626 inAs = this.alias + joiner + inAs;
629 this.inherits.push({alias: inAlias, as: inAs});
638 this.augments = this.comment.getTag("augments");
641 if (this.comment.getTag("extends")) {
642 this.augments = this.comment.getTag("extends");
647 var defaults = this.comment.getTag("default");
648 if (defaults.length) {
649 if (this.is("OBJECT")) {
650 this.defaultValue = defaults[0].desc;
659 var memberOfs = this.comment.getTag("memberOf");
660 if (memberOfs.length) {
661 this.memberOf = memberOfs[0].desc;
662 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
663 this.name = this.name.split('.').pop();
664 this.name = this.name.split('#').pop();
665 this.name = this.memberOf + this.name;
666 this._name = this.name
667 this.alias = this.name;
675 if (this.comment.getTag("public").length) {
676 this.isPrivate = false;
684 is : function(what) {
685 return this.isa === what;
688 isBuiltin : function() {
689 return SymbolSet.isBuiltin(this.alias);
692 setType : function(/**String*/comment, /**Boolean*/overwrite) {
693 if (!overwrite && this.type) return;
694 var typeComment = DocComment.unwrapComment(comment);
695 this.type = typeComment;
698 inherit : function(symbol) {
699 if (!this.hasMember(symbol.name) && !symbol.isInner) {
700 if (symbol.is("FUNCTION"))
701 this.methods.push(symbol);
702 else if (symbol.is("OBJECT"))
703 this.properties.push(symbol);
707 hasMember : function(name) {
708 return (this.hasMethod(name) || this.hasProperty(name));
711 addMember : function(symbol) {
712 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
714 if (symbol.comment.getTag("cfg").length == 1) {
715 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
716 this.addConfig(symbol.comment.getTag("cfg")[0]);
720 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
721 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
724 hasMethod : function(name) {
725 var thisMethods = this.methods;
726 for (var i = 0, l = thisMethods.length; i < l; i++) {
727 if (thisMethods[i].name == name) return true;
728 if (thisMethods[i].alias == name) return true;
733 addMethod : function(symbol) {
734 var methodAlias = symbol.alias;
735 var thisMethods = this.methods;
736 for (var i = 0, l = thisMethods.length; i < l; i++) {
737 if (thisMethods[i].alias == methodAlias) {
738 thisMethods[i] = symbol; // overwriting previous method
742 thisMethods.push(symbol); // new method with this alias
745 hasProperty : function(name) {
746 var thisProperties = this.properties;
747 for (var i = 0, l = thisProperties.length; i < l; i++) {
748 if (thisProperties[i].name == name) return true;
749 if (thisProperties[i].alias == name) return true;
754 addProperty : function(symbol) {
755 var propertyAlias = symbol.alias;
756 var thisProperties = this.properties;
757 for (var i = 0, l = thisProperties.length; i < l; i++) {
758 if (thisProperties[i].alias == propertyAlias) {
759 thisProperties[i] = symbol; // overwriting previous property
764 thisProperties.push(symbol); // new property with this alias
767 addDocTag : function(docTag)
769 this.comment.tags.push(docTag);
770 if (docTag.title == 'cfg') {
771 this.addConfig(docTag);
776 addConfig : function(docTag)
778 if (typeof(docTag['memberOf']) == 'undefined') {
779 // remove prototype data...
780 //var a = this.alias.split('#')[0];
781 //docTag.memberOf = a;
782 docTag.memberOf = this.alias;
784 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
785 this.cfgs[docTag.name] = docTag;
789 configToArray: function()
792 for(var ci in this.cfgs) {
793 // dont show hidden!!
794 if (this.cfgs[ci].desc.match(/@hide/)) {
797 r.push(this.cfgs[ci]);
805 * Elements that are not serialized
809 '$args' // not needed AFAIK
812 Symbol.srcFile = ""; //running reference to the current file being parsed
815 Symbol.fromDump = function(t)
817 var ns = new Symbol();
819 if (typeof(ns[i]) == "undefined") {
820 println("ERR:no default for Symbol:"+ i);