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"));
91 tokens.push(new Token(nm, "NAME", "NAME"));
99 @returns {Boolean} Was the token found?
101 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
104 while (!stream.look().eof && Lang.punc(found+stream.look())) {
105 found += stream.next();
112 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
118 @returns {Boolean} Was the token found?
120 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
123 while (!stream.look().eof && Lang.isSpace(stream.look())) {
124 found += stream.next();
131 if (this.collapseWhite) found = " ";
132 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
138 @returns {Boolean} Was the token found?
140 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
143 while (!stream.look().eof && Lang.isNewline(stream.look())) {
144 found += stream.next();
151 if (this.collapseWhite) found = "\n";
152 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
158 @returns {Boolean} Was the token found?
160 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
161 if (stream.look() == "/" && stream.look(1) == "*") {
162 var found = stream.next(2);
164 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
165 found += stream.next();
168 // to start doclet we allow /** or /*** but not /**/ or /****
169 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
170 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
177 @returns {Boolean} Was the token found?
179 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
182 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
184 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
187 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
188 found += stream.next();
191 if (this.keepComments) {
192 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
200 @returns {Boolean} Was the token found?
202 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
203 if (stream.look() == "\"") {
205 var string = stream.next();
207 while (!stream.look().eof) {
208 if (stream.look() == "\\") {
209 if (Lang.isNewline(stream.look(1))) {
212 } while (!stream.look().eof && Lang.isNewline(stream.look()));
216 string += stream.next(2);
219 else if (stream.look() == "\"") {
220 string += stream.next();
221 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
225 string += stream.next();
229 return false; // error! unterminated string
233 @returns {Boolean} Was the token found?
235 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
236 if (stream.look() == "'") {
238 var string = stream.next();
240 while (!stream.look().eof) {
241 if (stream.look() == "\\") { // escape sequence
242 string += stream.next(2);
244 else if (stream.look() == "'") {
245 string += stream.next();
246 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
250 string += stream.next();
254 return false; // error! unterminated string
258 @returns {Boolean} Was the token found?
260 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
261 if (stream.look() === "0" && stream.look(1) == "x") {
262 return this.read_hex(stream, tokens);
267 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
268 found += stream.next();
275 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
276 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
281 requires("../lib/JSDOC/TextStream.js");
282 requires("../lib/JSDOC/Token.js");
283 requires("../lib/JSDOC/Lang.js");
285 plan(3, "testing read_numb");
288 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
289 var tr = new TokenReader();
290 var tokens = tr.tokenize(new TextStream(src));
292 var hexToken, octToken, decToken;
293 for (var i = 0; i < tokens.length; i++) {
294 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
295 if (tokens[i].name == "OCTAL") octToken = tokens[i];
296 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
300 is(decToken.data, "8.0", "decimal number is found in source.");
301 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
302 is(octToken.data, "0777", "octal number is found in source.");
306 @returns {Boolean} Was the token found?
308 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
309 var found = stream.next(2);
311 while (!stream.look().eof) {
312 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
313 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
317 found += stream.next();
324 @returns {Boolean} Was the token found?
326 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
334 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
339 && !last.is("RIGHT_PAREN")
340 && !last.is("RIGHT_BRACKET")
345 var regex = stream.next();
347 while (!stream.look().eof) {
348 if (stream.look() == "\\") { // escape sequence
349 regex += stream.next(2);
351 else if (stream.look() == "/") {
352 regex += stream.next();
354 while (/[gmi]/.test(stream.look())) {
355 regex += stream.next();
358 tokens.push(new Token(regex, "REGX", "REGX"));
362 regex += stream.next();
365 // error: unterminated regex