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 + stream.look().to_string()).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()) {
419 print(stream.look().to_string());
420 if ( Lang.isNewline(stream.look().to_string())) {
423 found += stream.next();
425 if (!stream.lookEOF()) { // lookinng for end of line... if we got it, then do not eat the character..
426 found += stream.next();
428 if (this.keepComments) {
429 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
438 @returns {Boolean} Was the token found?
440 public bool read_dbquote (TextStream stream, TokenArray tokens)
442 if (stream.look() != '"') {
446 var str = stream.next();
448 while (!stream.lookEOF()) {
449 if (stream.look() == '\\') {
450 if (Lang.isNewline(stream.look(1).to_string())) {
453 } while (!stream.lookEOF() && Lang.isNewline(stream.look().to_string()));
457 str += stream.next(2);
461 if (stream.look() == '"') {
462 str += stream.next();
463 tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
467 str += stream.next();
474 @returns {Boolean} Was the token found?
476 public bool read_snquote (TextStream stream, TokenArray tokens)
478 if (stream.look() != '\'') {
482 var str = stream.next();
484 while (!stream.lookEOF()) {
485 if (stream.look() == '\\') { // escape sequence
486 str += stream.next(2);
489 if (stream.look() == '\'') {
490 str += stream.next();
491 tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
494 str += stream.next();
502 @returns {Boolean} Was the token found?
504 public bool read_numb (TextStream stream, TokenArray tokens)
506 if (stream.look() == '0' && stream.look(1) == 'x') {
507 return this.read_hex(stream, tokens);
512 while (!stream.lookEOF() && Lang.isNumber(found+stream.look().to_string())){
513 found += stream.next();
519 if (GLib.Regex.match_simple("^0[0-7]", found)) {
520 tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
523 tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
529 @returns {Boolean} Was the token found?
531 public bool read_hex (TextStream stream, TokenArray tokens)
533 var found = stream.next(2);
535 while (!stream.lookEOF()) {
536 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look().to_string())) { // done
537 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
541 found += stream.next();
548 @returns {Boolean} Was the token found?
550 public bool read_regx (TextStream stream, TokenArray tokens)
553 if (stream.look() != '/') {
556 var last = tokens.lastSym();
561 !last.is("NUMB") // stuff that can not appear before a regex..
563 && !last.is("RIGHT_PAREN")
564 && !last.is("RIGHT_BRACKET")
567 var regex = stream.next();
569 while (!stream.lookEOF()) {
570 if (stream.look() == '\\') { // escape sequence
571 regex += stream.next(2);
574 if (stream.look() == '/') {
575 regex += stream.next();
577 while (GLib.Regex.match_simple("[gmi]", stream.look().to_string())) {
578 regex += stream.next();
581 tokens.push(new Token(regex, "REGX", "REGX", this.line));
585 regex += stream.next();
588 // error: unterminated regex