Example #1
0
QString parseQuotes(QString::const_iterator &it)
{
    skipUntil(it, quotes);
    auto beg = ++ it;
    skipUntil(it, quotes);
    auto end = it ++;
    return QString(beg, end - beg);
}
Example #2
0
static void findProtobufTags (void)
{
	cppInit (false, false, false, false,
			 KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX, 0, 0);
	token.value = vStringNew ();

	nextToken ();

	while (token.type != TOKEN_EOF)
	{
		if (tokenIsKeyword (KEYWORD_PACKAGE))
			parseStatement (PK_PACKAGE);
		else if (tokenIsKeyword (KEYWORD_MESSAGE))
			parseStatement (PK_MESSAGE);
		else if (tokenIsKeyword (KEYWORD_ENUM))
			parseStatement (PK_ENUM);
		else if (tokenIsKeyword (KEYWORD_REPEATED) || tokenIsKeyword (KEYWORD_OPTIONAL) || tokenIsKeyword (KEYWORD_REQUIRED))
			parseStatement (PK_FIELD);
		else if (tokenIsKeyword (KEYWORD_SERVICE))
			parseStatement (PK_SERVICE);
		else if (tokenIsKeyword (KEYWORD_RPC))
			parseStatement (PK_RPC);

		skipUntil (";{}");
		nextToken ();
	}

	vStringDelete (token.value);
	cppTerminate ();
}
Example #3
0
void Foam::functionEntries::ifeqEntry::skipUntil
(
    DynamicList<filePos>& stack,
    const dictionary& parentDict,
    const word& endWord,
    Istream& is
)
{
    while (!is.eof())
    {
        token t;
        readToken(t, is);
        if (t.isWord())
        {
            if (t.wordToken() == "#if" || t.wordToken() == "#ifeq")
            {
                stack.append(filePos(is.name(), is.lineNumber()));
                skipUntil(stack, parentDict, "#endif", is);
                stack.remove();
            }
            else if (t.wordToken() == endWord)
            {
                return;
            }
        }
    }

    FatalIOErrorInFunction(parentDict)
        << "Did not find matching " << endWord << exit(FatalIOError);
}
Example #4
0
QString parseToken(QString::const_iterator &it)
{
    skipWhile(it, spaces);
    auto beg = it;
    skipUntil(it, spaces);
    return QString(beg, it - beg);
}
Example #5
0
XmlDoc *
parseDoc(const char *xmlText)
{
    XmlDoc* doc = malloc(sizeof(XmlDoc));

    doc->root = 0;
    doc->err = XML_SUCCESS;
    doc->line = 1;

    while(*xmlText)
    {
	if (*xmlText == '<')
	{
	    if (xmlText[1] == '!' || xmlText[1] == '?')
	    {
		++xmlText;
		skipUntil(doc, &xmlText, '>');
		if (!*xmlText)
		{
		    doc->err = XML_EOF;
		    freeElementList(doc->root);
		    doc->root = 0;
		    return doc;
		}
		++xmlText;
	    }
	    else
	    {
		if (doc->root)
		{
		    doc->col = xmlText - doc->currLine + 1;
		    doc->err = XML_SECONDROOT;
		    freeElementList(doc->root);
		    doc->root = 0;
		    return doc;
		}
		else
		{
		    doc->root = parseElement(doc, &xmlText, 0);
		    if (!doc->root) return doc;
		}
	    }
	}
	else if (isspace(*xmlText))
	{
	    skipWs(doc, &xmlText);
	}
	else
	{
	    doc->col = xmlText - doc->currLine + 1;
	    doc->err = XML_UNEXPECTED;
	    doc->errInfo.c = *xmlText;
	    freeElementList(doc->root);
	    doc->root = 0;
	    return doc;
	}
    }

    return doc;
}
Example #6
0
QString parseLine(QString::const_iterator &it)
{
    auto beg = it;
    skipUntil(it, newlns);
    auto end = it;
    skipWhile(it, newlns);
    return QString(beg, end - beg);
}
Example #7
0
/* Skips type blocks of the form <T:T<T>, ...> */
static void skipTypeBlock (lexerState *lexer)
{
	if (lexer->cur_token == '<')
	{
		skipUntil(lexer, NULL, 0);
		advanceToken(lexer, TRUE);
	}
}
Example #8
0
bool Foam::functionEntries::ifeqEntry::evaluate
(
    const bool doIf,
    DynamicList<filePos>& stack,
    dictionary& parentDict,
    Istream& is
)
{
    while (!is.eof())
    {
        token t;
        readToken(t, is);

        if (t.isWord() && t.wordToken() == "#ifeq")
        {
            // Recurse to evaluate
            execute(stack, parentDict, is);
        }
        else if (t.isWord() && t.wordToken() == "#if")
        {
            // Recurse to evaluate
            ifEntry::execute(stack, parentDict, is);
        }
        else if
        (
            doIf
         && t.isWord()
         && (t.wordToken() == "#else" || t.wordToken() == "#elif")
        )
        {
            // Now skip until #endif
            skipUntil(stack, parentDict, "#endif", is);
            stack.remove();
            break;
        }
        else if (t.isWord() && t.wordToken() == "#endif")
        {
            stack.remove();
            break;
        }
        else
        {
            is.putBack(t);
            bool ok = entry::New(parentDict, is);
            if (!ok)
            {
                return false;
            }
        }
    }
    return true;
}
Example #9
0
void Processor::skipWhitespaceCommentsAndFillBuffer(std::string* target)
{
	while (!buf.eof()) {

		// White space
		if (isWhitespace(buf.peek())) {
			skipWhitespace(target);
			continue;
		}

		// Comment
		if (buf.isNext("--")) {
			if (target) {
				*target += buf.getWithoutPrinting(2);
			} else {
				buf.skip(2);
			}
			skipUntil("\n", target);
			continue;
		}

		// Multi line comment
		if (buf.isNext("/*")) {
			if (target) {
				*target += buf.getWithoutPrinting(2);
			} else {
				buf.skip(2);
			}
			skipUntil("*/", target);
			continue;
		}

		// No whitespace, no comment, everything has been skipped
		break;
	}
}
Example #10
0
/* Trait format:
 * "trait" <ident> [<type_bounds>] "{" [<body>] "}"
 */
