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
106 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line
116 * findPuncToken - find the id of a token (previous to current)
117 * need to back check syntax..
119 * @arg {Array} tokens the array of tokens.
120 * @arg {String} token data (eg. '(')
121 * @arg {Number} offset where to start reading from
122 * @return {Number} position of token
124 findPuncToken : function(tokens, data, n) {
125 n = n || tokens.length -1;
129 if (!stack && tokens[n].data == data) {
133 if (tokens[n].data == ')' || tokens[n].data == '}') {
138 if (stack && (tokens[n].data == '{' || tokens[n].data == '(')) {
150 * lastSym - find the last token symbol
151 * need to back check syntax..
153 * @arg {Array} tokens the array of tokens.
154 * @arg {Number} offset where to start..
155 * @return {Token} the token
157 lastSym : function(tokens, n) {
158 for (var i = n-1; i >= 0; i--) {
159 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
167 @returns {Boolean} Was the token found?
169 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
171 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
172 found += stream.next();
180 if ((name = Lang.keyword(found))) {
181 if (found == 'return' && tokens.lastSym().data == ')') {
182 //Seed.print('@' + tokens.length);
183 var n = this.findPuncToken(tokens, ')');
184 //Seed.print(')@' + n);
185 n = this.findPuncToken(tokens, '(', n-1);
186 //Seed.print('(@' + n);
188 var lt = this.lastSym(tokens, n);
189 print(JSON.stringify(lt));
190 if (lt.type != 'KEYW' || ['IF', 'WHILE'].indexOf(lt.name) < -1) {
191 if (!this.ignoreBadGrammer) {
193 name : "ArgumentError",
194 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
203 tokens.push(new Token(found, "KEYW", name, this.line));
206 if (!this.sepIdents || found.indexOf('.') < 0 ) {
207 tokens.push(new Token(found, "NAME", "NAME", this.line));
210 var n = found.split('.');
213 n.forEach(function(nm) {
215 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
218 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
226 @returns {Boolean} Was the token found?
228 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
231 while (!stream.look().eof && Lang.punc(found+stream.look())) {
232 found += stream.next();
240 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
241 //print("Error - comma found before " + found);
242 //print(JSON.stringify(tokens.lastSym(), null,4));
243 if (this.ignoreBadGrammer) {
244 print("\n" + this.filename + ':' + this.line + " Error - comma found before " + found);
248 name : "ArgumentError",
249 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
254 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
260 @returns {Boolean} Was the token found?
262 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
265 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
266 found += stream.next();
272 //print("WHITE = " + JSON.stringify(found));
273 if (this.collapseWhite) found = " ";
274 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
280 @returns {Boolean} Was the token found?
282 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
284 var line = this.line;
285 while (!stream.look().eof && Lang.isNewline(stream.look())) {
287 found += stream.next();
294 if (this.collapseWhite) {
297 if (this.keepWhite) {
298 var last = tokens ? tokens.pop() : false;
299 if (last && last.name != "WHIT") {
303 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
309 @returns {Boolean} Was the token found?
311 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
312 if (stream.look() == "/" && stream.look(1) == "*") {
313 var found = stream.next(2);
315 var line = this.line;
316 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
318 if (c == "\n") this.line++;
322 // to start doclet we allow /** or /*** but not /**/ or /****
323 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
324 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
331 @returns {Boolean} Was the token found?
333 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
336 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
338 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
340 var line = this.line;
341 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
342 found += stream.next();
344 if (!stream.look().eof) {
345 found += stream.next();
347 if (this.keepComments) {
348 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
357 @returns {Boolean} Was the token found?
359 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
360 if (stream.look() == "\"") {
362 var string = stream.next();
364 while (!stream.look().eof) {
365 if (stream.look() == "\\") {
366 if (Lang.isNewline(stream.look(1))) {
369 } while (!stream.look().eof && Lang.isNewline(stream.look()));
373 string += stream.next(2);
376 else if (stream.look() == "\"") {
377 string += stream.next();
378 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
382 string += stream.next();
386 return false; // error! unterminated string
390 @returns {Boolean} Was the token found?
392 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
393 if (stream.look() == "'") {
395 var string = stream.next();
397 while (!stream.look().eof) {
398 if (stream.look() == "\\") { // escape sequence
399 string += stream.next(2);
401 else if (stream.look() == "'") {
402 string += stream.next();
403 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
407 string += stream.next();
411 return false; // error! unterminated string
415 @returns {Boolean} Was the token found?
417 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
418 if (stream.look() === "0" && stream.look(1) == "x") {
419 return this.read_hex(stream, tokens);
424 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
425 found += stream.next();
432 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
433 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
438 requires("../lib/JSDOC/TextStream.js");
439 requires("../lib/JSDOC/Token.js");
440 requires("../lib/JSDOC/Lang.js");
442 plan(3, "testing read_numb");
445 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
446 var tr = new TokenReader();
447 var tokens = tr.tokenize(new TextStream(src));
449 var hexToken, octToken, decToken;
450 for (var i = 0; i < tokens.length; i++) {
451 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
452 if (tokens[i].name == "OCTAL") octToken = tokens[i];
453 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
457 is(decToken.data, "8.0", "decimal number is found in source.");
458 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
459 is(octToken.data, "0777", "octal number is found in source.");
463 @returns {Boolean} Was the token found?
465 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
466 var found = stream.next(2);
468 while (!stream.look().eof) {
469 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
470 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
474 found += stream.next();
481 @returns {Boolean} Was the token found?
483 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
491 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
496 && !last.is("RIGHT_PAREN")
497 && !last.is("RIGHT_BRACKET")
502 var regex = stream.next();
504 while (!stream.look().eof) {
505 if (stream.look() == "\\") { // escape sequence
506 regex += stream.next(2);
508 else if (stream.look() == "/") {
509 regex += stream.next();
511 while (/[gmi]/.test(stream.look())) {
512 regex += stream.next();
515 tokens.push(new Token(regex, "REGX", "REGX", this.line));
519 regex += stream.next();
522 // error: unterminated regex