int getLexem(const std::string &str, std::string &out, size_t &pos){ size_t nPos = ignoreWhitespaces(str, pos), s = str.length(); bool in_quotes; pos = nPos; //update pos //if reached the end of line if (nPos == s) return -1; //if not, check if word starts with "e if ((in_quotes = (str[nPos] == '\"'))) {//why additional parentheses pack? To get rid of compiler's warning while (++nPos < s && str[nPos] != '\"'); ++nPos; } //without "e, find space (or end of string) else { std::string::const_iterator it = std::find_if(str.begin() + pos, str.end(), isspace); if (it == str.end()) nPos = s; else nPos = it - str.begin(); } //copy word out = str.substr(pos, nPos - pos); //check next lexem if (in_quotes && nPos < s && !isspace(str[nPos])) return 2; else if (in_quotes && nPos>s) return 1; if (in_quotes) //all okay for quotes clearFromQuotes(out); pos = nPos; //update pos return 0; }
void Lexer::readNextToken() { ignoreWhitespaces(); if (tryEof()) return; throwOnStreamError(); std::string input; if (tryOperatorOrSymbol()) return; else if (tryInteger()) return; else { input = readStringOfLetters(); if (input != "" && (tryBoolean(input) || tryReservedID(input) || tryIdentificator(input))) return; } throwUnknownToken(); }