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                 var _this = this;
96                 n.forEach(function(nm) {
97                     if (p) {
98                         tokens.push(new Token('.', "PUNC", "DOT", _this.line));
99                     }
100                     p=true;
101                     tokens.push(new Token(nm, "NAME", "NAME", _this.line));
102                 });
103                 return true;
104                 
105             }
106         },
107
108         /**
109             @returns {Boolean} Was the token found?
110          */
111         read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
112             var found = "";
113             var name;
114             while (!stream.look().eof && Lang.punc(found+stream.look())) {
115                 found += stream.next();
116             }
117             
118             if (found === "") {
119                 return false;
120             }
121             else {
122                 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
123                 return true;
124             }
125         },
126
127         /**
128             @returns {Boolean} Was the token found?
129          */
130         read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
131             var found = "";
132             
133             while (!stream.look().eof && Lang.isSpace(stream.look())) {
134                 found += stream.next();
135             }
136             
137             if (found === "") {
138                 return false;
139             }
140             else {
141                 if (this.collapseWhite) found = " ";
142                 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
143                 return true;
144             }
145         },
146
147         /**
148             @returns {Boolean} Was the token found?
149          */
150         read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
151             var found = "";
152             
153             while (!stream.look().eof && Lang.isNewline(stream.look())) {
154                 this.line++;
155                 found += stream.next();
156             }
157             
158             if (found === "") {
159                 return false;
160             }
161             else {
162                 if (this.collapseWhite) {
163                     found = "\n";
164                 }
165                 if (this.keepWhite) {
166                     var last = tokens.pop();
167                     if (last.name != "WHIT") {
168                         tokens.push(last);
169                     }
170                     
171                     
172                     tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
173                 }
174                 return true;
175             }
176         },
177
178         /**
179             @returns {Boolean} Was the token found?
180          */
181         read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
182             if (stream.look() == "/" && stream.look(1) == "*") {
183                 var found = stream.next(2);
184                 var c = '';
185                 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
186                     c = stream.next();
187                     if (c == "\n") this.line++;
188                     found += c;
189                 }
190                 
191                 // to start doclet we allow /** or /*** but not /**/ or /****
192                 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
193                 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
194                 return true;
195             }
196             return false;
197         },
198
199         /**
200             @returns {Boolean} Was the token found?
201          */
202         read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
203             var found;
204             if (
205                 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
206                 || 
207                 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
208             ) {
209                 
210                 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
211                     found += stream.next();
212                 }
213                 
214                 if (this.keepComments) {
215                     tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
216                 }
217                 this.line++;
218                 return true;
219             }
220             return false;
221         },
222
223         /**
224             @returns {Boolean} Was the token found?
225          */
226         read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
227             if (stream.look() == "\"") {
228                 // find terminator
229                 var string = stream.next();
230                 
231                 while (!stream.look().eof) {
232                     if (stream.look() == "\\") {
233                         if (Lang.isNewline(stream.look(1))) {
234                             do {
235                                 stream.next();
236                             } while (!stream.look().eof && Lang.isNewline(stream.look()));
237                             string += "\\\n";
238                         }
239                         else {
240                             string += stream.next(2);
241                         }
242                     }
243                     else if (stream.look() == "\"") {
244                         string += stream.next();
245                         tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
246                         return true;
247                     }
248                     else {
249                         string += stream.next();
250                     }
251                 }
252             }
253             return false; // error! unterminated string
254         },
255
256         /**
257             @returns {Boolean} Was the token found?
258          */
259         read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
260             if (stream.look() == "'") {
261                 // find terminator
262                 var string = stream.next();
263                 
264                 while (!stream.look().eof) {
265                     if (stream.look() == "\\") { // escape sequence
266                         string += stream.next(2);
267                     }
268                     else if (stream.look() == "'") {
269                         string += stream.next();
270                         tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
271                         return true;
272                     }
273                     else {
274                         string += stream.next();
275                     }
276                 }
277             }
278             return false; // error! unterminated string
279         },
280
281         /**
282             @returns {Boolean} Was the token found?
283          */
284         read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
285             if (stream.look() === "0" && stream.look(1) == "x") {
286                 return this.read_hex(stream, tokens);
287             }
288             
289             var found = "";
290             
291             while (!stream.look().eof && Lang.isNumber(found+stream.look())){
292                 found += stream.next();
293             }
294             
295             if (found === "") {
296                 return false;
297             }
298             else {
299                 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
300                 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
301                 return true;
302             }
303         },
304         /*t:
305             requires("../lib/JSDOC/TextStream.js");
306             requires("../lib/JSDOC/Token.js");
307             requires("../lib/JSDOC/Lang.js");
308             
309             plan(3, "testing read_numb");
310             
311             //// setup
312             var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
313             var tr = new TokenReader();
314             var tokens = tr.tokenize(new TextStream(src));
315             
316             var hexToken, octToken, decToken;
317             for (var i = 0; i < tokens.length; i++) {
318                 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
319                 if (tokens[i].name == "OCTAL") octToken = tokens[i];
320                 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
321             }
322             ////
323             
324             is(decToken.data, "8.0", "decimal number is found in source.");
325             is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
326             is(octToken.data, "0777", "octal number is found in source.");
327         */
328
329         /**
330             @returns {Boolean} Was the token found?
331          */
332         read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
333             var found = stream.next(2);
334             
335             while (!stream.look().eof) {
336                 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
337                     tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
338                     return true;
339                 }
340                 else {
341                     found += stream.next();
342                 }
343             }
344             return false;
345         },
346
347         /**
348             @returns {Boolean} Was the token found?
349          */
350         read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
351             var last;
352             if (
353                 stream.look() == "/"
354                 && 
355                 (
356                     
357                     (
358                         !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
359                         || 
360                         (
361                                !last.is("NUMB")
362                             && !last.is("NAME")
363                             && !last.is("RIGHT_PAREN")
364                             && !last.is("RIGHT_BRACKET")
365                         )
366                     )
367                 )
368             ) {
369                 var regex = stream.next();
370                 
371                 while (!stream.look().eof) {
372                     if (stream.look() == "\\") { // escape sequence
373                         regex += stream.next(2);
374                     }
375                     else if (stream.look() == "/") {
376                         regex += stream.next();
377                         
378                         while (/[gmi]/.test(stream.look())) {
379                             regex += stream.next();
380                         }
381                         
382                         tokens.push(new Token(regex, "REGX", "REGX", this.line));
383                         return true;
384                     }
385                     else {
386                         regex += stream.next();
387                     }
388                 }
389                 // error: unterminated regex
390             }
391             return false;
392         }
393 });