1 //<script type="text/javascript">
7 var tr = new JSDOC.TokenReader();
10 tr.keepComments = true;
12 tr.collapseWhite = false;
16 var toks = tr.tokenize(new JSDOC.TextStream(str)); // dont merge xxx + . + yyyy etc.
20 //const Token = imports.Token.Token;
21 //const Lang = imports.Lang.Lang;
24 @class Search a {@link JSDOC.TextStream} for language tokens.
32 public class TokenArray: Object {
34 public Gee.ArrayList<Token> tokens;
36 get { return this.tokens.size; }
41 this.tokens = new Gee.ArrayList<Token>();
44 public Token? last() {
45 if (this.tokens.size > 0) {
46 return this.tokens.get(this.tokens.size-1);
50 public Token? lastSym () {
51 for (var i = this.tokens.size-1; i >= 0; i--) {
52 if (!(this.tokens.get(i).is("WHIT") || this.tokens.get(i).is("COMM"))) {
53 return this.tokens.get(i);
58 public void push (Token t) {
63 if (this.tokens.size > 0) {
64 return this.tokens.remove_at(this.tokens.size-1);
69 public new Token get(int i) {
70 return this.tokens.get(i);
74 public errordomain TokenReader_Error {
79 public class TokenReader : Object
86 * I wonder if this will accept the prop: value, prop2 :value construxtor if we do not define one...
89 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
90 public bool collapseWhite = false; // only reduces white space...
91 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
92 public bool keepDocs = true;
93 /** @cfg {Boolean} keepWhite keep White space **/
94 public bool keepWhite = false;
95 /** @cfg {Boolean} keepComments keep all comments **/
96 public bool keepComments = false;
97 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
98 public bool sepIdents = false;
99 /** @cfg {String} filename name of file being parsed. **/
100 public string filename = "";
101 /** @config {Boolean} ignoreBadGrammer do not throw errors if we find stuff that might break compression **/
102 public bool ignoreBadGrammer = false;
109 * @return {Array} of tokens
111 * ts = new TextStream(File.read(str));
112 * tr = TokenReader({ keepComments : true, keepWhite : true });
116 public TokenArray tokenize(TextStream stream)
119 var tokens = new TokenArray();
122 while (!stream.lookEOF()) {
125 if (this.read_mlcomment(stream, tokens)) continue;
126 if (this.read_slcomment(stream, tokens)) continue;
127 if (this.read_dbquote(stream, tokens)) continue;
128 if (this.read_snquote(stream, tokens)) continue;
129 if (this.read_regx(stream, tokens)) continue;
130 if (this.read_numb(stream, tokens)) continue;
131 if (this.read_punc(stream, tokens)) continue;
132 if (this.read_newline(stream, tokens)) continue;
133 if (this.read_space(stream, tokens)) continue;
134 if (this.read_word(stream, tokens)) continue;
136 // if execution reaches here then an error has happened
138 new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line)
148 * findPuncToken - find the id of a token (previous to current)
149 * need to back check syntax..
151 * @arg {Array} tokens the array of tokens.
152 * @arg {String} token data (eg. '(')
153 * @arg {Number} offset where to start reading from
154 * @return {Number} position of token
156 public int findPuncToken(TokenArray tokens, string data, int n)
158 n = n > 0 ? n : tokens.length -1;
162 if (stack < 1 && tokens.get(n).data == data) {
166 if (tokens.get(n).data == ")" || tokens.get(n).data == "}") {
171 if (stack > 0 && (tokens.get(n).data == "{" || tokens.get(n).data == "(")) {
183 * lastSym - find the last token symbol
184 * need to back check syntax..
186 * @arg {Array} tokens the array of tokens.
187 * @arg {Number} offset where to start..
188 * @return {Token} the token
190 public Token? lastSym(TokenArray tokens, int n)
192 for (var i = n-1; i >= 0; i--) {
193 if (!(tokens.get(i).is("WHIT") || tokens.get(i).is("COMM"))) {
194 return tokens.get(i);
203 @returns {Boolean} Was the token found?
205 public bool read_word (TextStream stream, TokenArray tokens)
208 while (!stream.lookEOF() && Lang.isWordChar((string)stream.look())) {
209 found += stream.next();
216 var name = Lang.keyword(found);
219 // look for "()return" ?? why ???
220 var ls = tokens.lastSym();
221 if (found == "return" && ls != null && ls.data == ")") {
222 //Seed.print('@' + tokens.length);
223 var n = this.findPuncToken(tokens, ")", 0);
224 //Seed.print(')@' + n);
225 n = this.findPuncToken(tokens, "(", n-1);
226 //Seed.print('(@' + n);
228 //var lt = this.lastSym(tokens, n);
230 //print(JSON.stringify(lt));
231 if (lt.type != "KEYW" || ["IF", 'WHILE'].indexOf(lt.name) < -1) {
232 if (!this.ignoreBadGrammer) {
233 throw new TokenReader_Error.ArgumentError(
234 this.filename + ":" + this.line + " Error - return found after )"
243 tokens.push(new Token(found, "KEYW", name, this.line));
247 if (!this.sepIdents || found.index_of(".") < 0 ) {
248 tokens.push(new Token(found, "NAME", "NAME", this.line));
251 var n = found.split(".");
253 foreach (unowned string nm in n) {
255 tokens.push(new Token(".", "PUNC", "DOT", this.line));
258 tokens.push(new Token(nm, "NAME", "NAME", this.line));
266 @returns {Boolean} Was the token found?
268 public bool read_punc (TextStream stream, TokenArray tokens) throws TokenReader_Error
272 while (!stream.lookEOF() && Lang.punc(found + (string)stream.look()).length > 0) {
273 found += stream.next();
281 var ls = tokens.lastSym();
283 if ((found == "}" || found == "]") && ls != null && ls.data == ",") {
284 //print("Error - comma found before " + found);
285 //print(JSON.stringify(tokens.lastSym(), null,4));
286 if (this.ignoreBadGrammer) {
287 print("\n" + this.filename + ":" + this.line.to_string() + " Error - comma found before " + found);
289 throw new TokenReader_Error.ArgumentError(
290 this.filename + ":" + this.line.to_string() + " comma found before " + found
297 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
303 @returns {Boolean} Was the token found?
305 public bool read_space (TextStream stream, TokenArray tokens)
309 while (!stream.lookEOF() && Lang.isSpaceC( stream.look()) && !Lang.isNewlineC(stream.look())) {
310 found += stream.next();
316 //print("WHITE = " + JSON.stringify(found));
319 if (this.collapseWhite) {
320 found = " "; // this might work better if it was a '\n' ???
322 if (this.keepWhite) {
323 tokens.push(new Token(found, "WHIT", "SPACE", this.line));
330 @returns {Boolean} Was the token found?
332 public bool read_newline (TextStream stream, TokenArray tokens)
335 var line = this.line;
336 while (!stream.lookEOF() && Lang.isNewlineC(stream.look())) {
338 found += stream.next();
345 // if we found a new line, then we could check if previous character was a ';' - if so we can drop it.
346 // otherwise generally keep it.. in which case it should reduce our issue with stripping new lines..
350 if (this.collapseWhite) {
351 found = "\n"; // reduces multiple line breaks into a single one...
354 if (this.keepWhite) {
355 var last = tokens.pop();
356 if (last != null && last.name != "WHIT") {
359 // replaces last new line...
360 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
366 @returns {Boolean} Was the token found?
368 public bool read_mlcomment (TextStream stream, TokenArray tokens)
370 if (stream.look() != '/') {
373 if (stream.look(1) != '*') {
376 var found = stream.next(2);
378 var line = this.line;
379 while (!stream.lookEOF() && !(stream.look(-1) == '/' && stream.look(-2) == '*')) {
387 // to start doclet we allow /** or /*** but not /**/ or /****
388 //if (found.length /^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) {
389 if (this.keepDocs && found.length > 4 && found.index_of("/**") == 0 && found[3] != '/') {
390 tokens.push(new Token(found, "COMM", "JSDOC", this.line));
391 } else if (this.keepComments) {
392 tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
399 @returns {Boolean} Was the token found?
401 public bool read_slcomment (TextStream stream, TokenArray tokens)
405 (stream.look() == '/' && stream.look(1) == '/' && (""!=(found=stream.next(2))))
407 (stream.look() == '<' && stream.look(1) == '!' && stream.look(2) == '-' && stream.look(3) == '-' && (""!=(found=stream.next(4))))
409 var line = this.line;
410 while (!stream.lookEOF() && !Lang.isNewline((string)stream.look())) {
411 found += stream.next();
413 //if (!stream.lookEOF()) { // what? << eat the EOL?
414 found += stream.next();
416 if (this.keepComments) {
417 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
426 @returns {Boolean} Was the token found?
428 public bool read_dbquote (TextStream stream, TokenArray tokens)
430 if (stream.look() != '"') {
434 var str = stream.next();
436 while (!stream.lookEOF()) {
437 if (stream.look() == '\\') {
438 if (Lang.isNewline((string)stream.look(1))) {
441 } while (!stream.lookEOF() && Lang.isNewline((string)stream.look()));
445 str += stream.next(2);
449 if (stream.look() == '"') {
450 str += stream.next();
451 tokens.push(new Token(str, "STRN", "DOUBLE_QUOTE", this.line));
455 str += stream.next();
462 @returns {Boolean} Was the token found?
464 public bool read_snquote (TextStream stream, TokenArray tokens)
466 if (stream.look() != '\'') {
470 var str = stream.next();
472 while (!stream.lookEOF()) {
473 if (stream.look() == '\\') { // escape sequence
474 str += stream.next(2);
477 if (stream.look() == '\'') {
478 str += stream.next();
479 tokens.push(new Token(str, "STRN", "SINGLE_QUOTE", this.line));
482 str += stream.next();
490 @returns {Boolean} Was the token found?
492 public bool read_numb (TextStream stream, TokenArray tokens)
494 if (stream.look() == '0' && stream.look(1) == 'x') {
495 return this.read_hex(stream, tokens);
500 while (!stream.lookEOF() && Lang.isNumber(found+(string)stream.look())){
501 found += stream.next();
507 if (GLib.Regex.match_simple("^0[0-7]", found)) {
508 tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
511 tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
517 @returns {Boolean} Was the token found?
519 public bool read_hex (TextStream stream, TokenArray tokens)
521 var found = stream.next(2);
523 while (!stream.lookEOF()) {
524 if (Lang.isHexDec(found) && !Lang.isHexDec(found+(string)stream.look())) { // done
525 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
529 found += stream.next();
536 @returns {Boolean} Was the token found?
538 public bool read_regx (TextStream stream, TokenArray tokens)
541 if (stream.look() != '/') {
544 var last = tokens.lastSym();
549 !last.is("NUMB") // stuff that can not appear before a regex..
551 && !last.is("RIGHT_PAREN")
552 && !last.is("RIGHT_BRACKET")
555 var regex = stream.next();
557 while (!stream.lookEOF()) {
558 if (stream.look() == '\\') { // escape sequence
559 regex += stream.next(2);
562 if (stream.look() == '/') {
563 regex += stream.next();
565 while (GLib.Regex.match_simple("[gmi]", (string)stream.look())) {
566 regex += stream.next();
569 tokens.push(new Token(regex, "REGX", "REGX", this.line));
573 regex += stream.next();
576 // error: unterminated regex