1 //<script type="text/javascript">
2 JSDOC = imports['JSDOC.js'].JSDOC;
3 Roo = imports['Roo.js'].Roo;
4 console = imports['console.js'].console;
8 Token = imports['JSDOC/Token.js'].Token;
9 Lang = imports['JSDOC/Token.js'].Lang;
12 @class Search a {@link JSDOC.TextStream} for language tokens.
14 TokenReader = function(o) {
17 this.keepWhite = false;
18 this.keepComments = false;
19 Roo.apply(this, o || {});
24 Roo.apply(TokenReader.prototype, {
32 tokenize : function(/**JSDOC.TextStream*/stream) {
34 /**@ignore*/ tokens.last = function() { return tokens[tokens.length-1]; }
35 /**@ignore*/ tokens.lastSym = function() {
36 for (var i = tokens.length-1; i >= 0; i--) {
37 if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
41 while (!stream.look().eof) {
42 if (this.read_mlcomment(stream, tokens)) continue;
43 if (this.read_slcomment(stream, tokens)) continue;
44 if (this.read_dbquote(stream, tokens)) continue;
45 if (this.read_snquote(stream, tokens)) continue;
46 if (this.read_regx(stream, tokens)) continue;
47 if (this.read_numb(stream, tokens)) continue;
48 if (this.read_punc(stream, tokens)) continue;
49 if (this.read_newline(stream, tokens)) continue;
50 if (this.read_space(stream, tokens)) continue;
51 if (this.read_word(stream, tokens)) continue;
53 // if execution reaches here then an error has happened
54 tokens.push(new Token(stream.next(), "TOKN", "UNKNOWN_TOKEN"));
63 @returns {Boolean} Was the token found?
65 read_word : function(/**JSDOC.TokenStream*/stream, tokens) {
67 while (!stream.look().eof && Lang.isWordChar(stream.look())) {
68 found += stream.next();
76 if ((name = Lang.keyword(found))) tokens.push(new Token(found, "KEYW", name));
77 else tokens.push(new Token(found, "NAME", "NAME"));
83 @returns {Boolean} Was the token found?
85 read_punc : function(/**JSDOC.TokenStream*/stream, tokens) {
88 while (!stream.look().eof && Lang.punc(found+stream.look())) {
89 found += stream.next();
96 tokens.push(new Token(found, "PUNC", Lang.punc(found)));
102 @returns {Boolean} Was the token found?
104 read_space : function(/**JSDOC.TokenStream*/stream, tokens) {
107 while (!stream.look().eof && Lang.isSpace(stream.look())) {
108 found += stream.next();
115 if (this.collapseWhite) found = " ";
116 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "SPACE"));
122 @returns {Boolean} Was the token found?
124 read_newline : function(/**JSDOC.TokenStream*/stream, tokens) {
127 while (!stream.look().eof && Lang.isNewline(stream.look())) {
128 found += stream.next();
135 if (this.collapseWhite) found = "\n";
136 if (this.keepWhite) tokens.push(new Token(found, "WHIT", "NEWLINE"));
142 @returns {Boolean} Was the token found?
144 read_mlcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
145 if (stream.look() == "/" && stream.look(1) == "*") {
146 var found = stream.next(2);
148 while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
149 found += stream.next();
152 // to start doclet we allow /** or /*** but not /**/ or /****
153 if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new Token(found, "COMM", "JSDOC"));
154 else if (this.keepComments) tokens.push(new Token(found, "COMM", "MULTI_LINE_COMM"));
161 @returns {Boolean} Was the token found?
163 read_slcomment : function(/**JSDOC.TokenStream*/stream, tokens) {
166 (stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
168 (stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
171 while (!stream.look().eof && !Lang.isNewline(stream.look())) {
172 found += stream.next();
175 if (this.keepComments) {
176 tokens.push(new Token(found, "COMM", "SINGLE_LINE_COMM"));
184 @returns {Boolean} Was the token found?
186 read_dbquote : function(/**JSDOC.TokenStream*/stream, tokens) {
187 if (stream.look() == "\"") {
189 var string = stream.next();
191 while (!stream.look().eof) {
192 if (stream.look() == "\\") {
193 if (Lang.isNewline(stream.look(1))) {
196 } while (!stream.look().eof && Lang.isNewline(stream.look()));
200 string += stream.next(2);
203 else if (stream.look() == "\"") {
204 string += stream.next();
205 tokens.push(new Token(string, "STRN", "DOUBLE_QUOTE"));
209 string += stream.next();
213 return false; // error! unterminated string
217 @returns {Boolean} Was the token found?
219 read_snquote : function(/**JSDOC.TokenStream*/stream, tokens) {
220 if (stream.look() == "'") {
222 var string = stream.next();
224 while (!stream.look().eof) {
225 if (stream.look() == "\\") { // escape sequence
226 string += stream.next(2);
228 else if (stream.look() == "'") {
229 string += stream.next();
230 tokens.push(new Token(string, "STRN", "SINGLE_QUOTE"));
234 string += stream.next();
238 return false; // error! unterminated string
242 @returns {Boolean} Was the token found?
244 read_numb : function(/**JSDOC.TokenStream*/stream, tokens) {
245 if (stream.look() === "0" && stream.look(1) == "x") {
246 return this.read_hex(stream, tokens);
251 while (!stream.look().eof && Lang.isNumber(found+stream.look())){
252 found += stream.next();
259 if (/^0[0-7]/.test(found)) tokens.push(new Token(found, "NUMB", "OCTAL"));
260 else tokens.push(new Token(found, "NUMB", "DECIMAL"));
265 requires("../lib/JSDOC/TextStream.js");
266 requires("../lib/JSDOC/Token.js");
267 requires("../lib/JSDOC/Lang.js");
269 plan(3, "testing read_numb");
272 var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
273 var tr = new TokenReader();
274 var tokens = tr.tokenize(new TextStream(src));
276 var hexToken, octToken, decToken;
277 for (var i = 0; i < tokens.length; i++) {
278 if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
279 if (tokens[i].name == "OCTAL") octToken = tokens[i];
280 if (tokens[i].name == "DECIMAL") decToken = tokens[i];
284 is(decToken.data, "8.0", "decimal number is found in source.");
285 is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
286 is(octToken.data, "0777", "octal number is found in source.");
290 @returns {Boolean} Was the token found?
292 read_hex : function(/**JSDOC.TokenStream*/stream, tokens) {
293 var found = stream.next(2);
295 while (!stream.look().eof) {
296 if (Lang.isHexDec(found) && !Lang.isHexDec(found+stream.look())) { // done
297 tokens.push(new Token(found, "NUMB", "HEX_DEC"));
301 found += stream.next();
308 @returns {Boolean} Was the token found?
310 read_regx : function(/**JSDOC.TokenStream*/stream, tokens) {
318 !(last = tokens.lastSym()) // there is no last, the regex is the first symbol
323 && !last.is("RIGHT_PAREN")
324 && !last.is("RIGHT_BRACKET")
329 var regex = stream.next();
331 while (!stream.look().eof) {
332 if (stream.look() == "\\") { // escape sequence
333 regex += stream.next(2);
335 else if (stream.look() == "/") {
336 regex += stream.next();
338 while (/[gmi]/.test(stream.look())) {
339 regex += stream.next();
342 tokens.push(new Token(regex, "REGX", "REGX"));
346 regex += stream.next();
349 // error: unterminated regex