PageRenderTime 66ms CodeModel.GetById 16ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/jsdoc-toolkit/app/lib/JSDOC/TokenReader.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 340 lines | 246 code | 31 blank | 63 comment | 100 complexity | 89787d93a70ee6f1e6034c96bc7eda74 MD5 | raw file
  1if (typeof JSDOC == "undefined") JSDOC = {};
  2
  3/**
  4	@class Search a {@link JSDOC.TextStream} for language tokens.
  5*/
  6JSDOC.TokenReader = function() {
  7	this.keepDocs = true;
  8	this.keepWhite = false;
  9	this.keepComments = false;
 10}
 11
 12/**
 13	@type {JSDOC.Token[]}
 14 */
 15JSDOC.TokenReader.prototype.tokenize = function(/**JSDOC.TextStream*/stream) {
 16	var tokens = [];
 17	/**@ignore*/ tokens.last    = function() { return tokens[tokens.length-1]; }
 18	/**@ignore*/ tokens.lastSym = function() {
 19		for (var i = tokens.length-1; i >= 0; i--) {
 20			if (!(tokens[i].is("WHIT") || tokens[i].is("COMM"))) return tokens[i];
 21		}
 22	}
 23
 24	while (!stream.look().eof) {
 25		if (this.read_mlcomment(stream, tokens)) continue;
 26		if (this.read_slcomment(stream, tokens)) continue;
 27		if (this.read_dbquote(stream, tokens))   continue;
 28		if (this.read_snquote(stream, tokens))   continue;
 29		if (this.read_regx(stream, tokens))      continue;
 30		if (this.read_numb(stream, tokens))      continue;
 31		if (this.read_punc(stream, tokens))      continue;
 32		if (this.read_newline(stream, tokens))   continue;
 33		if (this.read_space(stream, tokens))     continue;
 34		if (this.read_word(stream, tokens))      continue;
 35		
 36		// if execution reaches here then an error has happened
 37		tokens.push(new JSDOC.Token(stream.next(), "TOKN", "UNKNOWN_TOKEN"));
 38	}
 39	return tokens;
 40}
 41
 42/**
 43	@returns {Boolean} Was the token found?
 44 */
 45JSDOC.TokenReader.prototype.read_word = function(/**JSDOC.TokenStream*/stream, tokens) {
 46	var found = "";
 47	while (!stream.look().eof && JSDOC.Lang.isWordChar(stream.look())) {
 48		found += stream.next();
 49	}
 50	
 51	if (found === "") {
 52		return false;
 53	}
 54	else {
 55		var name;
 56		if ((name = JSDOC.Lang.keyword(found))) tokens.push(new JSDOC.Token(found, "KEYW", name));
 57		else tokens.push(new JSDOC.Token(found, "NAME", "NAME"));
 58		return true;
 59	}
 60}
 61
 62/**
 63	@returns {Boolean} Was the token found?
 64 */
 65JSDOC.TokenReader.prototype.read_punc = function(/**JSDOC.TokenStream*/stream, tokens) {
 66	var found = "";
 67	var name;
 68	while (!stream.look().eof && JSDOC.Lang.punc(found+stream.look())) {
 69		found += stream.next();
 70	}
 71	
 72	if (found === "") {
 73		return false;
 74	}
 75	else {
 76		tokens.push(new JSDOC.Token(found, "PUNC", JSDOC.Lang.punc(found)));
 77		return true;
 78	}
 79}
 80
 81/**
 82	@returns {Boolean} Was the token found?
 83 */
 84JSDOC.TokenReader.prototype.read_space = function(/**JSDOC.TokenStream*/stream, tokens) {
 85	var found = "";
 86	
 87	while (!stream.look().eof && JSDOC.Lang.isSpace(stream.look())) {
 88		found += stream.next();
 89	}
 90	
 91	if (found === "") {
 92		return false;
 93	}
 94	else {
 95		if (this.collapseWhite) found = " ";
 96		if (this.keepWhite) tokens.push(new JSDOC.Token(found, "WHIT", "SPACE"));
 97		return true;
 98	}
 99}
