QString XmlReader::readXml() { QString s; int level = 1; for (;;) { QXmlStreamReader::TokenType t = readNext(); switch(t) { case QXmlStreamReader::StartElement: htmlToString(level, &s); break; case QXmlStreamReader::EndElement: return s; case QXmlStreamReader::Characters: s += text().toString().toHtmlEscaped(); break; case QXmlStreamReader::Comment: break; default: qDebug("htmlToString: read token: %s", qPrintable(tokenString())); return s; } } return s; }
int64 MoneyStringToMoney(const std::string& moneyString) { int64 money = 0; if (!(std::count(moneyString.begin(), moneyString.end(), 'g') == 1 || std::count(moneyString.begin(), moneyString.end(), 's') == 1 || std::count(moneyString.begin(), moneyString.end(), 'c') == 1)) return 0; // Bad format Tokens tokens(moneyString, ' '); for (Tokens::const_iterator itr = tokens.begin(); itr != tokens.end(); ++itr) { std::string tokenString(*itr); size_t gCount = std::count(tokenString.begin(), tokenString.end(), 'g'); size_t sCount = std::count(tokenString.begin(), tokenString.end(), 's'); size_t cCount = std::count(tokenString.begin(), tokenString.end(), 'c'); if (gCount + sCount + cCount != 1) return 0; uint64 amount = atol(*itr); if (gCount == 1) money += amount * 100 * 100; else if (sCount == 1) money += amount * 100; else if (cCount == 1) money += amount; } return money; }
std::string ClientToken::get() { json::Object obj = request("GET", "http://battle.cppse.nl/client/token"); std::string tokenString(obj["random_string"]); return tokenString; }
void XmlReader::htmlToString(int level, QString* s) { *s += QString("<%1").arg(name().toString()); for (const QXmlStreamAttribute& a : attributes()) *s += QString(" %1=\"%2\"").arg(a.name().toString()).arg(a.value().toString()); *s += ">"; ++level; for (;;) { QXmlStreamReader::TokenType t = readNext(); switch(t) { case QXmlStreamReader::StartElement: htmlToString(level, s); break; case QXmlStreamReader::EndElement: *s += QString("</%1>").arg(name().toString()); --level; return; case QXmlStreamReader::Characters: if (!isWhitespace()) *s += text().toString().toHtmlEscaped(); break; case QXmlStreamReader::Comment: break; default: qDebug("htmlToString: read token: %s", qPrintable(tokenString())); return; } } }
std::string FlowLexer::dump() const { char buf[4096]; std::size_t n = snprintf(buf, sizeof(buf), "[%04zu:%02zu] %3d %s", line(), column(), static_cast<int>(token()), tokenString().c_str()); return std::string(buf, n); }
bool Parameter::isOption(const char* token) { if (! token) return false; std::string tokenString(token); size_t length = tokenString.size(); if (length > 0 && tokenString.substr(0,1) != "-") return false; if (length > 1 && tokenString.substr(1,1).find_first_not_of("0123456789") == 0) return true; return false; }
// match a terminal symbol t void ExtQualModuleNames::match(tokenType t) { if (nextToken() == t) { // matches t. all is OK. currentTokenCode_ = scanner(); // get next token } else { *mxCUMptr << FAIL << DgSqlCode(-2212) << DgString0(tokenString(t)) << DgString1(currentTokenString()); } }
static void parseSuperclass (tokenInfo *token, int this_class) { tokenRead (token); if (tokenIsType (token, TCL_IDENTIFIER)) { tagEntryInfo *e = getEntryInCorkQueue(this_class); if (e) { if (e->extensionFields.inheritance) { /* superclass is used twice in a class. */ eFree ((void *)e->extensionFields.inheritance); } e->extensionFields.inheritance = eStrdup(tokenString(token)); } } skipToEndOfTclCmdline (token); }
TokenStream Scanner::lex(std::istream &programStream) { TokenStream tokens; unsigned line = 1; unsigned col = 1; ScanState state = ScanState::INITIAL; while (programStream.peek() != EOF) { if (programStream.peek() == '_' || std::isalpha(programStream.peek())) { std::string tokenString(1, programStream.get()); while (programStream.peek() == '_' || std::isalnum(programStream.peek())) { tokenString += programStream.get(); } Token newToken; newToken.type = getTokenType(tokenString); newToken.lineBegin = line; newToken.colBegin = col; newToken.lineEnd = line; newToken.colEnd = col + tokenString.length() - 1; if (newToken.type == Token::Type::IDENT) { newToken.str = tokenString; } tokens.addToken(newToken); col += tokenString.length(); } else { switch (programStream.peek()) { case '(': programStream.get(); tokens.addToken(Token::Type::LP, line, col++); break; case ')': programStream.get(); tokens.addToken(Token::Type::RP, line, col++); break; case '[': programStream.get(); tokens.addToken(Token::Type::LB, line, col++); break; case ']': programStream.get(); tokens.addToken(Token::Type::RB, line, col++); break; case '{': programStream.get(); tokens.addToken(Token::Type::LC, line, col++); break; case '}': programStream.get(); tokens.addToken(Token::Type::RC, line, col++); break; case '<': programStream.get(); if (programStream.peek() == '=') { programStream.get(); tokens.addToken(Token::Type::LE, line, col, 2); col += 2; } else { tokens.addToken(Token::Type::LA, line, col++); } break; case '>': programStream.get(); if (programStream.peek() == '=') { programStream.get(); tokens.addToken(Token::Type::GE, line, col, 2); col += 2; } else { tokens.addToken(Token::Type::RA, line, col++); } break; case ',': programStream.get(); tokens.addToken(Token::Type::COMMA, line, col++); break; case '.': programStream.get(); switch (programStream.peek()) { case '*': programStream.get(); tokens.addToken(Token::Type::DOTSTAR, line, col, 2); col += 2; break; case '/': programStream.get(); tokens.addToken(Token::Type::DOTSLASH, line, col, 2); col += 2; break; default: tokens.addToken(Token::Type::PERIOD, line, col++); break; } break; case ':': programStream.get(); tokens.addToken(Token::Type::COL, line, col++); break; case ';': programStream.get(); tokens.addToken(Token::Type::SEMICOL, line, col++); break; case '=': programStream.get(); if (programStream.peek() == '=') { programStream.get(); tokens.addToken(Token::Type::EQ, line, col, 2); col += 2; } else { tokens.addToken(Token::Type::ASSIGN, line, col++); } break; case '*': programStream.get(); tokens.addToken(Token::Type::STAR, line, col++); break; case '/': programStream.get(); tokens.addToken(Token::Type::SLASH, line, col++); break; case '\\': programStream.get(); tokens.addToken(Token::Type::BACKSLASH, line, col++); break; case '^': programStream.get(); tokens.addToken(Token::Type::EXP, line, col++); break; case '\'': programStream.get(); tokens.addToken(Token::Type::TRANSPOSE, line, col++); break; case '!': programStream.get(); if (programStream.peek() == '=') { programStream.get(); tokens.addToken(Token::Type::NE, line, col, 2); col += 2; } else { reportError("unexpected symbol '!'", line, col++); while (programStream.peek() != EOF && !std::isspace(programStream.peek())) { programStream.get(); ++col; } } break; case '%': programStream.get(); switch (programStream.peek()) { case '!': programStream.get(); tokens.addToken(Token::Type::TEST, line, col, 2); state = ScanState::SLTEST; col += 2; break; case '{': if (programStream.peek() == '!') { programStream.get(); tokens.addToken(Token::Type::TEST, line, col, 2); state = ScanState::MLTEST; col += 2; } else { ++col; std::string comment; while (programStream.peek() != EOF) { if (programStream.peek() == '%') { programStream.get(); if (programStream.peek() == '\n') { ++line; col = 1; } else { col += 2; } if (programStream.peek() == '}') { programStream.get(); // TODO: emit COMMENT token break; } else { comment += '%'; comment += programStream.get(); } } else { if (programStream.peek() == '\n') { ++line; col = 1; } else { ++col; } comment += programStream.get(); } } if (programStream.peek() == EOF) { reportError("unclosed comment", line, col); } } break; case '}': { programStream.get(); if (state == ScanState::MLTEST) { state = ScanState::INITIAL; } else { reportError("could not find corresponding '!%{'", line, col); } col += 2; break; } default: { std::string comment; while (programStream.peek() != '\n' && programStream.peek() != EOF) { comment += programStream.get(); } col += (comment.length() + 1); // TODO: emit COMMENT token break; } } break; case '"': { Token newToken; newToken.type = Token::Type::STRING_LITERAL; newToken.lineBegin = line; newToken.colBegin = col; programStream.get(); ++col; while (programStream.peek() != EOF && programStream.peek() != '"') { if (programStream.peek() == '\\') { programStream.get(); std::string escapedChar = ""; switch (programStream.peek()) { case 'a': escapedChar = "\a"; break; case 'b': escapedChar = "\b"; break; case 'f': escapedChar = "\f"; break; case 'n': escapedChar = "\n"; break; case 'r': escapedChar = "\r"; break; case 't': escapedChar = "\t"; break; case 'v': escapedChar = "\v"; break; case '\\': escapedChar = "\\"; break; case '\'': escapedChar = "\'"; break; case '"': escapedChar = "\""; break; case '?': escapedChar = "\?"; break; default: reportError("unrecognized escape sequence", line, col); ++col; break; } if (escapedChar != "") { newToken.str += escapedChar; programStream.get(); col += 2; } } else { newToken.str += programStream.get(); ++col; } } newToken.lineEnd = line; newToken.colEnd = col; tokens.addToken(newToken); if (programStream.peek() == '"') { programStream.get(); ++col; } else { reportError("unclosed string literal", line, col); } break; } case '\r': programStream.get(); if (programStream.peek() == '\n') { programStream.get(); } if (state == ScanState::SLTEST) { state = ScanState::INITIAL; } ++line; col = 1; break; case '\v': case '\f': case '\n': programStream.get(); if (state == ScanState::SLTEST) { state = ScanState::INITIAL; } ++line; col = 1; break; case ' ': case '\t': programStream.get(); ++col; break; case '+': programStream.get(); tokens.addToken(Token::Type::PLUS, line, col++); break; case '-': programStream.get(); if (programStream.peek() == '>') { programStream.get(); tokens.addToken(Token::Type::RARROW, line, col, 2); col += 2; } else { tokens.addToken(Token::Type::MINUS, line, col++); } break; default: { Token newToken; newToken.type = Token::Type::INT_LITERAL; newToken.lineBegin = line; newToken.colBegin = col; if (programStream.peek() != '.' && !std::isdigit(programStream.peek())) { std::stringstream errMsg; errMsg << "unexpected symbol '" << (char)programStream.peek() << "'"; reportError(errMsg.str(), line, col); while (programStream.peek() != EOF && !std::isspace(programStream.peek())) { programStream.get(); ++col; } break; } std::string tokenString; while (std::isdigit(programStream.peek())) { tokenString += programStream.get(); ++col; } if (programStream.peek() == '.') { newToken.type = Token::Type::FLOAT_LITERAL; tokenString += programStream.get(); ++col; if (!std::isdigit(programStream.peek())) { std::stringstream errMsg; errMsg << "unexpected symbol '" << (char)programStream.peek() << "'"; reportError(errMsg.str(), line, col); while (programStream.peek() != EOF && !std::isspace(programStream.peek())) { programStream.get(); ++col; } break; } tokenString += programStream.get(); ++col; while (std::isdigit(programStream.peek())) { tokenString += programStream.get(); ++col; } } if (programStream.peek() == 'e' || programStream.peek() == 'E') { newToken.type = Token::Type::FLOAT_LITERAL; tokenString += programStream.get(); ++col; if (programStream.peek() == '+' || programStream.peek() == '-') { tokenString += programStream.get(); ++col; } if (!std::isdigit(programStream.peek())) { std::stringstream errMsg; errMsg << "unexpected symbol '" << (char)programStream.peek() << "'"; reportError(errMsg.str(), line, col); while (programStream.peek() != EOF && !std::isspace(programStream.peek())) { programStream.get(); ++col; } break; } tokenString += programStream.get(); ++col; while (std::isdigit(programStream.peek())) { tokenString += programStream.get(); ++col; } } char *end; if (newToken.type == Token::Type::INT_LITERAL) { newToken.num = std::strtol(tokenString.c_str(), &end, 0); } else { newToken.fnum = std::strtod(tokenString.c_str(), &end); } newToken.lineEnd = line; newToken.colEnd = col - 1; tokens.addToken(newToken); break; } } } } if (state != ScanState::INITIAL) { reportError("unclosed test", line, col); } tokens.addToken(Token::Type::END, line, col); return tokens; }