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 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");
288 foreach(var d in sinces) {
289 this.since = this.since == "" ? "": "\n";
290 this.since += d.desc;
295 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
296 assertEqual(sym.since, "1.01", "@since tag, description is found.");
300 if (this.comment.getTag("constant").length) {
301 this.isConstant = true;
306 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
307 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
311 var versions = this.comment.getTag("version");
312 if (versions.length) {
314 foreach(var d in versions) {
315 this.version = this.version == "" ? "": "\n";
316 this.version += d.desc;
321 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
322 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
326 var deprecateds = this.comment.getTag("deprecated");
327 if (deprecateds.length) {
328 this.deprecated = "";
329 foreach(var d in deprecateds) {
330 this.deprecated = this.deprecated == "" ? "": "\n";
331 this.deprecated += d.desc;
336 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
337 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
341 var examples = this.comment.getTag("example");
342 if (examples.length) {
343 this.example = examples[0];
347 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
348 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
352 var sees = this.comment.getTag("see");
354 var thisSee = this.see;
355 sees.map(function($){thisSee.push($.desc);});
359 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
360 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
364 var classes = this.comment.getTag("class");
365 if (classes.length) {
366 //print(JSON.stringify(this,null,4));
367 this.isa = "CONSTRUCTOR";
368 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
369 if (!this.classDesc) {
370 this.classDesc = this.desc;
377 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
378 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
379 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
383 var namespaces = this.comment.getTag("namespace");
384 if (namespaces.length) {
385 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
386 this.isNamespace = true;
390 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
391 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
395 var params = this.comment.getTag("param");
397 // user-defined params overwrite those with same name defined by the parser
398 var thisParams = this.getParams();
400 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
401 this.setParams(params);
403 else { // need to overlay these user-defined params on to existing parser-defined params
404 for (var i = 0, l = params.length; i < l; i++) {
406 if (params[i].type) thisParams[i].type = params[i].type;
407 thisParams[i].name = params[i].name;
408 thisParams[i].desc = params[i].desc;
409 thisParams[i].isOptional = params[i].isOptional;
410 thisParams[i].defaultValue = params[i].defaultValue;
412 else thisParams[i] = params[i];
418 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
419 assertEqual(sym.params.length, 1, "parser defined param is found.");
421 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
422 assertEqual(sym.params.length, 1, "user defined param is found.");
423 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
424 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
426 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
427 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
428 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
429 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
431 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
432 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
433 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
434 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
436 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
437 assertEqual(sym.params.length, 1, "user defined param with description is found.");
438 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
442 if (this.comment.getTag("constructor").length) {
443 this.isa = "CONSTRUCTOR";
447 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
448 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
452 if (this.comment.getTag("static").length) {
453 this.isStatic = true;
454 if (this.isa == "CONSTRUCTOR") {
455 this.isNamespace = true;
460 if (this.comment.getTag("singleton").length) {
461 this.isStatic = true;
462 //print('------------- got singleton ---------------' + this.isa);
463 //if (this.isa == "CONSTRUCTOR") {
464 // this.isNamespace = true;
471 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
472 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
473 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
477 if (this.comment.getTag("inner").length) {
479 this.isStatic = false;
483 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
484 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
485 assertEqual(sym.isInner, true, "@inner makes isInner true.");
489 if (this.comment.getTag("field").length) {
494 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
495 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
499 if (this.comment.getTag("function").length) {
500 this.isa = "FUNCTION";
504 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
505 // change a property to a function..
506 this.isa = "FUNCTION";
511 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
512 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
516 var events = this.comment.getTag("event");
518 this.isa = "FUNCTION";
523 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
524 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
525 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
529 var names = this.comment.getTag("name");
531 this.setName(names[0].desc);
539 var properties = this.comment.getTag("property");
540 if (properties.length) {
541 thisProperties = this.properties;
542 for (var i = 0; i < properties.length; i++) {
543 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
544 // TODO: shouldn't the following happen in the addProperty method of Symbol?
545 property.name = properties[i].name;
546 property.memberOf = this.alias;
547 if (properties[i].type) property.type = properties[i].type;
548 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
549 this.addProperty(property);
550 imports.Parser.Parser.addSymbol(property);
555 var conf = this.comment.getTag("cfg");
557 for (var i = 0; i < conf.length; i++) {
558 this.addConfig(conf[i]);
567 var returns = this.comment.getTag("return");
568 if (returns.length) { // there can be many return tags in a single doclet
569 this.returns = returns;
570 this.type = returns.map(function($){return $.type}).join(", ");
578 this.exceptions = this.comment.getTag("throws");
585 var requires = this.comment.getTag("requires");
586 if (requires.length) {
587 this.requires = requires.map(function($){return $.desc});
595 var types = this.comment.getTag("type");
597 this.type = types[0].desc; //multiple type tags are ignored
605 if (this.comment.getTag("private").length || this.isInner) {
606 this.isPrivate = true;
610 if (this.comment.getTag("ignore").length) {
611 this.isIgnored = true;
618 // @inherits ... as ...
619 var inherits = this.comment.getTag("inherits");
620 if (inherits.length) {
621 for (var i = 0; i < inherits.length; i++) {
622 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
623 var inAlias = RegExp.$1;
624 var inAs = RegExp.$2 || inAlias;
626 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
629 inAs = inAs.replace(/\.prototype\.?/g, "#");
630 inAs = inAs.replace(/^this\.?/, "#");
633 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
635 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
638 inAs = this.alias + joiner + inAs;
641 this.inherits.push({alias: inAlias, as: inAs});
650 this.augments = this.comment.getTag("augments");
653 if (this.comment.getTag("extends")) {
654 this.augments = this.comment.getTag("extends");
659 var defaults = this.comment.getTag("default");
660 if (defaults.length) {
661 if (this.is("OBJECT")) {
662 this.defaultValue = defaults[0].desc;
671 var memberOfs = this.comment.getTag("memberOf");
672 if (memberOfs.length) {
673 this.memberOf = memberOfs[0].desc;
674 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
675 this.name = this.name.split('.').pop();
676 this.name = this.name.split('#').pop();
677 this.name = this.memberOf + this.name;
678 this._name = this.name
679 this.alias = this.name;
687 if (this.comment.getTag("public").length) {
688 this.isPrivate = false;
696 is : function(what) {
697 return this.isa === what;
700 isBuiltin : function() {
701 return SymbolSet.isBuiltin(this.alias);
704 setType : function(/**String*/comment, /**Boolean*/overwrite) {
705 if (!overwrite && this.type) return;
706 var typeComment = DocComment.unwrapComment(comment);
707 this.type = typeComment;
710 inherit : function(symbol) {
711 if (!this.hasMember(symbol.name) && !symbol.isInner) {
712 if (symbol.is("FUNCTION"))
713 this.methods.push(symbol);
714 else if (symbol.is("OBJECT"))
715 this.properties.push(symbol);
719 hasMember : function(name) {
720 return (this.hasMethod(name) || this.hasProperty(name));
723 addMember : function(symbol) {
724 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
726 if (symbol.comment.getTag("cfg").length == 1) {
727 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
728 this.addConfig(symbol.comment.getTag("cfg")[0]);
732 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
733 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
736 hasMethod : function(name) {
737 var thisMethods = this.methods;
738 for (var i = 0, l = thisMethods.length; i < l; i++) {
739 if (thisMethods[i].name == name) return true;
740 if (thisMethods[i].alias == name) return true;
745 addMethod : function(symbol) {
746 var methodAlias = symbol.alias;
747 var thisMethods = this.methods;
748 for (var i = 0, l = thisMethods.length; i < l; i++) {
749 if (thisMethods[i].alias == methodAlias) {
750 thisMethods[i] = symbol; // overwriting previous method
754 thisMethods.push(symbol); // new method with this alias
757 hasProperty : function(name) {
758 var thisProperties = this.properties;
759 for (var i = 0, l = thisProperties.length; i < l; i++) {
760 if (thisProperties[i].name == name) return true;
761 if (thisProperties[i].alias == name) return true;
766 addProperty : function(symbol) {
767 var propertyAlias = symbol.alias;
768 var thisProperties = this.properties;
769 for (var i = 0, l = thisProperties.length; i < l; i++) {
770 if (thisProperties[i].alias == propertyAlias) {
771 thisProperties[i] = symbol; // overwriting previous property
776 thisProperties.push(symbol); // new property with this alias
779 addDocTag : function(docTag)
781 this.comment.tags.push(docTag);
782 if (docTag.title == 'cfg') {
783 this.addConfig(docTag);
788 addConfig : function(docTag)
790 if (typeof(docTag['memberOf']) == 'undefined') {
791 // remove prototype data...
792 //var a = this.alias.split('#')[0];
793 //docTag.memberOf = a;
794 docTag.memberOf = this.alias;
796 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
797 this.cfgs[docTag.name] = docTag;
801 configToArray: function()
804 for(var ci in this.cfgs) {
805 // dont show hidden!!
806 if (this.cfgs[ci].desc.match(/@hide/)) {
809 r.push(this.cfgs[ci]);
817 * Elements that are not serialized
821 '$args' // not needed AFAIK
824 Symbol.srcFile = ""; //running reference to the current file being parsed
827 Symbol.fromDump = function(t)
829 var ns = new Symbol();
831 if (typeof(ns[i]) == "undefined") {
832 println("ERR:no default for Symbol:"+ i);