From 7c9492a63db4d36ae719e7c56c6616ccaae67280 Mon Sep 17 00:00:00 2001 From: jez_higgins <> Date: Fri, 19 Aug 2005 17:20:06 +0000 Subject: [PATCH] parameterised node_iter_t and chums --- XPath/impl/xpath_ast.hpp | 23 +++++--- XPath/impl/xpath_parser.hpp | 110 ++++++++++++++++++------------------ XPath/impl/xpath_step.hpp | 34 +++++------ 3 files changed, 88 insertions(+), 79 deletions(-) diff --git a/XPath/impl/xpath_ast.hpp b/XPath/impl/xpath_ast.hpp index bad35e09..d0735596 100644 --- a/XPath/impl/xpath_ast.hpp +++ b/XPath/impl/xpath_ast.hpp @@ -11,20 +11,29 @@ namespace Arabica namespace XPath { -typedef std::string::const_iterator str_iter_t; +template +class types +{ +public: + typedef typename string_type::const_iterator str_iter_t; + typedef boost::spirit::tree_match tree_match_t; + typedef typename tree_match_t::tree_iterator node_iter_t; + typedef boost::spirit::tree_parse_info tree_info_t; -typedef boost::spirit::tree_match tree_match_t; -typedef tree_match_t::tree_iterator node_iter_t; -typedef boost::spirit::tree_parse_info tree_info_t; +private: + ~types(); +}; // types -inline long getNodeId(node_iter_t const& node) +template +long getNodeId(typename types::node_iter_t const& node) { return static_cast(node->value.id().to_long()); } // getNodeId -inline node_iter_t& skipWhitespace(node_iter_t& node) +template +typename types::node_iter_t& skipWhitespace(typename types::node_iter_t& node) { - while(getNodeId(node) == impl::S_id) + while(getNodeId(node) == impl::S_id) ++node; return node; } // skipWhitespace diff --git a/XPath/impl/xpath_parser.hpp b/XPath/impl/xpath_parser.hpp index 5f4bd9f1..45936eb2 100644 --- a/XPath/impl/xpath_parser.hpp +++ b/XPath/impl/xpath_parser.hpp @@ -101,9 +101,9 @@ public: private: XPathExpressionPtr do_compile(const string_type& xpath, - tree_info_t(XPath::*fn)(const string_type& str) const) const + typename types::tree_info_t(XPath::*fn)(const string_type& str) const) const { - tree_info_t ast; + types::tree_info_t ast; try { ast = (this->*fn)(xpath); if(!ast.full) @@ -122,15 +122,15 @@ private: } // catch } // do_compile - tree_info_t parse_xpath(const string_type& str) const + typename types::tree_info_t parse_xpath(const string_type& str) const { - str_iter_t first = str.begin(), last = str.end(); + types::str_iter_t first = str.begin(), last = str.end(); return ast_parse(first, last, xpathg_); } // parse_xpath - tree_info_t parse_xpath_expr(const string_type& str) const + typename types::tree_info_t parse_xpath_expr(const string_type& str) const { - str_iter_t first = str.begin(), last = str.end(); + types::str_iter_t first = str.begin(), last = str.end(); return ast_parse(first, last, xpathge_); } // parse_xpath @@ -143,10 +143,10 @@ private: ///////////////////////////////////////////////////////////////////////////////// public: - static XPathExpression* compile_expression(node_iter_t const& i, + static XPathExpression* compile_expression(typename types::node_iter_t const& i, CompilationContext& context) { - long id = getNodeId(i); + long id = getNodeId(i); if(XPath::factory().find(id) == XPath::factory().end()) { @@ -158,22 +158,22 @@ public: } // compile_expression private: - static XPathExpression* createAbsoluteLocationPath(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createRelativeLocationPath(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createSingleStepRelativeLocationPath(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createExpression(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createFunction(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createBinaryExpression(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createLiteral(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createNumber(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createVariable(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createSingleStepAbsoluteLocationPath(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createUnaryExpression(node_iter_t const& i, CompilationContext& context); - static XPathExpression* createUnaryNegativeExpr(node_iter_t const& i, CompilationContext& context); + static XPathExpression* createAbsoluteLocationPath(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createRelativeLocationPath(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createSingleStepRelativeLocationPath(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createExpression(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createFunction(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createBinaryExpression(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createLiteral(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createNumber(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createVariable(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createSingleStepAbsoluteLocationPath(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createUnaryExpression(typename types::node_iter_t const& i, CompilationContext& context); + static XPathExpression* createUnaryNegativeExpr(typename types::node_iter_t const& i, CompilationContext& context); - static StepList createStepList(node_iter_t const& from, node_iter_t const& to, CompilationContext& context); + static StepList createStepList(typename types::node_iter_t const& from, typename types::node_iter_t const& to, CompilationContext& context); - typedef XPathExpression* (*compileFn)(node_iter_t const& i, CompilationContext& context); + typedef XPathExpression* (*compileFn)(typename types::node_iter_t const& i, CompilationContext& context); static std::map& factory() { static std::map f = init_createFunctions(); @@ -235,7 +235,7 @@ private: static const std::map init_debugNames() { - std::map names; + std::map names; names[impl::LocationPath_id] = "LocationPath"; names[impl::AbsoluteLocationPath_id] = "AbsoluteLocationPath"; @@ -331,7 +331,7 @@ private: } // init_debugNames /* - static void dump(node_iter_t const& i, int depth) + static void dump(typename types::node_iter_t const& i, int depth) { long id = static_cast(i->value.id().to_long()); @@ -339,7 +339,7 @@ private: std::cerr << ' '; std::cerr << names()[id] << " - " << std::string(i->value.begin(), i->value.end()) << std::endl; - for(node_iter_t c = i->children.begin(); c != i->children.end(); ++c) + for(typename types::node_iter_t c = i->children.begin(); c != i->children.end(); ++c) dump(c, depth+2); } // dump */ @@ -371,50 +371,50 @@ namespace XPath { template -XPathExpression* XPath::createAbsoluteLocationPath(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createAbsoluteLocationPath(typename types::node_iter_t const& i, CompilationContext& context) { return new AbsoluteLocationPath(createStepList(i->children.begin(), i->children.end(), context)); } // createAbsoluteLocationPath template -XPathExpression* XPath::createRelativeLocationPath(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createRelativeLocationPath(typename types::node_iter_t const& i, CompilationContext& context) { return new RelativeLocationPath(createStepList(i->children.begin(), i->children.end(), context)); } // createRelativeLocationPath template -XPathExpression* XPath::createSingleStepRelativeLocationPath(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createSingleStepRelativeLocationPath(typename types::node_iter_t const& i, CompilationContext& context) { - node_iter_t n = i; + typename types::node_iter_t n = i; return new RelativeLocationPath(StepFactory::createStep(n, context)); } // createSingleStepRelativeLocationPath template -XPathExpression* XPath::createExpression(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createExpression(typename types::node_iter_t const& i, CompilationContext& context) { - node_iter_t c = i->children.begin(); - skipWhitespace(c); + typename types::node_iter_t c = i->children.begin(); + skipWhitespace(c); return XPath::compile_expression(c, context); } // createExpression template -XPathExpression* XPath::createFunction(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createFunction(typename types::node_iter_t const& i, CompilationContext& context) { - node_iter_t c = i->children.begin(); + typename types::node_iter_t c = i->children.begin(); string_type name(c->value.begin(), c->value.end()); ++c; - skipWhitespace(c); - assert(getNodeId(c) == impl::LeftBracket_id); + skipWhitespace(c); + assert(getNodeId(c) == impl::LeftBracket_id); ++c; - skipWhitespace(c); + skipWhitespace(c); std::vector > args; - while(getNodeId(c) != impl::RightBracket_id) + while(getNodeId(c) != impl::RightBracket_id) { XPathExpressionPtr arg(XPath::compile_expression(c++, context)); args.push_back(arg); - skipWhitespace(c); + skipWhitespace(c); } // while ... // maybe trailing whitespace ... @@ -422,15 +422,15 @@ XPathExpression* XPath } // createFunction template -XPathExpression* XPath::createBinaryExpression(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createBinaryExpression(typename types::node_iter_t const& i, CompilationContext& context) { - node_iter_t c = i->children.begin(); + typename types::node_iter_t c = i->children.begin(); XPathExpression* p1 = XPath::compile_expression(c, context); ++c; do { - long op = getNodeId(c); + long op = getNodeId(c); ++c; XPathExpression* p2 = XPath::compile_expression(c, context); @@ -488,55 +488,55 @@ XPathExpression* XPath } // createBinaryExpression template -XPathExpression* XPath::createLiteral(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createLiteral(typename types::node_iter_t const& i, CompilationContext& context) { string_type str(i->value.begin(), i->value.end()); return new StringValue(str); } // createLiteral template -XPathExpression* XPath::createNumber(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createNumber(typename types::node_iter_t const& i, CompilationContext& context) { return new NumericValue(boost::lexical_cast(string_type(i->value.begin(), i->value.end()))); } // createNumber template -XPathExpression* XPath::createVariable(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createVariable(typename types::node_iter_t const& i, CompilationContext& context) { return new Variable(string_type(i->value.begin()+1, i->value.end())); // skip $ } // createVariable template -XPathExpression* XPath::createSingleStepAbsoluteLocationPath(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createSingleStepAbsoluteLocationPath(typename types::node_iter_t const& i, CompilationContext& context) { - node_iter_t n = i; + typename types::node_iter_t n = i; return new AbsoluteLocationPath(StepFactory::createStep(n, context)); } // createSingleStepAbsoluteLocationPath template -XPathExpression* XPath::createUnaryExpression(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createUnaryExpression(typename types::node_iter_t const& i, CompilationContext& context) { return XPath::compile_expression(i->children.begin(), context); } // createUnaryExpression template -XPathExpression* XPath::createUnaryNegativeExpr(node_iter_t const& i, CompilationContext& context) +XPathExpression* XPath::createUnaryNegativeExpr(typename types::node_iter_t const& i, CompilationContext& context) { return new UnaryNegative(XPath::compile_expression(i+1, context)); } // createUnaryNegativeExpr template -StepList XPath::createStepList(node_iter_t const& from, - node_iter_t const& to, +StepList XPath::createStepList(typename types::node_iter_t const& from, + typename types::node_iter_t const& to, CompilationContext& context) { StepList steps; - node_iter_t c = from; - node_iter_t end = to; + typename types::node_iter_t c = from; + typename types::node_iter_t end = to; while(c != end) - switch(getNodeId(c)) + switch(getNodeId(c)) { case impl::S_id: case impl::Slash_id: @@ -549,7 +549,7 @@ StepList XPath::create break; case impl::Step_id: { - node_iter_t step = c->children.begin(); + typename types::node_iter_t step = c->children.begin(); steps.push_back(StepFactory::createStep(step, c->children.end(), context)); ++c; } diff --git a/XPath/impl/xpath_step.hpp b/XPath/impl/xpath_step.hpp index 7e29d70e..f70a2310 100644 --- a/XPath/impl/xpath_step.hpp +++ b/XPath/impl/xpath_step.hpp @@ -184,8 +184,8 @@ class StepFactory { public: static StepExpression* - createStep(node_iter_t& node, - node_iter_t const& end, + createStep(typename types::node_iter_t& node, + typename types::node_iter_t const& end, CompilationContext& context) { Axis axis = getAxis(node); @@ -196,14 +196,14 @@ public: std::vector*> preds; - while((node != end) && (getNodeId(node) == impl::Predicate_id)) + while((node != end) && (getNodeId(node) == impl::Predicate_id)) { - node_iter_t c = node->children.begin(); - assert(getNodeId(c) == impl::LeftSquare_id); + typename types::node_iter_t c = node->children.begin(); + assert(getNodeId(c) == impl::LeftSquare_id); ++c; preds.push_back(XPath::compile_expression(c, context)); ++c; - assert(getNodeId(c) == impl::RightSquare_id); + assert(getNodeId(c) == impl::RightSquare_id); ++node; } // if ... @@ -212,7 +212,7 @@ public: return new TestStepExpression(axis, test, preds); } // createStep - static StepExpression* createStep(node_iter_t& node, CompilationContext& context) + static StepExpression* createStep(typename types::node_iter_t& node, CompilationContext& context) { Axis axis = getAxis(node); NodeTest* test = getTest(node, context.namespaceContext()); @@ -220,9 +220,9 @@ public: } // createStep private: - static Axis getAxis(node_iter_t& node) + static Axis getAxis(typename types::node_iter_t& node) { - long id = getNodeId(node); + long id = getNodeId(node); switch(id) { @@ -246,8 +246,8 @@ private: return CHILD; } // switch(id) - node_iter_t axis_node = node->children.begin(); - long axis = getNodeId(skipWhitespace(axis_node)); + typename types::node_iter_t axis_node = node->children.begin(); + long axis = getNodeId(skipWhitespace(axis_node)); ++node; switch(axis) { @@ -284,15 +284,15 @@ private: return CHILD; } // getAxis - static NodeTest* getTest(node_iter_t& node, const NamespaceContext& namespaceContext) + static NodeTest* getTest(typename types::node_iter_t& node, const NamespaceContext& namespaceContext) { - long id = getNodeId(skipWhitespace(node)); + long id = getNodeId(skipWhitespace(node)); switch(id) { case impl::NodeTest_id: { - node_iter_t c = node->children.begin(); + typename types::node_iter_t c = node->children.begin(); NodeTest* t = getTest(c, namespaceContext); ++node; return t; @@ -300,7 +300,7 @@ private: case impl::QName_id: { - node_iter_t c = node->children.begin(); + typename types::node_iter_t c = node->children.begin(); string_type prefix(c->value.begin(), c->value.end()); string_type uri = namespaceContext.namespaceURI(prefix); ++c; @@ -331,7 +331,7 @@ private: case impl::ProcessingInstruction_id: { ++node; - if(getNodeId(node) != impl::Literal_id) // not sure if this is always safe + if(getNodeId(node) != impl::Literal_id) // not sure if this is always safe return new ProcessingInstructionNodeTest(); string_type target(node->value.begin(), node->value.end()); @@ -359,7 +359,7 @@ private: case impl::NameTest_id: { - node_iter_t prefixNode = node->children.begin(); + typename types::node_iter_t prefixNode = node->children.begin(); ++node; string_type prefix(prefixNode->value.begin(), prefixNode->value.end()); string_type uri = namespaceContext.namespaceURI(prefix);