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