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;
46 get { return this.private_name; }
50 string defaultValue : "",
55 augments : [], // Doctag[]
56 exceptions : [], // Doctag[]
57 inherits : [], // Doctag[]
58 methods : [], // Symbol[]
60 _params : [], //Doctag[]
61 properties : [], //Doctag[]
62 requires : [], //Doctag[]
63 returns : [], //Doctag[]
73 //$args : [], // original arguments used when constructing.
75 public string alias = "";
78 string classDesc = "";
80 string deprecated = "";
86 string isa = "OBJECT"; // OBJECT//FUNCTION
88 public bool isEvent = false;
89 public bool isConstant = false;
90 public bool isIgnored = false;
91 public bool isInner = false;
92 public bool isNamespace = false;
93 public bool isPrivate = false;
94 public bool isStatic = false;
110 public void initArrays()
112 // only initialize arrays / objects..
119 this.exceptions = [];
122 this.isa = "OBJECT"; // OBJECT//FUNCTION
125 this.properties = [];
134 this.inheritsFrom = [];
135 this.childClasses = [];
137 this.comment = new DocComment();
138 this.comment.isUserComment = false;
143 Public Symbol.new_builtin(string name)
147 this.srcFile = DocParser.currentSourceFile;
148 this.setName( name );
149 this.alias = this.name;
150 this.isa = "CONSTRUCTOR";
151 this.comment = new DocComment("");
152 this.comment.isUserComment = false;
153 this.isNamespace = false;
155 this.isPrivate = false;
164 setParams :function(v) {
165 for (var i = 0, l = v.length; i < l; i++) {
166 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
167 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
168 this._params[i] = new DocTag(
169 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
172 this._params[i] = v[i];
175 this.params = this._params;
180 //__defineGetter__("params",
181 public getParams : function() {
186 public Symbol.new_populate_with_args(
188 Gee.ArrayList<string> params, // fixme???
194 this.$args = arguments;
195 //println("Symbol created: " + isa + ":" + name);
197 this.alias = this.getName();
198 this.setParams(params);
199 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
200 this.comment = comment || new DocComment("")
202 this.srcFile = DocParser.currentSourceFile;
206 if (this.is("FILE") && !this.alias) { // this will never hapen???
207 this.alias = this.srcFile;
214 setTags : function() {
216 var authors = this.comment.getTag("author");
217 if (authors.length) {
218 this.author = authors.map(function($){return $.desc;}).join(", ");
222 assert("testing Symbol");
224 requires("../lib/JSDOC/DocComment.js");
225 requires("../frame/String.js");
226 requires("../lib/JSDOC/DocTag.js");
228 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
229 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
232 var mth = this.comment.getTag("method");
234 this.isa = "FUNCTION";
237 var descs = this.comment.getTag("desc");
239 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
243 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
244 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
248 if (this.is("FILE")) {
249 if (!this.alias) this.alias = this.srcFile;
251 var overviews = this.comment.getTag("overview");
252 if (overviews.length) {
253 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
258 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
259 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
263 var sinces = this.comment.getTag("since");
265 this.since = sinces.map(function($){return $.desc;}).join(", ");
269 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
270 assertEqual(sym.since, "1.01", "@since tag, description is found.");
274 if (this.comment.getTag("constant").length) {
275 this.isConstant = true;
280 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
281 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
285 var versions = this.comment.getTag("version");
286 if (versions.length) {
287 this.version = versions.map(function($){return $.desc;}).join(", ");
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
292 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
296 var deprecateds = this.comment.getTag("deprecated");
297 if (deprecateds.length) {
298 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
303 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
307 var examples = this.comment.getTag("example");
308 if (examples.length) {
309 this.example = examples[0];
313 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
314 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
318 var sees = this.comment.getTag("see");
320 var thisSee = this.see;
321 sees.map(function($){thisSee.push($.desc);});
325 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
326 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
330 var classes = this.comment.getTag("class");
331 if (classes.length) {
332 //print(JSON.stringify(this,null,4));
333 this.isa = "CONSTRUCTOR";
334 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
335 if (!this.classDesc) {
336 this.classDesc = this.desc;
343 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
344 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
345 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
349 var namespaces = this.comment.getTag("namespace");
350 if (namespaces.length) {
351 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
352 this.isNamespace = true;
356 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
357 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
361 var params = this.comment.getTag("param");
363 // user-defined params overwrite those with same name defined by the parser
364 var thisParams = this.getParams();
366 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
367 this.setParams(params);
369 else { // need to overlay these user-defined params on to existing parser-defined params
370 for (var i = 0, l = params.length; i < l; i++) {
372 if (params[i].type) thisParams[i].type = params[i].type;
373 thisParams[i].name = params[i].name;
374 thisParams[i].desc = params[i].desc;
375 thisParams[i].isOptional = params[i].isOptional;
376 thisParams[i].defaultValue = params[i].defaultValue;
378 else thisParams[i] = params[i];
384 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
385 assertEqual(sym.params.length, 1, "parser defined param is found.");
387 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
388 assertEqual(sym.params.length, 1, "user defined param is found.");
389 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
390 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
392 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
393 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
394 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
395 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
397 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
398 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
399 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
400 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
402 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
403 assertEqual(sym.params.length, 1, "user defined param with description is found.");
404 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
408 if (this.comment.getTag("constructor").length) {
409 this.isa = "CONSTRUCTOR";
413 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
414 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
418 if (this.comment.getTag("static").length) {
419 this.isStatic = true;
420 if (this.isa == "CONSTRUCTOR") {
421 this.isNamespace = true;
426 if (this.comment.getTag("singleton").length) {
427 this.isStatic = true;
428 //print('------------- got singleton ---------------' + this.isa);
429 //if (this.isa == "CONSTRUCTOR") {
430 // this.isNamespace = true;
437 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
438 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
439 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
443 if (this.comment.getTag("inner").length) {
445 this.isStatic = false;
449 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
450 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
451 assertEqual(sym.isInner, true, "@inner makes isInner true.");
455 if (this.comment.getTag("field").length) {
460 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
461 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
465 if (this.comment.getTag("function").length) {
466 this.isa = "FUNCTION";
470 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
471 // change a property to a function..
472 this.isa = "FUNCTION";
477 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
478 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
482 var events = this.comment.getTag("event");
484 this.isa = "FUNCTION";
489 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
490 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
491 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
495 var names = this.comment.getTag("name");
497 this.setName(names[0].desc);
505 var properties = this.comment.getTag("property");
506 if (properties.length) {
507 thisProperties = this.properties;
508 for (var i = 0; i < properties.length; i++) {
509 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
510 // TODO: shouldn't the following happen in the addProperty method of Symbol?
511 property.name = properties[i].name;
512 property.memberOf = this.alias;
513 if (properties[i].type) property.type = properties[i].type;
514 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
515 this.addProperty(property);
516 imports.Parser.Parser.addSymbol(property);
521 var conf = this.comment.getTag("cfg");
523 for (var i = 0; i < conf.length; i++) {
524 this.addConfig(conf[i]);
533 var returns = this.comment.getTag("return");
534 if (returns.length) { // there can be many return tags in a single doclet
535 this.returns = returns;
536 this.type = returns.map(function($){return $.type}).join(", ");
544 this.exceptions = this.comment.getTag("throws");
551 var requires = this.comment.getTag("requires");
552 if (requires.length) {
553 this.requires = requires.map(function($){return $.desc});
561 var types = this.comment.getTag("type");
563 this.type = types[0].desc; //multiple type tags are ignored
571 if (this.comment.getTag("private").length || this.isInner) {
572 this.isPrivate = true;
576 if (this.comment.getTag("ignore").length) {
577 this.isIgnored = true;
584 // @inherits ... as ...
585 var inherits = this.comment.getTag("inherits");
586 if (inherits.length) {
587 for (var i = 0; i < inherits.length; i++) {
588 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
589 var inAlias = RegExp.$1;
590 var inAs = RegExp.$2 || inAlias;
592 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
595 inAs = inAs.replace(/\.prototype\.?/g, "#");
596 inAs = inAs.replace(/^this\.?/, "#");
599 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
601 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
604 inAs = this.alias + joiner + inAs;
607 this.inherits.push({alias: inAlias, as: inAs});
616 this.augments = this.comment.getTag("augments");
619 if (this.comment.getTag("extends")) {
620 this.augments = this.comment.getTag("extends");
625 var defaults = this.comment.getTag("default");
626 if (defaults.length) {
627 if (this.is("OBJECT")) {
628 this.defaultValue = defaults[0].desc;
637 var memberOfs = this.comment.getTag("memberOf");
638 if (memberOfs.length) {
639 this.memberOf = memberOfs[0].desc;
640 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
641 this.name = this.name.split('.').pop();
642 this.name = this.name.split('#').pop();
643 this.name = this.memberOf + this.name;
644 this._name = this.name
645 this.alias = this.name;
653 if (this.comment.getTag("public").length) {
654 this.isPrivate = false;
662 is : function(what) {
663 return this.isa === what;
666 isBuiltin : function() {
667 return SymbolSet.isBuiltin(this.alias);
670 setType : function(/**String*/comment, /**Boolean*/overwrite) {
671 if (!overwrite && this.type) return;
672 var typeComment = DocComment.unwrapComment(comment);
673 this.type = typeComment;
676 inherit : function(symbol) {
677 if (!this.hasMember(symbol.name) && !symbol.isInner) {
678 if (symbol.is("FUNCTION"))
679 this.methods.push(symbol);
680 else if (symbol.is("OBJECT"))
681 this.properties.push(symbol);
685 hasMember : function(name) {
686 return (this.hasMethod(name) || this.hasProperty(name));
689 addMember : function(symbol) {
690 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
692 if (symbol.comment.getTag("cfg").length == 1) {
693 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
694 this.addConfig(symbol.comment.getTag("cfg")[0]);
698 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
699 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
702 hasMethod : function(name) {
703 var thisMethods = this.methods;
704 for (var i = 0, l = thisMethods.length; i < l; i++) {
705 if (thisMethods[i].name == name) return true;
706 if (thisMethods[i].alias == name) return true;
711 addMethod : function(symbol) {
712 var methodAlias = symbol.alias;
713 var thisMethods = this.methods;
714 for (var i = 0, l = thisMethods.length; i < l; i++) {
715 if (thisMethods[i].alias == methodAlias) {
716 thisMethods[i] = symbol; // overwriting previous method
720 thisMethods.push(symbol); // new method with this alias
723 hasProperty : function(name) {
724 var thisProperties = this.properties;
725 for (var i = 0, l = thisProperties.length; i < l; i++) {
726 if (thisProperties[i].name == name) return true;
727 if (thisProperties[i].alias == name) return true;
732 addProperty : function(symbol) {
733 var propertyAlias = symbol.alias;
734 var thisProperties = this.properties;
735 for (var i = 0, l = thisProperties.length; i < l; i++) {
736 if (thisProperties[i].alias == propertyAlias) {
737 thisProperties[i] = symbol; // overwriting previous property
742 thisProperties.push(symbol); // new property with this alias
745 addDocTag : function(docTag)
747 this.comment.tags.push(docTag);
748 if (docTag.title == 'cfg') {
749 this.addConfig(docTag);
754 addConfig : function(docTag)
756 if (typeof(docTag['memberOf']) == 'undefined') {
757 // remove prototype data...
758 //var a = this.alias.split('#')[0];
759 //docTag.memberOf = a;
760 docTag.memberOf = this.alias;
762 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
763 this.cfgs[docTag.name] = docTag;
767 configToArray: function()
770 for(var ci in this.cfgs) {
771 // dont show hidden!!
772 if (this.cfgs[ci].desc.match(/@hide/)) {
775 r.push(this.cfgs[ci]);
783 * Elements that are not serialized
787 '$args' // not needed AFAIK
790 Symbol.srcFile = ""; //running reference to the current file being parsed
793 Symbol.fromDump = function(t)
795 var ns = new Symbol();
797 if (typeof(ns[i]) == "undefined") {
798 println("ERR:no default for Symbol:"+ i);