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) {
44 tokens.last = function() { return tokens[tokens.length-1]; }
46 tokens.lastSym = function() {
47 for (var i = tokens.length-1; i >= 0; i--) {
48 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
52 while (!stream.look().eof) {
53 if (this.read_mlcomment(stream, tokens)) continue;
54 if (this.read_slcomment(stream, tokens)) continue;
55 if (this.read_dbquote(stream, tokens)) continue;
56 if (this.read_snquote(stream, tokens)) continue;
57 if (this.read_regx(stream, tokens)) continue;
58 if (this.read_numb(stream, tokens)) continue;
59 if (this.read_punc(stream, tokens)) continue;
60 if (this.read_newline(stream, tokens)) continue;
61 if (this.read_space(stream, tokens)) continue;
62 if (this.read_word(stream, tokens)) continue;
64 // if execution reaches here then an error has happened
65 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN", this.line));
74 @returns {Boolean} Was the token found?
76 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
78 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
79 found += stream.next();
87 if ((name = Lang.keyword(found))) {
88 tokens.push(new Token(found, "KEYW", name, this.line));
91 if (!this.sepIdents || found.indexOf('.') < 0 ) {
92 tokens.push(new Token(found, "NAME", "NAME", this.line));
95 var n = found.split('.');
98 n.forEach(function(nm) {
100 tokens.push(new Token('.', "PUNC", "DOT", _this.line));
103 tokens.push(new Token(nm, "NAME", "NAME", _this.line));
111 @returns {Boolean} Was the token found?
113 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
116 while (!stream.look().eof && Lang.punc(found+stream.look())) {
117 found += stream.next();
125 if ((found == '}' || found == ']') && tokens.lastSym().data == ',') {
126 //print("Error - comma found before " + found);
127 //print(JSON.stringify(tokens.lastSym(), null,4));
129 name : "ArgumentError",
130 message: "Error - comma found before " + found + " on line " + this.line + "\n"
134 tokens.push(new Token(found, "PUNC", Lang.punc(found), this.line));
140 @returns {Boolean} Was the token found?
142 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
145 while (!stream.look().eof && Lang.isSpace(stream.look()) && !Lang.isNewline(stream.look())) {
146 found += stream.next();
152 //print("WHITE = " + JSON.stringify(found));
153 if (this.collapseWhite) found = " ";
154 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE", this.line));
160 @returns {Boolean} Was the token found?
162 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
164 var line = this.line;
165 while (!stream.look().eof && Lang.isNewline(stream.look())) {
167 found += stream.next();
174 if (this.collapseWhite) {
177 if (this.keepWhite) {
178 var last = tokens.pop();
179 if (last && last.name != "WHIT") {
183 tokens.push(new Token(found, "WHIT", "NEWLINE", line));
189 @returns {Boolean} Was the token found?
191 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
192 if (stream.look() == "/" && stream.look(1) == "*") {
193 var found = stream.next(2);
195 var line = this.line;
196 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
198 if (c == "\n") this.line++;
202 // to start doclet we allow /** or /*** but not /**/ or /****
203 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC", this.line));
204 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM", line));
211 @returns {Boolean} Was the token found?
213 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
216 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
218 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
220 var line = this.line;
221 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
222 found += stream.next();
224 if (!stream.look().eof) {
225 found += stream.next();
227 if (this.keepComments) {
228 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM", line));
237 @returns {Boolean} Was the token found?
239 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
240 if (stream.look() == "\"") {
242 var string = stream.next();
244 while (!stream.look().eof) {
245 if (stream.look() == "\\") {
246 if (Lang.isNewline(stream.look(1))) {
249 } while (!stream.look().eof && Lang.isNewline(stream.look()));
253 string += stream.next(2);
256 else if (stream.look() == "\"") {
257 string += stream.next();
258 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE", this.line));
262 string += stream.next();
266 return false; // error! unterminated string
270 @returns {Boolean} Was the token found?
272 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
273 if (stream.look() == "'") {
275 var string = stream.next();
277 while (!stream.look().eof) {
278 if (stream.look() == "\\") { // escape sequence
279 string += stream.next(2);
281 else if (stream.look() == "'") {
282 string += stream.next();
283 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE", this.line));
287 string += stream.next();
291 return false; // error! unterminated string
295 @returns {Boolean} Was the token found?
297 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
298 if (stream.look() === "0" && stream.look(1) == "x") {
299 return this.read_hex(stream, tokens);
304 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
305 found += stream.next();
312 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL", this.line));
313 else tokens.push(new Token(found, "NUMB", "DECIMAL", this.line));
318 requires("../lib/JSDOC/TextStream.js");
319 requires("../lib/JSDOC/Token.js");
320 requires("../lib/JSDOC/Lang.js");
322 plan(3, "testing read_numb");
325 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
326 var tr = new TokenReader();
327 var tokens = tr.tokenize(new TextStream(src));
329 var hexToken, octToken, decToken;
330 for (var i = 0; i < tokens.length; i++) {
331 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
332 if (tokens[i].name == "OCTAL") octToken = tokens[i];
333 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
337 is(decToken.data, "8.0", "decimal number is found in source.");
338 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
339 is(octToken.data, "0777", "octal number is found in source.");
343 @returns {Boolean} Was the token found?
345 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
346 var found = stream.next(2);
348 while (!stream.look().eof) {
349 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
350 tokens.push(new Token(found, "NUMB", "HEX_DEC", this.line));
354 found += stream.next();
361 @returns {Boolean} Was the token found?
363 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
371 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
376 && !last.is("RIGHT_PAREN")
377 && !last.is("RIGHT_BRACKET")
382 var regex = stream.next();
384 while (!stream.look().eof) {
385 if (stream.look() == "\\") { // escape sequence
386 regex += stream.next(2);
388 else if (stream.look() == "/") {
389 regex += stream.next();
391 while (/[gmi]/.test(stream.look())) {
392 regex += stream.next();
395 tokens.push(new Token(regex, "REGX", "REGX", this.line));
399 regex += stream.next();
402 // error: unterminated regex