// This file is distributed under the BSD License. // See LICENSE.TXT for details. #ifndef LANGKIT_PARSER_HPP_ #define LANGKIT_PARSER_HPP_ #include #include "langkit_lexer.hpp" typedef std::vector::iterator Token_Iterator; typedef boost::function(Token_Iterator, Token_Iterator, TokenPtr, bool, int)> RuleFun; typedef std::tr1::shared_ptr RuleImplPtr; struct RuleImpl { RuleFun rule; bool keep; int new_id; RuleImpl() : keep(true), new_id(-1) { } std::pair operator()(Token_Iterator iter, Token_Iterator end, TokenPtr parent) { return rule(iter, end, parent, keep, new_id); } }; std::pair String_Rule (Token_Iterator iter, Token_Iterator end, TokenPtr parent, bool keep, int new_id, const std::string &val); std::pair Type_Rule (Token_Iterator iter, Token_Iterator end, TokenPtr parent, bool keep, int new_id, const int val); std::pair Or_Rule (Token_Iterator iter, Token_Iterator end, TokenPtr parent, bool keep, int new_id, struct Rule lhs, struct Rule rhs); std::pair And_Rule (Token_Iterator iter, Token_Iterator end, TokenPtr parent, bool keep, int new_id, struct Rule lhs, struct Rule rhs); struct Rule { RuleImplPtr impl; Rule() : impl(new RuleImpl()) { } Rule(int id) : impl(new RuleImpl()) { impl->new_id = id; } Rule(RuleFun fun) : impl(new RuleImpl()) { impl->rule = fun; } Rule(RuleFun fun, bool keep) : impl(new RuleImpl()) { impl->rule = fun; impl->keep = keep; } std::pair operator()(Token_Iterator iter, Token_Iterator end, TokenPtr parent) { return (*impl)(iter, end, parent); } Rule &operator=(const Rule &rule) { *impl = *(rule.impl); return *this; } Rule operator|(const Rule &rhs) { return Rule(boost::bind(Or_Rule, _1, _2, _3, _4, _5, *this, rhs)); } Rule operator<<(const Rule &rhs) { return Rule(boost::bind(And_Rule, _1, _2, _3, _4, _5, *this, rhs)); } //const RuleImplPtr get_impl() const { return impl; } //private: //RuleImplPtr impl; }; Rule Str(const std::string &text, bool keep); Rule Id(int id, bool keep); Rule Str(const std::string &text); Rule Id(int id); Rule Ign(Rule rule); #endif /* LANGKIT_PARSER_HPP_ */