JSDOC/TokenReader.js
[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 Token_id = 1;
33
34
35         public class TokenKeyMap : Object {
36                 public Token key;
37                 public Gee.ArrayList<Token> vals;
38                 
39                 public TokenKeyMap()
40                 {
41                         this.key = new Token("","VOID", "VOID"); 
42                         this.vals = new  Gee.ArrayList<Token>();
43                 }
44                 
45                 
46         }
47
48     public class Token : Object {
49         
50         public int id;
51         
52         public string data;
53         public string type;
54         public string name;
55         public int line;
56         public string prefix; // white space prefix... (when outputing with WS)
57         
58         public string outData;
59         
60         public Identifier identifier;
61         
62          // used to stuff tokens together when building a tree..
63         public Gee.ArrayList<Gee.ArrayList<Token>> items;
64         // for a object definition, key -> array of tokens..
65             public Gee.HashMap<string,TokenKeyMap> props;
66         
67         // props??? what's this???
68         
69         public Token(string data, string type, string name, int line = -1)
70         {
71             this.data = data;
72             this.type = type;
73             this.name = name;
74             this.line = line;
75             this.prefix = "";    
76             this.outData = ""; // used by packer/scopeparser
77             this.identifier = null; // used by scope
78             this.id = Token_id++;
79             
80             
81             this.items = new Gee.ArrayList<Gee.ArrayList<Token>>();
82             this.props = new Gee.HashMap<string,TokenKeyMap>();
83         }
84     
85         public string asString()
86         {
87             return "line:%d, id %d, type %s, data : %s,  name %s, , outData: %s".printf(
88                     this.line,
89                     this.id,
90                     this.type,
91                     this.data,
92                     this.name,
93                     this.outData == null ? "" : this.outData
94             );
95             
96         }
97         
98         
99         public void dump(string indent)
100                 {
101                 print("%s%s\n",indent, this.asString());
102                 if (this.items.size > 0) {
103                         
104                                 for (var i = 0;i < this.items.size; i++) {
105                                 print("%s --ITEMS[%d] [ \n",indent,i);
106                                         for (var j = 0;j < this.items[i].size; j++) {
107                                                 this.items[i][j].dump(indent + "  ");
108                                         }
109                                 }
110                         }
111                         if (this.props.size > 0) {
112                                 var m = this.props.map_iterator();
113                                 while(m.next()) {
114                                 print("%s --KEY %s ::  \n",indent,m.get_key());
115                                 var vals = m.get_value().vals;
116                                         for (var i = 0;i < vals.size; i++) {
117
118                                                 vals[i].dump(indent + "  ");
119                                         }
120                                 }
121                         
122                         
123                         }
124                         
125                 }
126         
127         
128         public string toRaw(int lvl = 0)
129         {
130             
131             
132             var ret =  this.data ;
133             
134             foreach(var ai in this.items ) {
135                 // supposed to iterate properties???
136                 string str = "";
137                 //foreach( var it in ai) {
138                  //   str += it.toRaw(lvl+1);
139                // }
140                 ret += str;
141             }
142             
143             /* -- what is a prop..
144             if (this.props) {
145                 for (var i in this.props) {
146                     ret += this.props[i].key.toRaw(lvl+1) + ' : ';
147                     this.props[i].val.forEach( function(e) {
148                         ret+=e.toRaw(lvl+1);
149                     })
150                     
151                 }
152             }
153             
154             */
155             
156             return this.prefix +   ret;
157              
158         }
159         /*
160         toJS : function() {
161             
162             try {
163                 var _tmp = '';
164                 eval( "_tmp = " + this.data);
165                 return _tmp;
166             } catch( e) {
167                 return "ERROR unparsable" + this.data;
168             }
169         },
170         */
171                         
172
173         public bool is(string what) {
174             return this.name == what || this.type == what;
175         }
176     }
177 }
178