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", 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 public errordomain TokenReader_Error {
80 public class TokenReader : Object
87 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
90 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
91 public bool collapseWhite = false; // only reduces white space...
92 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
93 public bool keepDocs = true;
94 /** @cfg {Boolean} keepWhite keep White space **/
95 public bool keepWhite = false;
96 /** @cfg {Boolean} keepComments keep all comments **/
97 public bool keepComments = false;
98 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
99 public bool sepIdents = false;
100 /** @cfg {String} filename name of file being parsed. **/
101 public string filename = "";
102 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
103 public bool ignoreBadGrammer = false;
110 * @return {Array} of tokens
112 * ts = new TextStream(File.read(str));
113 * tr = TokenReader({ keepComments : true, keepWhite : true });
117 public TokenArray tokenize(TextStream stream)
120 var tokens = new TokenArray();
123 while (!stream.lookEOF()) {
126 if (this.read_mlcomment(stream, tokens)) continue;
127 if (this.read_slcomment(stream, tokens)) continue;
128 if (this.read_dbquote(stream, tokens)) continue;
129 if (this.read_snquote(stream, tokens)) continue;
130 if (this.read_regx(stream, tokens)) continue;
131 if (this.read_numb(stream, tokens)) continue;
132 if (this.read_punc(stream, tokens)) continue;
133 if (this.read_newline(stream, tokens)) continue;
134 if (this.read_space(stream, tokens)) continue;
135 if (this.read_word(stream, tokens)) continue;
137 // if execution reaches here then an error has happened
139 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
149 * findPuncToken - find the id of a token (previous to current)
150 * need to back check syntax..
152 * @arg {Array} tokens the array of tokens.
153 * @arg {String} token data (eg. '(')
154 * @arg {Number} offset where to start reading from
155 * @return {Number} position of token
157 public int findPuncToken(TokenArray tokens, string data, int n)
159 n = n > 0 ? n : tokens.length -1;
163 if (stack < 1 && tokens.get(n).data == data) {
167 if (tokens.get(n).data == ")" || tokens.get(n).data == "}") {
172 if (stack > 0 && (tokens.get(n).data == "{" || tokens.get(n).data == "(")) {
184 * lastSym - find the last token symbol
185 * need to back check syntax..
187 * @arg {Array} tokens the array of tokens.
188 * @arg {Number} offset where to start..
189 * @return {Token} the token
191 public Token? lastSym(TokenArray tokens, int n)
193 for (var i = n-1; i >= 0; i--) {
194 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
195 return tokens.get(i);
204 @returns {Boolean} Was the token found?
206 public bool read_word (TextStream stream, TokenArray tokens)
209 while (!stream.lookEOF() && Lang.isWordChar((string)stream.look())) {
210 found += stream.next();
217 var name = Lang.keyword(found);
220 // look for "()return" ?? why ???
221 var ls = tokens.lastSym();
222 if (found == "return" && ls != null && ls.data == ")") {
223 //Seed.print('@' + tokens.length);
224 var n = this.findPuncToken(tokens, ")", 0);
225 //Seed.print(')@' + n);
226 n = this.findPuncToken(tokens, "(", n-1);
227 //Seed.print('(@' + n);
229 //var lt = this.lastSym(tokens, n);
231 //print(JSON.stringify(lt));
232 if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
233 if (!this.ignoreBadGrammer) {
234 throw new TokenReader_Error.ArgumentError(
235 this.filename + ":" + this.line + " Error - return found after )"
244 tokens.push(new Token(found, "KEYW", name, this.line));
248 if (!this.sepIdents || found.index_of(".") < 0 ) {
249 tokens.push(new Token(found, "NAME", "NAME", this.line));
252 var n = found.split(".");
254 foreach (unowned string nm in n) {
256 tokens.push(new Token(".", "PUNC", "DOT", this.line));
259 tokens.push(new Token(nm, "NAME", "NAME", this.line));
267 @returns {Boolean} Was the token found?
269 public bool read_punc (TextStream stream, TokenArray tokens) throws TokenReader_Error
273 while (!stream.lookEOF() && Lang.punc(found + (string)stream.look()).length > 0) {
274 found += stream.next();
282 var ls = tokens.lastSym();
284 if ((found == "}" || found == "]") && ls != null && ls.data == ",") {
285 //print("Error - comma found before " + found);
286 //print(JSON.stringify(tokens.lastSym(), null,4));
287 if (this.ignoreBadGrammer) {
288 print("\n" + this.filename + ":" + this.line.to_string() + " Error - comma found before " + found);
290 throw new TokenReader_Error.ArgumentError(
291 this.filename + ":" + this.line.to_string() + " comma found before " + found
298 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
304 @returns {Boolean} Was the token found?
306 public bool read_space (TextStream stream, TokenArray tokens)
310 while (!stream.lookEOF() && Lang.isSpaceC( stream.look()) && !Lang.isNewlineC(stream.look())) {
311 found += stream.next();
317 //print("WHITE = " + JSON.stringify(found));
320 if (this.collapseWhite) {
321 found = " "; // this might work better if it was a '\n' ???
323 if (this.keepWhite) {
324 tokens.push(new Token(found, "WHIT", "SPACE", this.line));
331 @returns {Boolean} Was the token found?
333 public bool read_newline (TextStream stream, TokenArray tokens)
336 var line = this.line;
337 while (!stream.lookEOF() && Lang.isNewlineC(stream.look())) {
339 found += stream.next();
346 // if we found a new line, then we could check if previous character was a ';' - if so we can drop it.
347 // otherwise generally keep it.. in which case it should reduce our issue with stripping new lines..
351 if (this.collapseWhite) {
352 found = "\n"; // reduces multiple line breaks into a single one...
355 if (this.keepWhite) {
356 var last = tokens.pop();
357 if (last != null && last.name != "WHIT") {
360 // replaces last new line...
361 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
367 @returns {Boolean} Was the token found?
369 public bool read_mlcomment (TextStream stream, TokenArray tokens)
371 if (stream.look() != '/') {
374 if (stream.look(1) != '*') {
377 var found = stream.next(2);
379 var line = this.line;
380 while (!stream.lookEOF() && !(stream.look(-1) == '/' && stream.look(-2) == '*')) {
388 // to start doclet we allow /** or /*** but not /**/ or /****
389 //if (found.length /^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) {
390 if (this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != '/') {
391 tokens.push(new Token(found, "COMM", "JSDOC", this.line));
392 } else if (this.keepComments) {
393 tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
400 @returns {Boolean} Was the token found?
402 public bool read_slcomment (TextStream stream, TokenArray tokens)
406 (stream.look() == '/' && stream.look(1) == '/' && (""!=(found=stream.next(2))))
408 (stream.look() == '<' && stream.look(1) == '!' && stream.look(2) == '-' && stream.look(3) == '-' && (""!=(found=stream.next(4))))
410 var line = this.line;
411 while (!stream.lookEOF() && !Lang.isNewline((string)stream.look())) {
412 found += stream.next();
414 //if (!stream.lookEOF()) { // what? << eat the EOL?
415 found += stream.next();
417 if (this.keepComments) {
418 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
427 @returns {Boolean} Was the token found?
429 public bool read_dbquote (TextStream stream, TokenArray tokens)
431 if (stream.look() != '"') {
435 var str = stream.next();
437 while (!stream.lookEOF()) {
438 if (stream.look() == '\\') {
439 if (Lang.isNewline((string)stream.look(1))) {
442 } while (!stream.lookEOF() && Lang.isNewline((string)stream.look()));
446 str += stream.next(2);
450 if (stream.look() == '"') {
451 str += stream.next();
452 tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
456 str += stream.next();
463 @returns {Boolean} Was the token found?
465 public bool read_snquote (TextStream stream, TokenArray tokens)
467 if (stream.look() != '\'') {
471 var str = stream.next();
473 while (!stream.lookEOF()) {
474 if (stream.look() == '\\') { // escape sequence
475 str += stream.next(2);
478 if (stream.look() == '\'') {
479 str += stream.next();
480 tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
483 str += stream.next();
491 @returns {Boolean} Was the token found?
493 public bool read_numb (TextStream stream, TokenArray tokens)
495 if (stream.look() == '0' && stream.look(1) == 'x') {
496 return this.read_hex(stream, tokens);
501 while (!stream.lookEOF() && Lang.isNumber(found+(string)stream.look())){
502 found += stream.next();
508 if (GLib.Regex.match_simple("^0[0-7]", found)) {
509 tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
512 tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
518 @returns {Boolean} Was the token found?
520 public bool read_hex (TextStream stream, TokenArray tokens)
522 var found = stream.next(2);
524 while (!stream.lookEOF()) {
525 if (Lang.isHexDec(found) && !Lang.isHexDec(found+(string)stream.look())) { // done
526 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
530 found += stream.next();
537 @returns {Boolean} Was the token found?
539 public bool read_regx (TextStream stream, TokenArray tokens)
542 if (stream.look() != '/') {
545 var last = tokens.lastSym();
550 !last.is("NUMB") // stuff that can not appear before a regex..
552 && !last.is("RIGHT_PAREN")
553 && !last.is("RIGHT_BRACKET")
556 var regex = stream.next();
558 while (!stream.lookEOF()) {
559 if (stream.look() == '\\') { // escape sequence
560 regex += stream.next(2);
563 if (stream.look() == '/') {
564 regex += stream.next();
566 while (GLib.Regex.match_simple("[gmi]", (string)stream.look())) {
567 regex += stream.next();
570 tokens.push(new Token(regex, "REGX", "REGX", this.line));
574 regex += stream.next();
577 // error: unterminated regex