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) {
333 found = "\n"; // reduces multiple line breaks into a single one...
336 if (this.keepWhite) {
337 var last = tokens.pop();
338 if (last != null && last.name != "WHIT") {
341 // replaces last new line...
342 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
348 @returns {Boolean} Was the token found?
350 public bool read_mlcomment (TokenStream stream, TokenArray tokens)
352 if (stream.look() != "/") {
355 if (stream.look(1) != "*") {
358 var found = stream.next(2);
360 var line = this.line;
361 while (!stream.lookEOF() && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
369 // to start doclet we allow /** or /*** but not /**/ or /****
370 //if (found.length /^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) {
371 if ((this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != "/") {
372 tokens.push(new Token(found, "COMM", "JSDOC", this.line));
373 } else if (this.keepComments) {
374 tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
381 @returns {Boolean} Was the token found?
383 public bool read_slcomment (TokenStream stream, TokenArray tokens)
387 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
389 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
391 var line = this.line;
392 while (!stream.lookEOF() && !Lang.isNewline(stream.look())) {
393 found += stream.next();
395 //if (!stream.lookEOF()) { // what? << eat the EOL?
396 found += stream.next();
398 if (this.keepComments) {
399 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
408 @returns {Boolean} Was the token found?
410 public bool read_dbquote (TokenStream stream, TokenArray tokens)
412 if (stream.look() != "\"") {
416 var str = stream.next();
418 while (!stream.lookEOF()) {
419 if (stream.look() == "\\") {
420 if (Lang.isNewline(stream.look(1))) {
423 } while (!stream.lookEOF() && Lang.isNewline(stream.look()));
427 str += stream.next(2);
431 if (stream.look() == "\"") {
432 str += stream.next();
433 tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
437 str += stream.next();
444 @returns {Boolean} Was the token found?
446 public bool read_snquote (TokenStream stream, TokenArray tokens)
448 if (stream.look() != "'") {
452 var str = stream.next();
454 while (!stream.look().eof) {
455 if (stream.look() == "\\") { // escape sequence
456 str += stream.next(2);
459 if (stream.look() == "'") {
460 str += stream.next();
461 tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
464 str += stream.next();
472 @returns {Boolean} Was the token found?
474 public bool read_numb (TokenStream stream, TokenArray tokens)
476 if (stream.look() === "0" && stream.look(1) == "x") {
477 return this.read_hex(stream, tokens);
482 while (!stream.lookEOF() && Lang.isNumber(found+stream.look())){
483 found += stream.next();
489 if (GLib.Regex.match_simple("^0[0-7]", found)) {
490 tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
493 tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
499 @returns {Boolean} Was the token found?
501 public bool read_hex (TokenStream stream, TokenArray tokens)
503 var found = stream.next(2);
505 while (!stream.lookEOF()) {
506 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
507 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
511 found += stream.next();
518 @returns {Boolean} Was the token found?
520 public bool read_regx (TokenStream stream, TokenArray tokens)
523 if (stream.look() != "/") {
526 var last = tokens.lastSym();
531 !last.is("NUMB") // stuff that can not appear before a regex..
533 && !last.is("RIGHT_PAREN")
534 && !last.is("RIGHT_BRACKET")
537 var regex = stream.next();
539 while (!stream.lookEOF()) {
540 if (stream.look() == "\\") { // escape sequence
541 regex += stream.next(2);
544 if (stream.look() == "/") {
545 regex += stream.next();
547 while (GLib.Regex.match_simple("[gmi]", stream.look()) {
548 regex += stream.next();
551 tokens.push(new Token(regex, "REGX", "REGX", this.line));
555 regex += stream.next();
558 // error: unterminated regex