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))) tokens.push(new Token(found, "KEYW", name));
78 else tokens.push(new Token(found, "NAME", "NAME"));
84 @returns {Boolean} Was the token found?
86 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
89 while (!stream.look().eof && Lang.punc(found+stream.look())) {
90 found += stream.next();
97 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
103 @returns {Boolean} Was the token found?
105 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
108 while (!stream.look().eof && Lang.isSpace(stream.look())) {
109 found += stream.next();
116 if (this.collapseWhite) found = " ";
117 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
123 @returns {Boolean} Was the token found?
125 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
128 while (!stream.look().eof && Lang.isNewline(stream.look())) {
129 found += stream.next();
136 if (this.collapseWhite) found = "\n";
137 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
143 @returns {Boolean} Was the token found?
145 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
146 if (stream.look() == "/" && stream.look(1) == "*") {
147 var found = stream.next(2);
149 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
150 found += stream.next();
153 // to start doclet we allow /** or /*** but not /**/ or /****
154 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
155 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
162 @returns {Boolean} Was the token found?
164 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
167 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
169 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
172 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
173 found += stream.next();
176 if (this.keepComments) {
177 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
185 @returns {Boolean} Was the token found?
187 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
188 if (stream.look() == "\"") {
190 var string = stream.next();
192 while (!stream.look().eof) {
193 if (stream.look() == "\\") {
194 if (Lang.isNewline(stream.look(1))) {
197 } while (!stream.look().eof && Lang.isNewline(stream.look()));
201 string += stream.next(2);
204 else if (stream.look() == "\"") {
205 string += stream.next();
206 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
210 string += stream.next();
214 return false; // error! unterminated string
218 @returns {Boolean} Was the token found?
220 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
221 if (stream.look() == "'") {
223 var string = stream.next();
225 while (!stream.look().eof) {
226 if (stream.look() == "\\") { // escape sequence
227 string += stream.next(2);
229 else if (stream.look() == "'") {
230 string += stream.next();
231 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
235 string += stream.next();
239 return false; // error! unterminated string
243 @returns {Boolean} Was the token found?
245 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
246 if (stream.look() === "0" && stream.look(1) == "x") {
247 return this.read_hex(stream, tokens);
252 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
253 found += stream.next();
260 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
261 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
266 requires("../lib/JSDOC/TextStream.js");
267 requires("../lib/JSDOC/Token.js");
268 requires("../lib/JSDOC/Lang.js");
270 plan(3, "testing read_numb");
273 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
274 var tr = new TokenReader();
275 var tokens = tr.tokenize(new TextStream(src));
277 var hexToken, octToken, decToken;
278 for (var i = 0; i < tokens.length; i++) {
279 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
280 if (tokens[i].name == "OCTAL") octToken = tokens[i];
281 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
285 is(decToken.data, "8.0", "decimal number is found in source.");
286 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
287 is(octToken.data, "0777", "octal number is found in source.");
291 @returns {Boolean} Was the token found?
293 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
294 var found = stream.next(2);
296 while (!stream.look().eof) {
297 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
298 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
302 found += stream.next();
309 @returns {Boolean} Was the token found?
311 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
319 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
324 && !last.is("RIGHT_PAREN")
325 && !last.is("RIGHT_BRACKET")
330 var regex = stream.next();
332 while (!stream.look().eof) {
333 if (stream.look() == "\\") { // escape sequence
334 regex += stream.next(2);
336 else if (stream.look() == "/") {
337 regex += stream.next();
339 while (/[gmi]/.test(stream.look())) {
340 regex += stream.next();
343 tokens.push(new Token(regex, "REGX", "REGX"));
347 regex += stream.next();
350 // error: unterminated regex