Beispiel #1
0
void TBParser::ConsumeValue(TBValue &dst_value, char *&line)
{
	// Find value (As quoted string, or as auto)
	char *value = line;
	if (*line == '\"' || *line == '\'')
	{
		const char quote_type = *line;
		// Consume starting quote
		line++;
		value++;
		// Find ending quote or end
		while (!IsEndQuote(value, line, quote_type) && *line != 0)
			line++;
		// Terminate away the quote
		if (*line == quote_type)
			*line++ = 0;

		// consume any whitespace
		while (is_white_space(line))
			line++;
		// consume any comma
		if (*line == ',')
			line++;

		UnescapeString(value);
		dst_value.SetString(value, TBValue::SET_AS_STATIC);
	}
	else
	{
		// Find next comma or end
		while (*line != ',' && *line != 0)
			line++;
		// Terminate away the comma
		if (*line == ',')
			*line++ = 0;

		UnescapeString(value);
		dst_value.SetFromStringAuto(value, TBValue::SET_AS_STATIC);
	}

	// Check if we still have pending value data on the following line and set pending_multiline.
	bool continuing_multiline = pending_multiline;
	pending_multiline = is_pending_multiline(line);

	// Append the multi line value to the buffer.
	if (continuing_multiline || pending_multiline)
		multi_line_value.AppendString(dst_value.GetString());
}
Beispiel #2
0
void TBParser::OnLine(char *line, TBParserTarget *target)
{
	if (is_space_or_comment(line))
	{
		if (*line == '#')
			target->OnComment(current_line_nr, line + 1);
		return;
	}
	if (pending_multiline)
	{
		OnMultiline(line, target);
		return;
	}

	// Check indent
	int indent = 0;
	while (line[indent] == '\t' && line[indent] != 0)
		indent++;
	line += indent;

	if (indent - current_indent > 1)
	{
		target->OnError(current_line_nr, "Indentation error. (Line skipped)");
		return;
	}

	if (indent > current_indent)
	{
		// FIX: Report indentation error if more than 1 higher!
		assert(indent - current_indent == 1);
		target->Enter();
		current_indent++;
	}
	else if (indent < current_indent)
	{
		while (indent < current_indent)
		{
			target->Leave();
			current_indent--;
		}
	}

	if (*line == 0)
		return;
	else
	{
		char *token = line;
		// Read line while consuming it and copy over to token buf
		while (!is_white_space(line) && *line != 0)
			line++;
		int token_len = line - token;
		// Consume any white space after the token
		while (is_white_space(line))
			line++;

		bool is_compact_line = token_len && token[token_len - 1] == ':';

		TBValue value;
		if (is_compact_line)
		{
			token_len--;
			token[token_len] = 0;

			// Check if the first argument is not a child but the value for this token
			if (*line == '[' || *line == '\"' || *line == '\'' ||
				is_start_of_number(line) ||
				is_start_of_color(line) ||
				is_start_of_reference(line))
			{
				ConsumeValue(value, line);

				if (pending_multiline)
				{
					// The value wrapped to the next line, so we should remember the token and continue.
					multi_line_token.Set(token);
					return;
				}
			}
		}
		else if (token[token_len])
		{
			token[token_len] = 0;
			UnescapeString(line);
			value.SetFromStringAuto(line, TBValue::SET_AS_STATIC);
		}
		target->OnToken(current_line_nr, token, value);

		if (is_compact_line)
			OnCompactLine(line, target);
	}
}