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();
33 * tr = TokenReader({ keepComments : true, keepWhite : true });
43 tokenize : function(/**JSDOC.TextStream*/stream) {
46 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
47 /**@ignore*/ tokens.lastSym = function() {
48 for (var i = tokens.length-1; i >= 0; i--) {
49 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
53 while (!stream.look().eof) {
54 if (this.read_mlcomment(stream, tokens)) continue;
55 if (this.read_slcomment(stream, tokens)) continue;
56 if (this.read_dbquote(stream, tokens)) continue;
57 if (this.read_snquote(stream, tokens)) continue;
58 if (this.read_regx(stream, tokens)) continue;
59 if (this.read_numb(stream, tokens)) continue;
60 if (this.read_punc(stream, tokens)) continue;
61 if (this.read_newline(stream, tokens)) continue;
62 if (this.read_space(stream, tokens)) continue;
63 if (this.read_word(stream, tokens)) continue;
65 // if execution reaches here then an error has happened
66 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
75 @returns {Boolean} Was the token found?
77 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
79 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
80 found += stream.next();
88 if ((name = Lang.keyword(found))) {
89 tokens.push(new Token(found, "KEYW", name, this.line));
92 if (!this.sepIdents || found.indexOf('.') < 0 ) {
93 tokens.push(new Token(found, "NAME", "NAME", this.line));
96 var n = found.split('.');
98 n.forEach(function(nm) {
100 tokens.push(new Token('.', "PUNC", "DOT", this.line));
103 tokens.push(new Token(nm, "NAME", "NAME", this.line));
111 @returns {Boolean} Was the token found?
113 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
116 while (!stream.look().eof && Lang.punc(found+stream.look())) {
117 found += stream.next();
124 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
130 @returns {Boolean} Was the token found?
132 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
135 while (!stream.look().eof && Lang.isSpace(stream.look())) {
136 found += stream.next();
143 if (this.collapseWhite) found = " ";
144 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
150 @returns {Boolean} Was the token found?
152 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
155 while (!stream.look().eof && Lang.isNewline(stream.look())) {
157 found += stream.next();
164 if (this.collapseWhite) found = "\n";
165 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
171 @returns {Boolean} Was the token found?
173 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
174 if (stream.look() == "/" && stream.look(1) == "*") {
175 var found = stream.next(2);
177 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
179 if (c == "\n") this.line++;
183 // to start doclet we allow /** or /*** but not /**/ or /****
184 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
185 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
192 @returns {Boolean} Was the token found?
194 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
197 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
199 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
202 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
203 found += stream.next();
206 if (this.keepComments) {
207 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
216 @returns {Boolean} Was the token found?
218 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
219 if (stream.look() == "\"") {
221 var string = stream.next();
223 while (!stream.look().eof) {
224 if (stream.look() == "\\") {
225 if (Lang.isNewline(stream.look(1))) {
228 } while (!stream.look().eof && Lang.isNewline(stream.look()));
232 string += stream.next(2);
235 else if (stream.look() == "\"") {
236 string += stream.next();
237 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
241 string += stream.next();
245 return false; // error! unterminated string
249 @returns {Boolean} Was the token found?
251 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
252 if (stream.look() == "'") {
254 var string = stream.next();
256 while (!stream.look().eof) {
257 if (stream.look() == "\\") { // escape sequence
258 string += stream.next(2);
260 else if (stream.look() == "'") {
261 string += stream.next();
262 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
266 string += stream.next();
270 return false; // error! unterminated string
274 @returns {Boolean} Was the token found?
276 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
277 if (stream.look() === "0" && stream.look(1) == "x") {
278 return this.read_hex(stream, tokens);
283 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
284 found += stream.next();
291 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
292 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
297 requires("../lib/JSDOC/TextStream.js");
298 requires("../lib/JSDOC/Token.js");
299 requires("../lib/JSDOC/Lang.js");
301 plan(3, "testing read_numb");
304 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
305 var tr = new TokenReader();
306 var tokens = tr.tokenize(new TextStream(src));
308 var hexToken, octToken, decToken;
309 for (var i = 0; i < tokens.length; i++) {
310 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
311 if (tokens[i].name == "OCTAL") octToken = tokens[i];
312 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
316 is(decToken.data, "8.0", "decimal number is found in source.");
317 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
318 is(octToken.data, "0777", "octal number is found in source.");
322 @returns {Boolean} Was the token found?
324 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
325 var found = stream.next(2);
327 while (!stream.look().eof) {
328 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
329 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
333 found += stream.next();
340 @returns {Boolean} Was the token found?
342 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
350 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
355 && !last.is("RIGHT_PAREN")
356 && !last.is("RIGHT_BRACKET")
361 var regex = stream.next();
363 while (!stream.look().eof) {
364 if (stream.look() == "\\") { // escape sequence
365 regex += stream.next(2);
367 else if (stream.look() == "/") {
368 regex += stream.next();
370 while (/[gmi]/.test(stream.look())) {
371 regex += stream.next();
374 tokens.push(new Token(regex, "REGX", "REGX", this.line));
378 regex += stream.next();
381 // error: unterminated regex