2008-11-05 03:57:18 +01:00
|
|
|
#ifndef ARABICA_XSLT_PRECEDENCE_HPP
|
|
|
|
#define ARABICA_XSLT_PRECEDENCE_HPP
|
|
|
|
|
|
|
|
class Precedence
|
|
|
|
{
|
|
|
|
public:
|
2008-11-19 18:26:07 +01:00
|
|
|
static const Precedence& InitialPrecedence()
|
2008-11-05 23:55:53 +01:00
|
|
|
{
|
|
|
|
static Precedence initial_(0);
|
|
|
|
return initial_;
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
static const Precedence& FrozenPrecedence()
|
2008-11-05 23:55:53 +01:00
|
|
|
{
|
|
|
|
static Precedence frozen_(-1);
|
|
|
|
return frozen_;
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
Precedence() :
|
|
|
|
precedence_()
|
|
|
|
{
|
2008-11-05 23:55:53 +01:00
|
|
|
//precedence_.push_back(0);
|
2008-11-05 03:57:18 +01:00
|
|
|
} // Precedence
|
|
|
|
|
|
|
|
Precedence(const Precedence& rhs) :
|
|
|
|
precedence_(rhs.precedence_)
|
|
|
|
{
|
|
|
|
} // Precedence
|
|
|
|
|
|
|
|
private:
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence(const std::vector<int> precedence) :
|
2008-11-05 03:57:18 +01:00
|
|
|
precedence_(precedence)
|
|
|
|
{
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence(int precedence) :
|
|
|
|
precedence_()
|
|
|
|
{
|
|
|
|
precedence_.push_back(precedence);
|
|
|
|
} // Precedence
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
public:
|
|
|
|
~Precedence() { }
|
|
|
|
|
|
|
|
bool operator==(const Precedence& rhs) const
|
|
|
|
{
|
|
|
|
return precedence_ == rhs.precedence_;
|
|
|
|
} // operator==
|
|
|
|
|
2008-11-05 23:55:53 +01:00
|
|
|
bool operator>(const Precedence& rhs) const
|
|
|
|
{
|
|
|
|
return precedence_ > rhs.precedence_;
|
|
|
|
} // operator>
|
|
|
|
|
2008-11-05 03:57:18 +01:00
|
|
|
Precedence& operator=(const Precedence& rhs)
|
|
|
|
{
|
2008-11-25 00:11:22 +01:00
|
|
|
std::vector<int> other(rhs.precedence_);
|
|
|
|
precedence_.swap(other);
|
2008-11-05 03:57:18 +01:00
|
|
|
return *this;
|
|
|
|
} // operator=
|
|
|
|
|
|
|
|
Precedence nextGeneration(int p) const
|
|
|
|
{
|
2008-11-25 00:11:22 +01:00
|
|
|
Precedence next(precedence_);
|
|
|
|
next.precedence_.push_back(p);
|
2008-11-05 03:57:18 +01:00
|
|
|
return next;
|
|
|
|
} // nextGeneration
|
|
|
|
|
|
|
|
private:
|
2008-11-25 00:11:22 +01:00
|
|
|
std::vector<int> precedence_;
|
2008-11-24 23:05:16 +01:00
|
|
|
|
|
|
|
friend bool operator<(const Precedence& lhs, const Precedence& rhs);
|
2008-11-25 00:11:22 +01:00
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Precedence& prec);
|
2008-11-05 03:57:18 +01:00
|
|
|
}; // class Precedence
|
|
|
|
|
2008-11-24 23:05:16 +01:00
|
|
|
bool operator<(const Precedence& lhs, const Precedence& rhs)
|
|
|
|
{
|
2008-11-25 00:11:22 +01:00
|
|
|
return lhs.precedence_.back() < rhs.precedence_.back();
|
2008-11-24 23:05:16 +01:00
|
|
|
} // PrecedenceCompare
|
|
|
|
|
2008-11-25 00:11:22 +01:00
|
|
|
std::ostream& operator<<(std::ostream& os, const Precedence& prec)
|
|
|
|
{
|
|
|
|
os << "(";
|
|
|
|
for(std::vector<int>::const_iterator p = prec.precedence_.begin(), pe = prec.precedence_.end(); p != pe; ++p)
|
|
|
|
{
|
|
|
|
if(p != prec.precedence_.begin())
|
|
|
|
os << ",";
|
|
|
|
os << *p;
|
|
|
|
} // for ..
|
|
|
|
os << ")";
|
|
|
|
} // operator<<
|
|
|
|
|
2008-11-05 23:55:53 +01:00
|
|
|
class PrecedenceStack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrecedenceStack() :
|
2008-11-25 00:11:22 +01:00
|
|
|
stack_(),
|
|
|
|
count_(1)
|
2008-11-05 23:55:53 +01:00
|
|
|
{
|
2008-11-19 18:26:07 +01:00
|
|
|
stack_.push(Precedence::InitialPrecedence());
|
2008-11-05 23:55:53 +01:00
|
|
|
} // PrecedenceStack
|
|
|
|
|
|
|
|
PrecedenceStack(const PrecedenceStack& rhs) :
|
|
|
|
stack_(rhs.stack_)
|
|
|
|
{
|
|
|
|
} // PrecedenceStack
|
|
|
|
|
2008-11-19 18:26:07 +01:00
|
|
|
const Precedence& top() const { return stack_.top(); }
|
2008-11-25 00:11:22 +01:00
|
|
|
void push()
|
|
|
|
{
|
|
|
|
stack_.push(top().nextGeneration(count_++));
|
|
|
|
std::cout << "Pushed " << top() << std::endl;
|
|
|
|
}
|
2008-11-19 18:26:07 +01:00
|
|
|
void pop() { stack_.pop(); }
|
2008-11-05 23:55:53 +01:00
|
|
|
void freeze()
|
|
|
|
{
|
2008-11-19 18:26:07 +01:00
|
|
|
while(!stack_.empty())
|
|
|
|
stack_.pop();
|
|
|
|
stack_.push(Precedence::FrozenPrecedence());
|
2008-11-05 23:55:53 +01:00
|
|
|
} // freeze
|
|
|
|
|
|
|
|
private:
|
2008-11-19 18:26:07 +01:00
|
|
|
std::stack<Precedence> stack_;
|
2008-11-25 00:11:22 +01:00
|
|
|
int count_;
|
2008-11-05 23:55:53 +01:00
|
|
|
}; // class PrecedenceStack
|
2008-11-05 03:57:18 +01:00
|
|
|
#endif
|