1 //<script type="text/javascript">
4 const XObject = imports.XObject.XObject;
5 const console = imports.console.console;
8 const Token = imports.Token.Token;
9 const Lang = imports.Lang.Lang;
12 @class Search a {@link JSDOC.TextStream} for language tokens.
14 const 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];
58 while (!stream.look().eof) {
59 if (this.read_mlcomment(stream, tokens)) continue;
60 if (this.read_slcomment(stream, tokens)) continue;
61 if (this.read_dbquote(stream, tokens)) continue;
62 if (this.read_snquote(stream, tokens)) continue;
63 if (this.read_regx(stream, tokens)) continue;
64 if (this.read_numb(stream, tokens)) continue;
65 if (this.read_punc(stream, tokens)) continue;
66 if (this.read_newline(stream, tokens)) continue;
67 if (this.read_space(stream, tokens)) continue;
68 if (this.read_word(stream, tokens)) continue;
70 // if execution reaches here then an error has happened
71 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
80 * findPuncToken - find the id of a token (previous to current)
81 * need to back check syntax..
83 * @arg {Array} tokens the array of tokens.
84 * @arg {String} token data (eg. '(')
85 * @arg {Number} offset where to start reading from
86 * @return {Number} position of token
88 findPuncToken : function(tokens, data, n) {
89 n = n || tokens.length -1;
93 if (!stack && tokens[n].data == data) {
97 if (tokens[n].data == ')' || tokens[n].data == '}') {
102 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
114 * lastSym - find the last token symbol
115 * need to back check syntax..
117 * @arg {Array} tokens the array of tokens.
118 * @arg {Number} offset where to start..
119 * @return {Token} the token
121 lastSym : function(tokens, n) {
122 for (var i = n-1; i >= 0; i--) {
123 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
131 @returns {Boolean} Was the token found?
133 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
135 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
136 found += stream.next();
144 if ((name = Lang.keyword(found))) {
145 if (found == 'return' && tokens.lastSym().data == ')') {
146 //Seed.print('@' + tokens.length);
147 var n = this.findPuncToken(tokens, ')');
148 //Seed.print(')@' + n);
149 n = this.findPuncToken(tokens, '(', n-1);
150 //Seed.print('(@' + n);
152 var lt = this.lastSym(tokens, n);
153 print(JSON.stringify(lt));
154 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
155 if (!this.ignoreBadGrammer) {
157 name : "ArgumentError",
158 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
167 tokens.push(new Token(found, "KEYW", name, this.line));
170 if (!this.sepIdents || found.indexOf('.') < 0 ) {
171 tokens.push(new Token(found, "NAME", "NAME", this.line));
174 var n = found.split('.');
177 n.forEach(function(nm) {
179 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
182 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
190 @returns {Boolean} Was the token found?
192 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
195 while (!stream.look().eof && Lang.punc(found+stream.look())) {
196 found += stream.next();
204 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
205 //print("Error - comma found before " + found);
206 //print(JSON.stringify(tokens.lastSym(), null,4));
207 if (this.ignoreBadGrammer) {
208 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
212 name : "ArgumentError",
213 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
218 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
224 @returns {Boolean} Was the token found?
226 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
229 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
230 found += stream.next();
236 //print("WHITE = " + JSON.stringify(found));
237 if (this.collapseWhite) found = " ";
238 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
244 @returns {Boolean} Was the token found?
246 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
248 var line = this.line;
249 while (!stream.look().eof && Lang.isNewline(stream.look())) {
251 found += stream.next();
258 if (this.collapseWhite) {
261 if (this.keepWhite) {
262 var last = tokens ? tokens.pop() : false;
263 if (last && last.name != "WHIT") {
267 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
273 @returns {Boolean} Was the token found?
275 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
276 if (stream.look() == "/" && stream.look(1) == "*") {
277 var found = stream.next(2);
279 var line = this.line;
280 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
282 if (c == "\n") this.line++;
286 // to start doclet we allow /** or /*** but not /**/ or /****
287 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
288 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
295 @returns {Boolean} Was the token found?
297 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
300 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
302 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
304 var line = this.line;
305 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
306 found += stream.next();
308 if (!stream.look().eof) {
309 found += stream.next();
311 if (this.keepComments) {
312 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
321 @returns {Boolean} Was the token found?
323 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
324 if (stream.look() == "\"") {
326 var string = stream.next();
328 while (!stream.look().eof) {
329 if (stream.look() == "\\") {
330 if (Lang.isNewline(stream.look(1))) {
333 } while (!stream.look().eof && Lang.isNewline(stream.look()));
337 string += stream.next(2);
340 else if (stream.look() == "\"") {
341 string += stream.next();
342 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
346 string += stream.next();
350 return false; // error! unterminated string
354 @returns {Boolean} Was the token found?
356 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
357 if (stream.look() == "'") {
359 var string = stream.next();
361 while (!stream.look().eof) {
362 if (stream.look() == "\\") { // escape sequence
363 string += stream.next(2);
365 else if (stream.look() == "'") {
366 string += stream.next();
367 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
371 string += stream.next();
375 return false; // error! unterminated string
379 @returns {Boolean} Was the token found?
381 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
382 if (stream.look() === "0" && stream.look(1) == "x") {
383 return this.read_hex(stream, tokens);
388 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
389 found += stream.next();
396 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
397 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
402 requires("../lib/JSDOC/TextStream.js");
403 requires("../lib/JSDOC/Token.js");
404 requires("../lib/JSDOC/Lang.js");
406 plan(3, "testing read_numb");
409 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
410 var tr = new TokenReader();
411 var tokens = tr.tokenize(new TextStream(src));
413 var hexToken, octToken, decToken;
414 for (var i = 0; i < tokens.length; i++) {
415 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
416 if (tokens[i].name == "OCTAL") octToken = tokens[i];
417 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
421 is(decToken.data, "8.0", "decimal number is found in source.");
422 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
423 is(octToken.data, "0777", "octal number is found in source.");
427 @returns {Boolean} Was the token found?
429 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
430 var found = stream.next(2);
432 while (!stream.look().eof) {
433 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
434 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
438 found += stream.next();
445 @returns {Boolean} Was the token found?
447 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
455 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
460 && !last.is("RIGHT_PAREN")
461 && !last.is("RIGHT_BRACKET")
466 var regex = stream.next();
468 while (!stream.look().eof) {
469 if (stream.look() == "\\") { // escape sequence
470 regex += stream.next(2);
472 else if (stream.look() == "/") {
473 regex += stream.next();
475 while (/[gmi]/.test(stream.look())) {
476 regex += stream.next();
479 tokens.push(new Token(regex, "REGX", "REGX", this.line));
483 regex += stream.next();
486 // error: unterminated regex