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 Gee.ArrayList<Token> tokens;
21 this.items = new Gee.ArrayList<Token>();
24 public Token? last() {
25 if (this.tokens > 0) {
26 return this.tokens[this.tokens.length-1];
30 public Token? lastSym () {
31 for (var i = this.tokens.length-1; i >= 0; i--) {
32 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
33 return this.tokens.get(i);
38 public void push (Token t) {
44 public class TokenReader : Object
51 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
54 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
55 public bool collapseWhite = false, // only reduces white space...
56 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
57 public bool keepDocs = true,
58 /** @cfg {Boolean} keepWhite keep White space **/
59 public bool keepWhite = false,
60 /** @cfg {Boolean} keepComments keep all comments **/
61 public bool keepComments = false,
62 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
63 public bool sepIdents = false,
64 /** @cfg {String} filename name of file being parsed. **/
65 public string filename = "";
66 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
67 public bool ignoreBadGrammer = false,
74 * @return {Array} of tokens
76 * ts = new TextStream(File.read(str));
77 * tr = TokenReader({ keepComments : true, keepWhite : true });
81 public TokenArray tokenize(TextStream stream)
84 var tokens = new TokenArray();
89 stream.look(0, out eof)
93 if (this.read_mlcomment(stream, tokens)) continue;
94 if (this.read_slcomment(stream, tokens)) continue;
95 if (this.read_dbquote(stream, tokens)) continue;
96 if (this.read_snquote(stream, tokens)) continue;
97 if (this.read_regx(stream, tokens)) continue;
98 if (this.read_numb(stream, tokens)) continue;
99 if (this.read_punc(stream, tokens)) continue;
100 if (this.read_newline(stream, tokens)) continue;
101 if (this.read_space(stream, tokens)) continue;
102 if (this.read_word(stream, tokens)) continue;
104 // if execution reaches here then an error has happened
105 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
114 * findPuncToken - find the id of a token (previous to current)
115 * need to back check syntax..
117 * @arg {Array} tokens the array of tokens.
118 * @arg {String} token data (eg. '(')
119 * @arg {Number} offset where to start reading from
120 * @return {Number} position of token
122 findPuncToken : function(tokens, data, n) {
123 n = n || tokens.length -1;
127 if (!stack && tokens[n].data == data) {
131 if (tokens[n].data == ')' || tokens[n].data == '}') {
136 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
148 * lastSym - find the last token symbol
149 * need to back check syntax..
151 * @arg {Array} tokens the array of tokens.
152 * @arg {Number} offset where to start..
153 * @return {Token} the token
155 lastSym : function(tokens, n) {
156 for (var i = n-1; i >= 0; i--) {
157 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
165 @returns {Boolean} Was the token found?
167 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
169 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
170 found += stream.next();
178 if ((name = Lang.keyword(found))) {
179 if (found == 'return' && tokens.lastSym().data == ')') {
180 //Seed.print('@' + tokens.length);
181 var n = this.findPuncToken(tokens, ')');
182 //Seed.print(')@' + n);
183 n = this.findPuncToken(tokens, '(', n-1);
184 //Seed.print('(@' + n);
186 var lt = this.lastSym(tokens, n);
187 print(JSON.stringify(lt));
188 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
189 if (!this.ignoreBadGrammer) {
191 name : "ArgumentError",
192 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
201 tokens.push(new Token(found, "KEYW", name, this.line));
204 if (!this.sepIdents || found.indexOf('.') < 0 ) {
205 tokens.push(new Token(found, "NAME", "NAME", this.line));
208 var n = found.split('.');
211 n.forEach(function(nm) {
213 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
216 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
224 @returns {Boolean} Was the token found?
226 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
229 while (!stream.look().eof && Lang.punc(found+stream.look())) {
230 found += stream.next();
238 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
239 //print("Error - comma found before " + found);
240 //print(JSON.stringify(tokens.lastSym(), null,4));
241 if (this.ignoreBadGrammer) {
242 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
246 name : "ArgumentError",
247 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
252 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
258 @returns {Boolean} Was the token found?
260 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
263 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
264 found += stream.next();
270 //print("WHITE = " + JSON.stringify(found));
271 if (this.collapseWhite) found = " ";
272 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
278 @returns {Boolean} Was the token found?
280 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
282 var line = this.line;
283 while (!stream.look().eof && Lang.isNewline(stream.look())) {
285 found += stream.next();
292 if (this.collapseWhite) {
295 if (this.keepWhite) {
296 var last = tokens ? tokens.pop() : false;
297 if (last && last.name != "WHIT") {
301 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
307 @returns {Boolean} Was the token found?
309 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
310 if (stream.look() == "/" && stream.look(1) == "*") {
311 var found = stream.next(2);
313 var line = this.line;
314 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
316 if (c == "\n") this.line++;
320 // to start doclet we allow /** or /*** but not /**/ or /****
321 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
322 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
329 @returns {Boolean} Was the token found?
331 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
334 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
336 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
338 var line = this.line;
339 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
340 found += stream.next();
342 if (!stream.look().eof) {
343 found += stream.next();
345 if (this.keepComments) {
346 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
355 @returns {Boolean} Was the token found?
357 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
358 if (stream.look() == "\"") {
360 var string = stream.next();
362 while (!stream.look().eof) {
363 if (stream.look() == "\\") {
364 if (Lang.isNewline(stream.look(1))) {
367 } while (!stream.look().eof && Lang.isNewline(stream.look()));
371 string += stream.next(2);
374 else if (stream.look() == "\"") {
375 string += stream.next();
376 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
380 string += stream.next();
384 return false; // error! unterminated string
388 @returns {Boolean} Was the token found?
390 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
391 if (stream.look() == "'") {
393 var string = stream.next();
395 while (!stream.look().eof) {
396 if (stream.look() == "\\") { // escape sequence
397 string += stream.next(2);
399 else if (stream.look() == "'") {
400 string += stream.next();
401 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
405 string += stream.next();
409 return false; // error! unterminated string
413 @returns {Boolean} Was the token found?
415 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
416 if (stream.look() === "0" && stream.look(1) == "x") {
417 return this.read_hex(stream, tokens);
422 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
423 found += stream.next();
430 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
431 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
436 requires("../lib/JSDOC/TextStream.js");
437 requires("../lib/JSDOC/Token.js");
438 requires("../lib/JSDOC/Lang.js");
440 plan(3, "testing read_numb");
443 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
444 var tr = new TokenReader();
445 var tokens = tr.tokenize(new TextStream(src));
447 var hexToken, octToken, decToken;
448 for (var i = 0; i < tokens.length; i++) {
449 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
450 if (tokens[i].name == "OCTAL") octToken = tokens[i];
451 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
455 is(decToken.data, "8.0", "decimal number is found in source.");
456 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
457 is(octToken.data, "0777", "octal number is found in source.");
461 @returns {Boolean} Was the token found?
463 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
464 var found = stream.next(2);
466 while (!stream.look().eof) {
467 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
468 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
472 found += stream.next();
479 @returns {Boolean} Was the token found?
481 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
489 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
494 && !last.is("RIGHT_PAREN")
495 && !last.is("RIGHT_BRACKET")
500 var regex = stream.next();
502 while (!stream.look().eof) {
503 if (stream.look() == "\\") { // escape sequence
504 regex += stream.next(2);
506 else if (stream.look() == "/") {
507 regex += stream.next();
509 while (/[gmi]/.test(stream.look())) {
510 regex += stream.next();
513 tokens.push(new Token(regex, "REGX", "REGX", this.line));
517 regex += stream.next();
520 // error: unterminated regex