100
101/**
102	@returns {Boolean} Was the token found?
103 */
104JSDOC.TokenReader.prototype.read_newline = function(/**JSDOC.TokenStream*/stream, tokens) {
105	var found = "";
106	
107	while (!stream.look().eof && JSDOC.Lang.isNewline(stream.look())) {
108		found += stream.next();
109	}
110	
111	if (found === "") {
112		return false;
113	}
114	else {
115		if (this.collapseWhite) found = "\n";
116		if (this.keepWhite) tokens.push(new JSDOC.Token(found, "WHIT", "NEWLINE"));
117		return true;
118	}
119}
120
121/**
122	@returns {Boolean} Was the token found?
123 */
124JSDOC.TokenReader.prototype.read_mlcomment = function(/**JSDOC.TokenStream*/stream, tokens) {
125	if (stream.look() == "/" && stream.look(1) == "*") {
126		var found = stream.next(2);
127		
128		while (!stream.look().eof && !(stream.look(-1) == "/" && stream.look(-2) == "*")) {
129			found += stream.next();
130		}
131		
132		// to start doclet we allow /** or /*** but not /**/ or /****
133		if (/^\/\*\*([^\/]|\*[^*])/.test(found) && this.keepDocs) tokens.push(new JSDOC.Token(found, "COMM", "JSDOC"));
134		else if (this.keepComments) tokens.push(new JSDOC.Token(found, "COMM", "MULTI_LINE_COMM"));
135		return true;
136	}
137	return false;
138}
139
140/**
141	@returns {Boolean} Was the token found?
142 */
143JSDOC.TokenReader.prototype.read_slcomment = function(/**JSDOC.TokenStream*/stream, tokens) {
144	var found;
145	if (
146		(stream.look() == "/" && stream.look(1) == "/" && (found=stream.next(2)))
147		|| 
148		(stream.look() == "<" && stream.look(1) == "!" && stream.look(2) == "-" && stream.look(3) == "-" && (found=stream.next(4)))
149	) {
150		
151		while (!stream.look().eof && !JSDOC.Lang.isNewline(stream.look())) {
152			found += stream.next();
153		}
154		
155		if (this.keepComments) {
156			tokens.push(new JSDOC.Token(found, "COMM", "SINGLE_LINE_COMM"));
157		}
158		return true;
159	}
160	return false;
161}
162
163/**
164	@returns {Boolean} Was the token found?
165 */
166JSDOC.TokenReader.prototype.read_dbquote = function(/**JSDOC.TokenStream*/stream, tokens) {
167	if (stream.look() == "\"") {
168		// find terminator
169		var string = stream.next();
170		
171		while (!stream.look().eof) {
172			if (stream.look() == "\\") {
173				if (JSDOC.Lang.isNewline(stream.look(1))) {
174					do {
175						stream.next();
176					} while (!stream.look().eof && JSDOC.Lang.isNewline(stream.look()));
177					string += "\\\n";
178				}
179				else {
180					string += stream.next(2);
181				}
182			}
183			else if (stream.look() == "\"") {
184				string += stream.next();
185 				if ( stream.look(0, false) == ":" ) {
186 					if ( string.indexOf('.') != -1 ) {
187 						LOG.warn( "The symbol '"+string+"' uses dot notation, but is written as a string literal." );
188 					}
189 					tokens.push(new JSDOC.Token(string.substr(1, string.length-2), "NAME", "NAME"));
190 				}
191 				else {
192 					tokens.push(new JSDOC.Token(string, "STRN", "DOUBLE_QUOTE"));
193 				}
194				return true;
195			}
196			else {
197				string += stream.next();
198			}
199		}
200	}
201	return false; // error! unterminated string
202}
203
204/**
205	@returns {Boolean} Was the token found?
206 */
207JSDOC.TokenReader.prototype.read_snquote = function(/**JSDOC.TokenStream*/stream, tokens) {
208	if (stream.look() == "'") {
209		// find terminator
210		var string = stream.next();
211		
212		while (!stream.look().eof) {
213			if (stream.look() == "\\") { // escape sequence
214				string += stream.next(2);
215			}
216			else if (stream.look() == "'") {
217				string += stream.next();
218				tokens.push(new JSDOC.Token(string, "STRN", "SINGLE_QUOTE"));
219				return true;
220			}
221			else {
222				string += stream.next();
223			}
224		}
225	}
226	return false; // error! unterminated string
227}
228
229/**
230	@returns {Boolean} Was the token found?
231 */
232JSDOC.TokenReader.prototype.read_numb = function(/**JSDOC.TokenStream*/stream, tokens) {
233	if (stream.look() === "0" && stream.look(1) == "x") {
234		return this.read_hex(stream, tokens);
235	}
236	
237	var found = "";
238	
239	while (!stream.look().eof && JSDOC.Lang.isNumber(found+stream.look())){
240		found += stream.next();
241	}
242	
243	if (found === "") {
244		return false;
245	}
246	else {
247		if (/^0[0-7]/.test(found)) tokens.push(new JSDOC.Token(found, "NUMB", "OCTAL"));
248		else tokens.push(new JSDOC.Token(found, "NUMB", "DECIMAL"));
249		return true;
250	}
251}
252/*t:
253	requires("../lib/JSDOC/TextStream.js");
254	requires("../lib/JSDOC/Token.js");
255	requires("../lib/JSDOC/Lang.js");
256	
257	plan(3, "testing JSDOC.TokenReader.prototype.read_numb");
258	
259	//// setup
260	var src = "function foo(num){while (num+8.0 >= 0x20 && num < 0777){}}";
261	var tr = new JSDOC.TokenReader();
262	var tokens = tr.tokenize(new JSDOC.TextStream(src));
263	
264	var hexToken, octToken, decToken;
265	for (var i = 0; i < tokens.length; i++) {
266		if (tokens[i].name == "HEX_DEC") hexToken = tokens[i];
267		if (tokens[i].name == "OCTAL") octToken = tokens[i];
268		if (tokens[i].name == "DECIMAL") decToken = tokens[i];
269	}
270	////
271	
272	is(decToken.data, "8.0", "decimal number is found in source.");
273	is(hexToken.data, "0x20", "hexdec number is found in source (issue #99).");
274	is(octToken.data, "0777", "octal number is found in source.");
275*/
276
277/**
278	@returns {Boolean} Was the token found?
279 */
280JSDOC.TokenReader.prototype.read_hex = function(/**JSDOC.TokenStream*/stream, tokens) {
281	var found = stream.next(2);
282	
283	while (!stream.look().eof) {
284		if (JSDOC.Lang.isHexDec(found) && !JSDOC.Lang.isHexDec(found+stream.look())) { // done
285			tokens.push(new JSDOC.Token(found, "NUMB", "HEX_DEC"));
286			return true;
287		}
288		else {
289			found += stream.next();
290		}
291	}
292	return false;
293}
294
295/**
296	@returns {Boolean} Was the token found?
297 */
298JSDOC.TokenReader.prototype.read_regx = function(/**JSDOC.TokenStream*/stream, tokens) {
299	var last;
300	if (
301		stream.look() == "/"
302		&& 
303		(
304			
305			(
306				!(last = tokens.lastSym()) // there is no last, the regex is the first symbol
307				|| 
308				(
309					   !last.is("NUMB")
310					&& !last.is("NAME")
311					&& !last.is("RIGHT_PAREN")
312					&& !last.is("RIGHT_BRACKET")
313				)
314			)
315		)
316	) {
317		var regex = stream.next();
318		
319		while (!stream.look().eof) {
320			if (stream.look() == "\\") { // escape sequence
321				regex += stream.next(2);
322			}
323			else if (stream.look() == "/") {
324				regex += stream.next();
325				
326				while (/[gmi]/.test(stream.look())) {
327					regex += stream.next();
328				}
329				
330				tokens.push(new JSDOC.Token(regex, "REGX", "REGX"));
331				return true;
332			}
333			else {
334				regex += stream.next();
335			}
336		}
337		// error: unterminated regex
338	}
339	return false;
340}