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 return this.tokens.remove_at(this.size-1);
52 public Token get(int i) {
53 return this.tokens.get(i);
57 errordomain TokenReader_Error {
62 public class TokenReader : Object
69 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
72 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
73 public bool collapseWhite = false, // only reduces white space...
74 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
75 public bool keepDocs = true,
76 /** @cfg {Boolean} keepWhite keep White space **/
77 public bool keepWhite = false,
78 /** @cfg {Boolean} keepComments keep all comments **/
79 public bool keepComments = false,
80 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
81 public bool sepIdents = false,
82 /** @cfg {String} filename name of file being parsed. **/
83 public string filename = "";
84 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
85 public bool ignoreBadGrammer = false,
92 * @return {Array} of tokens
94 * ts = new TextStream(File.read(str));
95 * tr = TokenReader({ keepComments : true, keepWhite : true });
99 public TokenArray tokenize(TextStream stream)
102 var tokens = new TokenArray();
105 while (!stream.lookEOF()) {
108 if (this.read_mlcomment(stream, tokens)) continue;
109 if (this.read_slcomment(stream, tokens)) continue;
110 if (this.read_dbquote(stream, tokens)) continue;
111 if (this.read_snquote(stream, tokens)) continue;
112 if (this.read_regx(stream, tokens)) continue;
113 if (this.read_numb(stream, tokens)) continue;
114 if (this.read_punc(stream, tokens)) continue;
115 if (this.read_newline(stream, tokens)) continue;
116 if (this.read_space(stream, tokens)) continue;
117 if (this.read_word(stream, tokens)) continue;
119 // if execution reaches here then an error has happened
121 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
131 * findPuncToken - find the id of a token (previous to current)
132 * need to back check syntax..
134 * @arg {Array} tokens the array of tokens.
135 * @arg {String} token data (eg. '(')
136 * @arg {Number} offset where to start reading from
137 * @return {Number} position of token
139 public int findPuncToken(TokenArray tokens, string data, int n)
141 n = n || tokens.length -1;
145 if (!stack && tokens.get(n).data == data) {
149 if (tokens.get(n).data == ')' || tokens.get(n).data == '}') {
154 if (stack && (tokens.get(n).data == '{' || tokens.get(n).data == '(')) {
166 * lastSym - find the last token symbol
167 * need to back check syntax..
169 * @arg {Array} tokens the array of tokens.
170 * @arg {Number} offset where to start..
171 * @return {Token} the token
173 public Token lastSym(TokenArray tokens, int n)
175 for (var i = n-1; i >= 0; i--) {
176 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
177 return tokens.get(i);
186 @returns {Boolean} Was the token found?
188 public bool read_word (TokenStream stream, TokenArray tokens)
191 while (!stream.lookEOF() && Lang.isWordChar(stream.look())) {
192 found += stream.next();
199 var name = Lang.keyword(found);
202 // look for "()return" ?? why ???
203 var ls = tokens.lastSym();
204 if (found == "return" && ls != null && ls.data == ")") {
205 //Seed.print('@' + tokens.length);
206 var n = this.findPuncToken(tokens, ")");
207 //Seed.print(')@' + n);
208 n = this.findPuncToken(tokens, "(", n-1);
209 //Seed.print('(@' + n);
211 var lt = this.lastSym(tokens, n);
213 //print(JSON.stringify(lt));
214 if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
215 if (!this.ignoreBadGrammer) {
216 throw new TokenReader_Error.ArgumentError(
217 this.filename + ":" + this.line + " Error - return found after )"
226 tokens.push(new Token(found, "KEYW", name, this.line));
230 if (!this.sepIdents || found.indexOf('.') < 0 ) {
231 tokens.push(new Token(found, "NAME", "NAME", this.line));
234 var n = found.split('.');
236 foreach (unowned string nm in n) {
238 tokens.push(new Token('.', "PUNC", "DOT", this.line));
241 tokens.push(new Token(nm, "NAME", "NAME", this.line));
249 @returns {Boolean} Was the token found?
251 public bool read_punc (TokenStream stream, TokenArray tokens)
255 while (!stream.lookEOF() && Lang.punc(found + stream.look()).length > 0) {
256 found += stream.next();
264 var ls = tokens.lastSym();
266 if ((found == "}" || found == "]") && ls != null && ls.data == ",") {
267 //print("Error - comma found before " + found);
268 //print(JSON.stringify(tokens.lastSym(), null,4));
269 if (this.ignoreBadGrammer) {
270 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
272 throw new TokenReader_Error.ArgumentError(
273 this.filename + ":" + this.line + " comma found before " + found
280 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
286 @returns {Boolean} Was the token found?
288 public bool read_space (TokenStream stream, TokenArray tokens)
292 while (!stream.lookEOF() && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
293 found += stream.next();
299 //print("WHITE = " + JSON.stringify(found));
302 if (this.collapseWhite) {
303 found = " "; // this might work better if it was a '\n' ???
305 if (this.keepWhite) {
306 tokens.push(new Token(found, "WHIT", "SPACE", this.line));
313 @returns {Boolean} Was the token found?
315 public bool read_newline (TokenStream stream, TokenArray tokens)
317 var line = this.line;
318 while (!stream.lookEOF() && Lang.isNewline(stream.look())) {
320 found += stream.next();
327 // if we found a new line, then we could check if previous character was a ';' - if so we can drop it.
328 // otherwise generally keep it.. in which case it should reduce our issue with stripping new lines..
332 if (this.collapseWhite) {
335 if (this.keepWhite) {
336 var last = tokens ? tokens.pop() : false;
337 if (last && last.name != "WHIT") {
341 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
347 @returns {Boolean} Was the token found?
349 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
350 if (stream.look() == "/" && stream.look(1) == "*") {
351 var found = stream.next(2);
353 var line = this.line;
354 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
356 if (c == "\n") this.line++;
360 // to start doclet we allow /** or /*** but not /**/ or /****
361 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
362 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
369 @returns {Boolean} Was the token found?
371 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
374 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
376 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
378 var line = this.line;
379 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
380 found += stream.next();
382 if (!stream.look().eof) {
383 found += stream.next();
385 if (this.keepComments) {
386 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
395 @returns {Boolean} Was the token found?
397 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
398 if (stream.look() == "\"") {
400 var string = stream.next();
402 while (!stream.look().eof) {
403 if (stream.look() == "\\") {
404 if (Lang.isNewline(stream.look(1))) {
407 } while (!stream.look().eof && Lang.isNewline(stream.look()));
411 string += stream.next(2);
414 else if (stream.look() == "\"") {
415 string += stream.next();
416 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
420 string += stream.next();
424 return false; // error! unterminated string
428 @returns {Boolean} Was the token found?
430 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
431 if (stream.look() == "'") {
433 var string = stream.next();
435 while (!stream.look().eof) {
436 if (stream.look() == "\\") { // escape sequence
437 string += stream.next(2);
439 else if (stream.look() == "'") {
440 string += stream.next();
441 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
445 string += stream.next();
449 return false; // error! unterminated string
453 @returns {Boolean} Was the token found?
455 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
456 if (stream.look() === "0" && stream.look(1) == "x") {
457 return this.read_hex(stream, tokens);
462 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
463 found += stream.next();
470 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
471 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
476 requires("../lib/JSDOC/TextStream.js");
477 requires("../lib/JSDOC/Token.js");
478 requires("../lib/JSDOC/Lang.js");
480 plan(3, "testing read_numb");
483 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
484 var tr = new TokenReader();
485 var tokens = tr.tokenize(new TextStream(src));
487 var hexToken, octToken, decToken;
488 for (var i = 0; i < tokens.length; i++) {
489 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
490 if (tokens[i].name == "OCTAL") octToken = tokens[i];
491 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
495 is(decToken.data, "8.0", "decimal number is found in source.");
496 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
497 is(octToken.data, "0777", "octal number is found in source.");
501 @returns {Boolean} Was the token found?
503 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
504 var found = stream.next(2);
506 while (!stream.look().eof) {
507 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
508 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
512 found += stream.next();
519 @returns {Boolean} Was the token found?
521 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
529 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
534 && !last.is("RIGHT_PAREN")
535 && !last.is("RIGHT_BRACKET")
540 var regex = stream.next();
542 while (!stream.look().eof) {
543 if (stream.look() == "\\") { // escape sequence
544 regex += stream.next(2);
546 else if (stream.look() == "/") {
547 regex += stream.next();
549 while (/[gmi]/.test(stream.look())) {
550 regex += stream.next();
553 tokens.push(new Token(regex, "REGX", "REGX", this.line));
557 regex += stream.next();
560 // error: unterminated regex