1 //<script type="text/javascript">
7 var tr = new JSDOC.TokenReader();
10 tr.keepComments = true;
12 tr.collapseWhite = false;
15 FileUtils.get_contents("/home/alan/gitlive/gnome.introspection-doc-generator/JSDOC/Walker2.js", out str);
17 var toks = tr.tokenize(new JSDOC.TextStream(str)); // dont merge xxx + . + yyyy etc.
21 //const Token = imports.Token.Token;
22 //const Lang = imports.Lang.Lang;
25 @class Search a {@link JSDOC.TextStream} for language tokens.
33 public class TokenArray: Object {
35 public Gee.ArrayList<Token> tokens;
37 get { return this.tokens.size; }
42 this.tokens = new Gee.ArrayList<Token>();
45 public Token? last() {
46 if (this.tokens.size > 0) {
47 return this.tokens.get(this.tokens.size-1);
51 public Token? lastSym () {
52 for (var i = this.tokens.size-1; i >= 0; i--) {
53 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
54 return this.tokens.get(i);
59 public void push (Token t) {
64 if (this.tokens.size > 0) {
65 return this.tokens.remove_at(this.tokens.size-1);
70 public new Token get(int i) {
71 return this.tokens.get(i);
75 foreach(var token in this.tokens) {
76 print(token.asString());
82 public errordomain TokenReader_Error {
87 public class TokenReader : Object
94 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
97 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
98 public bool collapseWhite = false; // only reduces white space...
99 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
100 public bool keepDocs = true;
101 /** @cfg {Boolean} keepWhite keep White space **/
102 public bool keepWhite = false;
103 /** @cfg {Boolean} keepComments keep all comments **/
104 public bool keepComments = false;
105 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
106 public bool sepIdents = false;
107 /** @cfg {String} filename name of file being parsed. **/
108 public string filename = "";
109 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
110 public bool ignoreBadGrammer = false;
117 * @return {Array} of tokens
119 * ts = new TextStream(File.read(str));
120 * tr = TokenReader({ keepComments : true, keepWhite : true });
124 public TokenArray tokenize(TextStream stream)
127 var tokens = new TokenArray();
130 while (!stream.lookEOF()) {
133 if (this.read_mlcomment(stream, tokens)) continue;
134 if (this.read_slcomment(stream, tokens)) continue;
135 if (this.read_dbquote(stream, tokens)) continue;
136 if (this.read_snquote(stream, tokens)) continue;
137 if (this.read_regx(stream, tokens)) continue;
138 if (this.read_numb(stream, tokens)) continue;
139 if (this.read_punc(stream, tokens)) continue;
140 if (this.read_newline(stream, tokens)) continue;
141 if (this.read_space(stream, tokens)) continue;
142 if (this.read_word(stream, tokens)) continue;
144 // if execution reaches here then an error has happened
146 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
156 * findPuncToken - find the id of a token (previous to current)
157 * need to back check syntax..
159 * @arg {Array} tokens the array of tokens.
160 * @arg {String} token data (eg. '(')
161 * @arg {Number} offset where to start reading from
162 * @return {Number} position of token
164 public int findPuncToken(TokenArray tokens, string data, int n)
166 n = n > 0 ? n : tokens.length -1;
170 if (stack < 1 && tokens.get(n).data == data) {
174 if (tokens.get(n).data == ")" || tokens.get(n).data == "}") {
179 if (stack > 0 && (tokens.get(n).data == "{" || tokens.get(n).data == "(")) {
191 * lastSym - find the last token symbol
192 * need to back check syntax..
194 * @arg {Array} tokens the array of tokens.
195 * @arg {Number} offset where to start..
196 * @return {Token} the token
198 public Token? lastSym(TokenArray tokens, int n)
200 for (var i = n-1; i >= 0; i--) {
201 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
202 return tokens.get(i);
211 @returns {Boolean} Was the token found?
213 public bool read_word (TextStream stream, TokenArray tokens)
216 while (!stream.lookEOF() && Lang.isWordChar(stream.look().to_string())) {
217 found += stream.next();
224 var name = Lang.keyword(found);
227 // look for "()return" ?? why ???
228 var ls = tokens.lastSym();
229 if (found == "return" && ls != null && ls.data == ")") {
230 //Seed.print('@' + tokens.length);
231 var n = this.findPuncToken(tokens, ")", 0);
232 //Seed.print(')@' + n);
233 n = this.findPuncToken(tokens, "(", n-1);
234 //Seed.print('(@' + n);
236 //var lt = this.lastSym(tokens, n);
238 //print(JSON.stringify(lt));
239 if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
240 if (!this.ignoreBadGrammer) {
241 throw new TokenReader_Error.ArgumentError(
242 this.filename + ":" + this.line + " Error - return found after )"
251 tokens.push(new Token(found, "KEYW", name, this.line));
255 if (!this.sepIdents || found.index_of(".") < 0 ) {
256 tokens.push(new Token(found, "NAME", "NAME", this.line));
259 var n = found.split(".");
261 foreach (unowned string nm in n) {
263 tokens.push(new Token(".", "PUNC", "DOT", this.line));
266 tokens.push(new Token(nm, "NAME", "NAME", this.line));
274 @returns {Boolean} Was the token found?
276 public bool read_punc (TextStream stream, TokenArray tokens) throws TokenReader_Error
280 while (!stream.lookEOF() && Lang.punc(found + (string)stream.look()).length > 0) {
281 found += stream.next();
289 var ls = tokens.lastSym();
291 if ((found == "}" || found == "]") && ls != null && ls.data == ",") {
292 //print("Error - comma found before " + found);
293 //print(JSON.stringify(tokens.lastSym(), null,4));
294 if (this.ignoreBadGrammer) {
295 print("\n" + this.filename + ":" + this.line.to_string() + " Error - comma found before " + found);
297 throw new TokenReader_Error.ArgumentError(
298 this.filename + ":" + this.line.to_string() + " comma found before " + found
305 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
311 @returns {Boolean} Was the token found?
313 public bool read_space (TextStream stream, TokenArray tokens)
317 while (!stream.lookEOF() && Lang.isSpaceC( stream.look()) && !Lang.isNewlineC(stream.look())) {
318 found += stream.next();
324 //print("WHITE = " + JSON.stringify(found));
327 if (this.collapseWhite) {
328 found = " "; // this might work better if it was a '\n' ???
330 if (this.keepWhite) {
331 tokens.push(new Token(found, "WHIT", "SPACE", this.line));
338 @returns {Boolean} Was the token found?
340 public bool read_newline (TextStream stream, TokenArray tokens)
343 var line = this.line;
344 while (!stream.lookEOF() && Lang.isNewlineC(stream.look())) {
346 found += stream.next();
353 // if we found a new line, then we could check if previous character was a ';' - if so we can drop it.
354 // otherwise generally keep it.. in which case it should reduce our issue with stripping new lines..
358 if (this.collapseWhite) {
359 found = "\n"; // reduces multiple line breaks into a single one...
362 if (this.keepWhite) {
363 var last = tokens.pop();
364 if (last != null && last.name != "WHIT") {
367 // replaces last new line...
368 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
374 @returns {Boolean} Was the token found?
376 public bool read_mlcomment (TextStream stream, TokenArray tokens)
378 if (stream.look() != '/') {
381 if (stream.look(1) != '*') {
384 var found = stream.next(2);
386 var line = this.line;
387 while (!stream.lookEOF() && !(stream.look(-1) == '/' && stream.look(-2) == '*')) {
395 // to start doclet we allow /** or /*** but not /**/ or /****
396 //if (found.length /^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) {
397 if (this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != '/') {
398 tokens.push(new Token(found, "COMM", "JSDOC", this.line));
399 } else if (this.keepComments) {
400 tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
407 @returns {Boolean} Was the token found?
409 public bool read_slcomment (TextStream stream, TokenArray tokens)
413 (stream.look() == '/' && stream.look(1) == '/' && (""!=(found=stream.next(2))))
415 (stream.look() == '<' && stream.look(1) == '!' && stream.look(2) == '-' && stream.look(3) == '-' && (""!=(found=stream.next(4))))
417 var line = this.line;
418 while (!stream.lookEOF() && !Lang.isNewline((string)stream.look())) {
419 found += stream.next();
421 //if (!stream.lookEOF()) { // what? << eat the EOL?
422 found += stream.next();
424 if (this.keepComments) {
425 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
434 @returns {Boolean} Was the token found?
436 public bool read_dbquote (TextStream stream, TokenArray tokens)
438 if (stream.look() != '"') {
442 var str = stream.next();
444 while (!stream.lookEOF()) {
445 if (stream.look() == '\\') {
446 if (Lang.isNewline((string)stream.look(1))) {
449 } while (!stream.lookEOF() && Lang.isNewline((string)stream.look()));
453 str += stream.next(2);
457 if (stream.look() == '"') {
458 str += stream.next();
459 tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
463 str += stream.next();
470 @returns {Boolean} Was the token found?
472 public bool read_snquote (TextStream stream, TokenArray tokens)
474 if (stream.look() != '\'') {
478 var str = stream.next();
480 while (!stream.lookEOF()) {
481 if (stream.look() == '\\') { // escape sequence
482 str += stream.next(2);
485 if (stream.look() == '\'') {
486 str += stream.next();
487 tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
490 str += stream.next();
498 @returns {Boolean} Was the token found?
500 public bool read_numb (TextStream stream, TokenArray tokens)
502 if (stream.look() == '0' && stream.look(1) == 'x') {
503 return this.read_hex(stream, tokens);
508 while (!stream.lookEOF() && Lang.isNumber(found+(string)stream.look())){
509 found += stream.next();
515 if (GLib.Regex.match_simple("^0[0-7]", found)) {
516 tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
519 tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
525 @returns {Boolean} Was the token found?
527 public bool read_hex (TextStream stream, TokenArray tokens)
529 var found = stream.next(2);
531 while (!stream.lookEOF()) {
532 if (Lang.isHexDec(found) && !Lang.isHexDec(found+(string)stream.look())) { // done
533 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
537 found += stream.next();
544 @returns {Boolean} Was the token found?
546 public bool read_regx (TextStream stream, TokenArray tokens)
549 if (stream.look() != '/') {
552 var last = tokens.lastSym();
557 !last.is("NUMB") // stuff that can not appear before a regex..
559 && !last.is("RIGHT_PAREN")
560 && !last.is("RIGHT_BRACKET")
563 var regex = stream.next();
565 while (!stream.lookEOF()) {
566 if (stream.look() == '\\') { // escape sequence
567 regex += stream.next(2);
570 if (stream.look() == '/') {
571 regex += stream.next();
573 while (GLib.Regex.match_simple("[gmi]", (string)stream.look())) {
574 regex += stream.next();
577 tokens.push(new Token(regex, "REGX", "REGX", this.line));
581 regex += stream.next();
584 // error: unterminated regex