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. **/
34 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
35 ignoreBadGrammer : false,
38 * @return {Array} of tokens
40 * ts = new TextStream(File.read(str));
41 * tr = TokenReader({ keepComments : true, keepWhite : true });
45 tokenize : function(/**JSDOC.TextStream*/stream) {
49 tokens.last = function() { return tokens[tokens.length-1]; }
51 tokens.lastSym = function() {
52 for (var i = tokens.length-1; i >= 0; i--) {
53 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
57 while (!stream.look().eof) {
58 if (this.read_mlcomment(stream, tokens)) continue;
59 if (this.read_slcomment(stream, tokens)) continue;
60 if (this.read_dbquote(stream, tokens)) continue;
61 if (this.read_snquote(stream, tokens)) continue;
62 if (this.read_regx(stream, tokens)) continue;
63 if (this.read_numb(stream, tokens)) continue;
64 if (this.read_punc(stream, tokens)) continue;
65 if (this.read_newline(stream, tokens)) continue;
66 if (this.read_space(stream, tokens)) continue;
67 if (this.read_word(stream, tokens)) continue;
69 // if execution reaches here then an error has happened
70 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
79 * findPuncToken - find the id of a token (previous to current)
80 * need to back check syntax..
82 * @arg {Array} tokens the array of tokens.
83 * @arg {String} token data (eg. '(')
84 * @arg {Number} offset where to start reading from
85 * @return {Number} position of token
87 findPuncToken : function(tokens, data, n) {
88 n = n || tokens.length -1;
92 if (!stack && tokens[n].data == data) {
96 if (tokens[n].data == ')' || tokens[n].data == '}') {
101 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
113 * lastSym - find the last token symbol
114 * need to back check syntax..
116 * @arg {Array} tokens the array of tokens.
117 * @arg {Number} offset where to start..
118 * @return {Token} the token
120 lastSym : function(tokens, n) {
121 for (var i = n-1; i >= 0; i--) {
122 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
130 @returns {Boolean} Was the token found?
132 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
134 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
135 found += stream.next();
143 if ((name = Lang.keyword(found))) {
144 if (found == 'return' && tokens.lastSym().data == ')') {
145 //Seed.print('@' + tokens.length);
146 var n = this.findPuncToken(tokens, ')');
147 //Seed.print(')@' + n);
148 n = this.findPuncToken(tokens, '(', n-1);
149 //Seed.print('(@' + n);
151 var lt = this.lastSym(tokens, n);
152 Seed.print(JSON.stringify(lt));
153 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
154 if (!this.ignoreBadGrammer) {
156 name : "ArgumentError",
157 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
166 tokens.push(new Token(found, "KEYW", name, this.line));
169 if (!this.sepIdents || found.indexOf('.') < 0 ) {
170 tokens.push(new Token(found, "NAME", "NAME", this.line));
173 var n = found.split('.');
176 n.forEach(function(nm) {
178 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
181 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
189 @returns {Boolean} Was the token found?
191 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
194 while (!stream.look().eof && Lang.punc(found+stream.look())) {
195 found += stream.next();
203 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
204 //print("Error - comma found before " + found);
205 //print(JSON.stringify(tokens.lastSym(), null,4));
206 if (this.ignoreBadGrammer) {
207 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
211 name : "ArgumentError",
212 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
217 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
223 @returns {Boolean} Was the token found?
225 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
228 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
229 found += stream.next();
235 //print("WHITE = " + JSON.stringify(found));
236 if (this.collapseWhite) found = " ";
237 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
243 @returns {Boolean} Was the token found?
245 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
247 var line = this.line;
248 while (!stream.look().eof && Lang.isNewline(stream.look())) {
250 found += stream.next();
257 if (this.collapseWhite) {
260 if (this.keepWhite) {
261 var last = tokens.pop();
262 if (last && last.name != "WHIT") {
266 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
272 @returns {Boolean} Was the token found?
274 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
275 if (stream.look() == "/" && stream.look(1) == "*") {
276 var found = stream.next(2);
278 var line = this.line;
279 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
281 if (c == "\n") this.line++;
285 // to start doclet we allow /** or /*** but not /**/ or /****
286 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
287 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
294 @returns {Boolean} Was the token found?
296 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
299 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
301 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
303 var line = this.line;
304 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
305 found += stream.next();
307 if (!stream.look().eof) {
308 found += stream.next();
310 if (this.keepComments) {
311 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
320 @returns {Boolean} Was the token found?
322 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
323 if (stream.look() == "\"") {
325 var string = stream.next();
327 while (!stream.look().eof) {
328 if (stream.look() == "\\") {
329 if (Lang.isNewline(stream.look(1))) {
332 } while (!stream.look().eof && Lang.isNewline(stream.look()));
336 string += stream.next(2);
339 else if (stream.look() == "\"") {
340 string += stream.next();
341 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
345 string += stream.next();
349 return false; // error! unterminated string
353 @returns {Boolean} Was the token found?
355 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
356 if (stream.look() == "'") {
358 var string = stream.next();
360 while (!stream.look().eof) {
361 if (stream.look() == "\\") { // escape sequence
362 string += stream.next(2);
364 else if (stream.look() == "'") {
365 string += stream.next();
366 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
370 string += stream.next();
374 return false; // error! unterminated string
378 @returns {Boolean} Was the token found?
380 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
381 if (stream.look() === "0" && stream.look(1) == "x") {
382 return this.read_hex(stream, tokens);
387 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
388 found += stream.next();
395 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
396 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
401 requires("../lib/JSDOC/TextStream.js");
402 requires("../lib/JSDOC/Token.js");
403 requires("../lib/JSDOC/Lang.js");
405 plan(3, "testing read_numb");
408 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
409 var tr = new TokenReader();
410 var tokens = tr.tokenize(new TextStream(src));
412 var hexToken, octToken, decToken;
413 for (var i = 0; i < tokens.length; i++) {
414 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
415 if (tokens[i].name == "OCTAL") octToken = tokens[i];
416 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
420 is(decToken.data, "8.0", "decimal number is found in source.");
421 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
422 is(octToken.data, "0777", "octal number is found in source.");
426 @returns {Boolean} Was the token found?
428 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
429 var found = stream.next(2);
431 while (!stream.look().eof) {
432 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
433 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
437 found += stream.next();
444 @returns {Boolean} Was the token found?
446 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
454 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
459 && !last.is("RIGHT_PAREN")
460 && !last.is("RIGHT_BRACKET")
465 var regex = stream.next();
467 while (!stream.look().eof) {
468 if (stream.look() == "\\") { // escape sequence
469 regex += stream.next(2);
471 else if (stream.look() == "/") {
472 regex += stream.next();
474 while (/[gmi]/.test(stream.look())) {
475 regex += stream.next();
478 tokens.push(new Token(regex, "REGX", "REGX", this.line));
482 regex += stream.next();
485 // error: unterminated regex