|
// released into the public domain
// by Christopher Diggins 2004
// http://www.cdiggins.com
#ifndef TOKENIZER_HPP_INCLUDED
#define TOKENIZER_HPP_INCLUDED
#include <list>
#include <utility>
#include "parser_input_stream.hpp"
namespace yard
{
typedef std::pair<char const*, char const*> Token;
typedef std::list<Token> TokenList;
typedef TokenList::const_iterator TokenIter;
template<typename Rules_T>
struct Tokenizer
{
void Tokenize(char const* pBegin, char const* pEnd)
{
ParserInputStream<char> input(pBegin, pEnd);
while (!input.AtEnd()) {
char const* pos = input.GetPos();
if (Rules_T::Accept(input)) {
mTkns.push_back(Token(pos, input.GetPos()));
}
input.GotoNext();
}
}
TokenIter begin() { return mTkns.begin(); }
TokenIter end() { return mTkns.end(); }
private:
TokenList mTkns;
};
void OutputTokens(TokenIter iter, TokenIter end)
{
// outputs first 10 tokens as strings
for (int i=0; (i < 10) && (iter != end); i++, iter++) {
int n = static_cast<int>(iter->second - iter->first);
std::string s(iter->first, 0, n);
std::cout << s.c_str() << std::endl;
}
}
}
#endif // #ifndef TOKENIZER_HPP_INCLUDED
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here