1 //<script type="text/javascript">
3 //imports['Object.js'].load(Object);
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 || {});
33 tokenize : function(/**JSDOC.TextStream*/stream) {
36 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
37 /**@ignore*/ tokens.lastSym = function() {
38 for (var i = tokens.length-1; i >= 0; i--) {
39 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
43 while (!stream.look().eof) {
44 if (this.read_mlcomment(stream, tokens)) continue;
45 if (this.read_slcomment(stream, tokens)) continue;
46 if (this.read_dbquote(stream, tokens)) continue;
47 if (this.read_snquote(stream, tokens)) continue;
48 if (this.read_regx(stream, tokens)) continue;
49 if (this.read_numb(stream, tokens)) continue;
50 if (this.read_punc(stream, tokens)) continue;
51 if (this.read_newline(stream, tokens)) continue;
52 if (this.read_space(stream, tokens)) continue;
53 if (this.read_word(stream, tokens)) continue;
55 // if execution reaches here then an error has happened
56 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
65 @returns {Boolean} Was the token found?
67 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
69 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
70 found += stream.next();
78 if ((name = Lang.keyword(found))) {
79 tokens.push(new Token(found, "KEYW", name, this.line));
82 if (!this.sepIdents || found.indexOf('.') < 0 ) {
83 tokens.push(new Token(found, "NAME", "NAME", this.line));
86 var n = found.split('.');
88 n.forEach(function(nm) {
90 tokens.push(new Token('.', "PUNC", "DOT", this.line));
93 tokens.push(new Token(nm, "NAME", "NAME", this.line));
101 @returns {Boolean} Was the token found?
103 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
106 while (!stream.look().eof && Lang.punc(found+stream.look())) {
107 found += stream.next();
114 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
120 @returns {Boolean} Was the token found?
122 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
125 while (!stream.look().eof && Lang.isSpace(stream.look())) {
126 found += stream.next();
133 if (this.collapseWhite) found = " ";
134 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
140 @returns {Boolean} Was the token found?
142 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
145 while (!stream.look().eof && Lang.isNewline(stream.look())) {
147 found += stream.next();
154 if (this.collapseWhite) found = "\n";
155 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
161 @returns {Boolean} Was the token found?
163 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
164 if (stream.look() == "/" && stream.look(1) == "*") {
165 var found = stream.next(2);
167 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
169 if (c == "\n") this.line++;
173 // to start doclet we allow /** or /*** but not /**/ or /****
174 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
175 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
182 @returns {Boolean} Was the token found?
184 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
187 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
189 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
192 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
193 found += stream.next();
196 if (this.keepComments) {
197 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
206 @returns {Boolean} Was the token found?
208 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
209 if (stream.look() == "\"") {
211 var string = stream.next();
213 while (!stream.look().eof) {
214 if (stream.look() == "\\") {
215 if (Lang.isNewline(stream.look(1))) {
218 } while (!stream.look().eof && Lang.isNewline(stream.look()));
222 string += stream.next(2);
225 else if (stream.look() == "\"") {
226 string += stream.next();
227 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
231 string += stream.next();
235 return false; // error! unterminated string
239 @returns {Boolean} Was the token found?
241 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
242 if (stream.look() == "'") {
244 var string = stream.next();
246 while (!stream.look().eof) {
247 if (stream.look() == "\\") { // escape sequence
248 string += stream.next(2);
250 else if (stream.look() == "'") {
251 string += stream.next();
252 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
256 string += stream.next();
260 return false; // error! unterminated string
264 @returns {Boolean} Was the token found?
266 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
267 if (stream.look() === "0" && stream.look(1) == "x") {
268 return this.read_hex(stream, tokens);
273 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
274 found += stream.next();
281 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
282 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
287 requires("../lib/JSDOC/TextStream.js");
288 requires("../lib/JSDOC/Token.js");
289 requires("../lib/JSDOC/Lang.js");
291 plan(3, "testing read_numb");
294 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
295 var tr = new TokenReader();
296 var tokens = tr.tokenize(new TextStream(src));
298 var hexToken, octToken, decToken;
299 for (var i = 0; i < tokens.length; i++) {
300 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
301 if (tokens[i].name == "OCTAL") octToken = tokens[i];
302 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
306 is(decToken.data, "8.0", "decimal number is found in source.");
307 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
308 is(octToken.data, "0777", "octal number is found in source.");
312 @returns {Boolean} Was the token found?
314 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
315 var found = stream.next(2);
317 while (!stream.look().eof) {
318 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
319 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
323 found += stream.next();
330 @returns {Boolean} Was the token found?
332 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
340 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
345 && !last.is("RIGHT_PAREN")
346 && !last.is("RIGHT_BRACKET")
351 var regex = stream.next();
353 while (!stream.look().eof) {
354 if (stream.look() == "\\") { // escape sequence
355 regex += stream.next(2);
357 else if (stream.look() == "/") {
358 regex += stream.next();
360 while (/[gmi]/.test(stream.look())) {
361 regex += stream.next();
364 tokens.push(new Token(regex, "REGX", "REGX", this.line));
368 regex += stream.next();
371 // error: unterminated regex