1 //<script type="text/javascript">
3 imports['Object.js'].load(Object);
4 console = imports['console.js'].console;
6 JSDOC = imports['JSDOC.js'].JSDOC;
7 Token = imports['JSDOC/Token.js'].Token;
8 Lang = imports['JSDOC/Token.js'].Lang;
11 @class Search a {@link JSDOC.TextStream} for language tokens.
13 TokenReader = Object.define(
17 this.keepWhite = false;
18 this.keepComments = false;
19 Roo.apply(this, o || {});
31 tokenize : function(/**JSDOC.TextStream*/stream) {
33 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
34 /**@ignore*/ tokens.lastSym = function() {
35 for (var i = tokens.length-1; i >= 0; i--) {
36 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
40 while (!stream.look().eof) {
41 if (this.read_mlcomment(stream, tokens)) continue;
42 if (this.read_slcomment(stream, tokens)) continue;
43 if (this.read_dbquote(stream, tokens)) continue;
44 if (this.read_snquote(stream, tokens)) continue;
45 if (this.read_regx(stream, tokens)) continue;
46 if (this.read_numb(stream, tokens)) continue;
47 if (this.read_punc(stream, tokens)) continue;
48 if (this.read_newline(stream, tokens)) continue;
49 if (this.read_space(stream, tokens)) continue;
50 if (this.read_word(stream, tokens)) continue;
52 // if execution reaches here then an error has happened
53 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN"));
62 @returns {Boolean} Was the token found?
64 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
66 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
67 found += stream.next();
75 if ((name = Lang.keyword(found))) tokens.push(new Token(found, "KEYW", name));
76 else tokens.push(new Token(found, "NAME", "NAME"));
82 @returns {Boolean} Was the token found?
84 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
87 while (!stream.look().eof && Lang.punc(found+stream.look())) {
88 found += stream.next();
95 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
101 @returns {Boolean} Was the token found?
103 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
106 while (!stream.look().eof && Lang.isSpace(stream.look())) {
107 found += stream.next();
114 if (this.collapseWhite) found = " ";
115 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
121 @returns {Boolean} Was the token found?
123 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
126 while (!stream.look().eof && Lang.isNewline(stream.look())) {
127 found += stream.next();
134 if (this.collapseWhite) found = "\n";
135 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
141 @returns {Boolean} Was the token found?
143 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
144 if (stream.look() == "/" && stream.look(1) == "*") {
145 var found = stream.next(2);
147 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
148 found += stream.next();
151 // to start doclet we allow /** or /*** but not /**/ or /****
152 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
153 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
160 @returns {Boolean} Was the token found?
162 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
165 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
167 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
170 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
171 found += stream.next();
174 if (this.keepComments) {
175 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
183 @returns {Boolean} Was the token found?
185 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
186 if (stream.look() == "\"") {
188 var string = stream.next();
190 while (!stream.look().eof) {
191 if (stream.look() == "\\") {
192 if (Lang.isNewline(stream.look(1))) {
195 } while (!stream.look().eof && Lang.isNewline(stream.look()));
199 string += stream.next(2);
202 else if (stream.look() == "\"") {
203 string += stream.next();
204 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
208 string += stream.next();
212 return false; // error! unterminated string
216 @returns {Boolean} Was the token found?
218 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
219 if (stream.look() == "'") {
221 var string = stream.next();
223 while (!stream.look().eof) {
224 if (stream.look() == "\\") { // escape sequence
225 string += stream.next(2);
227 else if (stream.look() == "'") {
228 string += stream.next();
229 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
233 string += stream.next();
237 return false; // error! unterminated string
241 @returns {Boolean} Was the token found?
243 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
244 if (stream.look() === "0" && stream.look(1) == "x") {
245 return this.read_hex(stream, tokens);
250 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
251 found += stream.next();
258 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
259 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
264 requires("../lib/JSDOC/TextStream.js");
265 requires("../lib/JSDOC/Token.js");
266 requires("../lib/JSDOC/Lang.js");
268 plan(3, "testing read_numb");
271 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
272 var tr = new TokenReader();
273 var tokens = tr.tokenize(new TextStream(src));
275 var hexToken, octToken, decToken;
276 for (var i = 0; i < tokens.length; i++) {
277 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
278 if (tokens[i].name == "OCTAL") octToken = tokens[i];
279 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
283 is(decToken.data, "8.0", "decimal number is found in source.");
284 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
285 is(octToken.data, "0777", "octal number is found in source.");
289 @returns {Boolean} Was the token found?
291 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
292 var found = stream.next(2);
294 while (!stream.look().eof) {
295 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
296 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
300 found += stream.next();
307 @returns {Boolean} Was the token found?
309 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
317 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
322 && !last.is("RIGHT_PAREN")
323 && !last.is("RIGHT_BRACKET")
328 var regex = stream.next();
330 while (!stream.look().eof) {
331 if (stream.look() == "\\") { // escape sequence
332 regex += stream.next(2);
334 else if (stream.look() == "/") {
335 regex += stream.next();
337 while (/[gmi]/.test(stream.look())) {
338 regex += stream.next();
341 tokens.push(new Token(regex, "REGX", "REGX"));
345 regex += stream.next();
348 // error: unterminated regex