1 //<script type="text/javascript">
4 XObject = imports.XObject.XObject;
5 console = imports.console.console;
8 Token = imports.Token.Token;
9 Lang = imports.Lang.Lang;
12 @class Search a {@link JSDOC.TextStream} for language tokens.
14 TokenReader = XObject.define(
17 XObject.extend(this, o || {});
22 /** @cfg {Boolean} collapseWhite merge multiple whitespace/comments into a single token **/
23 collapseWhite : false, // only reduces white space...
24 /** @cfg {Boolean} keepDocs keep JSDOC comments **/
26 /** @cfg {Boolean} keepWhite keep White space **/
28 /** @cfg {Boolean} keepComments keep all comments **/
30 /** @cfg {Boolean} sepIdents seperate identifiers (eg. a.b.c into ['a', '.', 'b', '.', 'c'] ) **/
32 /** @cfg {String} filename name of file being parsed. **/
37 * @return {Array} of tokens
39 * ts = new TextStream(File.read(str));
40 * tr = TokenReader({ keepComments : true, keepWhite : true });
47 tokenize : function(/**JSDOC.TextStream*/stream) {
51 tokens.last = function() { return tokens[tokens.length-1]; }
53 tokens.lastSym = function() {
54 for (var i = tokens.length-1; i >= 0; i--) {
55 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
59 while (!stream.look().eof) {
60 if (this.read_mlcomment(stream, tokens)) continue;
61 if (this.read_slcomment(stream, tokens)) continue;
62 if (this.read_dbquote(stream, tokens)) continue;
63 if (this.read_snquote(stream, tokens)) continue;
64 if (this.read_regx(stream, tokens)) continue;
65 if (this.read_numb(stream, tokens)) continue;
66 if (this.read_punc(stream, tokens)) continue;
67 if (this.read_newline(stream, tokens)) continue;
68 if (this.read_space(stream, tokens)) continue;
69 if (this.read_word(stream, tokens)) continue;
71 // if execution reaches here then an error has happened
72 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
81 @returns {Boolean} Was the token found?
83 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
85 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
86 found += stream.next();
94 if ((name = Lang.keyword(found))) {
95 if (found == 'return' && tokens.lastSym().data == ')') {
97 name : "ArgumentError",
98 message: "\n" + this.filename + ':' + this.line + " Error - return found after )"
102 tokens.push(new Token(found, "KEYW", name, this.line));
105 if (!this.sepIdents || found.indexOf('.') < 0 ) {
106 tokens.push(new Token(found, "NAME", "NAME", this.line));
109 var n = found.split('.');
112 n.forEach(function(nm) {
114 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
117 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
125 @returns {Boolean} Was the token found?
127 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
130 while (!stream.look().eof && Lang.punc(found+stream.look())) {
131 found += stream.next();
139 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
140 //print("Error - comma found before " + found);
141 //print(JSON.stringify(tokens.lastSym(), null,4));
143 name : "ArgumentError",
144 message: "\n" + this.filename + ':' + this.line + " Error - comma found before " + found
148 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
154 @returns {Boolean} Was the token found?
156 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
159 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
160 found += stream.next();
166 //print("WHITE = " + JSON.stringify(found));
167 if (this.collapseWhite) found = " ";
168 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
174 @returns {Boolean} Was the token found?
176 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
178 var line = this.line;
179 while (!stream.look().eof && Lang.isNewline(stream.look())) {
181 found += stream.next();
188 if (this.collapseWhite) {
191 if (this.keepWhite) {
192 var last = tokens.pop();
193 if (last && last.name != "WHIT") {
197 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
203 @returns {Boolean} Was the token found?
205 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
206 if (stream.look() == "/" && stream.look(1) == "*") {
207 var found = stream.next(2);
209 var line = this.line;
210 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
212 if (c == "\n") this.line++;
216 // to start doclet we allow /** or /*** but not /**/ or /****
217 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
218 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
225 @returns {Boolean} Was the token found?
227 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
230 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
232 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
234 var line = this.line;
235 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
236 found += stream.next();
238 if (!stream.look().eof) {
239 found += stream.next();
241 if (this.keepComments) {
242 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
251 @returns {Boolean} Was the token found?
253 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
254 if (stream.look() == "\"") {
256 var string = stream.next();
258 while (!stream.look().eof) {
259 if (stream.look() == "\\") {
260 if (Lang.isNewline(stream.look(1))) {
263 } while (!stream.look().eof && Lang.isNewline(stream.look()));
267 string += stream.next(2);
270 else if (stream.look() == "\"") {
271 string += stream.next();
272 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
276 string += stream.next();
280 return false; // error! unterminated string
284 @returns {Boolean} Was the token found?
286 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
287 if (stream.look() == "'") {
289 var string = stream.next();
291 while (!stream.look().eof) {
292 if (stream.look() == "\\") { // escape sequence
293 string += stream.next(2);
295 else if (stream.look() == "'") {
296 string += stream.next();
297 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
301 string += stream.next();
305 return false; // error! unterminated string
309 @returns {Boolean} Was the token found?
311 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
312 if (stream.look() === "0" && stream.look(1) == "x") {
313 return this.read_hex(stream, tokens);
318 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
319 found += stream.next();
326 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
327 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
332 requires("../lib/JSDOC/TextStream.js");
333 requires("../lib/JSDOC/Token.js");
334 requires("../lib/JSDOC/Lang.js");
336 plan(3, "testing read_numb");
339 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
340 var tr = new TokenReader();
341 var tokens = tr.tokenize(new TextStream(src));
343 var hexToken, octToken, decToken;
344 for (var i = 0; i < tokens.length; i++) {
345 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
346 if (tokens[i].name == "OCTAL") octToken = tokens[i];
347 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
351 is(decToken.data, "8.0", "decimal number is found in source.");
352 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
353 is(octToken.data, "0777", "octal number is found in source.");
357 @returns {Boolean} Was the token found?
359 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
360 var found = stream.next(2);
362 while (!stream.look().eof) {
363 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
364 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
368 found += stream.next();
375 @returns {Boolean} Was the token found?
377 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
385 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
390 && !last.is("RIGHT_PAREN")
391 && !last.is("RIGHT_BRACKET")
396 var regex = stream.next();
398 while (!stream.look().eof) {
399 if (stream.look() == "\\") { // escape sequence
400 regex += stream.next(2);
402 else if (stream.look() == "/") {
403 regex += stream.next();
405 while (/[gmi]/.test(stream.look())) {
406 regex += stream.next();
409 tokens.push(new Token(regex, "REGX", "REGX", this.line));
413 regex += stream.next();
416 // error: unterminated regex