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