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();
93 while (!stream.lookEOF()) {
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)
129 n = n || tokens.length -1;
133 if (!stack && tokens.get(n).data == data) {
137 if (tokens.get(n).data == ')' || tokens.get(n).data == '}') {
142 if (stack && (tokens.get(n).data == '{' || tokens.get(n).data == '(')) {
154 * lastSym - find the last token symbol
155 * need to back check syntax..
157 * @arg {Array} tokens the array of tokens.
158 * @arg {Number} offset where to start..
159 * @return {Token} the token
161 public Token lastSym(TokenArray tokens, int n)
163 for (var i = n-1; i >= 0; i--) {
164 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
165 return tokens.get(i);
174 @returns {Boolean} Was the token found?
176 public bool read_word (TokenStream stream, TokenArray tokens) {
178 while (!stream.lookEOF() && Lang.isWordChar(stream.look())) {
179 found += stream.next();
187 if ((name = Lang.keyword(found))) {
188 if (found == 'return' && tokens.lastSym().data == ')') {
189 //Seed.print('@' + tokens.length);
190 var n = this.findPuncToken(tokens, ')');
191 //Seed.print(')@' + n);
192 n = this.findPuncToken(tokens, '(', n-1);
193 //Seed.print('(@' + n);
195 var lt = this.lastSym(tokens, n);
196 print(JSON.stringify(lt));
197 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
198 if (!this.ignoreBadGrammer) {
200 name : "ArgumentError",
201 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
210 tokens.push(new Token(found, "KEYW", name, this.line));
213 if (!this.sepIdents || found.indexOf('.') < 0 ) {
214 tokens.push(new Token(found, "NAME", "NAME", this.line));
217 var n = found.split('.');
220 n.forEach(function(nm) {
222 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
225 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
233 @returns {Boolean} Was the token found?
235 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
238 while (!stream.look().eof && Lang.punc(found+stream.look())) {
239 found += stream.next();
247 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
248 //print("Error - comma found before " + found);
249 //print(JSON.stringify(tokens.lastSym(), null,4));
250 if (this.ignoreBadGrammer) {
251 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
255 name : "ArgumentError",
256 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
261 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
267 @returns {Boolean} Was the token found?
269 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
272 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
273 found += stream.next();
279 //print("WHITE = " + JSON.stringify(found));
280 if (this.collapseWhite) found = " ";
281 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
287 @returns {Boolean} Was the token found?
289 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
291 var line = this.line;
292 while (!stream.look().eof && Lang.isNewline(stream.look())) {
294 found += stream.next();
301 if (this.collapseWhite) {
304 if (this.keepWhite) {
305 var last = tokens ? tokens.pop() : false;
306 if (last && last.name != "WHIT") {
310 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
316 @returns {Boolean} Was the token found?
318 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
319 if (stream.look() == "/" && stream.look(1) == "*") {
320 var found = stream.next(2);
322 var line = this.line;
323 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
325 if (c == "\n") this.line++;
329 // to start doclet we allow /** or /*** but not /**/ or /****
330 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
331 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
338 @returns {Boolean} Was the token found?
340 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
343 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
345 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
347 var line = this.line;
348 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
349 found += stream.next();
351 if (!stream.look().eof) {
352 found += stream.next();
354 if (this.keepComments) {
355 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
364 @returns {Boolean} Was the token found?
366 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
367 if (stream.look() == "\"") {
369 var string = stream.next();
371 while (!stream.look().eof) {
372 if (stream.look() == "\\") {
373 if (Lang.isNewline(stream.look(1))) {
376 } while (!stream.look().eof && Lang.isNewline(stream.look()));
380 string += stream.next(2);
383 else if (stream.look() == "\"") {
384 string += stream.next();
385 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
389 string += stream.next();
393 return false; // error! unterminated string
397 @returns {Boolean} Was the token found?
399 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
400 if (stream.look() == "'") {
402 var string = stream.next();
404 while (!stream.look().eof) {
405 if (stream.look() == "\\") { // escape sequence
406 string += stream.next(2);
408 else if (stream.look() == "'") {
409 string += stream.next();
410 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
414 string += stream.next();
418 return false; // error! unterminated string
422 @returns {Boolean} Was the token found?
424 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
425 if (stream.look() === "0" && stream.look(1) == "x") {
426 return this.read_hex(stream, tokens);
431 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
432 found += stream.next();
439 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
440 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
445 requires("../lib/JSDOC/TextStream.js");
446 requires("../lib/JSDOC/Token.js");
447 requires("../lib/JSDOC/Lang.js");
449 plan(3, "testing read_numb");
452 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
453 var tr = new TokenReader();
454 var tokens = tr.tokenize(new TextStream(src));
456 var hexToken, octToken, decToken;
457 for (var i = 0; i < tokens.length; i++) {
458 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
459 if (tokens[i].name == "OCTAL") octToken = tokens[i];
460 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
464 is(decToken.data, "8.0", "decimal number is found in source.");
465 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
466 is(octToken.data, "0777", "octal number is found in source.");
470 @returns {Boolean} Was the token found?
472 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
473 var found = stream.next(2);
475 while (!stream.look().eof) {
476 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
477 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
481 found += stream.next();
488 @returns {Boolean} Was the token found?
490 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
498 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
503 && !last.is("RIGHT_PAREN")
504 && !last.is("RIGHT_BRACKET")
509 var regex = stream.next();
511 while (!stream.look().eof) {
512 if (stream.look() == "\\") { // escape sequence
513 regex += stream.next(2);
515 else if (stream.look() == "/") {
516 regex += stream.next();
518 while (/[gmi]/.test(stream.look())) {
519 regex += stream.next();
522 tokens.push(new Token(regex, "REGX", "REGX", this.line));
526 regex += stream.next();
529 // error: unterminated regex