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);
49 errordomain TokenReader_Error {
54 public class TokenReader : Object
61 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
64 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
65 public bool collapseWhite = false, // only reduces white space...
66 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
67 public bool keepDocs = true,
68 /** @cfg {Boolean} keepWhite keep White space **/
69 public bool keepWhite = false,
70 /** @cfg {Boolean} keepComments keep all comments **/
71 public bool keepComments = false,
72 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
73 public bool sepIdents = false,
74 /** @cfg {String} filename name of file being parsed. **/
75 public string filename = "";
76 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
77 public bool ignoreBadGrammer = false,
84 * @return {Array} of tokens
86 * ts = new TextStream(File.read(str));
87 * tr = TokenReader({ keepComments : true, keepWhite : true });
91 public TokenArray tokenize(TextStream stream)
94 var tokens = new TokenArray();
97 while (!stream.lookEOF()) {
100 if (this.read_mlcomment(stream, tokens)) continue;
101 if (this.read_slcomment(stream, tokens)) continue;
102 if (this.read_dbquote(stream, tokens)) continue;
103 if (this.read_snquote(stream, tokens)) continue;
104 if (this.read_regx(stream, tokens)) continue;
105 if (this.read_numb(stream, tokens)) continue;
106 if (this.read_punc(stream, tokens)) continue;
107 if (this.read_newline(stream, tokens)) continue;
108 if (this.read_space(stream, tokens)) continue;
109 if (this.read_word(stream, tokens)) continue;
111 // if execution reaches here then an error has happened
113 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
123 * findPuncToken - find the id of a token (previous to current)
124 * need to back check syntax..
126 * @arg {Array} tokens the array of tokens.
127 * @arg {String} token data (eg. '(')
128 * @arg {Number} offset where to start reading from
129 * @return {Number} position of token
131 public int findPuncToken(TokenArray tokens, string data, int n)
133 n = n || tokens.length -1;
137 if (!stack && tokens.get(n).data == data) {
141 if (tokens.get(n).data == ')' || tokens.get(n).data == '}') {
146 if (stack && (tokens.get(n).data == '{' || tokens.get(n).data == '(')) {
158 * lastSym - find the last token symbol
159 * need to back check syntax..
161 * @arg {Array} tokens the array of tokens.
162 * @arg {Number} offset where to start..
163 * @return {Token} the token
165 public Token lastSym(TokenArray tokens, int n)
167 for (var i = n-1; i >= 0; i--) {
168 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
169 return tokens.get(i);
178 @returns {Boolean} Was the token found?
180 public bool read_word (TokenStream stream, TokenArray tokens)
183 while (!stream.lookEOF() && Lang.isWordChar(stream.look())) {
184 found += stream.next();
191 var name = Lang.keyword(found);
194 // look for "()return" ?? why ???
195 var ls = tokens.lastSym();
196 if (found == "return" && ls != null && ls.data == ")") {
197 //Seed.print('@' + tokens.length);
198 var n = this.findPuncToken(tokens, ")");
199 //Seed.print(')@' + n);
200 n = this.findPuncToken(tokens, "(", n-1);
201 //Seed.print('(@' + n);
203 var lt = this.lastSym(tokens, n);
205 //print(JSON.stringify(lt));
206 if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
207 if (!this.ignoreBadGrammer) {
208 throw new TokenReader_Error.ArgumentError(
209 this.filename + ":" + this.line + " Error - return found after )"
218 tokens.push(new Token(found, "KEYW", name, this.line));
222 if (!this.sepIdents || found.indexOf('.') < 0 ) {
223 tokens.push(new Token(found, "NAME", "NAME", this.line));
226 var n = found.split('.');
228 foreach (unowned string nm in n) {
230 tokens.push(new Token('.', "PUNC", "DOT", this.line));
233 tokens.push(new Token(nm, "NAME", "NAME", this.line));
241 @returns {Boolean} Was the token found?
243 public bool read_punc (TokenStream stream, TokenArray tokens)
247 while (!stream.lookEOF() && Lang.punc(found + stream.look()).length > 0) {
248 found += stream.next();
256 var ls = tokens.lastSym();
258 if ((found == "}" || found == "]") && ls != null && ls.data == ",") {
259 //print("Error - comma found before " + found);
260 //print(JSON.stringify(tokens.lastSym(), null,4));
261 if (this.ignoreBadGrammer) {
262 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
264 throw new TokenReader_Error.ArgumentError(
265 this.filename + ":" + this.line + " comma found before " + found
272 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
278 @returns {Boolean} Was the token found?
280 public bool read_space (TokenStream stream, TokenArray tokens)
284 while (!stream.lookEOF() && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
285 found += stream.next();
291 //print("WHITE = " + JSON.stringify(found));
292 if (this.collapseWhite) {
293 found = " "; // this might work better if it was a ';' ???
295 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
301 @returns {Boolean} Was the token found?
303 public bool read_newline (TokenStream stream, TokenArray tokens)
305 var line = this.line;
306 while (!stream.look().eof && Lang.isNewline(stream.look())) {
308 found += stream.next();
315 if (this.collapseWhite) {
318 if (this.keepWhite) {
319 var last = tokens ? tokens.pop() : false;
320 if (last && last.name != "WHIT") {
324 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
330 @returns {Boolean} Was the token found?
332 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
333 if (stream.look() == "/" && stream.look(1) == "*") {
334 var found = stream.next(2);
336 var line = this.line;
337 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
339 if (c == "\n") this.line++;
343 // to start doclet we allow /** or /*** but not /**/ or /****
344 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
345 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
352 @returns {Boolean} Was the token found?
354 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
357 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
359 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
361 var line = this.line;
362 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
363 found += stream.next();
365 if (!stream.look().eof) {
366 found += stream.next();
368 if (this.keepComments) {
369 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
378 @returns {Boolean} Was the token found?
380 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
381 if (stream.look() == "\"") {
383 var string = stream.next();
385 while (!stream.look().eof) {
386 if (stream.look() == "\\") {
387 if (Lang.isNewline(stream.look(1))) {
390 } while (!stream.look().eof && Lang.isNewline(stream.look()));
394 string += stream.next(2);
397 else if (stream.look() == "\"") {
398 string += stream.next();
399 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
403 string += stream.next();
407 return false; // error! unterminated string
411 @returns {Boolean} Was the token found?
413 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
414 if (stream.look() == "'") {
416 var string = stream.next();
418 while (!stream.look().eof) {
419 if (stream.look() == "\\") { // escape sequence
420 string += stream.next(2);
422 else if (stream.look() == "'") {
423 string += stream.next();
424 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
428 string += stream.next();
432 return false; // error! unterminated string
436 @returns {Boolean} Was the token found?
438 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
439 if (stream.look() === "0" && stream.look(1) == "x") {
440 return this.read_hex(stream, tokens);
445 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
446 found += stream.next();
453 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
454 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
459 requires("../lib/JSDOC/TextStream.js");
460 requires("../lib/JSDOC/Token.js");
461 requires("../lib/JSDOC/Lang.js");
463 plan(3, "testing read_numb");
466 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
467 var tr = new TokenReader();
468 var tokens = tr.tokenize(new TextStream(src));
470 var hexToken, octToken, decToken;
471 for (var i = 0; i < tokens.length; i++) {
472 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
473 if (tokens[i].name == "OCTAL") octToken = tokens[i];
474 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
478 is(decToken.data, "8.0", "decimal number is found in source.");
479 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
480 is(octToken.data, "0777", "octal number is found in source.");
484 @returns {Boolean} Was the token found?
486 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
487 var found = stream.next(2);
489 while (!stream.look().eof) {
490 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
491 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
495 found += stream.next();
502 @returns {Boolean} Was the token found?
504 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
512 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
517 && !last.is("RIGHT_PAREN")
518 && !last.is("RIGHT_BRACKET")
523 var regex = stream.next();
525 while (!stream.look().eof) {
526 if (stream.look() == "\\") { // escape sequence
527 regex += stream.next(2);
529 else if (stream.look() == "/") {
530 regex += stream.next();
532 while (/[gmi]/.test(stream.look())) {
533 regex += stream.next();
536 tokens.push(new Token(regex, "REGX", "REGX", this.line));
540 regex += stream.next();
543 // error: unterminated regex