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
38 tokenize : function(/**JSDOC.TextStream*/stream) {
41 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
42 /**@ignore*/ tokens.lastSym = function() {
43 for (var i = tokens.length-1; i >= 0; i--) {
44 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
48 while (!stream.look().eof) {
49 if (this.read_mlcomment(stream, tokens)) continue;
50 if (this.read_slcomment(stream, tokens)) continue;
51 if (this.read_dbquote(stream, tokens)) continue;
52 if (this.read_snquote(stream, tokens)) continue;
53 if (this.read_regx(stream, tokens)) continue;
54 if (this.read_numb(stream, tokens)) continue;
55 if (this.read_punc(stream, tokens)) continue;
56 if (this.read_newline(stream, tokens)) continue;
57 if (this.read_space(stream, tokens)) continue;
58 if (this.read_word(stream, tokens)) continue;
60 // if execution reaches here then an error has happened
61 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
70 @returns {Boolean} Was the token found?
72 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
74 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
75 found += stream.next();
83 if ((name = Lang.keyword(found))) {
84 tokens.push(new Token(found, "KEYW", name, this.line));
87 if (!this.sepIdents || found.indexOf('.') < 0 ) {
88 tokens.push(new Token(found, "NAME", "NAME", this.line));
91 var n = found.split('.');
93 n.forEach(function(nm) {
95 tokens.push(new Token('.', "PUNC", "DOT", this.line));
98 tokens.push(new Token(nm, "NAME", "NAME", this.line));
106 @returns {Boolean} Was the token found?
108 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
111 while (!stream.look().eof && Lang.punc(found+stream.look())) {
112 found += stream.next();
119 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
125 @returns {Boolean} Was the token found?
127 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
130 while (!stream.look().eof && Lang.isSpace(stream.look())) {
131 found += stream.next();
138 if (this.collapseWhite) found = " ";
139 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
145 @returns {Boolean} Was the token found?
147 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
150 while (!stream.look().eof && Lang.isNewline(stream.look())) {
152 found += stream.next();
159 if (this.collapseWhite) found = "\n";
160 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
166 @returns {Boolean} Was the token found?
168 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
169 if (stream.look() == "/" && stream.look(1) == "*") {
170 var found = stream.next(2);
172 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
174 if (c == "\n") this.line++;
178 // to start doclet we allow /** or /*** but not /**/ or /****
179 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
180 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
187 @returns {Boolean} Was the token found?
189 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
192 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
194 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
197 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
198 found += stream.next();
201 if (this.keepComments) {
202 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
211 @returns {Boolean} Was the token found?
213 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
214 if (stream.look() == "\"") {
216 var string = stream.next();
218 while (!stream.look().eof) {
219 if (stream.look() == "\\") {
220 if (Lang.isNewline(stream.look(1))) {
223 } while (!stream.look().eof && Lang.isNewline(stream.look()));
227 string += stream.next(2);
230 else if (stream.look() == "\"") {
231 string += stream.next();
232 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
236 string += stream.next();
240 return false; // error! unterminated string
244 @returns {Boolean} Was the token found?
246 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
247 if (stream.look() == "'") {
249 var string = stream.next();
251 while (!stream.look().eof) {
252 if (stream.look() == "\\") { // escape sequence
253 string += stream.next(2);
255 else if (stream.look() == "'") {
256 string += stream.next();
257 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
261 string += stream.next();
265 return false; // error! unterminated string
269 @returns {Boolean} Was the token found?
271 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
272 if (stream.look() === "0" && stream.look(1) == "x") {
273 return this.read_hex(stream, tokens);
278 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
279 found += stream.next();
286 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
287 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
292 requires("../lib/JSDOC/TextStream.js");
293 requires("../lib/JSDOC/Token.js");
294 requires("../lib/JSDOC/Lang.js");
296 plan(3, "testing read_numb");
299 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
300 var tr = new TokenReader();
301 var tokens = tr.tokenize(new TextStream(src));
303 var hexToken, octToken, decToken;
304 for (var i = 0; i < tokens.length; i++) {
305 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
306 if (tokens[i].name == "OCTAL") octToken = tokens[i];
307 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
311 is(decToken.data, "8.0", "decimal number is found in source.");
312 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
313 is(octToken.data, "0777", "octal number is found in source.");
317 @returns {Boolean} Was the token found?
319 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
320 var found = stream.next(2);
322 while (!stream.look().eof) {
323 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
324 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
328 found += stream.next();
335 @returns {Boolean} Was the token found?
337 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
345 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
350 && !last.is("RIGHT_PAREN")
351 && !last.is("RIGHT_BRACKET")
356 var regex = stream.next();
358 while (!stream.look().eof) {
359 if (stream.look() == "\\") { // escape sequence
360 regex += stream.next(2);
362 else if (stream.look() == "/") {
363 regex += stream.next();
365 while (/[gmi]/.test(stream.look())) {
366 regex += stream.next();
369 tokens.push(new Token(regex, "REGX", "REGX", this.line));
373 regex += stream.next();
376 // error: unterminated regex