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];
129 @returns {Boolean} Was the token found?
131 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
133 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
134 found += stream.next();
142 if ((name = Lang.keyword(found))) {
143 if (found == 'return' && tokens.lastSym().data == ')') {
144 //Seed.print('@' + tokens.length);
145 var n = this.findPuncToken(tokens, ')');
146 //Seed.print(')@' + n);
147 n = this.findPuncToken(tokens, '(', n-1);
148 //Seed.print('(@' + n);
150 var lt = this.lastSym(tokens, n);
151 Seed.print(JSON.stringify(lt));
152 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
154 name : "ArgumentError",
155 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
163 tokens.push(new Token(found, "KEYW", name, this.line));
166 if (!this.sepIdents || found.indexOf('.') < 0 ) {
167 tokens.push(new Token(found, "NAME", "NAME", this.line));
170 var n = found.split('.');
173 n.forEach(function(nm) {
175 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
178 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
186 @returns {Boolean} Was the token found?
188 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
191 while (!stream.look().eof && Lang.punc(found+stream.look())) {
192 found += stream.next();
200 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
201 //print("Error - comma found before " + found);
202 //print(JSON.stringify(tokens.lastSym(), null,4));
204 name : "ArgumentError",
205 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
209 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
215 @returns {Boolean} Was the token found?
217 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
220 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
221 found += stream.next();
227 //print("WHITE = " + JSON.stringify(found));
228 if (this.collapseWhite) found = " ";
229 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
235 @returns {Boolean} Was the token found?
237 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
239 var line = this.line;
240 while (!stream.look().eof && Lang.isNewline(stream.look())) {
242 found += stream.next();
249 if (this.collapseWhite) {
252 if (this.keepWhite) {
253 var last = tokens.pop();
254 if (last && last.name != "WHIT") {
258 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
264 @returns {Boolean} Was the token found?
266 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
267 if (stream.look() == "/" && stream.look(1) == "*") {
268 var found = stream.next(2);
270 var line = this.line;
271 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
273 if (c == "\n") this.line++;
277 // to start doclet we allow /** or /*** but not /**/ or /****
278 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
279 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
286 @returns {Boolean} Was the token found?
288 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
291 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
293 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
295 var line = this.line;
296 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
297 found += stream.next();
299 if (!stream.look().eof) {
300 found += stream.next();
302 if (this.keepComments) {
303 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
312 @returns {Boolean} Was the token found?
314 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
315 if (stream.look() == "\"") {
317 var string = stream.next();
319 while (!stream.look().eof) {
320 if (stream.look() == "\\") {
321 if (Lang.isNewline(stream.look(1))) {
324 } while (!stream.look().eof && Lang.isNewline(stream.look()));
328 string += stream.next(2);
331 else if (stream.look() == "\"") {
332 string += stream.next();
333 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
337 string += stream.next();
341 return false; // error! unterminated string
345 @returns {Boolean} Was the token found?
347 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
348 if (stream.look() == "'") {
350 var string = stream.next();
352 while (!stream.look().eof) {
353 if (stream.look() == "\\") { // escape sequence
354 string += stream.next(2);
356 else if (stream.look() == "'") {
357 string += stream.next();
358 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
362 string += stream.next();
366 return false; // error! unterminated string
370 @returns {Boolean} Was the token found?
372 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
373 if (stream.look() === "0" && stream.look(1) == "x") {
374 return this.read_hex(stream, tokens);
379 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
380 found += stream.next();
387 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
388 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
393 requires("../lib/JSDOC/TextStream.js");
394 requires("../lib/JSDOC/Token.js");
395 requires("../lib/JSDOC/Lang.js");
397 plan(3, "testing read_numb");
400 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
401 var tr = new TokenReader();
402 var tokens = tr.tokenize(new TextStream(src));
404 var hexToken, octToken, decToken;
405 for (var i = 0; i < tokens.length; i++) {
406 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
407 if (tokens[i].name == "OCTAL") octToken = tokens[i];
408 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
412 is(decToken.data, "8.0", "decimal number is found in source.");
413 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
414 is(octToken.data, "0777", "octal number is found in source.");
418 @returns {Boolean} Was the token found?
420 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
421 var found = stream.next(2);
423 while (!stream.look().eof) {
424 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
425 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
429 found += stream.next();
436 @returns {Boolean} Was the token found?
438 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
446 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
451 && !last.is("RIGHT_PAREN")
452 && !last.is("RIGHT_BRACKET")
457 var regex = stream.next();
459 while (!stream.look().eof) {
460 if (stream.look() == "\\") { // escape sequence
461 regex += stream.next(2);
463 else if (stream.look() == "/") {
464 regex += stream.next();
466 while (/[gmi]/.test(stream.look())) {
467 regex += stream.next();
470 tokens.push(new Token(regex, "REGX", "REGX", this.line));
474 regex += stream.next();
477 // error: unterminated regex