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