Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73597 - trunk/libs/spirit/example/qi/compiler_tutorial/conjure3
From: joel_at_[hidden]
Date: 2011-08-07 11:15:57


Author: djowel
Date: 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
New Revision: 73597
URL: http://svn.boost.org/trac/boost/changeset/73597

Log:
error report on prefix++/-- when an lvalue is not found
Text files modified:
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/annotation.hpp | 8 ++++++++
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp | 32 ++++++++++++++++----------------
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp | 8 ++++----
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp | 4 ++--
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp | 4 ++--
   trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp | 23 ++++++++++++++---------
   6 files changed, 46 insertions(+), 33 deletions(-)

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/annotation.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/annotation.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/annotation.hpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -99,6 +99,14 @@
             ast.id = id;
         }
 
+ void operator()(ast::primary_expr& ast, Iterator pos) const
+ {
+ int id = iters.size();
+ iters.push_back(pos);
+ boost::apply_visitor(set_annotation_id(id), ast);
+ ast.id = id;
+ }
+
         void operator()(ast::variable_declaration& ast, Iterator pos) const
         {
             int id = iters.size();

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/ast.hpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -32,7 +32,7 @@
     };
 
     struct nil {};
- struct unary;
+ struct unary_expr;
     struct function_call;
     struct expression;
 
@@ -42,18 +42,22 @@
         std::string name;
     };
 
- struct literal :
+ struct primary_expr :
         tagged,
         boost::spirit::extended_variant<
             nil
           , bool
           , unsigned int
+ , identifier
+ , boost::recursive_wrapper<expression>
>
     {
- literal() : base_type() {}
- literal(bool val) : base_type(val) {}
- literal(unsigned int val) : base_type(val) {}
- literal(literal const& rhs)
+ primary_expr() : base_type() {}
+ primary_expr(bool val) : base_type(val) {}
+ primary_expr(unsigned int val) : base_type(val) {}
+ primary_expr(identifier const& val) : base_type(val) {}
+ primary_expr(expression const& val) : base_type(val) {}
+ primary_expr(primary_expr const& rhs)
             : base_type(rhs.get()) {}
     };
 
@@ -61,24 +65,20 @@
         tagged,
         boost::spirit::extended_variant<
             nil
- , literal
- , identifier
- , boost::recursive_wrapper<unary>
+ , primary_expr
+ , boost::recursive_wrapper<unary_expr>
           , boost::recursive_wrapper<function_call>
- , boost::recursive_wrapper<expression>
>
     {
         operand() : base_type() {}
- operand(literal const& val) : base_type(val) {}
- operand(identifier const& val) : base_type(val) {}
- operand(unary const& val) : base_type(val) {}
+ operand(primary_expr const& val) : base_type(val) {}
+ operand(unary_expr const& val) : base_type(val) {}
         operand(function_call const& val) : base_type(val) {}
- operand(expression const& val) : base_type(val) {}
         operand(operand const& rhs)
             : base_type(rhs.get()) {}
     };
 
- struct unary : tagged
+ struct unary_expr : tagged
     {
         token_ids::type operator_;
         operand operand_;
@@ -175,7 +175,7 @@
 }}
 
 BOOST_FUSION_ADAPT_STRUCT(
- client::ast::unary,
+ client::ast::unary_expr,
     (client::token_ids::type, operator_)
     (client::ast::operand, operand_)
 )

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.cpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -193,7 +193,7 @@
         return val(x);
     }
 
- value compiler::operator()(ast::literal const& x)
+ value compiler::operator()(ast::primary_expr const& x)
     {
         return boost::apply_visitor(*this, x.get());
     }
@@ -209,7 +209,7 @@
         return named_values[x.name];
     }
 
- value compiler::operator()(ast::unary const& x)
+ value compiler::operator()(ast::unary_expr const& x)
     {
         value operand = boost::apply_visitor(*this, x.operand_);
         if (!operand.is_valid())
@@ -225,7 +225,7 @@
             {
                 if (!operand.is_lvalue())
                 {
- // $$$ JDG Error here $$$
+ error_handler(x.id, "++ needs an lvalue");
                     return val();
                 }
 
@@ -237,7 +237,7 @@
             {
                 if (!operand.is_lvalue())
                 {
- // $$$ JDG Error here $$$
+ error_handler(x.id, "-- needs an lvalue");
                     return val();
                 }
 

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/compiler.hpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -139,9 +139,9 @@
         value operator()(ast::nil) { BOOST_ASSERT(0); return val(); }
         value operator()(unsigned int x);
         value operator()(bool x);
- value operator()(ast::literal const& x);
+ value operator()(ast::primary_expr const& x);
         value operator()(ast::identifier const& x);
- value operator()(ast::unary const& x);
+ value operator()(ast::unary_expr const& x);
         value operator()(ast::function_call const& x);
         value operator()(ast::expression const& x);
         value operator()(ast::assignment const& x);

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression.hpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -46,11 +46,11 @@
         Lexer const& lexer;
 
         qi::rule<Iterator, ast::expression()> expr;
- qi::rule<Iterator, ast::operand()> unary_expr, primary_expr;
+ qi::rule<Iterator, ast::operand()> unary_expr, postfix_expr;
         qi::rule<Iterator, ast::function_call()> function_call;
         qi::rule<Iterator, std::list<ast::expression>()> argument_list;
         qi::rule<Iterator, std::string()> identifier;
- qi::rule<Iterator, ast::literal()> literal;
+ qi::rule<Iterator, ast::primary_expr()> primary_expr;
     };
 }}
 

Modified: trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/conjure3/expression_def.hpp 2011-08-07 11:15:56 EDT (Sun, 07 Aug 2011)
@@ -45,20 +45,20 @@
             ;
 
         unary_expr =
- primary_expr
+ postfix_expr
             | (tokenid_mask(token_ids::op_unary) > unary_expr)
             ;
 
- primary_expr =
- literal
- | function_call
- | identifier
- | '(' > expr > ')'
+ postfix_expr =
+ function_call
+ | primary_expr
             ;
 
- literal =
+ primary_expr =
                 lexer.lit_uint
             | lexer.true_or_false
+ | identifier
+ | '(' > expr > ')'
             ;
 
         function_call =
@@ -76,8 +76,8 @@
         BOOST_SPIRIT_DEBUG_NODES(
             (expr)
             (unary_expr)
+ (postfix_expr)
             (primary_expr)
- (literal)
             (function_call)
             (argument_list)
             (identifier)
@@ -90,7 +90,12 @@
                 "Error! Expecting ", _4, _3));
 
         ///////////////////////////////////////////////////////////////////////
- // Annotation: on success in primary_expr, call annotation.
+ // Annotation: on success in unary_expr, postfix_expr,
+ // and primary_expr call annotation.
+ on_success(unary_expr,
+ annotation_function(error_handler.iters)(_val, _1));
+ on_success(postfix_expr,
+ annotation_function(error_handler.iters)(_val, _1));
         on_success(primary_expr,
             annotation_function(error_handler.iters)(_val, _1));
     }


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