1 //<script type="text/javascript">
4 XObject = imports.XObject.XObject;
5 console = imports.console.console;
8 Token = imports.Token.Token;
9 Lang = imports.Lang.Lang;
12 @class Search a {@link JSDOC.TextStream} for language tokens.
14 TokenReader = XObject.define(
17 XObject.extend(this, o || {});
22 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
23 collapseWhite : false, // only reduces white space...
24 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
26 /** @cfg {Boolean} keepWhite keep White space **/
28 /** @cfg {Boolean} keepComments keep all comments **/
30 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
32 /** @cfg {String} filename name of file being parsed. **/
37 * @return {Array} of tokens
39 * ts = new TextStream(File.read(str));
40 * tr = TokenReader({ keepComments : true, keepWhite : true });
44 tokenize : function(/**JSDOC.TextStream*/stream) {
48 tokens.last = function() { return tokens[tokens.length-1]; }
50 tokens.lastSym = function() {
51 for (var i = tokens.length-1; i >= 0; i--) {
52 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
56 while (!stream.look().eof) {
57 if (this.read_mlcomment(stream, tokens)) continue;
58 if (this.read_slcomment(stream, tokens)) continue;
59 if (this.read_dbquote(stream, tokens)) continue;
60 if (this.read_snquote(stream, tokens)) continue;
61 if (this.read_regx(stream, tokens)) continue;
62 if (this.read_numb(stream, tokens)) continue;
63 if (this.read_punc(stream, tokens)) continue;
64 if (this.read_newline(stream, tokens)) continue;
65 if (this.read_space(stream, tokens)) continue;
66 if (this.read_word(stream, tokens)) continue;
68 // if execution reaches here then an error has happened
69 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
78 * findPuncToken - find the id of a token (previous to current)
79 * need to back check syntax..
81 * @arg {Array} tokens the array of tokens.
82 * @arg {String} token data (eg. '(')
83 * @arg {Number} offset where to start reading from
84 * @return {Number} position of token
86 findPuncToken : function(tokens, data, n) {
87 n = n || tokens.length -1;
91 if (!stack && tokens[n].data == data) {
95 if (tokens[n].data == ')' || tokens[n].data == '}') {
100 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
112 * lastSym - find the last token symbol
113 * need to back check syntax..
115 * @arg {Array} tokens the array of tokens.
116 * @arg {Number} offset where to start..
117 * @return {Token} the token
119 lastSym : function(tokens, n) {
120 for (var i = n-1; i >= 0; i--) {
121 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
128 @returns {Boolean} Was the token found?
130 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
132 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
133 found += stream.next();
141 if ((name = Lang.keyword(found))) {
142 if (found == 'return' && tokens.lastSym().data == ')') {
143 //Seed.print('@' + tokens.length);
144 var n = this.findPuncToken(tokens, ')');
145 //Seed.print(')@' + n);
146 n = this.findPuncToken(tokens, '(', n-1);
147 //Seed.print('(@' + n);
149 var lt = this.lastSym(tokens, n);
150 Seed.print(JSON.stringify(lt));
151 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
153 name : "ArgumentError",
154 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
162 tokens.push(new Token(found, "KEYW", name, this.line));
165 if (!this.sepIdents || found.indexOf('.') < 0 ) {
166 tokens.push(new Token(found, "NAME", "NAME", this.line));
169 var n = found.split('.');
172 n.forEach(function(nm) {
174 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
177 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
185 @returns {Boolean} Was the token found?
187 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
190 while (!stream.look().eof && Lang.punc(found+stream.look())) {
191 found += stream.next();
199 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
200 //print("Error - comma found before " + found);
201 //print(JSON.stringify(tokens.lastSym(), null,4));
203 name : "ArgumentError",
204 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
208 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
214 @returns {Boolean} Was the token found?
216 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
219 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
220 found += stream.next();
226 //print("WHITE = " + JSON.stringify(found));
227 if (this.collapseWhite) found = " ";
228 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
234 @returns {Boolean} Was the token found?
236 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
238 var line = this.line;
239 while (!stream.look().eof && Lang.isNewline(stream.look())) {
241 found += stream.next();
248 if (this.collapseWhite) {
251 if (this.keepWhite) {
252 var last = tokens.pop();
253 if (last && last.name != "WHIT") {
257 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
263 @returns {Boolean} Was the token found?
265 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
266 if (stream.look() == "/" && stream.look(1) == "*") {
267 var found = stream.next(2);
269 var line = this.line;
270 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
272 if (c == "\n") this.line++;
276 // to start doclet we allow /** or /*** but not /**/ or /****
277 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
278 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
285 @returns {Boolean} Was the token found?
287 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
290 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
292 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
294 var line = this.line;
295 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
296 found += stream.next();
298 if (!stream.look().eof) {
299 found += stream.next();
301 if (this.keepComments) {
302 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
311 @returns {Boolean} Was the token found?
313 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
314 if (stream.look() == "\"") {
316 var string = stream.next();
318 while (!stream.look().eof) {
319 if (stream.look() == "\\") {
320 if (Lang.isNewline(stream.look(1))) {
323 } while (!stream.look().eof && Lang.isNewline(stream.look()));
327 string += stream.next(2);
330 else if (stream.look() == "\"") {
331 string += stream.next();
332 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
336 string += stream.next();
340 return false; // error! unterminated string
344 @returns {Boolean} Was the token found?
346 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
347 if (stream.look() == "'") {
349 var string = stream.next();
351 while (!stream.look().eof) {
352 if (stream.look() == "\\") { // escape sequence
353 string += stream.next(2);
355 else if (stream.look() == "'") {
356 string += stream.next();
357 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
361 string += stream.next();
365 return false; // error! unterminated string
369 @returns {Boolean} Was the token found?
371 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
372 if (stream.look() === "0" && stream.look(1) == "x") {
373 return this.read_hex(stream, tokens);
378 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
379 found += stream.next();
386 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
387 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
392 requires("../lib/JSDOC/TextStream.js");
393 requires("../lib/JSDOC/Token.js");
394 requires("../lib/JSDOC/Lang.js");
396 plan(3, "testing read_numb");
399 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
400 var tr = new TokenReader();
401 var tokens = tr.tokenize(new TextStream(src));
403 var hexToken, octToken, decToken;
404 for (var i = 0; i < tokens.length; i++) {
405 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
406 if (tokens[i].name == "OCTAL") octToken = tokens[i];
407 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
411 is(decToken.data, "8.0", "decimal number is found in source.");
412 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
413 is(octToken.data, "0777", "octal number is found in source.");
417 @returns {Boolean} Was the token found?
419 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
420 var found = stream.next(2);
422 while (!stream.look().eof) {
423 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
424 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
428 found += stream.next();
435 @returns {Boolean} Was the token found?
437 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
445 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
450 && !last.is("RIGHT_PAREN")
451 && !last.is("RIGHT_BRACKET")
456 var regex = stream.next();
458 while (!stream.look().eof) {
459 if (stream.look() == "\\") { // escape sequence
460 regex += stream.next(2);
462 else if (stream.look() == "/") {
463 regex += stream.next();
465 while (/[gmi]/.test(stream.look())) {
466 regex += stream.next();
469 tokens.push(new Token(regex, "REGX", "REGX", this.line));
473 regex += stream.next();
476 // error: unterminated regex