JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / Token.js
1 //<Script type="text/javascript">
2
3 const XObject = imports.XObject.XObject;
4  
5 const console = imports.console.console;
6 /**
7  *      @class Token
8  * 
9  *  @prop data {String} raw value of token
10  *  @prop type {String} type of token
11  *     TOKN  (unknown)          - name is UNKNOWN_TOKEN
12  * 
13  *     KEYW  (keyword)          - name is upper case version of keyword
14  *     NAME  (name/identifier)  - name is NAME
15  *     COMM  (comment)          - name is MULTI_LINE_COMM, JSDOC, SINGLE_LINE_COMM
16  *     PUNC  (puctuation)       - name is String description of punctionan (eg LEFTPARAM)
17  *     WHIT  (white space)      - name is SPACE,NEWLINE
18  *     STRN  (string)           - name is DOBULE_QUOTE, SINGLE_QUOTE
19  *     NUMB  (number)           - name is OCTAL,DECIMAL,HEC_DEC
20  *     REGX   (reg.expression)  - name is REGX
21  *  @prop name {String} see type details above
22  *  @prop identifier {Identifier} identifier class if relivant
23  * 
24  * 
25  * 
26  * old mappings:
27  * 
28  * Script.TOKidentifier  - type == 'NAME'
29  * Script.TOKassign  = data == '='
30  * Script.TOKsemicolon data == '';
31  * 
32  * 
33  * 
34 */
35
36 const Token = XObject.define(
37     function(data, type, name, line) {
38         this.data = data;
39         this.type = type;
40         this.name = name;
41         this.line = line;
42         this.prefix = '';    
43         this.outData = false; // used by packer/scopeparser
44         this.identifier = false; // used by scope
45         this.id = Token.id++;
46     }, 
47     Object, 
48     {
49          toString: function()
50         {
51             return 'line:' + this.line + ', type:' + this.type + 
52                 ', name:' + this.name + ', data:' + this.data + 
53                 ((this.outData === false) ? '' : ( 'outData : ' + this.outData));
54         },
55         
56         
57         toRaw : function(lvl)
58         {
59             lvl = lvl || 0;
60             
61             var ret =  this.data ;
62             
63             
64             if (this.items) {
65                 var ar = [];
66                 this.items.forEach(  function(ai) {
67                     
68                     var str = '';
69                     ai.forEach(  function(it) {
70                         str += it.toRaw(lvl + 1);
71                     })
72                     ar.push(str);
73                     
74                 })
75                 ret +=   ar.join('');
76                 
77             }
78             if (this.props) {
79                 for (var i in this.props) {
80                     ret += this.props[i].key.toRaw(lvl+1) + ' : ';
81                     this.props[i].val.forEach( function(e) {
82                         ret+=e.toRaw(lvl+1);
83                     })
84                     
85                 }
86             }
87             
88             
89             
90             return this.prefix +   ret;
91              
92         },
93
94         toJS : function() {
95             
96             try {
97                 var _tmp = '';
98                 eval( "_tmp = " + this.data);
99                 return _tmp;
100             } catch( e) {
101                 return "ERROR unparsable" + this.data;
102             }
103         },
104          
105                         
106
107         is : function(what) {
108             return this.name === what || this.type === what;
109         }
110 });
111 Token.id = 0;