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) {
47 public class TokenReader : Object
54 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
57 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
58 public bool collapseWhite = false, // only reduces white space...
59 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
60 public bool keepDocs = true,
61 /** @cfg {Boolean} keepWhite keep White space **/
62 public bool keepWhite = false,
63 /** @cfg {Boolean} keepComments keep all comments **/
64 public bool keepComments = false,
65 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
66 public bool sepIdents = false,
67 /** @cfg {String} filename name of file being parsed. **/
68 public string filename = "";
69 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
70 public bool ignoreBadGrammer = false,
77 * @return {Array} of tokens
79 * ts = new TextStream(File.read(str));
80 * tr = TokenReader({ keepComments : true, keepWhite : true });
84 public TokenArray tokenize(TextStream stream)
87 var tokens = new TokenArray();
92 stream.look(0, out eof)
96 if (this.read_mlcomment(stream, tokens)) continue;
97 if (this.read_slcomment(stream, tokens)) continue;
98 if (this.read_dbquote(stream, tokens)) continue;
99 if (this.read_snquote(stream, tokens)) continue;
100 if (this.read_regx(stream, tokens)) continue;
101 if (this.read_numb(stream, tokens)) continue;
102 if (this.read_punc(stream, tokens)) continue;
103 if (this.read_newline(stream, tokens)) continue;
104 if (this.read_space(stream, tokens)) continue;
105 if (this.read_word(stream, tokens)) continue;
107 // if execution reaches here then an error has happened
109 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
119 * findPuncToken - find the id of a token (previous to current)
120 * need to back check syntax..
122 * @arg {Array} tokens the array of tokens.
123 * @arg {String} token data (eg. '(')
124 * @arg {Number} offset where to start reading from
125 * @return {Number} position of token
127 public int findPuncToken(TokenArray tokens, string data, int n) {
128 n = n || tokens.length -1;
132 if (!stack && tokens[n].data == data) {
136 if (tokens[n].data == ')' || tokens[n].data == '}') {
141 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
153 * lastSym - find the last token symbol
154 * need to back check syntax..
156 * @arg {Array} tokens the array of tokens.
157 * @arg {Number} offset where to start..
158 * @return {Token} the token
160 lastSym : function(tokens, n) {
161 for (var i = n-1; i >= 0; i--) {
162 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
170 @returns {Boolean} Was the token found?
172 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
174 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
175 found += stream.next();
183 if ((name = Lang.keyword(found))) {
184 if (found == 'return' && tokens.lastSym().data == ')') {
185 //Seed.print('@' + tokens.length);
186 var n = this.findPuncToken(tokens, ')');
187 //Seed.print(')@' + n);
188 n = this.findPuncToken(tokens, '(', n-1);
189 //Seed.print('(@' + n);
191 var lt = this.lastSym(tokens, n);
192 print(JSON.stringify(lt));
193 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
194 if (!this.ignoreBadGrammer) {
196 name : "ArgumentError",
197 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
206 tokens.push(new Token(found, "KEYW", name, this.line));
209 if (!this.sepIdents || found.indexOf('.') < 0 ) {
210 tokens.push(new Token(found, "NAME", "NAME", this.line));
213 var n = found.split('.');
216 n.forEach(function(nm) {
218 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
221 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
229 @returns {Boolean} Was the token found?
231 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
234 while (!stream.look().eof && Lang.punc(found+stream.look())) {
235 found += stream.next();
243 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
244 //print("Error - comma found before " + found);
245 //print(JSON.stringify(tokens.lastSym(), null,4));
246 if (this.ignoreBadGrammer) {
247 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
251 name : "ArgumentError",
252 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
257 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
263 @returns {Boolean} Was the token found?
265 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
268 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
269 found += stream.next();
275 //print("WHITE = " + JSON.stringify(found));
276 if (this.collapseWhite) found = " ";
277 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
283 @returns {Boolean} Was the token found?
285 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
287 var line = this.line;
288 while (!stream.look().eof && Lang.isNewline(stream.look())) {
290 found += stream.next();
297 if (this.collapseWhite) {
300 if (this.keepWhite) {
301 var last = tokens ? tokens.pop() : false;
302 if (last && last.name != "WHIT") {
306 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
312 @returns {Boolean} Was the token found?
314 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
315 if (stream.look() == "/" && stream.look(1) == "*") {
316 var found = stream.next(2);
318 var line = this.line;
319 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
321 if (c == "\n") this.line++;
325 // to start doclet we allow /** or /*** but not /**/ or /****
326 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
327 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
334 @returns {Boolean} Was the token found?
336 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
339 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
341 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
343 var line = this.line;
344 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
345 found += stream.next();
347 if (!stream.look().eof) {
348 found += stream.next();
350 if (this.keepComments) {
351 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
360 @returns {Boolean} Was the token found?
362 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
363 if (stream.look() == "\"") {
365 var string = stream.next();
367 while (!stream.look().eof) {
368 if (stream.look() == "\\") {
369 if (Lang.isNewline(stream.look(1))) {
372 } while (!stream.look().eof && Lang.isNewline(stream.look()));
376 string += stream.next(2);
379 else if (stream.look() == "\"") {
380 string += stream.next();
381 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
385 string += stream.next();
389 return false; // error! unterminated string
393 @returns {Boolean} Was the token found?
395 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
396 if (stream.look() == "'") {
398 var string = stream.next();
400 while (!stream.look().eof) {
401 if (stream.look() == "\\") { // escape sequence
402 string += stream.next(2);
404 else if (stream.look() == "'") {
405 string += stream.next();
406 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
410 string += stream.next();
414 return false; // error! unterminated string
418 @returns {Boolean} Was the token found?
420 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
421 if (stream.look() === "0" && stream.look(1) == "x") {
422 return this.read_hex(stream, tokens);
427 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
428 found += stream.next();
435 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
436 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
441 requires("../lib/JSDOC/TextStream.js");
442 requires("../lib/JSDOC/Token.js");
443 requires("../lib/JSDOC/Lang.js");
445 plan(3, "testing read_numb");
448 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
449 var tr = new TokenReader();
450 var tokens = tr.tokenize(new TextStream(src));
452 var hexToken, octToken, decToken;
453 for (var i = 0; i < tokens.length; i++) {
454 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
455 if (tokens[i].name == "OCTAL") octToken = tokens[i];
456 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
460 is(decToken.data, "8.0", "decimal number is found in source.");
461 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
462 is(octToken.data, "0777", "octal number is found in source.");
466 @returns {Boolean} Was the token found?
468 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
469 var found = stream.next(2);
471 while (!stream.look().eof) {
472 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
473 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
477 found += stream.next();
484 @returns {Boolean} Was the token found?
486 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
494 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
499 && !last.is("RIGHT_PAREN")
500 && !last.is("RIGHT_BRACKET")
505 var regex = stream.next();
507 while (!stream.look().eof) {
508 if (stream.look() == "\\") { // escape sequence
509 regex += stream.next(2);
511 else if (stream.look() == "/") {
512 regex += stream.next();
514 while (/[gmi]/.test(stream.look())) {
515 regex += stream.next();
518 tokens.push(new Token(regex, "REGX", "REGX", this.line));
522 regex += stream.next();
525 // error: unterminated regex