|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r69164 - branches/quickbook-filenames/tools/quickbook/src
From: dnljms_at_[hidden]
Date: 2011-02-22 15:27:29
Author: danieljames
Date: 2011-02-22 15:27:27 EST (Tue, 22 Feb 2011)
New Revision: 69164
URL: http://svn.boost.org/trac/boost/changeset/69164
Log:
Generalize the lookbehind parser, and use it to implement a cleaner simple_markup parser.
Added:
branches/quickbook-filenames/tools/quickbook/src/parsers.hpp (contents, props changed)
Removed:
branches/quickbook-filenames/tools/quickbook/src/scoped_parser.hpp
Text files modified:
branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp | 2
branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp | 79 ++++++++-------------------------------
2 files changed, 17 insertions(+), 64 deletions(-)
Modified: branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/actions_class.hpp 2011-02-22 15:27:27 EST (Tue, 22 Feb 2011)
@@ -11,7 +11,7 @@
#define BOOST_SPIRIT_ACTIONS_CLASS_HPP
#include "actions.hpp"
-#include "scoped_parser.hpp"
+#include "parsers.hpp"
#include "values_parse.hpp"
#include <boost/tuple/tuple.hpp>
#include <boost/scoped_ptr.hpp>
Modified: branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp (original)
+++ branches/quickbook-filenames/tools/quickbook/src/main_grammar.cpp 2011-02-22 15:27:27 EST (Tue, 22 Feb 2011)
@@ -12,6 +12,7 @@
#include "actions_class.hpp"
#include "utils.hpp"
#include "template_tags.hpp"
+#include "parsers.hpp"
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_confix.hpp>
#include <boost/spirit/include/classic_chset.hpp>
@@ -22,63 +23,7 @@
namespace quickbook
{
namespace cl = boost::spirit::classic;
-
- struct simple_markup_start_parser
- : public cl::unary< cl::chlit<char>, cl::parser< simple_markup_start_parser > >
- {
- typedef simple_markup_start_parser self_t;
- typedef cl::unary< cl::chlit<char>, cl::parser< simple_markup_start_parser > > base_t;
-
- template <typename ScannerT>
- struct result
- {
- typedef typename cl::parser_result<cl::chlit<char>, ScannerT>::type type;
- };
-
- simple_markup_start_parser(char c)
- : base_t(cl::ch_p(c))
- , mark_(c)
- {}
-
- template <typename ScannerT>
- typename result<ScannerT>::type parse(ScannerT const &scan) const
- {
- typedef typename ScannerT::iterator_t iterator_t;
- iterator_t save = scan.first;
-
- typename cl::parser_result<cl::chlit<char>, ScannerT>::type result
- = this->subject().parse(scan);
-
- typename iterator_t::lookback_range::iterator
- lookback_begin = save.lookback().begin(),
- lookback_end = save.lookback().end();
-
- if (lookback_begin != lookback_end) {
- if (*lookback_begin == mark_)
- return scan.no_match();
-
- if (!cl::punct_p.test(*lookback_begin) &&
- !cl::space_p.test(*lookback_begin))
- return scan.no_match();
- }
-
- if (result)
- {
- return scan.create_match(result.length(), cl::nil_t(), save, scan.first);
- }
- else {
- return scan.no_match();
- }
- }
-
- char mark_;
- };
- simple_markup_start_parser start_parser(char c)
- {
- return simple_markup_start_parser(c);
- }
-
template <typename Rule, typename Action>
inline void
simple_markup(
@@ -89,12 +34,21 @@
)
{
simple =
- start_parser(mark) // first mark must be preceeded
+ mark
+ >> lookback
+ [ cl::anychar_p
+ >> ~cl::eps_p(mark) // first mark not be preceeded by
+ // the same character.
+ >> (cl::space_p | cl::punct_p | cl::end_p)
+ // first mark must be preceeded
// by space or punctuation or the
- // mark character.
- >> cl::eps_p(cl::graph_p) // graph_p must follow first mark
- >> ( *(cl::anychar_p -
- ( cl::graph_p // graph_p must precede final mark
+ // mark character or a the start.
+ ]
+ >> ( cl::graph_p // graph_p must follow first mark
+ >> *( cl::anychar_p -
+ ( lookback[cl::graph_p]
+ // final mark must be preceeded by
+ // graph_p
>> mark
>> ~cl::eps_p(mark) // final mark not be followed by
// the same character.
@@ -105,8 +59,7 @@
// past a single block
)
)
- >> cl::graph_p
- >> cl::eps_p(mark >> (cl::space_p | cl::punct_p | cl::end_p))
+ >> cl::eps_p(mark)
) [action]
>> mark
;
Added: branches/quickbook-filenames/tools/quickbook/src/parsers.hpp
==============================================================================
--- (empty file)
+++ branches/quickbook-filenames/tools/quickbook/src/parsers.hpp 2011-02-22 15:27:27 EST (Tue, 22 Feb 2011)
@@ -0,0 +1,145 @@
+/*=============================================================================
+ Copyright (c) 2010-2011 Daniel James
+ Copyright (c) 2003 Martin Wille
+ http://spirit.sourceforge.net/
+
+ 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)
+ =============================================================================*/
+
+// Some custom parsers for use in quickbook.
+
+#ifndef BOOST_QUICKBOOK_PARSERS_HPP
+#define BOOST_QUICKBOOK_PARSERS_HPP
+
+#include <boost/spirit/include/classic_core.hpp>
+#include <boost/spirit/include/classic_nil.hpp>
+
+namespace quickbook {
+ namespace cl = boost::spirit::classic;
+
+ // Used to store variables/state while parsing
+
+ template <typename ScopeT, typename DataT, typename ParserT>
+ struct scoped_parser_impl
+ : public cl::unary< ParserT, cl::parser< scoped_parser_impl<ScopeT, DataT, ParserT> > >
+ {
+ typedef scoped_parser_impl<ScopeT, DataT, ParserT> self_t;
+ typedef cl::unary< ParserT, cl::parser< scoped_parser_impl<ScopeT, DataT, ParserT> > > base_t;
+
+ template <typename ScannerT>
+ struct result { typedef cl::match<> type; };
+
+ scoped_parser_impl(DataT& actions, ParserT const &p)
+ : base_t(p)
+ , actions(actions)
+ {}
+
+ template <typename ScannerT>
+ typename result<ScannerT>::type parse(ScannerT const &scan) const
+ {
+ typedef typename ScannerT::iterator_t iterator_t;
+ iterator_t save = scan.first;
+
+ ScopeT scope(actions);
+ typename cl::parser_result<ParserT, ScannerT>::type result
+ = this->subject().parse(scan);
+
+ if (result) {
+ scope.success(result);
+ return scan.create_match(result.length(), cl::nil_t(), save, scan.first);
+ }
+ else {
+ scope.failure();
+ return scan.no_match();
+ }
+ }
+
+ DataT& actions;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ //
+ // scoped_parser
+ //
+ // generator for scoped_parser_impl objects
+ // operator[] returns scoped_parser_impl according to its argument
+ //
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename ScopeT>
+ struct scoped_parser
+ {
+ typedef typename ScopeT::data_type data_type;
+
+ explicit scoped_parser(data_type& actions)
+ : actions(actions) {}
+
+ template<typename ParserT>
+ scoped_parser_impl
+ <
+ ScopeT,
+ data_type,
+ typename cl::as_parser<ParserT>::type
+ >
+ operator[](ParserT const &p) const
+ {
+ typedef cl::as_parser<ParserT> as_parser_t;
+ typedef typename as_parser_t::type parser_t;
+
+ return scoped_parser_impl<ScopeT, data_type, parser_t>
+ (actions, as_parser_t::convert(p));
+ }
+
+ data_type& actions;
+ };
+
+ // Lookback parser
+ //
+ // Requires that iterator has typedef 'lookback_range' and function
+ // 'lookback' returning a 'lookback_range'.
+
+ template <typename ParserT>
+ struct lookback_parser
+ : public cl::unary< ParserT, cl::parser< lookback_parser<ParserT> > >
+ {
+ typedef lookback_parser<ParserT> self_t;
+ typedef cl::unary< ParserT, cl::parser< lookback_parser<ParserT> > > base_t;
+
+ template <typename ScannerT>
+ struct result
+ {
+ typedef typename cl::parser_result<ParserT, ScannerT>::type type;
+ };
+
+ lookback_parser(ParserT const& p)
+ : base_t(p)
+ {}
+
+ template <typename ScannerT>
+ typename result<ScannerT>::type parse(ScannerT const &scan) const
+ {
+ typedef typename ScannerT::iterator_t::lookback_range::iterator iterator_t;
+ typedef cl::scanner<iterator_t, typename ScannerT::policies_t> scanner_t;
+
+ iterator_t begin = scan.first.lookback().begin();
+ scanner_t lookback_scan(begin, scan.first.lookback().end(), scan);
+
+ if (this->subject().parse(lookback_scan))
+ return scan.empty_match();
+ else
+ return scan.no_match();
+ }
+ };
+
+ struct lookback_gen
+ {
+ template <typename ParserT>
+ lookback_parser<ParserT> operator[](ParserT const& p) const
+ {
+ return lookback_parser<ParserT>(p);
+ }
+ };
+
+ lookback_gen const lookback = lookback_gen();
+}
+#endif // BOOST_QUICKBOOK_SCOPED_BLOCK_HPP
Deleted: branches/quickbook-filenames/tools/quickbook/src/scoped_parser.hpp
==============================================================================
--- branches/quickbook-filenames/tools/quickbook/src/scoped_parser.hpp 2011-02-22 15:27:27 EST (Tue, 22 Feb 2011)
+++ (empty file)
@@ -1,94 +0,0 @@
-/*=============================================================================
- Copyright (c) 2010 Daniel James
- Copyright (c) 2003 Martin Wille
- http://spirit.sourceforge.net/
-
- 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)
- =============================================================================*/
-
-// Used to store variables/state while parsing
-
-#ifndef BOOST_QUICKBOOK_SCOPED_PARSER_HPP
-#define BOOST_QUICKBOOK_SCOPED_PARSER_HPP
-
-#include <boost/spirit/include/classic_core.hpp>
-#include <boost/spirit/include/classic_nil.hpp>
-
-namespace quickbook {
- namespace cl = boost::spirit::classic;
-
- template <typename ScopeT, typename DataT, typename ParserT>
- struct scoped_parser_impl
- : public cl::unary< ParserT, cl::parser< scoped_parser_impl<ScopeT, DataT, ParserT> > >
- {
- typedef scoped_parser_impl<ScopeT, DataT, ParserT> self_t;
- typedef cl::unary< ParserT, cl::parser< scoped_parser_impl<ScopeT, DataT, ParserT> > > base_t;
-
- template <typename ScannerT>
- struct result { typedef cl::match<> type; };
-
- scoped_parser_impl(DataT& actions, ParserT const &p)
- : base_t(p)
- , actions(actions)
- {}
-
- template <typename ScannerT>
- typename result<ScannerT>::type parse(ScannerT const &scan) const
- {
- typedef typename ScannerT::iterator_t iterator_t;
- iterator_t save = scan.first;
-
- ScopeT scope(actions);
- typename cl::parser_result<ParserT, ScannerT>::type result
- = this->subject().parse(scan);
-
- if (result) {
- scope.success(result);
- return scan.create_match(result.length(), cl::nil_t(), save, scan.first);
- }
- else {
- scope.failure();
- return scan.no_match();
- }
- }
-
- DataT& actions;
- };
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // scoped_parser
- //
- // generator for scoped_parser_impl objects
- // operator[] returns scoped_parser_impl according to its argument
- //
- ///////////////////////////////////////////////////////////////////////////
- template <typename ScopeT>
- struct scoped_parser
- {
- typedef typename ScopeT::data_type data_type;
-
- explicit scoped_parser(data_type& actions)
- : actions(actions) {}
-
- template<typename ParserT>
- scoped_parser_impl
- <
- ScopeT,
- data_type,
- typename cl::as_parser<ParserT>::type
- >
- operator[](ParserT const &p) const
- {
- typedef cl::as_parser<ParserT> as_parser_t;
- typedef typename as_parser_t::type parser_t;
-
- return scoped_parser_impl<ScopeT, data_type, parser_t>
- (actions, as_parser_t::convert(p));
- }
-
- data_type& actions;
- };
-}
-#endif // BOOST_QUICKBOOK_SCOPED_BLOCK_HPP
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