static void parseTrait (lexerState *lexer, vString *scope, int parent_kind)
{
	int goal_tokens[] = {'{'};

	advanceToken(lexer, TRUE);
	if (lexer->cur_token != TOKEN_IDENT)
		return;

	addTag(lexer->token_str, NULL, K_TRAIT, lexer->line, lexer->pos, scope, parent_kind);
	addToScope(scope, lexer->token_str);

	advanceToken(lexer, TRUE);

	skipUntil(lexer, goal_tokens, 1);

	parseBlock(lexer, TRUE, K_TRAIT, scope);
}
Example #11
0
static bool skipQuotesIfNeeded(CharacterType*& position, CharacterType* const end, bool& completeQuotes)
{
    ASSERT(position <= end);
    unsigned char quote;
    if (skipExactly<CharacterType>(position, end, '\''))
        quote = '\'';
    else if (skipExactly<CharacterType>(position, end, '"'))
        quote = '"';
    else
        return false;

    while (!completeQuotes && position < end) {
        skipUntil(position, end, static_cast<CharacterType>(quote));
        if (*(position - 1) != '\\')
            completeQuotes = true;
        completeQuotes = skipExactly(position, end, static_cast<CharacterType>(quote)) && completeQuotes;
    }
    return true;
}
Example #12
0
static XmlAttribute *
parseAttribute(XmlDoc *doc, const char **xmlText, XmlElement *element)
{
    const char *startval;
    XmlAttribute *attribute = calloc(1, sizeof(XmlAttribute));
    attribute->next = attribute->prev = attribute;
    attribute->parent = element;

    attribute->name = readBareWord(xmlText, "=");
    if (!attribute->name) FAIL(XML_UNNAMEDATTR);
    if (!**xmlText) FAIL(XML_EOF);
    skipWs(doc, xmlText);
    if (**xmlText != '=') FAILC(XML_UNEXPECTED, **xmlText);
    ++(*xmlText);
    skipWs(doc, xmlText);
    if (**xmlText == '"' || **xmlText == '\'')
    {
	++(*xmlText);
	startval = *xmlText;
	skipUntil(doc, xmlText, *(*xmlText-1));
	if (!**xmlText) FAIL(XML_EOF);
	if (*xmlText - startval)
	{
	    attribute->value = calloc(1, (size_t)(*xmlText - startval) + 1);
	    memcpy(attribute->value, startval, (size_t)(*xmlText - startval));
	}
	++(*xmlText);
	return attribute;
    }
    else
    {
	attribute->value = readBareWord(xmlText, "/>");
	if (!**xmlText) FAIL(XML_EOF);
	return attribute;
    }

fail:
    doc->col = *xmlText - doc->currLine + 1;
    freeAttributeList(attribute);
    return 0;
}
Example #13
0
	//
	// r e a d S t r i n g U n t i l
	//
	bool XmlScanner::readStringUntil(char searchCharacter,
										 bool includeSearchCharacter){

		// Remember start position
		LineBufferPosition startPosition = m_pLineBuffer->getCurrentPosition();

		// Use skipUntil()
		if (skipUntil(searchCharacter, includeSearchCharacter)){

			// Copy found string to m_currentToken
			m_pLineBuffer->extractString(startPosition, m_pLineBuffer->getCurrentPosition(), m_currentToken);

			return true;

		}
		// An error occurred
		else{
			return false;
		}

	} // getStringUntil
