Code coverage report for options-parser/tokenizer.js

Statements: 100% (53 / 53)      Branches: 100% (33 / 33)      Functions: 100% (4 / 4)      Lines: 100% (53 / 53)      Ignored: none     

All files » options-parser/ » tokenizer.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111  1   22 22     1   1   1   1   77 77 77 77 77 77   448 448   448       62   55 55 55   7       302   13   1     12 12       289   34 34     255   5       250     268       84   13 1 12       71 1 70   84     359 359     43     1   22 22 22   55 55   22     1   22        
 
function Tokenizer(input)
{
	this.input = input;
	this.len = input.length;
}
 
Tokenizer.prototype.input = null;
 
Tokenizer.prototype.len = 0;
 
Tokenizer.prototype.pos = 0;
 
Tokenizer.prototype.nextToken = function()
{
	var state = 0;
	var token = null;
	var strch = "";
	var escape = false;
	var saveEscape = false;
	while(this.pos < this.len)
	{
		var ch = this.input[this.pos];
		saveEscape = escape;
 
		switch(state)
		{
			// eat up whitespace between tokens
			case 0:
				if(ch != ' ' && ch != '\t')
				{
					state = 1;
					token = "";
					continue;
				}
				break;
 
			case 1:
				// check for start of string
				if(ch == '"' || ch == "'")
				{
					if(escape)
					{
						token += ch;
					} 
					else {
						state = 2;
						strch = ch;
					}
				}
				// end of token?
				else if(!escape && (ch == ' ' || ch == '\t'))
				{
					this.pos++;
					return token;
				}
				else {
					if(!escape && ch == "\\")
					{
						escape = true;
					}
					else
					{
						token += ch;
					}
				}
				break;
 
			// string handling
			case 2:
				if(ch == strch)
				{
					if(escape && ch == '"')
						token += ch;
					else state = 1;
				}
				else
				{
					if(!escape && ch == "\\" && strch == '"')
						escape = true;
					else token += ch;
				}
				break;
		}
 
		escape = (saveEscape != escape);
		this.pos++;
	}
 
	return token;
};
 
Tokenizer.prototype.allTokens = function()
{
	var tokens = [];
	var token = this.nextToken();
	while(token != null)
	{
		tokens.push(token);
		token = this.nextToken();
	}
	return tokens;
};
 
module.exports = {
	create: function(str){
		return new Tokenizer(str);
	}
};