Boost logo

Boost-Commit :

From: andreas.pokorny_at_[hidden]
Date: 2007-08-19 12:16:28


Author: mumiee
Date: 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
New Revision: 38761
URL: http://svn.boost.org/trac/boost/changeset/38761

Log:
* Addition of a compiler for nested dynamic regular expressions.

Added:
   branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matcher/basic_regex_ptr_matcher.hpp (contents, props changed)
   branches/xpressive/nested_dynamic_regex/boost/xpressive/nested_dynamic_compiler.hpp (contents, props changed)
Text files modified:
   branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matchers.hpp | 1 +
   branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser.hpp | 15 +++++++++++++++
   branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser_traits.hpp | 15 +++++++++++++++
   branches/xpressive/nested_dynamic_regex/boost/xpressive/regex_compiler.hpp | 24 ++++++++++++++++++++----
   branches/xpressive/nested_dynamic_regex/boost/xpressive/xpressive_fwd.hpp | 7 +++++++
   5 files changed, 58 insertions(+), 4 deletions(-)

Added: branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matcher/basic_regex_ptr_matcher.hpp
==============================================================================
--- (empty file)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matcher/basic_regex_ptr_matcher.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -0,0 +1,73 @@
+///////////////////////////////////////////////////////////////////////////////
+// basic_regex_ptr_matcher
+//
+// Copyright 2007 Andreas Pokorny. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_XPRESSIVE_DETAIL_CORE_MATCHER_BASIC_REGEX_PTR_MATCHER_HPP_INCLUDED
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+#include <boost/assert.hpp>
+#include <boost/mpl/assert.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/xpressive/regex_error.hpp>
+#include <boost/xpressive/regex_constants.hpp>
+#include <boost/xpressive/detail/detail_fwd.hpp>
+#include <boost/xpressive/detail/core/quant_style.hpp>
+#include <boost/xpressive/detail/core/state.hpp>
+#include <boost/xpressive/detail/core/regex_impl.hpp>
+#include <boost/xpressive/detail/core/adaptor.hpp>
+#include <boost/xpressive/detail/core/access.hpp>
+
+namespace boost { namespace xpressive { namespace detail
+{
+ ///////////////////////////////////////////////////////////////////////////////
+ // basic_regex_ptr_matcher
+ //
+ template<typename BidiIter>
+ struct basic_regex_ptr_matcher
+ : quant_style<quant_variable_width, unknown_width::value, false>
+ {
+ basic_regex<BidiIter> const * pimpl_;
+
+ basic_regex_ptr_matcher(basic_regex<BidiIter> const* ptr)
+ : pimpl_(ptr)
+ {
+ BOOST_ASSERT(this->pimpl_);
+ }
+
+ template<typename Next>
+ bool match(match_state<BidiIter> &state, Next const &next) const
+ {
+ ensure(core_access<BidiIter>::get_regex_impl(*this->pimpl_)->xpr_, regex_constants::error_badref, "bad regex reference");
+
+ return push_context_match(
+ *core_access<BidiIter>::get_regex_impl(*this->pimpl_),
+ state,
+ this->wrap_(next, is_static_xpression<Next>())
+ );
+ }
+
+ private:
+ template<typename Next>
+ static xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> > wrap_(Next const &next, mpl::true_)
+ {
+ // wrap the static xpression in a matchable interface
+ return xpression_adaptor<reference_wrapper<Next const>, matchable<BidiIter> >(boost::cref(next));
+ }
+
+ template<typename Next>
+ static Next const &wrap_(Next const &next, mpl::false_)
+ {
+ return next;
+ }
+ };
+
+}}}
+
+#endif

Modified: branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matchers.hpp
==============================================================================
--- branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matchers.hpp (original)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/core/matchers.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -23,6 +23,7 @@
 #include <boost/xpressive/detail/core/matcher/assert_eos_matcher.hpp>
 #include <boost/xpressive/detail/core/matcher/assert_word_matcher.hpp>
 #include <boost/xpressive/detail/core/matcher/attr_matcher.hpp>
