1 //<script type="text/javascript">
3 //imports['Object.js'].load(Object);
4 XObject = imports.XObject.XObject;
5 console = imports['console.js'].console;
7 JSDOC = imports['JSDOC.js'].JSDOC;
8 Token = imports['JSDOC/Token.js'].Token;
9 Lang = imports['JSDOC/Lang.js'].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 || {});
33 tokenize : function(/**JSDOC.TextStream*/stream) {
35 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
36 /**@ignore*/ tokens.lastSym = function() {
37 for (var i = tokens.length-1; i >= 0; i--) {
38 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
42 while (!stream.look().eof) {
43 if (this.read_mlcomment(stream, tokens)) continue;
44 if (this.read_slcomment(stream, tokens)) continue;
45 if (this.read_dbquote(stream, tokens)) continue;
46 if (this.read_snquote(stream, tokens)) continue;
47 if (this.read_regx(stream, tokens)) continue;
48 if (this.read_numb(stream, tokens)) continue;
49 if (this.read_punc(stream, tokens)) continue;
50 if (this.read_newline(stream, tokens)) continue;
51 if (this.read_space(stream, tokens)) continue;
52 if (this.read_word(stream, tokens)) continue;
54 // if execution reaches here then an error has happened
55 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN"));
64 @returns {Boolean} Was the token found?
66 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
68 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
69 found += stream.next();
77 if ((name = Lang.keyword(found))) {
78 tokens.push(new Token(found, "KEYW", name));
81 if (!this.sepIdents || found.indexOf('.') < 0 ) {
82 tokens.push(new Token(found, "NAME", "NAME"));
85 var n = found.split('.');
87 n.forEach(function(nm) {
89 tokens.push(new Token('.', "PUNC", "DOT"));
92 tokens.push(new Token(nm, "NAME", "NAME"));
100 @returns {Boolean} Was the token found?
102 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
105 while (!stream.look().eof && Lang.punc(found+stream.look())) {
106 found += stream.next();
113 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
119 @returns {Boolean} Was the token found?
121 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
124 while (!stream.look().eof && Lang.isSpace(stream.look())) {
125 found += stream.next();
132 if (this.collapseWhite) found = " ";
133 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
139 @returns {Boolean} Was the token found?
141 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
144 while (!stream.look().eof && Lang.isNewline(stream.look())) {
145 found += stream.next();
152 if (this.collapseWhite) found = "\n";
153 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
159 @returns {Boolean} Was the token found?
161 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
162 if (stream.look() == "/" && stream.look(1) == "*") {
163 var found = stream.next(2);
165 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
166 found += stream.next();
169 // to start doclet we allow /** or /*** but not /**/ or /****
170 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
171 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
178 @returns {Boolean} Was the token found?
180 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
183 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
185 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
188 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
189 found += stream.next();
192 if (this.keepComments) {
193 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
201 @returns {Boolean} Was the token found?
203 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
204 if (stream.look() == "\"") {
206 var string = stream.next();
208 while (!stream.look().eof) {
209 if (stream.look() == "\\") {
210 if (Lang.isNewline(stream.look(1))) {
213 } while (!stream.look().eof && Lang.isNewline(stream.look()));
217 string += stream.next(2);
220 else if (stream.look() == "\"") {
221 string += stream.next();
222 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
226 string += stream.next();
230 return false; // error! unterminated string
234 @returns {Boolean} Was the token found?
236 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
237 if (stream.look() == "'") {
239 var string = stream.next();
241 while (!stream.look().eof) {
242 if (stream.look() == "\\") { // escape sequence
243 string += stream.next(2);
245 else if (stream.look() == "'") {
246 string += stream.next();
247 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
251 string += stream.next();
255 return false; // error! unterminated string
259 @returns {Boolean} Was the token found?
261 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
262 if (stream.look() === "0" && stream.look(1) == "x") {
263 return this.read_hex(stream, tokens);
268 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
269 found += stream.next();
276 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
277 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
282 requires("../lib/JSDOC/TextStream.js");
283 requires("../lib/JSDOC/Token.js");
284 requires("../lib/JSDOC/Lang.js");
286 plan(3, "testing read_numb");
289 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
290 var tr = new TokenReader();
291 var tokens = tr.tokenize(new TextStream(src));
293 var hexToken, octToken, decToken;
294 for (var i = 0; i < tokens.length; i++) {
295 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
296 if (tokens[i].name == "OCTAL") octToken = tokens[i];
297 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
301 is(decToken.data, "8.0", "decimal number is found in source.");
302 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
303 is(octToken.data, "0777", "octal number is found in source.");
307 @returns {Boolean} Was the token found?
309 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
310 var found = stream.next(2);
312 while (!stream.look().eof) {
313 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
314 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
318 found += stream.next();
325 @returns {Boolean} Was the token found?
327 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
335 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
340 && !last.is("RIGHT_PAREN")
341 && !last.is("RIGHT_BRACKET")
346 var regex = stream.next();
348 while (!stream.look().eof) {
349 if (stream.look() == "\\") { // escape sequence
350 regex += stream.next(2);
352 else if (stream.look() == "/") {
353 regex += stream.next();
355 while (/[gmi]/.test(stream.look())) {
356 regex += stream.next();
359 tokens.push(new Token(regex, "REGX", "REGX"));
363 regex += stream.next();
366 // error: unterminated regex