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