Let's say I want to parse a simple programming language with boost regular expressions.
import a
import b
class c
Now I want to have a layout like this:
#include <string>
#include <boost/filesystem.hpp>
#include <exception>
using namespace std;
using namespace boost;
using namespace boost::filesystem;
class parser
{
string _source;
unsigned int pos;
public:
parser(const string& source) : _source(source), pos(0) {}
string expect(const regex& expr)
{
string s = next(expr)
if (s != "")
return s;
else
{
--pos;
throw exception("Expected another expression.");
}
}
string next(const regex& expr)
{
// Removing all whitespace before first non-whitespace character
// Check if characters 0 till x matches regex expr
// Return matched string of "" if not matched.
}
bool peek(const regex& expr);
parse()
{
regex identifier("\a*");
if (peek("import"))
string package = expect(identifier);
else if (peek("class"))
string classname = expect(identifier);
}
};
Now I need your help to define the function parser::next(const regex&). It's not clear to me how to iterate with boost regular expression trough a std::string.
I hope some one can help me!