+#include <boost/xpressive/detail/core/matcher/basic_regex_ptr_matcher.hpp>
 #include <boost/xpressive/detail/core/matcher/charset_matcher.hpp>
 #include <boost/xpressive/detail/core/matcher/end_matcher.hpp>
 #include <boost/xpressive/detail/core/matcher/epsilon_matcher.hpp>

Modified: branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser.hpp
==============================================================================
--- branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser.hpp (original)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -134,6 +134,21 @@
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+// make_reference_expression
+//
+template<typename BidiIter>
+inline sequence<BidiIter> make_reference_expression
+(
+ basic_regex<BidiIter> const *ptr
+)
+{
+ BOOST_ASSERT(ptr);
+ basic_regex_ptr_matcher<BidiIter> matcher(ptr);
+ return make_dynamic<BidiIter>(matcher);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
 // make_backref_xpression
 //
 template<typename BidiIter, typename Traits>

Modified: branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser_traits.hpp
==============================================================================
--- branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser_traits.hpp (original)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/detail/dynamic/parser_traits.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -447,6 +447,21 @@
         return begin;
     }
 
+
+ ///////////////////////////////////////////////////////////////////////////////
+ // is_reference
+ bool is_reference( string_type const& t )
+ {
+ return false;
+ }
+
+ template<typename BidiIter>
+ basic_regex<BidiIter> const* get_reference( string_type const& name )
+ {
+ return NULL;
+ }
+
+
     regex_traits traits_;
     regex_constants::syntax_option_type flags_;
     typename regex_traits::char_class_type space_;

Added: branches/xpressive/nested_dynamic_regex/boost/xpressive/nested_dynamic_compiler.hpp
==============================================================================
--- (empty file)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/nested_dynamic_compiler.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -0,0 +1,126 @@
+///////////////////////////////////////////////////////////////////////////////
+/// \file nested_dynamic_compiler.hpp
+/// Contains the definition of an extended regex_compiler, which supports
+/// nested regular expressions.
+//
+// Copyright 2007 Andreas Pokorny. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_XPRESSIVE_NESTED_DYNAMIC_COMPILER_HPP_INCLUDED
+#define BOOST_XPRESSIVE_NESTED_DYNAMIC_COMPILER_HPP_INCLUDED
+
+// MS compatible compilers support #pragma once
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+
+#include <boost/xpressive/xpressive_fwd.hpp>
+#include <boost/xpressive/regex_compiler.hpp>
+#include <boost/xpressive/detail/dynamic/parser_traits.hpp>
+
+namespace boost{ namespace xpressive{
+ namespace detail{
+ template<typename BidiIter, typename RegexTraits>
+ struct nested_parser
+ : compiler_traits<RegexTraits>
+ {
+ typedef typename compiler_traits<RegexTraits>::string_type string_type;
+
+ struct _values
+ {
+ string_type expression;
+ basic_regex<BidiIter> regex;
+ };
+ typedef std::map<string_type,_values> table_type;
+ typedef typename table_type::value_type value_type;
+
+ nested_parser( RegexTraits const& t )
+ : compiler_traits<RegexTraits>(t),
+ start(0)
+ {}
+
+ bool is_reference( string_type const& name )
+ {
+ return this->table.find(name) != this->table.end();
+ }
+
+ void add_start(string_type const& name, string_type const& regex)
+ {
+ table[name].expression = regex;
+ start = &table[name];
+ }
+
+ void add(string_type const& name, string_type const& regex)
+ {
+ table[name].expression = regex;
+ }
+
+ basic_regex<BidiIter> const* get_reference( string_type const& name )
+ {
+ typename table_type::const_iterator it = this->table.find(name);
+ if( it != this->table.end() )
+ {
+ return &it->second.regex;
+ }
+ return NULL;
+ }
+
+ _values * start;
+ table_type table;
+ };
+ }
+
+ template<typename BidiIter, typename RegexTraits>
+ struct nested_dynamic_compiler :
+ regex_compiler<BidiIter, RegexTraits, detail::nested_parser<BidiIter, RegexTraits> >
+ {
+ typedef detail::nested_parser<BidiIter,RegexTraits> traits_type;
+ typedef typename traits_type::table_type table_type;
+ typedef typename traits_type::string_type string_type;
+ typedef regex_compiler<BidiIter, RegexTraits, traits_type > base_compiler_type;
+ typedef typename base_compiler_type::flag_type flag_type;
+ explicit nested_dynamic_compiler(
+ RegexTraits const &traits = RegexTraits()
+ )
+ : regex_compiler<
+ BidiIter,
+ RegexTraits,
+ traits_type
+ >(traits)
+ {
+ }
+
+ using base_compiler_type::compile;
+
+ void compile(flag_type flags = regex_constants::ECMAScript )
+ {
+ for( typename table_type::iterator it = this->traits().table.begin(),
+ e = this->traits().table.end();
+ it != e;
+ ++it )
+ {
+ it->second.regex = this->compile(it->second.expression, flags );
+ }
+ }
+
+ basic_regex<BidiIter> & regex()
+ {
+ BOOST_ASSERT(this->traits().start);
+ return this->traits().start->regex;
+ }
+
+ void add_start(string_type const& name, string_type const& regex)
+ {
+ this->traits().add_start( name, regex );
+ }
+
+ void add(string_type const& name, string_type const& regex)
+ {
+ this->traits().add( name, regex );
+ }
+ };
+}}
+#endif
+

