JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / JSDOC / TokenReader.js
1 //<script type="text/javascript">
2
3  
4 XObject = imports.XObject.XObject;
5 console = imports.console.console;
6
7
8 Token   = imports.Token.Token;
9 Lang    = imports.Lang.Lang;
10
11 /**
12         @class Search a {@link JSDOC.TextStream} for language tokens.
13 */
14 TokenReader = XObject.define(
15     function(o) {
16         
17         this.keepDocs = true;
18         this.keepWhite = false;
19         this.keepComments = false;
20         this.sepIdents = false; // seperate '.' in identifiers..
21         XObject.extend(this, o || {});
22         
23     },
24     Object,
25     {
26         collapseWhite : false, // only reduces white space...
27
28         /**
29          * tokenize a stream
30          * @return {Array} of tokens
31          * 
32          * ts = new TextStream(File.read(str));
33          * tr = TokenReader({ keepComments : true, keepWhite : true });
34          * tr.tokenize(ts)
35          * 
36          */
37             
38
39
40         tokenize : function(/**JSDOC.TextStream*/stream) {
41             this.line =1;
42             var tokens = [];
43             /**@ignore*/ tokens.last    = function() { return tokens[tokens.length-1]; }
44             /**@ignore*/ tokens.lastSym = function() {
45                 for (var i = tokens.length-1; i >= 0; i--) {
46                     if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
47                 }
48             }
49
50             while (!stream.look().eof) {
51                 if (this.read_mlcomment(stream, tokens)) continue;
52                 if (this.read_slcomment(stream, tokens)) continue;
53                 if (this.read_dbquote(stream, tokens))   continue;
54                 if (this.read_snquote(stream, tokens))   continue;
55                 if (this.read_regx(stream, tokens))      continue;
56                 if (this.read_numb(stream, tokens))      continue;
57                 if (this.read_punc(stream, tokens))      continue;
58                 if (this.read_newline(stream, tokens))   continue;
59                 if (this.read_space(stream, tokens))     continue;
60                 if (this.read_word(stream, tokens))      continue;
61                 
62                 // if execution reaches here then an error has happened
63                 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
64             }
65             
66             
67             
68             return tokens;
69         },
70
71         /**
72             @returns {Boolean} Was the token found?
73          */
74         read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
75             var found = "";
76             while (!stream.look().eof && Lang.isWordChar(stream.look())) {
77                 found += stream.next();
78             }
79             
80             if (found === "") {
81                 return false;
82             }
83             else {
84                 var name;
85                 if ((name = Lang.keyword(found))) {
86                     tokens.push(new Token(found, "KEYW", name, this.line));
87                     return true;
88                 }
89                 if (!this.sepIdents || found.indexOf('.') < 0 ) {
90                     tokens.push(new Token(found, "NAME", "NAME", this.line));
91                     return true;
92                 }
93                 var n = found.split('.');
94                 var p = false;
95                 n.forEach(function(nm) {
96                     if (p) {
97                         tokens.push(new Token('.', "PUNC", "DOT", this.line));
98                     }
99                     p=true;
100                     tokens.push(new Token(nm, "NAME", "NAME", this.line));
101                 });
102                 return true;
103                 
104             }
105         },
106
107         /**
108             @returns {Boolean} Was the token found?
109          */
110         read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
111             var found = "";
112             var name;
113             while (!stream.look().eof && Lang.punc(found+stream.look())) {
114                 found += stream.next();
115             }
116             
117             if (found === "") {
118                 return false;
119             }
120             else {
121                 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
122                 return true;
123             }
124         },
125
126         /**
127             @returns {Boolean} Was the token found?
128          */
129         read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
130             var found = "";
131             
132             while (!stream.look().eof && Lang.isSpace(stream.look())) {
133                 found += stream.next();
134             }
135             
136             if (found === "") {
137                 return false;
138             }
139             else {
140                 if (this.collapseWhite) found = " ";
141                 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
142                 return true;
143             }
144         },
145
146         /**
147             @returns {Boolean} Was the token found?
148          */
149         read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
150             var found = "";
151             
152             while (!stream.look().eof && Lang.isNewline(stream.look())) {
153                 this.line++;
154                 found += stream.next();
155             }
156             
157             if (found === "") {
158                 return false;
159             }
160             else {
161                 if (this.collapseWhite) {
162                     found = "\n";
163                 }
164                 if (this.keepWhite) {
165                     tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
166                 }
167                 return true;
168             }
169         },
170
171         /**
172             @returns {Boolean} Was the token found?
173          */
174         read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
175             if (stream.look() == "/" && stream.look(1) == "*") {
176                 var found = stream.next(2);
177                 var c = '';
178                 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
179                     c = stream.next();
180                     if (c == "\n") this.line++;
181                     found += c;
182                 }
183                 
184                 // to start doclet we allow /** or /*** but not /**/ or /****
185                 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
186                 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
187                 return true;
188             }
189             return false;
190         },
191
192         /**
193             @returns {Boolean} Was the token found?
194          */
195         read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
196             var found;
197             if (
198                 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
199                 || 
200                 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
201             ) {
202                 
203                 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
204                     found += stream.next();
205                 }
206                 
207                 if (this.keepComments) {
208                     tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
209                 }
210                 this.line++;
211                 return true;
212             }
213             return false;
214         },
215
216         /**
217             @returns {Boolean} Was the token found?
218          */
219         read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
220             if (stream.look() == "\"") {
221                 // find terminator
222                 var string = stream.next();
223                 
224                 while (!stream.look().eof) {
225                     if (stream.look() == "\\") {
226                         if (Lang.isNewline(stream.look(1))) {
227                             do {
228                                 stream.next();
229                             } while (!stream.look().eof && Lang.isNewline(stream.look()));
230                             string += "\\\n";
231                         }
232                         else {
233                             string += stream.next(2);
234                         }
235                     }
236                     else if (stream.look() == "\"") {
237                         string += stream.next();
238                         tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
239                         return true;
240                     }
241                     else {
242                         string += stream.next();
243                     }
244                 }
245             }
246             return false; // error! unterminated string
247         },
248
249         /**
250             @returns {Boolean} Was the token found?
251          */
252         read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
253             if (stream.look() == "'") {
254                 // find terminator
255                 var string = stream.next();
256                 
257                 while (!stream.look().eof) {
258                     if (stream.look() == "\\") { // escape sequence
259                         string += stream.next(2);
260                     }
261                     else if (stream.look() == "'") {
262                         string += stream.next();
263                         tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
264                         return true;
265                     }
266                     else {
267                         string += stream.next();
268                     }
269                 }
270             }
271             return false; // error! unterminated string
272         },
273
274         /**
275             @returns {Boolean} Was the token found?
276          */
277         read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
278             if (stream.look() === "0" && stream.look(1) == "x") {
279                 return this.read_hex(stream, tokens);
280             }
281             
282             var found = "";
283             
284             while (!stream.look().eof && Lang.isNumber(found+stream.look())){
285                 found += stream.next();
286             }
287             
288             if (found === "") {
289                 return false;
290             }
291             else {
292                 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
293                 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
294                 return true;
295             }
296         },
297         /*t:
298             requires("../lib/JSDOC/TextStream.js");
299             requires("../lib/JSDOC/Token.js");
300             requires("../lib/JSDOC/Lang.js");
301             
302             plan(3, "testing read_numb");
303             
304             //// setup
305             var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
306             var tr = new TokenReader();
307             var tokens = tr.tokenize(new TextStream(src));
308             
309             var hexToken, octToken, decToken;
310             for (var i = 0; i < tokens.length; i++) {
311                 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
312                 if (tokens[i].name == "OCTAL") octToken = tokens[i];
313                 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
314             }
315             ////
316             
317             is(decToken.data, "8.0", "decimal number is found in source.");
318             is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
319             is(octToken.data, "0777", "octal number is found in source.");
320         */
321
322         /**
323             @returns {Boolean} Was the token found?
324          */
325         read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
326             var found = stream.next(2);
327             
328             while (!stream.look().eof) {
329                 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
330                     tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
331                     return true;
332                 }
333                 else {
334                     found += stream.next();
335                 }
336             }
337             return false;
338         },
339
340         /**
341             @returns {Boolean} Was the token found?
342          */
343         read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
344             var last;
345             if (
346                 stream.look() == "/"
347                 && 
348                 (
349                     
350                     (
351                         !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
352                         || 
353                         (
354                                !last.is("NUMB")
355                             && !last.is("NAME")
356                             && !last.is("RIGHT_PAREN")
357                             && !last.is("RIGHT_BRACKET")
358                         )
359                     )
360                 )
361             ) {
362                 var regex = stream.next();
363                 
364                 while (!stream.look().eof) {
365                     if (stream.look() == "\\") { // escape sequence
366                         regex += stream.next(2);
367                     }
368                     else if (stream.look() == "/") {
369                         regex += stream.next();
370                         
371                         while (/[gmi]/.test(stream.look())) {
372                             regex += stream.next();
373                         }
374                         
375                         tokens.push(new Token(regex, "REGX", "REGX", this.line));
376                         return true;
377                     }
378                     else {
379                         regex += stream.next();
380                     }
381                 }
382                 // error: unterminated regex
383             }
384             return false;
385         }
386 });