Example #1
0
	chars.forEach(function (char, i) {

		// Parse the option string
		if (char == ' ' && key == null && buf != '') {
			// End of chunk (Key)
			key = helper.toUpperCase(buf);
			if (self.OPTION_KEYS.indexOf(key) == -1) { // Invalid key
				key = null;
			}
			chunk = new String();
			buf = new String();
			return;

		} else if (!is_escaped && char == ' ' && quot == '') {
			// End of chunk
			chunk += buf;
			buf = new String();
			// Don't return

		} else if (!is_escaped && char == '\'') {
			// Quot
			if (quot == '') {
				quot = char;
			} else {
				quot = '';
			}
			return;

		} else if (char == '\\') {
			// Escape
			buf = buf + '' + char;
			is_escaped = true;
			return;

		} else {
			// Other character
			is_escaped = false;
			buf = buf + '' + char;
			return;

		}

		// ----

		if (key == 'DEFAULT') {
			// Default Value
			chunk = chunk.replace(/^['"]/, '');
			chunk = chunk.replace(/([a-zA-Z])['"]$/, '$1');
			self.default = self._getDefaultValueByChunk(chunk);
			key = null;
			return;
		}

		if (key == 'REGEXP') {
			// Regular Expression
			self.regExp = new RegExp(chunk);
			key = null;
			return;
		}

	});
Example #2
0
SSpecParser.prototype._parseSpec = function (spec_str) {

	var self = this;

	if (!spec_str.match(/^([a-zA-Z]+)(\((.*)\)|)/)) throw new Error('Could not parse the specification');

	// Type
	var type_chunk = helper.toUpperCase(RegExp.$1);
	if (self.VARIABLE_TYPES.indexOf(type_chunk) == -1) throw new Error('Variable type is unknown: ' + type_chunk);
	self.type = type_chunk;

	// Limitation of the value range
	var limit_chunk = RegExp.$3 || null;
	if (limit_chunk != null) {
		var limit_chunks = limit_chunk.split(/,/);
		if (limit_chunks.length == 1) {
			self.min = null;
			self.max = parseInt(limit_chunks[0]);
		} else if (limit_chunks.length == 2) {
			self.min = parseInt(limit_chunks[0]);
			self.max = parseInt(limit_chunks[1]);
		}
	}

	// Additional options
	var chars = spec_str.split('');
	chars.push(' ');
	var buf = new String(), chunk = new String(), key = null, quot = '', is_escaped = false;
	chars.forEach(function (char, i) {

		// Parse the option string
		if (char == ' ' && key == null && buf != '') {
			// End of chunk (Key)
			key = helper.toUpperCase(buf);
			if (self.OPTION_KEYS.indexOf(key) == -1) { // Invalid key
				key = null;
			}
			chunk = new String();
			buf = new String();
			return;

		} else if (!is_escaped && char == ' ' && quot == '') {
			// End of chunk
			chunk += buf;
			buf = new String();
			// Don't return

		} else if (!is_escaped && char == '\'') {
			// Quot
			if (quot == '') {
				quot = char;
			} else {
				quot = '';
			}
			return;

		} else if (char == '\\') {
			// Escape
			buf = buf + '' + char;
			is_escaped = true;
			return;

		} else {
			// Other character
			is_escaped = false;
			buf = buf + '' + char;
			return;

		}

		// ----

		if (key == 'DEFAULT') {
			// Default Value
			chunk = chunk.replace(/^['"]/, '');
			chunk = chunk.replace(/([a-zA-Z])['"]$/, '$1');
			self.default = self._getDefaultValueByChunk(chunk);
			key = null;
			return;
		}

		if (key == 'REGEXP') {
			// Regular Expression
			self.regExp = new RegExp(chunk);
			key = null;
			return;
		}

	});

};