1 //<script type="text/javascript">
6 //const Token = imports.Token.Token;
7 //const Lang = imports.Lang.Lang;
10 @class Search a {@link JSDOC.TextStream} for language tokens.
15 public class TokenArray: Object {
17 Gee.ArrayList<Token> tokens;
21 this.items = new Gee.ArrayList<Token>();
24 public Token? last() {
25 if (this.tokens > 0) {
26 return this.tokens[this.tokens.length-1];
30 public Token? lastSym = function() {
31 for (var i = this.tokens.length-1; i >= 0; i--) {
32 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
33 return this.tokens.get(i);
41 public class TokenReader : Object
48 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
51 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
52 public bool collapseWhite = false, // only reduces white space...
53 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
54 public bool keepDocs = true,
55 /** @cfg {Boolean} keepWhite keep White space **/
56 public bool keepWhite = false,
57 /** @cfg {Boolean} keepComments keep all comments **/
58 public bool keepComments = false,
59 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
60 public bool sepIdents = false,
61 /** @cfg {String} filename name of file being parsed. **/
62 public string filename = "";
63 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
64 public bool ignoreBadGrammer = false,
71 * @return {Array} of tokens
73 * ts = new TextStream(File.read(str));
74 * tr = TokenReader({ keepComments : true, keepWhite : true });
78 public TokenArray tokenize(TextStream stream)
81 var tokens = new TokenArray();
84 while (!stream.look().eof) {
85 if (this.read_mlcomment(stream, tokens)) continue;
86 if (this.read_slcomment(stream, tokens)) continue;
87 if (this.read_dbquote(stream, tokens)) continue;
88 if (this.read_snquote(stream, tokens)) continue;
89 if (this.read_regx(stream, tokens)) continue;
90 if (this.read_numb(stream, tokens)) continue;
91 if (this.read_punc(stream, tokens)) continue;
92 if (this.read_newline(stream, tokens)) continue;
93 if (this.read_space(stream, tokens)) continue;
94 if (this.read_word(stream, tokens)) continue;
96 // if execution reaches here then an error has happened
97 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
106 * findPuncToken - find the id of a token (previous to current)
107 * need to back check syntax..
109 * @arg {Array} tokens the array of tokens.
110 * @arg {String} token data (eg. '(')
111 * @arg {Number} offset where to start reading from
112 * @return {Number} position of token
114 findPuncToken : function(tokens, data, n) {
115 n = n || tokens.length -1;
119 if (!stack && tokens[n].data == data) {
123 if (tokens[n].data == ')' || tokens[n].data == '}') {
128 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
140 * lastSym - find the last token symbol
141 * need to back check syntax..
143 * @arg {Array} tokens the array of tokens.
144 * @arg {Number} offset where to start..
145 * @return {Token} the token
147 lastSym : function(tokens, n) {
148 for (var i = n-1; i >= 0; i--) {
149 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
157 @returns {Boolean} Was the token found?
159 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
161 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
162 found += stream.next();
170 if ((name = Lang.keyword(found))) {
171 if (found == 'return' && tokens.lastSym().data == ')') {
172 //Seed.print('@' + tokens.length);
173 var n = this.findPuncToken(tokens, ')');
174 //Seed.print(')@' + n);
175 n = this.findPuncToken(tokens, '(', n-1);
176 //Seed.print('(@' + n);
178 var lt = this.lastSym(tokens, n);
179 print(JSON.stringify(lt));
180 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
181 if (!this.ignoreBadGrammer) {
183 name : "ArgumentError",
184 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
193 tokens.push(new Token(found, "KEYW", name, this.line));
196 if (!this.sepIdents || found.indexOf('.') < 0 ) {
197 tokens.push(new Token(found, "NAME", "NAME", this.line));
200 var n = found.split('.');
203 n.forEach(function(nm) {
205 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
208 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
216 @returns {Boolean} Was the token found?
218 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
221 while (!stream.look().eof && Lang.punc(found+stream.look())) {
222 found += stream.next();
230 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
231 //print("Error - comma found before " + found);
232 //print(JSON.stringify(tokens.lastSym(), null,4));
233 if (this.ignoreBadGrammer) {
234 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
238 name : "ArgumentError",
239 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
244 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
250 @returns {Boolean} Was the token found?
252 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
255 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
256 found += stream.next();
262 //print("WHITE = " + JSON.stringify(found));
263 if (this.collapseWhite) found = " ";
264 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
270 @returns {Boolean} Was the token found?
272 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
274 var line = this.line;
275 while (!stream.look().eof && Lang.isNewline(stream.look())) {
277 found += stream.next();
284 if (this.collapseWhite) {
287 if (this.keepWhite) {
288 var last = tokens ? tokens.pop() : false;
289 if (last && last.name != "WHIT") {
293 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
299 @returns {Boolean} Was the token found?
301 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
302 if (stream.look() == "/" && stream.look(1) == "*") {
303 var found = stream.next(2);
305 var line = this.line;
306 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
308 if (c == "\n") this.line++;
312 // to start doclet we allow /** or /*** but not /**/ or /****
313 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
314 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
321 @returns {Boolean} Was the token found?
323 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
326 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
328 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
330 var line = this.line;
331 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
332 found += stream.next();
334 if (!stream.look().eof) {
335 found += stream.next();
337 if (this.keepComments) {
338 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
347 @returns {Boolean} Was the token found?
349 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
350 if (stream.look() == "\"") {
352 var string = stream.next();
354 while (!stream.look().eof) {
355 if (stream.look() == "\\") {
356 if (Lang.isNewline(stream.look(1))) {
359 } while (!stream.look().eof && Lang.isNewline(stream.look()));
363 string += stream.next(2);
366 else if (stream.look() == "\"") {
367 string += stream.next();
368 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
372 string += stream.next();
376 return false; // error! unterminated string
380 @returns {Boolean} Was the token found?
382 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
383 if (stream.look() == "'") {
385 var string = stream.next();
387 while (!stream.look().eof) {
388 if (stream.look() == "\\") { // escape sequence
389 string += stream.next(2);
391 else if (stream.look() == "'") {
392 string += stream.next();
393 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
397 string += stream.next();
401 return false; // error! unterminated string
405 @returns {Boolean} Was the token found?
407 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
408 if (stream.look() === "0" && stream.look(1) == "x") {
409 return this.read_hex(stream, tokens);
414 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
415 found += stream.next();
422 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
423 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
428 requires("../lib/JSDOC/TextStream.js");
429 requires("../lib/JSDOC/Token.js");
430 requires("../lib/JSDOC/Lang.js");
432 plan(3, "testing read_numb");
435 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
436 var tr = new TokenReader();
437 var tokens = tr.tokenize(new TextStream(src));
439 var hexToken, octToken, decToken;
440 for (var i = 0; i < tokens.length; i++) {
441 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
442 if (tokens[i].name == "OCTAL") octToken = tokens[i];
443 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
447 is(decToken.data, "8.0", "decimal number is found in source.");
448 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
449 is(octToken.data, "0777", "octal number is found in source.");
453 @returns {Boolean} Was the token found?
455 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
456 var found = stream.next(2);
458 while (!stream.look().eof) {
459 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
460 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
464 found += stream.next();
471 @returns {Boolean} Was the token found?
473 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
481 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
486 && !last.is("RIGHT_PAREN")
487 && !last.is("RIGHT_BRACKET")
492 var regex = stream.next();
494 while (!stream.look().eof) {
495 if (stream.look() == "\\") { // escape sequence
496 regex += stream.next(2);
498 else if (stream.look() == "/") {
499 regex += stream.next();
501 while (/[gmi]/.test(stream.look())) {
502 regex += stream.next();
505 tokens.push(new Token(regex, "REGX", "REGX", this.line));
509 regex += stream.next();
512 // error: unterminated regex