Modified: branches/xpressive/nested_dynamic_regex/boost/xpressive/regex_compiler.hpp
==============================================================================
--- branches/xpressive/nested_dynamic_regex/boost/xpressive/regex_compiler.hpp (original)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/regex_compiler.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -164,6 +164,11 @@
         BOOST_ASSERT(!name.empty());
         return this->rules_[name];
     }
+protected:
+ CompilerTraits & traits()
+ {
+ return traits_;
+ }
 
 private:
 
@@ -476,10 +481,20 @@
         switch(this->traits_.get_token(begin, end))
         {
         case token_literal:
- return detail::make_literal_xpression<BidiIter>
- (
- this->parse_literal(begin, end), this->traits_.flags(), this->rxtraits()
- );
+ {
+ string_type lit = this->parse_literal(begin, end);
+ if( this->traits_.is_reference(lit) )
+ {
+ return detail::make_reference_expression<BidiIter>(
+ this->traits_.get_reference(lit)
+ );
+ }
+ else
+ return detail::make_literal_xpression<BidiIter>
+ (
+ lit, this->traits_.flags(), this->rxtraits()
+ );
+ }
 
         case token_any:
             return detail::make_any_xpression<BidiIter>(this->traits_.flags(), this->rxtraits());
@@ -618,6 +633,7 @@
 
         return seq;
     }
+
 
     ///////////////////////////////////////////////////////////////////////////
     // parse_literal

Modified: branches/xpressive/nested_dynamic_regex/boost/xpressive/xpressive_fwd.hpp
==============================================================================
--- branches/xpressive/nested_dynamic_regex/boost/xpressive/xpressive_fwd.hpp (original)
+++ branches/xpressive/nested_dynamic_regex/boost/xpressive/xpressive_fwd.hpp 2007-08-19 12:16:26 EDT (Sun, 19 Aug 2007)
@@ -133,6 +133,13 @@
>
     struct regex_compiler;
 
+ template
+ <
+ typename BidiIter
+ , typename RegexTraits = regex_traits<typename iterator_value<BidiIter>::type>
+ >
+ struct nested_dynamic_compiler;
+
     ///////////////////////////////////////////////////////////////////////////////
     // Common typedefs
     //


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk