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 (found.indexOf('.') < 0 || !this.sepIdents) {
82 tokens.push(new Token(found, "NAME", "NAME"));
85 var n = found.split('.');
86 n.forEach(function(nm) {
87 tokens.push(new Token(nm, "NAME", "NAME"));
95 @returns {Boolean} Was the token found?
97 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
100 while (!stream.look().eof && Lang.punc(found+stream.look())) {
101 found += stream.next();
108 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
114 @returns {Boolean} Was the token found?
116 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
119 while (!stream.look().eof && Lang.isSpace(stream.look())) {
120 found += stream.next();
127 if (this.collapseWhite) found = " ";
128 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
134 @returns {Boolean} Was the token found?
136 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
139 while (!stream.look().eof && Lang.isNewline(stream.look())) {
140 found += stream.next();
147 if (this.collapseWhite) found = "\n";
148 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
154 @returns {Boolean} Was the token found?
156 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
157 if (stream.look() == "/" && stream.look(1) == "*") {
158 var found = stream.next(2);
160 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
161 found += stream.next();
164 // to start doclet we allow /** or /*** but not /**/ or /****
165 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
166 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
173 @returns {Boolean} Was the token found?
175 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
178 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
180 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
183 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
184 found += stream.next();
187 if (this.keepComments) {
188 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
196 @returns {Boolean} Was the token found?
198 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
199 if (stream.look() == "\"") {
201 var string = stream.next();
203 while (!stream.look().eof) {
204 if (stream.look() == "\\") {
205 if (Lang.isNewline(stream.look(1))) {
208 } while (!stream.look().eof && Lang.isNewline(stream.look()));
212 string += stream.next(2);
215 else if (stream.look() == "\"") {
216 string += stream.next();
217 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
221 string += stream.next();
225 return false; // error! unterminated string
229 @returns {Boolean} Was the token found?
231 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
232 if (stream.look() == "'") {
234 var string = stream.next();
236 while (!stream.look().eof) {
237 if (stream.look() == "\\") { // escape sequence
238 string += stream.next(2);
240 else if (stream.look() == "'") {
241 string += stream.next();
242 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
246 string += stream.next();
250 return false; // error! unterminated string
254 @returns {Boolean} Was the token found?
256 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
257 if (stream.look() === "0" && stream.look(1) == "x") {
258 return this.read_hex(stream, tokens);
263 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
264 found += stream.next();
271 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
272 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
277 requires("../lib/JSDOC/TextStream.js");
278 requires("../lib/JSDOC/Token.js");
279 requires("../lib/JSDOC/Lang.js");
281 plan(3, "testing read_numb");
284 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
285 var tr = new TokenReader();
286 var tokens = tr.tokenize(new TextStream(src));
288 var hexToken, octToken, decToken;
289 for (var i = 0; i < tokens.length; i++) {
290 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
291 if (tokens[i].name == "OCTAL") octToken = tokens[i];
292 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
296 is(decToken.data, "8.0", "decimal number is found in source.");
297 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
298 is(octToken.data, "0777", "octal number is found in source.");
302 @returns {Boolean} Was the token found?
304 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
305 var found = stream.next(2);
307 while (!stream.look().eof) {
308 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
309 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
313 found += stream.next();
320 @returns {Boolean} Was the token found?
322 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
330 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
335 && !last.is("RIGHT_PAREN")
336 && !last.is("RIGHT_BRACKET")
341 var regex = stream.next();
343 while (!stream.look().eof) {
344 if (stream.look() == "\\") { // escape sequence
345 regex += stream.next(2);
347 else if (stream.look() == "/") {
348 regex += stream.next();
350 while (/[gmi]/.test(stream.look())) {
351 regex += stream.next();
354 tokens.push(new Token(regex, "REGX", "REGX"));
358 regex += stream.next();
361 // error: unterminated regex