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));
207 name : "ArgumentError",
208 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
212 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
218 @returns {Boolean} Was the token found?
220 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
223 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
224 found += stream.next();
230 //print("WHITE = " + JSON.stringify(found));
231 if (this.collapseWhite) found = " ";
232 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
238 @returns {Boolean} Was the token found?
240 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
242 var line = this.line;
243 while (!stream.look().eof && Lang.isNewline(stream.look())) {
245 found += stream.next();
252 if (this.collapseWhite) {
255 if (this.keepWhite) {
256 var last = tokens.pop();
257 if (last && last.name != "WHIT") {
261 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
267 @returns {Boolean} Was the token found?
269 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
270 if (stream.look() == "/" && stream.look(1) == "*") {
271 var found = stream.next(2);
273 var line = this.line;
274 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
276 if (c == "\n") this.line++;
280 // to start doclet we allow /** or /*** but not /**/ or /****
281 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
282 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
289 @returns {Boolean} Was the token found?
291 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
294 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
296 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
298 var line = this.line;
299 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
300 found += stream.next();
302 if (!stream.look().eof) {
303 found += stream.next();
305 if (this.keepComments) {
306 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
315 @returns {Boolean} Was the token found?
317 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
318 if (stream.look() == "\"") {
320 var string = stream.next();
322 while (!stream.look().eof) {
323 if (stream.look() == "\\") {
324 if (Lang.isNewline(stream.look(1))) {
327 } while (!stream.look().eof && Lang.isNewline(stream.look()));
331 string += stream.next(2);
334 else if (stream.look() == "\"") {
335 string += stream.next();
336 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
340 string += stream.next();
344 return false; // error! unterminated string
348 @returns {Boolean} Was the token found?
350 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
351 if (stream.look() == "'") {
353 var string = stream.next();
355 while (!stream.look().eof) {
356 if (stream.look() == "\\") { // escape sequence
357 string += stream.next(2);
359 else if (stream.look() == "'") {
360 string += stream.next();
361 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
365 string += stream.next();
369 return false; // error! unterminated string
373 @returns {Boolean} Was the token found?
375 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
376 if (stream.look() === "0" && stream.look(1) == "x") {
377 return this.read_hex(stream, tokens);
382 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
383 found += stream.next();
390 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
391 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
396 requires("../lib/JSDOC/TextStream.js");
397 requires("../lib/JSDOC/Token.js");
398 requires("../lib/JSDOC/Lang.js");
400 plan(3, "testing read_numb");
403 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
404 var tr = new TokenReader();
405 var tokens = tr.tokenize(new TextStream(src));
407 var hexToken, octToken, decToken;
408 for (var i = 0; i < tokens.length; i++) {
409 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
410 if (tokens[i].name == "OCTAL") octToken = tokens[i];
411 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
415 is(decToken.data, "8.0", "decimal number is found in source.");
416 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
417 is(octToken.data, "0777", "octal number is found in source.");
421 @returns {Boolean} Was the token found?
423 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
424 var found = stream.next(2);
426 while (!stream.look().eof) {
427 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
428 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
432 found += stream.next();
439 @returns {Boolean} Was the token found?
441 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
449 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
454 && !last.is("RIGHT_PAREN")
455 && !last.is("RIGHT_BRACKET")
460 var regex = stream.next();
462 while (!stream.look().eof) {
463 if (stream.look() == "\\") { // escape sequence
464 regex += stream.next(2);
466 else if (stream.look() == "/") {
467 regex += stream.next();
469 while (/[gmi]/.test(stream.look())) {
470 regex += stream.next();
473 tokens.push(new Token(regex, "REGX", "REGX", this.line));
477 regex += stream.next();
480 // error: unterminated regex