Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73560 - trunk/libs/spirit/example/qi/compiler_tutorial/conjure3
From: joel_at_[hidden]
Date: 2011-08-05 21:24:07


Author: djowel
Date: 2011-08-05 21:24:06 EDT (Fri, 05 Aug 2011)
New Revision: 73560
URL: http://svn.boost.org/trac/boost/changeset/73560

Log:
Better token addition syntax
Text files modified:
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.cpp | 2 +-
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp | 17 ++++++++++++++++-
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp | 21 ++++++++++-----------
   3 files changed, 27 insertions(+), 13 deletions(-)

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.cpp 2011-08-05 21:24:06 EDT (Fri, 05 Aug 2011)
@@ -13,5 +13,5 @@
 
 typedef std::string::const_iterator base_iterator_type;
 template client::lexer::conjure_tokens<base_iterator_type>::conjure_tokens();
-template bool client::lexer::conjure_tokens<base_iterator_type>::add_keyword(
+template bool client::lexer::conjure_tokens<base_iterator_type>::add_(
     std::string const&, int);

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer.hpp 2011-08-05 21:24:06 EDT (Fri, 05 Aug 2011)
@@ -88,7 +88,22 @@
 
     protected:
         // add a keyword to the mapping table
- bool add_keyword(std::string const& keyword, int id = token_ids::invalid);
+ bool add_(std::string const& keyword, int id = token_ids::invalid);
+
+ struct keyword_adder
+ {
+ conjure_tokens& l;
+ keyword_adder(conjure_tokens& l) : l(l) {}
+ keyword_adder& operator()(
+ std::string const& keyword, int id = token_ids::invalid)
+ {
+ l.add_(keyword, id);
+ return *this;
+ }
+ };
+
+ friend struct keyword_adder;
+ keyword_adder add;
 
     public:
         typedef BaseIterator base_iterator_type;

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/lexer_def.hpp 2011-08-05 21:24:06 EDT (Fri, 05 Aug 2011)
@@ -15,21 +15,20 @@
       : identifier("[a-zA-Z_][a-zA-Z_0-9]*", token_ids::identifier)
       , lit_uint("[0-9]+", token_ids::lit_uint)
       , true_or_false("true|false", token_ids::true_or_false)
+ , add(*this)
     {
         lex::_pass_type _pass;
 
         this->self = lit_uint | true_or_false;
 
- add_keyword("void");
- add_keyword("int");
- add_keyword("if");
- add_keyword("else");
- add_keyword("while");
- add_keyword("return");
-
- add_keyword("=", token_ids::assign);
-
- this->self.add
+ this->add
+ ("void")
+ ("int")
+ ("if")
+ ("else")
+ ("while")
+ ("return")
+ ("=", token_ids::assign)
                 ("\\|\\|", token_ids::logical_or)
                 ("&&", token_ids::logical_and)
                 ("==", token_ids::equal)
@@ -61,7 +60,7 @@
     }
 
     template <typename BaseIterator>
- bool conjure_tokens<BaseIterator>::add_keyword(
+ bool conjure_tokens<BaseIterator>::add_(
         std::string const& keyword, int id_)
     {
         // add the token to the lexer


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