Example #14
0
static void parseEnumConstants (void)
{
	if (token.type != '{')
		return;
	nextToken ();

	while (token.type != TOKEN_EOF && token.type != '}')
	{
		if (token.type == TOKEN_ID && !tokenIsKeyword (KEYWORD_OPTION))
		{
			nextToken ();  /* doesn't clear token.value if it's punctuation */
			if (token.type == '=')
				createProtobufTag (token.value, PK_ENUMERATOR);
		}

		skipUntil (";}");

		if (token.type == ';')
			nextToken ();
	}
}
Example #15
0
/* Structs and enums are very similar syntax-wise.
 * It is possible to parse variants a bit more cleverly (e.g. make tuple variants functions and
 * struct variants structs) but it'd be too clever and the signature wouldn't make too much sense without
 * the enum's definition (e.g. for the type bounds)
 *
 * Struct/Enum format:
 * "struct/enum" <ident>[<type_bounds>] "{" [<ident>,]+ "}"
 * "struct/enum" <ident>[<type_bounds>] ";"
 * */
static void parseStructOrEnum (lexerState *lexer, vString *scope, int parent_kind, boolean is_struct)
{
	int kind = is_struct ? K_STRUCT : K_ENUM;
	int field_kind = is_struct ? K_FIELD : K_VARIANT;
	int goal_tokens1[] = {';', '{'};

	advanceToken(lexer, TRUE);
	if (lexer->cur_token != TOKEN_IDENT)
		return;

	addTag(lexer->token_str, NULL, kind, lexer->line, lexer->pos, scope, parent_kind);
	addToScope(scope, lexer->token_str);

	skipUntil(lexer, goal_tokens1, 2);

	if (lexer->cur_token == '{')
	{
		vString *field_name = vStringNew();
		while (lexer->cur_token != TOKEN_EOF)
		{
			int goal_tokens2[] = {'}', ','};
			/* Skip attributes. Format:
			 * #[..] or #![..]
			 * */
			if (lexer->cur_token == '#')
			{
				advanceToken(lexer, TRUE);
				if (lexer->cur_token == '!')
					advanceToken(lexer, TRUE);
				if (lexer->cur_token == '[')
				{
					/* It's an attribute, skip it. */
					skipUntil(lexer, NULL, 0);
				}
				else
				{
					/* Something's up with this field, skip to the next one */
					skipUntil(lexer, goal_tokens2, 2);
					continue;
				}
			}
			if (lexer->cur_token == TOKEN_IDENT)
			{
				if (strcmp(lexer->token_str->buffer, "priv") == 0
				    || strcmp(lexer->token_str->buffer, "pub") == 0)
				{
					advanceToken(lexer, TRUE);
					if (lexer->cur_token != TOKEN_IDENT)
					{
						/* Something's up with this field, skip to the next one */
						skipUntil(lexer, goal_tokens2, 2);
						continue;
					}
				}

				vStringClear(field_name);
				vStringCat(field_name, lexer->token_str);
				addTag(field_name, NULL, field_kind, lexer->line, lexer->pos, scope, kind);
				skipUntil(lexer, goal_tokens2, 2);
			}
			if (lexer->cur_token == '}')
			{
				advanceToken(lexer, TRUE);
				break;
			}
			advanceToken(lexer, TRUE);
		}
		vStringDelete(field_name);
	}
}
Example #16
0
bool Foam::functionEntries::ifeqEntry::execute
(
    const bool doIf,
    DynamicList<filePos>& stack,
    dictionary& parentDict,
    Istream& is
)
{
    if (doIf)
    {
        evaluate(true, stack, parentDict, is);
    }
    else
    {
        // Fast-forward to #else
        token t;
        while (!is.eof())
        {
            readToken(t, is);
            if
            (
                t.isWord()
             && (t.wordToken() == "#if" || t.wordToken() == "#ifeq")
            )
            {
                stack.append(filePos(is.name(), is.lineNumber()));
                skipUntil(stack, parentDict, "#endif", is);
                stack.remove();
            }
            else if (t.isWord() && t.wordToken() == "#else")
            {
                break;
            }
            else if (t.isWord() && t.wordToken() == "#elif")
            {
                // const label lineNo = is.lineNumber();

                // Read line
                string line;
                dynamic_cast<ISstream&>(is).getLine(line);
                line += ';';
                IStringStream lineStream(line);
                const primitiveEntry e("ifEntry", parentDict, lineStream);
                const Switch doIf(e.stream());

                if (doIf)
                {
                    // Info<< "Using #elif " << doIf << " at line " << lineNo
                    //     << " in file " << is.name() << endl;
                    break;
                }
            }
            else if (t.isWord() && t.wordToken() == "#endif")
            {
                stack.remove();
                break;
            }
        }

        if (t.wordToken() == "#else")
        {
            // Evaluate until we hit #endif
            evaluate(false, stack, parentDict, is);
        }
        else if (t.wordToken() == "#elif")
        {
            // Evaluate until we hit #else or #endif
            evaluate(true, stack, parentDict, is);
        }
    }
    return true;
}
Example #17
0
void Processor::process()
{
	Regexp re_create_table("^CREATE +TABLE\\s", REG_EXTENDED | REG_ICASE);
	Regexp re_insert_into("^INSERT +INTO\\s", REG_EXTENDED | REG_ICASE);
	Regexp re_values("^VALUES\\s", REG_EXTENDED | REG_ICASE);
	Regexp re_ignored("^(DROP|LOCK|UNLOCK) +TABLE", REG_EXTENDED | REG_ICASE);

	while (!buf.eof()) {

		// White space
		if (isWhitespace(buf.peek())) {
			skipWhitespace();
			continue;
		}

		// Comment
		if (buf.isNext("--")) {
			buf.skip(2);
			skipUntil("\n");
			continue;
		}

		// Multi line comment
		if (buf.isNext("/*")) {
			buf.skip(2);
			skipUntil("*/");
			continue;
		}

		// Semicolon
		if (buf.peek() == ';') {
			buf.skip();
			continue;
		}

		// Ignored statement
		if (re_ignored.match(buf)) {
			buf.skip(re_ignored.getMatchLength());
			skipUntil(";");
			continue;
		}

		// CREATE TABLE statement
		if (re_create_table.match(buf)) {
			buf.skip(re_create_table.getMatchLength());

			skipWhitespaceCommentsAndFillBuffer();

			std::string table_name = readName();

			skipWhitespaceCommentsAndFillBuffer();

			if (buf.eof() || buf.get() != '(') {
				throw std::runtime_error("Expected \"(\" was not found!");
			}

			skipWhitespaceCommentsAndFillBuffer();

			// Read names of columns
			Strings cols;
			while (!buf.eof() && buf.peek() != ')') {
				std::string col_name = readName();

				// Make sure this is a column
				if (!col_name.empty()) {
					cols.push_back(col_name);
				}

				// Skip all the details
				int open_brackets = 0;
				while (true) {
					if (buf.eof() || (buf.peek() == ')' && open_brackets == 0)) {
						break;
					}

					char byte = buf.get();
					if (byte == '(') {
						++ open_brackets;
					} else if (byte == ')') {
						if (open_brackets == 0) {
							throw std::runtime_error("Unexpected closing bracket!");
						}
						-- open_brackets;
					} else if (byte == ',' && open_brackets == 0) {
						break;
					}
				}

				skipWhitespaceCommentsAndFillBuffer();
			}

			if (buf.eof() || buf.get() != ')') {
				throw std::runtime_error("Expected \")\" was not found!");
			}

			skipUntil(";");

			rules.setTableColumnNames(table_name, cols);

			continue;
		}

		// INSERT INTO statement
		if (re_insert_into.match(buf)) {
			std::string insert_into_statement = buf.getWithoutPrinting(re_insert_into.getMatchLength());

			skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

			std::string table_name = readName(&insert_into_statement);

			bool delete_all_rows = rules.getDeleteAllRows(table_name);

			skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

			if (!re_values.match(buf)) {
				throw std::runtime_error("Expected \"VALUES\" was not found!");
			}
			insert_into_statement += buf.getWithoutPrinting(re_values.getMatchLength());

			skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

			// Read inserting of values
			while (true) {
				if (buf.eof()) {
					throw std::runtime_error("Expected \"(\" was not found!");
				}
				char c = buf.getWithoutPrinting();
				insert_into_statement += c;
				if (c != '(') {
					throw std::runtime_error("Expected \"(\" was not found!");
				}

				skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);
// TODO: Fill this container with sane values!
Equation::Variables vars;

				if (buf.eof() || buf.peek() != ')') {
					unsigned int col_i = 0;
					while (true) {
						std::string value = readValueWithoutPrinting();

						// Let rules modify the value
						if (!delete_all_rows) {
							value = rules.getModifiedValue(table_name, col_i, value, vars);
						}
						insert_into_statement += value;

						skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

						if (buf.eof()) {
							break;
						}
						char next_byte = buf.getWithoutPrinting();
						insert_into_statement += next_byte;
						if (next_byte == ')') {
							break;
						} else if (next_byte != ',') {
							throw std::runtime_error("Expected \"(\" or \",\" was not found!");
						}

						skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

						++ col_i;
					}
				} else {
					// Skip ')'
					insert_into_statement += buf.getWithoutPrinting();
				}

				skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);

				if (buf.eof()) {
					break;
				}
				char comma_or_semicolon = buf.getWithoutPrinting();
				insert_into_statement += comma_or_semicolon;
				if (comma_or_semicolon != ',') {
					if (comma_or_semicolon != ';') {
						throw std::runtime_error("Expected \";\" was not found!");
					}
					break;
				}

				skipWhitespaceCommentsAndFillBuffer(&insert_into_statement);
			}

			if (!delete_all_rows) {
				std::cout << insert_into_statement;
			}

			continue;
		}

		throw std::runtime_error("Unable to process input \"" + buf.peek(40) + "\"!");
	}
}
Example #18
0
static XmlElement *
parseElement(XmlDoc *doc, const char **xmlText, XmlElement *parent)
{
    XmlElement *element = 0;
    XmlElement *childnode = 0;
    XmlAttribute *attribute = 0;
    const char *startval = 0;
    const char *endval = 0;
    size_t valLen = 0;

    ++(*xmlText);
    if (!**xmlText) FAIL(XML_EOF);
    if (**xmlText == '/')
    {
	++(*xmlText);
	FAILS(XML_CLOSEWOOPEN, readBareWord(xmlText, ">"));
    }

    element = calloc(1, sizeof(XmlElement));
    element->prev = element->next = element;
    element->parent = parent;
    if (parent)
    {
	element->depth = parent->depth + 1;
    }
    else
    {
	element->depth = 0;
    }
    element->name = readBareWord(xmlText, ">");
    if (!element->name) FAIL(XML_UNNAMEDTAG);
    if (!**xmlText) FAIL(XML_EOF);

    while (1)
    {
	skipWs(doc, xmlText);
	if (!**xmlText) FAIL(XML_EOF);

	if (**xmlText == '>')
	{
	    ++(*xmlText);
	    break;
	}
	if (**xmlText == '/')
	{
	    ++(*xmlText);
	    skipWs(doc, xmlText);
	    if (!**xmlText) FAIL(XML_EOF);
	    if (**xmlText != '>') FAILC(XML_UNEXPECTED, **xmlText);
	    ++(*xmlText);
	    return element;
	}
	attribute = parseAttribute(doc, xmlText, element);
	if (!attribute) goto failp;
	if (element->attributes)
	{
	    attribute->prev = element->attributes->prev;
	    attribute->next = element->attributes;
	    element->attributes->prev->next = attribute;
	    element->attributes->prev = attribute;
	}
	else
	{
	    element->attributes = attribute;
	}
	attribute = 0;
	if (!**xmlText) FAIL(XML_EOF);
    }

    startval = *xmlText;
    while (**xmlText)
    {
	if (**xmlText == '<')
	{
	    if (hasNonWs(startval, *xmlText))
	    {
		endval = *xmlText;
		while (isspace(*(endval-1))) --endval;
		appendString(&(element->value), startval, &valLen,
			(size_t)(endval - startval));
	    }
	    if ((*xmlText)[1] == '/')
	    {
		*xmlText += 2;
		skipWs(doc, xmlText);
		if (!**xmlText) FAIL(XML_EOF);
		if (strncmp(*xmlText, element->name, strlen(element->name)))
		{
		    FAILS(XML_UNMATCHEDCLOSE, cloneString(element->name));
		}
		*xmlText += strlen(element->name);
		skipWs(doc, xmlText);
		if (!**xmlText) FAIL(XML_EOF);
		if (**xmlText != '>') FAILC(XML_UNEXPECTED, **xmlText);
		++(*xmlText);
		return element;
	    }
	    else
	    {
		childnode = parseElement(doc, xmlText, element);
		if (!childnode) goto failp;
		if (element->children)
		{
		    childnode->prev = element->children->prev;
		    childnode->next = element->children;
		    element->children->prev->next = childnode;
		    element->children->prev = childnode;
		}
		else
		{
		    element->children = childnode;
		}
		childnode = 0;
		startval = *xmlText;
	    }
	}
	else skipUntil(doc, xmlText, '<');
    }
    doc->err = XML_EOF;

fail:
    doc->col = *xmlText - doc->currLine + 1;
failp:
    freeElementList(element);
    return 0;
}