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 public Gee.ArrayList<Token> tokens;
19 get { return this.tokens.size }
24 this.items = new Gee.ArrayList<Token>();
27 public Token? last() {
28 if (this.tokens > 0) {
29 return this.tokens[this.tokens.length-1];
33 public Token? lastSym () {
34 for (var i = this.tokens.length-1; i >= 0; i--) {
35 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
36 return this.tokens.get(i);
41 public void push (Token t) {
44 public Token get(int i) {
45 return this.tokens.get(i);
50 public class TokenReader : Object
57 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
60 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
61 public bool collapseWhite = false, // only reduces white space...
62 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
63 public bool keepDocs = true,
64 /** @cfg {Boolean} keepWhite keep White space **/
65 public bool keepWhite = false,
66 /** @cfg {Boolean} keepComments keep all comments **/
67 public bool keepComments = false,
68 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
69 public bool sepIdents = false,
70 /** @cfg {String} filename name of file being parsed. **/
71 public string filename = "";
72 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
73 public bool ignoreBadGrammer = false,
80 * @return {Array} of tokens
82 * ts = new TextStream(File.read(str));
83 * tr = TokenReader({ keepComments : true, keepWhite : true });
87 public TokenArray tokenize(TextStream stream)
90 var tokens = new TokenArray();
95 stream.look(0, out eof)
99 if (this.read_mlcomment(stream, tokens)) continue;
100 if (this.read_slcomment(stream, tokens)) continue;
101 if (this.read_dbquote(stream, tokens)) continue;
102 if (this.read_snquote(stream, tokens)) continue;
103 if (this.read_regx(stream, tokens)) continue;
104 if (this.read_numb(stream, tokens)) continue;
105 if (this.read_punc(stream, tokens)) continue;
106 if (this.read_newline(stream, tokens)) continue;
107 if (this.read_space(stream, tokens)) continue;
108 if (this.read_word(stream, tokens)) continue;
110 // if execution reaches here then an error has happened
112 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
122 * findPuncToken - find the id of a token (previous to current)
123 * need to back check syntax..
125 * @arg {Array} tokens the array of tokens.
126 * @arg {String} token data (eg. '(')
127 * @arg {Number} offset where to start reading from
128 * @return {Number} position of token
130 public int findPuncToken(TokenArray tokens, string data, int n) {
131 n = n || tokens.length -1;
135 if (!stack && tokens[n].data == data) {
139 if (tokens[n].data == ')' || tokens[n].data == '}') {
144 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
156 * lastSym - find the last token symbol
157 * need to back check syntax..
159 * @arg {Array} tokens the array of tokens.
160 * @arg {Number} offset where to start..
161 * @return {Token} the token
163 lastSym : function(tokens, n) {
164 for (var i = n-1; i >= 0; i--) {
165 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
173 @returns {Boolean} Was the token found?
175 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
177 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
178 found += stream.next();
186 if ((name = Lang.keyword(found))) {
187 if (found == 'return' && tokens.lastSym().data == ')') {
188 //Seed.print('@' + tokens.length);
189 var n = this.findPuncToken(tokens, ')');
190 //Seed.print(')@' + n);
191 n = this.findPuncToken(tokens, '(', n-1);
192 //Seed.print('(@' + n);
194 var lt = this.lastSym(tokens, n);
195 print(JSON.stringify(lt));
196 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
197 if (!this.ignoreBadGrammer) {
199 name : "ArgumentError",
200 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
209 tokens.push(new Token(found, "KEYW", name, this.line));
212 if (!this.sepIdents || found.indexOf('.') < 0 ) {
213 tokens.push(new Token(found, "NAME", "NAME", this.line));
216 var n = found.split('.');
219 n.forEach(function(nm) {
221 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
224 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
232 @returns {Boolean} Was the token found?
234 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
237 while (!stream.look().eof && Lang.punc(found+stream.look())) {
238 found += stream.next();
246 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
247 //print("Error - comma found before " + found);
248 //print(JSON.stringify(tokens.lastSym(), null,4));
249 if (this.ignoreBadGrammer) {
250 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
254 name : "ArgumentError",
255 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
260 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
266 @returns {Boolean} Was the token found?
268 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
271 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
272 found += stream.next();
278 //print("WHITE = " + JSON.stringify(found));
279 if (this.collapseWhite) found = " ";
280 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
286 @returns {Boolean} Was the token found?
288 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
290 var line = this.line;
291 while (!stream.look().eof && Lang.isNewline(stream.look())) {
293 found += stream.next();
300 if (this.collapseWhite) {
303 if (this.keepWhite) {
304 var last = tokens ? tokens.pop() : false;
305 if (last && last.name != "WHIT") {
309 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
315 @returns {Boolean} Was the token found?
317 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
318 if (stream.look() == "/" && stream.look(1) == "*") {
319 var found = stream.next(2);
321 var line = this.line;
322 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
324 if (c == "\n") this.line++;
328 // to start doclet we allow /** or /*** but not /**/ or /****
329 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
330 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
337 @returns {Boolean} Was the token found?
339 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
342 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
344 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
346 var line = this.line;
347 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
348 found += stream.next();
350 if (!stream.look().eof) {
351 found += stream.next();
353 if (this.keepComments) {
354 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
363 @returns {Boolean} Was the token found?
365 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
366 if (stream.look() == "\"") {
368 var string = stream.next();
370 while (!stream.look().eof) {
371 if (stream.look() == "\\") {
372 if (Lang.isNewline(stream.look(1))) {
375 } while (!stream.look().eof && Lang.isNewline(stream.look()));
379 string += stream.next(2);
382 else if (stream.look() == "\"") {
383 string += stream.next();
384 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
388 string += stream.next();
392 return false; // error! unterminated string
396 @returns {Boolean} Was the token found?
398 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
399 if (stream.look() == "'") {
401 var string = stream.next();
403 while (!stream.look().eof) {
404 if (stream.look() == "\\") { // escape sequence
405 string += stream.next(2);
407 else if (stream.look() == "'") {
408 string += stream.next();
409 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
413 string += stream.next();
417 return false; // error! unterminated string
421 @returns {Boolean} Was the token found?
423 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
424 if (stream.look() === "0" && stream.look(1) == "x") {
425 return this.read_hex(stream, tokens);
430 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
431 found += stream.next();
438 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
439 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
444 requires("../lib/JSDOC/TextStream.js");
445 requires("../lib/JSDOC/Token.js");
446 requires("../lib/JSDOC/Lang.js");
448 plan(3, "testing read_numb");
451 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
452 var tr = new TokenReader();
453 var tokens = tr.tokenize(new TextStream(src));
455 var hexToken, octToken, decToken;
456 for (var i = 0; i < tokens.length; i++) {
457 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
458 if (tokens[i].name == "OCTAL") octToken = tokens[i];
459 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
463 is(decToken.data, "8.0", "decimal number is found in source.");
464 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
465 is(octToken.data, "0777", "octal number is found in source.");
469 @returns {Boolean} Was the token found?
471 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
472 var found = stream.next(2);
474 while (!stream.look().eof) {
475 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
476 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
480 found += stream.next();
487 @returns {Boolean} Was the token found?
489 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
497 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
502 && !last.is("RIGHT_PAREN")
503 && !last.is("RIGHT_BRACKET")
508 var regex = stream.next();
510 while (!stream.look().eof) {
511 if (stream.look() == "\\") { // escape sequence
512 regex += stream.next(2);
514 else if (stream.look() == "/") {
515 regex += stream.next();
517 while (/[gmi]/.test(stream.look())) {
518 regex += stream.next();
521 tokens.push(new Token(regex, "REGX", "REGX", this.line));
525 regex += stream.next();
528 // error: unterminated regex