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(
18 this.keepWhite = false;
19 this.keepComments = false;
20 this.sepIdents = false; // seperate '.' in identifiers..
21 XObject.extend(this, o || {});
30 * @return {Array} of tokens
32 * ts = new TextStream(File.read(str));
33 * tr = TokenReader({ keepComments : true, keepWhite : true });
40 tokenize : function(/**JSDOC.TextStream*/stream) {
43 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
44 /**@ignore*/ tokens.lastSym = function() {
45 for (var i = tokens.length-1; i >= 0; i--) {
46 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
50 while (!stream.look().eof) {
51 if (this.read_mlcomment(stream, tokens)) continue;
52 if (this.read_slcomment(stream, tokens)) continue;
53 if (this.read_dbquote(stream, tokens)) continue;
54 if (this.read_snquote(stream, tokens)) continue;
55 if (this.read_regx(stream, tokens)) continue;
56 if (this.read_numb(stream, tokens)) continue;
57 if (this.read_punc(stream, tokens)) continue;
58 if (this.read_newline(stream, tokens)) continue;
59 if (this.read_space(stream, tokens)) continue;
60 if (this.read_word(stream, tokens)) continue;
62 // if execution reaches here then an error has happened
63 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
72 @returns {Boolean} Was the token found?
74 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
76 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
77 found += stream.next();
85 if ((name = Lang.keyword(found))) {
86 tokens.push(new Token(found, "KEYW", name, this.line));
89 if (!this.sepIdents || found.indexOf('.') < 0 ) {
90 tokens.push(new Token(found, "NAME", "NAME", this.line));
93 var n = found.split('.');
95 n.forEach(function(nm) {
97 tokens.push(new Token('.', "PUNC", "DOT", this.line));
100 tokens.push(new Token(nm, "NAME", "NAME", this.line));
108 @returns {Boolean} Was the token found?
110 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
113 while (!stream.look().eof && Lang.punc(found+stream.look())) {
114 found += stream.next();
121 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
127 @returns {Boolean} Was the token found?
129 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
132 while (!stream.look().eof && Lang.isSpace(stream.look())) {
133 found += stream.next();
140 if (this.collapseWhite) found = " ";
141 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
147 @returns {Boolean} Was the token found?
149 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
152 while (!stream.look().eof && Lang.isNewline(stream.look())) {
154 found += stream.next();
161 if (this.collapseWhite) found = "\n";
162 if (this.keepWhite) {
163 tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
170 @returns {Boolean} Was the token found?
172 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
173 if (stream.look() == "/" && stream.look(1) == "*") {
174 var found = stream.next(2);
176 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
178 if (c == "\n") this.line++;
182 // to start doclet we allow /** or /*** but not /**/ or /****
183 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
184 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
191 @returns {Boolean} Was the token found?
193 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
196 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
198 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
201 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
202 found += stream.next();
205 if (this.keepComments) {
206 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
215 @returns {Boolean} Was the token found?
217 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
218 if (stream.look() == "\"") {
220 var string = stream.next();
222 while (!stream.look().eof) {
223 if (stream.look() == "\\") {
224 if (Lang.isNewline(stream.look(1))) {
227 } while (!stream.look().eof && Lang.isNewline(stream.look()));
231 string += stream.next(2);
234 else if (stream.look() == "\"") {
235 string += stream.next();
236 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
240 string += stream.next();
244 return false; // error! unterminated string
248 @returns {Boolean} Was the token found?
250 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
251 if (stream.look() == "'") {
253 var string = stream.next();
255 while (!stream.look().eof) {
256 if (stream.look() == "\\") { // escape sequence
257 string += stream.next(2);
259 else if (stream.look() == "'") {
260 string += stream.next();
261 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
265 string += stream.next();
269 return false; // error! unterminated string
273 @returns {Boolean} Was the token found?
275 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
276 if (stream.look() === "0" && stream.look(1) == "x") {
277 return this.read_hex(stream, tokens);
282 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
283 found += stream.next();
290 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
291 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
296 requires("../lib/JSDOC/TextStream.js");
297 requires("../lib/JSDOC/Token.js");
298 requires("../lib/JSDOC/Lang.js");
300 plan(3, "testing read_numb");
303 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
304 var tr = new TokenReader();
305 var tokens = tr.tokenize(new TextStream(src));
307 var hexToken, octToken, decToken;
308 for (var i = 0; i < tokens.length; i++) {
309 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
310 if (tokens[i].name == "OCTAL") octToken = tokens[i];
311 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
315 is(decToken.data, "8.0", "decimal number is found in source.");
316 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
317 is(octToken.data, "0777", "octal number is found in source.");
321 @returns {Boolean} Was the token found?
323 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
324 var found = stream.next(2);
326 while (!stream.look().eof) {
327 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
328 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
332 found += stream.next();
339 @returns {Boolean} Was the token found?
341 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
349 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
354 && !last.is("RIGHT_PAREN")
355 && !last.is("RIGHT_BRACKET")
360 var regex = stream.next();
362 while (!stream.look().eof) {
363 if (stream.look() == "\\") { // escape sequence
364 regex += stream.next(2);
366 else if (stream.look() == "/") {
367 regex += stream.next();
369 while (/[gmi]/.test(stream.look())) {
370 regex += stream.next();
373 tokens.push(new Token(regex, "REGX", "REGX", this.line));
377 regex += stream.next();
380 // error: unterminated regex