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) {
128 n = n || tokens.length -1;
132 if (!stack && tokens.get(n).data == data) {
136 if (tokens.get(n).data == ')' || tokens.get(n).data == '}') {
141 if (stack && (tokens.get(n).data == '{' || tokens.get(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 public Token lastSym(TokenArray tokens, int n) {
161 for (var i = n-1; i >= 0; i--) {
162 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
163 return tokens.get(i);
172 @returns {Boolean} Was the token found?
174 public bool read_word (TokenStream stream, TokenArray tokens) {
176 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
177 found += stream.next();
185 if ((name = Lang.keyword(found))) {
186 if (found == 'return' && tokens.lastSym().data == ')') {
187 //Seed.print('@' + tokens.length);
188 var n = this.findPuncToken(tokens, ')');
189 //Seed.print(')@' + n);
190 n = this.findPuncToken(tokens, '(', n-1);
191 //Seed.print('(@' + n);
193 var lt = this.lastSym(tokens, n);
194 print(JSON.stringify(lt));
195 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
196 if (!this.ignoreBadGrammer) {
198 name : "ArgumentError",
199 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
208 tokens.push(new Token(found, "KEYW", name, this.line));
211 if (!this.sepIdents || found.indexOf('.') < 0 ) {
212 tokens.push(new Token(found, "NAME", "NAME", this.line));
215 var n = found.split('.');
218 n.forEach(function(nm) {
220 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
223 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
231 @returns {Boolean} Was the token found?
233 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
236 while (!stream.look().eof && Lang.punc(found+stream.look())) {
237 found += stream.next();
245 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
246 //print("Error - comma found before " + found);
247 //print(JSON.stringify(tokens.lastSym(), null,4));
248 if (this.ignoreBadGrammer) {
249 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
253 name : "ArgumentError",
254 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
259 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
265 @returns {Boolean} Was the token found?
267 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
270 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
271 found += stream.next();
277 //print("WHITE = " + JSON.stringify(found));
278 if (this.collapseWhite) found = " ";
279 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
285 @returns {Boolean} Was the token found?
287 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
289 var line = this.line;
290 while (!stream.look().eof && Lang.isNewline(stream.look())) {
292 found += stream.next();
299 if (this.collapseWhite) {
302 if (this.keepWhite) {
303 var last = tokens ? tokens.pop() : false;
304 if (last && last.name != "WHIT") {
308 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
314 @returns {Boolean} Was the token found?
316 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
317 if (stream.look() == "/" && stream.look(1) == "*") {
318 var found = stream.next(2);
320 var line = this.line;
321 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
323 if (c == "\n") this.line++;
327 // to start doclet we allow /** or /*** but not /**/ or /****
328 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
329 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
336 @returns {Boolean} Was the token found?
338 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
341 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
343 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
345 var line = this.line;
346 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
347 found += stream.next();
349 if (!stream.look().eof) {
350 found += stream.next();
352 if (this.keepComments) {
353 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
362 @returns {Boolean} Was the token found?
364 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
365 if (stream.look() == "\"") {
367 var string = stream.next();
369 while (!stream.look().eof) {
370 if (stream.look() == "\\") {
371 if (Lang.isNewline(stream.look(1))) {
374 } while (!stream.look().eof && Lang.isNewline(stream.look()));
378 string += stream.next(2);
381 else if (stream.look() == "\"") {
382 string += stream.next();
383 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
387 string += stream.next();
391 return false; // error! unterminated string
395 @returns {Boolean} Was the token found?
397 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
398 if (stream.look() == "'") {
400 var string = stream.next();
402 while (!stream.look().eof) {
403 if (stream.look() == "\\") { // escape sequence
404 string += stream.next(2);
406 else if (stream.look() == "'") {
407 string += stream.next();
408 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
412 string += stream.next();
416 return false; // error! unterminated string
420 @returns {Boolean} Was the token found?
422 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
423 if (stream.look() === "0" && stream.look(1) == "x") {
424 return this.read_hex(stream, tokens);
429 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
430 found += stream.next();
437 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
438 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
443 requires("../lib/JSDOC/TextStream.js");
444 requires("../lib/JSDOC/Token.js");
445 requires("../lib/JSDOC/Lang.js");
447 plan(3, "testing read_numb");
450 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
451 var tr = new TokenReader();
452 var tokens = tr.tokenize(new TextStream(src));
454 var hexToken, octToken, decToken;
455 for (var i = 0; i < tokens.length; i++) {
456 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
457 if (tokens[i].name == "OCTAL") octToken = tokens[i];
458 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
462 is(decToken.data, "8.0", "decimal number is found in source.");
463 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
464 is(octToken.data, "0777", "octal number is found in source.");
468 @returns {Boolean} Was the token found?
470 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
471 var found = stream.next(2);
473 while (!stream.look().eof) {
474 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
475 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
479 found += stream.next();
486 @returns {Boolean} Was the token found?
488 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
496 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
501 && !last.is("RIGHT_PAREN")
502 && !last.is("RIGHT_BRACKET")
507 var regex = stream.next();
509 while (!stream.look().eof) {
510 if (stream.look() == "\\") { // escape sequence
511 regex += stream.next(2);
513 else if (stream.look() == "/") {
514 regex += stream.next();
516 while (/[gmi]/.test(stream.look())) {
517 regex += stream.next();
520 tokens.push(new Token(regex, "REGX", "REGX", this.line));
524 regex += stream.next();
527 // error: unterminated regex