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 __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);
50 get { return this.__name; }
54 string defaultValue : "",
59 augments : [], // Doctag[]
60 exceptions : [], // Doctag[]
61 inherits : [], // Doctag[]
62 methods : [], // Symbol[]
64 _params : [], //Doctag[]
65 properties : [], //Doctag[]
66 requires : [], //Doctag[]
67 returns : [], //Doctag[]
77 //$args : [], // original arguments used when constructing.
79 public string alias = "";
82 string classDesc = "";
84 string deprecated = "";
90 string isa = "OBJECT"; // OBJECT//FUNCTION
92 public bool isEvent = false;
93 public bool isConstant = false;
94 public bool isIgnored = false;
95 public bool isInner = false;
96 public bool isNamespace = false;
97 public bool isPrivate = false;
98 public bool isStatic = false;
100 string memberOf : "",
114 public void initArrays()
116 // only initialize arrays / objects..
123 this.exceptions = [];
126 this.isa = "OBJECT"; // OBJECT//FUNCTION
129 this.properties = [];
138 this.inheritsFrom = [];
139 this.childClasses = [];
141 this.comment = new DocComment();
142 this.comment.isUserComment = false;
147 Public Symbol.new_builtin(string name)
151 this.srcFile = DocParser.currentSourceFile;
153 this.alias = this.name;
154 this.isa = "CONSTRUCTOR";
155 this.comment = new DocComment("");
156 this.comment.isUserComment = false;
157 this.isNamespace = false;
159 this.isPrivate = false;
167 //__defineSetter__("name",
170 //__defineGetter__("name",
171 getName : function() { return this._name; },
173 //__defineSetter__("params",
174 setParams :function(v) {
175 for (var i = 0, l = v.length; i < l; i++) {
176 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
177 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
178 this._params[i] = new DocTag(
179 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
182 this._params[i] = v[i];
185 this.params = this._params;
190 //__defineGetter__("params",
191 public getParams : function() {
196 public Symbol.new_populate_with_args(
198 Gee.ArrayList<string> params, // fixme???
204 this.$args = arguments;
205 //println("Symbol created: " + isa + ":" + name);
207 this.alias = this.getName();
208 this.setParams(params);
209 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
210 this.comment = comment || new DocComment("")
212 this.srcFile = DocParser.currentSourceFile;
216 if (this.is("FILE") && !this.alias) { // this will never hapen???
217 this.alias = this.srcFile;
224 setTags : function() {
226 var authors = this.comment.getTag("author");
227 if (authors.length) {
228 this.author = authors.map(function($){return $.desc;}).join(", ");
232 assert("testing Symbol");
234 requires("../lib/JSDOC/DocComment.js");
235 requires("../frame/String.js");
236 requires("../lib/JSDOC/DocTag.js");
238 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
239 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
242 var mth = this.comment.getTag("method");
244 this.isa = "FUNCTION";
247 var descs = this.comment.getTag("desc");
249 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
253 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
254 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
258 if (this.is("FILE")) {
259 if (!this.alias) this.alias = this.srcFile;
261 var overviews = this.comment.getTag("overview");
262 if (overviews.length) {
263 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
268 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
269 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
273 var sinces = this.comment.getTag("since");
275 this.since = sinces.map(function($){return $.desc;}).join(", ");
279 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
280 assertEqual(sym.since, "1.01", "@since tag, description is found.");
284 if (this.comment.getTag("constant").length) {
285 this.isConstant = true;
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
291 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
295 var versions = this.comment.getTag("version");
296 if (versions.length) {
297 this.version = versions.map(function($){return $.desc;}).join(", ");
301 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
302 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
306 var deprecateds = this.comment.getTag("deprecated");
307 if (deprecateds.length) {
308 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
312 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
313 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
317 var examples = this.comment.getTag("example");
318 if (examples.length) {
319 this.example = examples[0];
323 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
324 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
328 var sees = this.comment.getTag("see");
330 var thisSee = this.see;
331 sees.map(function($){thisSee.push($.desc);});
335 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
336 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
340 var classes = this.comment.getTag("class");
341 if (classes.length) {
342 //print(JSON.stringify(this,null,4));
343 this.isa = "CONSTRUCTOR";
344 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
345 if (!this.classDesc) {
346 this.classDesc = this.desc;
353 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
354 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
355 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
359 var namespaces = this.comment.getTag("namespace");
360 if (namespaces.length) {
361 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
362 this.isNamespace = true;
366 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
367 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
371 var params = this.comment.getTag("param");
373 // user-defined params overwrite those with same name defined by the parser
374 var thisParams = this.getParams();
376 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
377 this.setParams(params);
379 else { // need to overlay these user-defined params on to existing parser-defined params
380 for (var i = 0, l = params.length; i < l; i++) {
382 if (params[i].type) thisParams[i].type = params[i].type;
383 thisParams[i].name = params[i].name;
384 thisParams[i].desc = params[i].desc;
385 thisParams[i].isOptional = params[i].isOptional;
386 thisParams[i].defaultValue = params[i].defaultValue;
388 else thisParams[i] = params[i];
394 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
395 assertEqual(sym.params.length, 1, "parser defined param is found.");
397 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
398 assertEqual(sym.params.length, 1, "user defined param is found.");
399 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
400 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
402 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
403 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
404 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
405 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
407 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
408 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
409 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
410 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
412 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
413 assertEqual(sym.params.length, 1, "user defined param with description is found.");
414 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
418 if (this.comment.getTag("constructor").length) {
419 this.isa = "CONSTRUCTOR";
423 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
424 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
428 if (this.comment.getTag("static").length) {
429 this.isStatic = true;
430 if (this.isa == "CONSTRUCTOR") {
431 this.isNamespace = true;
436 if (this.comment.getTag("singleton").length) {
437 this.isStatic = true;
438 //print('------------- got singleton ---------------' + this.isa);
439 //if (this.isa == "CONSTRUCTOR") {
440 // this.isNamespace = true;
447 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
448 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
449 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
453 if (this.comment.getTag("inner").length) {
455 this.isStatic = false;
459 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
460 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
461 assertEqual(sym.isInner, true, "@inner makes isInner true.");
465 if (this.comment.getTag("field").length) {
470 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
471 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
475 if (this.comment.getTag("function").length) {
476 this.isa = "FUNCTION";
480 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
481 // change a property to a function..
482 this.isa = "FUNCTION";
487 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
488 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
492 var events = this.comment.getTag("event");
494 this.isa = "FUNCTION";
499 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
500 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
501 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
505 var names = this.comment.getTag("name");
507 this.setName(names[0].desc);
515 var properties = this.comment.getTag("property");
516 if (properties.length) {
517 thisProperties = this.properties;
518 for (var i = 0; i < properties.length; i++) {
519 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
520 // TODO: shouldn't the following happen in the addProperty method of Symbol?
521 property.name = properties[i].name;
522 property.memberOf = this.alias;
523 if (properties[i].type) property.type = properties[i].type;
524 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
525 this.addProperty(property);
526 imports.Parser.Parser.addSymbol(property);
531 var conf = this.comment.getTag("cfg");
533 for (var i = 0; i < conf.length; i++) {
534 this.addConfig(conf[i]);
543 var returns = this.comment.getTag("return");
544 if (returns.length) { // there can be many return tags in a single doclet
545 this.returns = returns;
546 this.type = returns.map(function($){return $.type}).join(", ");
554 this.exceptions = this.comment.getTag("throws");
561 var requires = this.comment.getTag("requires");
562 if (requires.length) {
563 this.requires = requires.map(function($){return $.desc});
571 var types = this.comment.getTag("type");
573 this.type = types[0].desc; //multiple type tags are ignored
581 if (this.comment.getTag("private").length || this.isInner) {
582 this.isPrivate = true;
586 if (this.comment.getTag("ignore").length) {
587 this.isIgnored = true;
594 // @inherits ... as ...
595 var inherits = this.comment.getTag("inherits");
596 if (inherits.length) {
597 for (var i = 0; i < inherits.length; i++) {
598 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
599 var inAlias = RegExp.$1;
600 var inAs = RegExp.$2 || inAlias;
602 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
605 inAs = inAs.replace(/\.prototype\.?/g, "#");
606 inAs = inAs.replace(/^this\.?/, "#");
609 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
611 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
614 inAs = this.alias + joiner + inAs;
617 this.inherits.push({alias: inAlias, as: inAs});
626 this.augments = this.comment.getTag("augments");
629 if (this.comment.getTag("extends")) {
630 this.augments = this.comment.getTag("extends");
635 var defaults = this.comment.getTag("default");
636 if (defaults.length) {
637 if (this.is("OBJECT")) {
638 this.defaultValue = defaults[0].desc;
647 var memberOfs = this.comment.getTag("memberOf");
648 if (memberOfs.length) {
649 this.memberOf = memberOfs[0].desc;
650 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
651 this.name = this.name.split('.').pop();
652 this.name = this.name.split('#').pop();
653 this.name = this.memberOf + this.name;
654 this._name = this.name
655 this.alias = this.name;
663 if (this.comment.getTag("public").length) {
664 this.isPrivate = false;
672 is : function(what) {
673 return this.isa === what;
676 isBuiltin : function() {
677 return SymbolSet.isBuiltin(this.alias);
680 setType : function(/**String*/comment, /**Boolean*/overwrite) {
681 if (!overwrite && this.type) return;
682 var typeComment = DocComment.unwrapComment(comment);
683 this.type = typeComment;
686 inherit : function(symbol) {
687 if (!this.hasMember(symbol.name) && !symbol.isInner) {
688 if (symbol.is("FUNCTION"))
689 this.methods.push(symbol);
690 else if (symbol.is("OBJECT"))
691 this.properties.push(symbol);
695 hasMember : function(name) {
696 return (this.hasMethod(name) || this.hasProperty(name));
699 addMember : function(symbol) {
700 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
702 if (symbol.comment.getTag("cfg").length == 1) {
703 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
704 this.addConfig(symbol.comment.getTag("cfg")[0]);
708 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
709 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
712 hasMethod : function(name) {
713 var thisMethods = this.methods;
714 for (var i = 0, l = thisMethods.length; i < l; i++) {
715 if (thisMethods[i].name == name) return true;
716 if (thisMethods[i].alias == name) return true;
721 addMethod : function(symbol) {
722 var methodAlias = symbol.alias;
723 var thisMethods = this.methods;
724 for (var i = 0, l = thisMethods.length; i < l; i++) {
725 if (thisMethods[i].alias == methodAlias) {
726 thisMethods[i] = symbol; // overwriting previous method
730 thisMethods.push(symbol); // new method with this alias
733 hasProperty : function(name) {
734 var thisProperties = this.properties;
735 for (var i = 0, l = thisProperties.length; i < l; i++) {
736 if (thisProperties[i].name == name) return true;
737 if (thisProperties[i].alias == name) return true;
742 addProperty : function(symbol) {
743 var propertyAlias = symbol.alias;
744 var thisProperties = this.properties;
745 for (var i = 0, l = thisProperties.length; i < l; i++) {
746 if (thisProperties[i].alias == propertyAlias) {
747 thisProperties[i] = symbol; // overwriting previous property
752 thisProperties.push(symbol); // new property with this alias
755 addDocTag : function(docTag)
757 this.comment.tags.push(docTag);
758 if (docTag.title == 'cfg') {
759 this.addConfig(docTag);
764 addConfig : function(docTag)
766 if (typeof(docTag['memberOf']) == 'undefined') {
767 // remove prototype data...
768 //var a = this.alias.split('#')[0];
769 //docTag.memberOf = a;
770 docTag.memberOf = this.alias;
772 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
773 this.cfgs[docTag.name] = docTag;
777 configToArray: function()
780 for(var ci in this.cfgs) {
781 // dont show hidden!!
782 if (this.cfgs[ci].desc.match(/@hide/)) {
785 r.push(this.cfgs[ci]);
793 * Elements that are not serialized
797 '$args' // not needed AFAIK
800 Symbol.srcFile = ""; //running reference to the current file being parsed
803 Symbol.fromDump = function(t)
805 var ns = new Symbol();
807 if (typeof(ns[i]) == "undefined") {
808 println("ERR:no default for Symbol:"+ i);