/*
* This file contains utilities for the recursive descent parser.
*/
#ifndef _util_h_
#define _util_h_
#include <string>
using namespace std;
/* Print some spaces to a stream. This class and the operator function
allows stuff like:
s << spaces(5) << "more stuff";
*/
class spaces {
public:
spaces(int nsp): m_nsp(nsp) { }
private:
int m_nsp;
friend ostream &operator <<(ostream &strm, const spaces &);
};
inline ostream &operator <<(ostream &st, const spaces &sp)
{
for(int n = sp.m_nsp; n--; ) st << " ";
return st;
}
/* Abstract base class for stuff used by the parser. */
class ParserObject {
protected:
// Print the object with the indicated indent amount.
// Each object must implement this.
virtual void pr(ostream &s, int indent) const = 0;
public:
ostream & pro(ostream &s, int indent) const {
pr(s,indent);
return s;
}
// Print the object through the abstract printing method.
void print(ostream &s) const {
pr(s, 0);
}
};
inline ostream &operator <<(ostream &st, const ParserObject &po)
{
po.print(st);
return st;
}
#endif