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) {
153 if (!this.ignoreBadGrammer) {
155 name : "ArgumentError",
156 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
165 tokens.push(new Token(found, "KEYW", name, this.line));
168 if (!this.sepIdents || found.indexOf('.') < 0 ) {
169 tokens.push(new Token(found, "NAME", "NAME", this.line));
172 var n = found.split('.');
175 n.forEach(function(nm) {
177 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
180 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
188 @returns {Boolean} Was the token found?
190 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
193 while (!stream.look().eof && Lang.punc(found+stream.look())) {
194 found += stream.next();
202 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
203 //print("Error - comma found before " + found);
204 //print(JSON.stringify(tokens.lastSym(), null,4));
206 name : "ArgumentError",
207 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
211 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
217 @returns {Boolean} Was the token found?
219 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
222 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
223 found += stream.next();
229 //print("WHITE = " + JSON.stringify(found));
230 if (this.collapseWhite) found = " ";
231 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
237 @returns {Boolean} Was the token found?
239 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
241 var line = this.line;
242 while (!stream.look().eof && Lang.isNewline(stream.look())) {
244 found += stream.next();
251 if (this.collapseWhite) {
254 if (this.keepWhite) {
255 var last = tokens.pop();
256 if (last && last.name != "WHIT") {
260 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
266 @returns {Boolean} Was the token found?
268 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
269 if (stream.look() == "/" && stream.look(1) == "*") {
270 var found = stream.next(2);
272 var line = this.line;
273 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
275 if (c == "\n") this.line++;
279 // to start doclet we allow /** or /*** but not /**/ or /****
280 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
281 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
288 @returns {Boolean} Was the token found?
290 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
293 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
295 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
297 var line = this.line;
298 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
299 found += stream.next();
301 if (!stream.look().eof) {
302 found += stream.next();
304 if (this.keepComments) {
305 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
314 @returns {Boolean} Was the token found?
316 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
317 if (stream.look() == "\"") {
319 var string = stream.next();
321 while (!stream.look().eof) {
322 if (stream.look() == "\\") {
323 if (Lang.isNewline(stream.look(1))) {
326 } while (!stream.look().eof && Lang.isNewline(stream.look()));
330 string += stream.next(2);
333 else if (stream.look() == "\"") {
334 string += stream.next();
335 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
339 string += stream.next();
343 return false; // error! unterminated string
347 @returns {Boolean} Was the token found?
349 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
350 if (stream.look() == "'") {
352 var string = stream.next();
354 while (!stream.look().eof) {
355 if (stream.look() == "\\") { // escape sequence
356 string += stream.next(2);
358 else if (stream.look() == "'") {
359 string += stream.next();
360 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
364 string += stream.next();
368 return false; // error! unterminated string
372 @returns {Boolean} Was the token found?
374 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
375 if (stream.look() === "0" && stream.look(1) == "x") {
376 return this.read_hex(stream, tokens);
381 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
382 found += stream.next();
389 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
390 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
395 requires("../lib/JSDOC/TextStream.js");
396 requires("../lib/JSDOC/Token.js");
397 requires("../lib/JSDOC/Lang.js");
399 plan(3, "testing read_numb");
402 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
403 var tr = new TokenReader();
404 var tokens = tr.tokenize(new TextStream(src));
406 var hexToken, octToken, decToken;
407 for (var i = 0; i < tokens.length; i++) {
408 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
409 if (tokens[i].name == "OCTAL") octToken = tokens[i];
410 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
414 is(decToken.data, "8.0", "decimal number is found in source.");
415 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
416 is(octToken.data, "0777", "octal number is found in source.");
420 @returns {Boolean} Was the token found?
422 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
423 var found = stream.next(2);
425 while (!stream.look().eof) {
426 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
427 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
431 found += stream.next();
438 @returns {Boolean} Was the token found?
440 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
448 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
453 && !last.is("RIGHT_PAREN")
454 && !last.is("RIGHT_BRACKET")
459 var regex = stream.next();
461 while (!stream.look().eof) {
462 if (stream.look() == "\\") { // escape sequence
463 regex += stream.next(2);
465 else if (stream.look() == "/") {
466 regex += stream.next();
468 while (/[gmi]/.test(stream.look())) {
469 regex += stream.next();
472 tokens.push(new Token(regex, "REGX", "REGX", this.line));
476 regex += stream.next();
479 // error: unterminated regex