1 //<script type="text/javascript">
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 || {});
26 collapseWhite : false, // only reduces white space...
30 * @return {Array} of tokens
32 * ts = new TextStream(File.read(str));
33 * tr = TokenReader({ keepComments : true, keepWhite : true });
40 tokenize : function(/**JSDOC.TextStream*/stream) {
43 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
44 /**@ignore*/ tokens.lastSym = function() {
45 for (var i = tokens.length-1; i >= 0; i--) {
46 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
50 while (!stream.look().eof) {
51 if (this.read_mlcomment(stream, tokens)) continue;
52 if (this.read_slcomment(stream, tokens)) continue;
53 if (this.read_dbquote(stream, tokens)) continue;
54 if (this.read_snquote(stream, tokens)) continue;
55 if (this.read_regx(stream, tokens)) continue;
56 if (this.read_numb(stream, tokens)) continue;
57 if (this.read_punc(stream, tokens)) continue;
58 if (this.read_newline(stream, tokens)) continue;
59 if (this.read_space(stream, tokens)) continue;
60 if (this.read_word(stream, tokens)) continue;
62 // if execution reaches here then an error has happened
63 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
72 @returns {Boolean} Was the token found?
74 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
76 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
77 found += stream.next();
85 if ((name = Lang.keyword(found))) {
86 tokens.push(new Token(found, "KEYW", name, this.line));
89 if (!this.sepIdents || found.indexOf('.') < 0 ) {
90 tokens.push(new Token(found, "NAME", "NAME", this.line));
93 var n = found.split('.');
96 n.forEach(function(nm) {
98 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
101 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
109 @returns {Boolean} Was the token found?
111 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
114 while (!stream.look().eof && Lang.punc(found+stream.look())) {
115 found += stream.next();
122 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
128 @returns {Boolean} Was the token found?
130 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
133 while (!stream.look().eof && Lang.isSpace(stream.look())) {
134 found += stream.next();
141 if (this.collapseWhite) found = " ";
142 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
148 @returns {Boolean} Was the token found?
150 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
153 while (!stream.look().eof && Lang.isNewline(stream.look())) {
155 found += stream.next();
162 if (this.collapseWhite) {
165 if (this.keepWhite) {
166 var last = tokens.pop();
167 if (last.name != "WHIT") {
171 tokens.push(new Token(found, "WHIT", "NEWLINE", this.line));
177 @returns {Boolean} Was the token found?
179 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
180 if (stream.look() == "/" && stream.look(1) == "*") {
181 var found = stream.next(2);
183 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
185 if (c == "\n") this.line++;
189 // to start doclet we allow /** or /*** but not /**/ or /****
190 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
191 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", this.line));
198 @returns {Boolean} Was the token found?
200 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
203 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
205 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
208 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
209 found += stream.next();
212 if (this.keepComments) {
213 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", this.line));
222 @returns {Boolean} Was the token found?
224 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
225 if (stream.look() == "\"") {
227 var string = stream.next();
229 while (!stream.look().eof) {
230 if (stream.look() == "\\") {
231 if (Lang.isNewline(stream.look(1))) {
234 } while (!stream.look().eof && Lang.isNewline(stream.look()));
238 string += stream.next(2);
241 else if (stream.look() == "\"") {
242 string += stream.next();
243 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
247 string += stream.next();
251 return false; // error! unterminated string
255 @returns {Boolean} Was the token found?
257 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
258 if (stream.look() == "'") {
260 var string = stream.next();
262 while (!stream.look().eof) {
263 if (stream.look() == "\\") { // escape sequence
264 string += stream.next(2);
266 else if (stream.look() == "'") {
267 string += stream.next();
268 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
272 string += stream.next();
276 return false; // error! unterminated string
280 @returns {Boolean} Was the token found?
282 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
283 if (stream.look() === "0" && stream.look(1) == "x") {
284 return this.read_hex(stream, tokens);
289 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
290 found += stream.next();
297 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
298 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
303 requires("../lib/JSDOC/TextStream.js");
304 requires("../lib/JSDOC/Token.js");
305 requires("../lib/JSDOC/Lang.js");
307 plan(3, "testing read_numb");
310 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
311 var tr = new TokenReader();
312 var tokens = tr.tokenize(new TextStream(src));
314 var hexToken, octToken, decToken;
315 for (var i = 0; i < tokens.length; i++) {
316 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
317 if (tokens[i].name == "OCTAL") octToken = tokens[i];
318 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
322 is(decToken.data, "8.0", "decimal number is found in source.");
323 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
324 is(octToken.data, "0777", "octal number is found in source.");
328 @returns {Boolean} Was the token found?
330 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
331 var found = stream.next(2);
333 while (!stream.look().eof) {
334 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
335 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
339 found += stream.next();
346 @returns {Boolean} Was the token found?
348 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
356 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
361 && !last.is("RIGHT_PAREN")
362 && !last.is("RIGHT_BRACKET")
367 var regex = stream.next();
369 while (!stream.look().eof) {
370 if (stream.look() == "\\") { // escape sequence
371 regex += stream.next(2);
373 else if (stream.look() == "/") {
374 regex += stream.next();
376 while (/[gmi]/.test(stream.look())) {
377 regex += stream.next();
380 tokens.push(new Token(regex, "REGX", "REGX", this.line));
384 regex += stream.next();
387 // error: unterminated regex