Name TokenStream::consumeName(int match) { Name value = name_from_string(nextStr().c_str()); consume(match); return value; }
std::string TokenStream::consumeStr(int match) { std::string out = nextStr(); consume(match); return out; }
string countAndSay(int n) { if(n<=0) return ""; string re="1"; for(int i=1;i<n;i++){ re=nextStr(re); } return re; }
void TokenStream::dump() { int lookbehind = 5; int lookahead = 15; for (int i=-lookbehind; i < lookahead; i++) { int index = position() + i; if (index < 0) continue; if (index >= length()) continue; std::cout << "[" << i << "] " << get_token_text(next(i).match) << " '" << nextStr(i) << "'" << std::endl; } }
void getNext(string s, unordered_set<string> &dict, unordered_set<string> &isVisited, unordered_set<string> &nextStrs) { for(int i = 0; i < s.length(); ++i) { string nextStr(s); for(char c = 'a'; c <= 'z'; ++c) { if(c != nextStr[i]) { swap(c, nextStr[i]); if(dict.count(nextStr) && !isVisited.count(nextStr)) { nextStrs.insert(nextStr); isVisited.insert(nextStr); } swap(c, nextStr[i]); } } } }
void TokenStream::consume(int match) { if (finished()) throw std::runtime_error(std::string("Unexpected EOF while looking for ") + get_token_text(match)); if ((match != -1) && next().match != match) { std::stringstream msg; msg << "Unexpected token (expected " << get_token_text(match) << ", found " << get_token_text(next().match) << " '" << nextStr() << "')"; throw std::runtime_error(msg.str()); } _position++; }