Advertisement
Guest User

error_handler.hpp

a guest
Mar 2nd, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <boost/spirit/home/x3.hpp>
  6. #include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
  7. #include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
  8. #include "SchematicLibraryGrammar.hpp"
  9. #include <map>
  10.  
  11. namespace client { namespace KicadParser_grammar
  12. {
  13.     namespace x3 = boost::spirit::x3;
  14.  
  15. //    struct error_handler
  16. //        {
  17. //            //  Our error handler
  18. //            template <typename Iterator, typename Exception, typename Context>
  19. //            x3::error_handler_result
  20. //            on_error(Iterator&, Iterator const& last, Exception const& x, Context const& context)
  21. //            {
  22. //                std::cout
  23. //                    << "Error! Expecting: "
  24. //                    << x.which()
  25. //                    << " here: \""
  26. //                    << std::string(x.where(), last)
  27. //                    << "\""
  28. //                    << std::endl
  29. //                    ;
  30. //                return x3::error_handler_result::fail;
  31. //            }
  32. //        };
  33.     ////////////////////////////////////////////////////////////////////////////
  34.     //  Our error handler
  35.     ////////////////////////////////////////////////////////////////////////////
  36.     // ERROR_HANDLER1_VISIT_BEGIN
  37.     // X3 Error Handler Utility
  38.     template <typename Iterator>
  39.     using error_handler = x3::error_handler<Iterator>;
  40.  
  41.     // tag used to get our error handler from the context
  42.     using x3::error_handler_tag;
  43.  
  44.     struct error_handler_base
  45.     {
  46.         error_handler_base();
  47.  
  48.         template <typename Iterator, typename Exception, typename Context>
  49.         x3::error_handler_result on_error(
  50.             Iterator& first, Iterator const& last
  51.           , Exception const& x, Context const& context);
  52.  
  53.         std::map<std::string, std::string> id_map;
  54.     };
  55.     // ERROR_HANDLER1_VISIT_END
  56.  
  57.     ////////////////////////////////////////////////////////////////////////////
  58.     // Implementation
  59.     ////////////////////////////////////////////////////////////////////////////
  60.  
  61.     // ERROR_HANDLER2_VISIT_BEGIN
  62.     inline error_handler_base::error_handler_base()
  63.     {
  64.                 id_map["schematicLibrary"] = "schematicLibrary";
  65.                 id_map["schematicSymbolDefinition"] = "schematicSymbolDefinition";
  66.                 id_map["arcShape"]                  = "arcShape";
  67.                 id_map["circleShape"]               = "circleShape";
  68.                 id_map["symbolAlias"]               = "symbolAlias";
  69.                 id_map["rectangleShape"]            = "rectangleShape";
  70.                 id_map["symbolField"]               = "symbolField";
  71.                 id_map["textShape"]                 = "textShape";
  72.                 id_map["footprintFilterList"]       = "footprintFilterList";
  73.                 id_map["polygonShape"]              = "polygonShape";
  74.                 id_map["pinShape"]                  = "pinShape";
  75.                 id_map["bezierShape"]               = "bezierShape";
  76.                 id_map["schematicSymbol"]           = "schematicSymbol";
  77.                 id_map["symbolShape"]               = "symbolShape";
  78.                 id_map["nonQuotedString"]           = "nonQuotedString";
  79.                 id_map["nonQuotedStringMultiWord"]  = "nonQuotedStringMultiWord";
  80.                 id_map["optionalChar"]              = "optionalChar";
  81.                 id_map["quotedString"]              = "quotedString";
  82.     }
  83.     // ERROR_HANDLER2_VISIT_END
  84.  
  85.     // ERROR_HANDLER3_VISIT_BEGIN
  86.     template <typename Iterator, typename Exception, typename Context>
  87.     inline x3::error_handler_result
  88.     error_handler_base::on_error(
  89.         Iterator& first, Iterator const& last
  90.       , Exception const& x, Context const& context)
  91.     {
  92.         std::string which = x.which();
  93.         auto iter = id_map.find(which);
  94.         if (iter != id_map.end())
  95.             which = iter->second;
  96.  
  97.         std::string message = "Error! Expecting: " + which + " here:";
  98.         auto& error_handler = x3::get<error_handler_tag>(context).get();
  99.         error_handler(x.where(), message);
  100.         return x3::error_handler_result::fail;
  101.     }
  102.     // ERROR_HANDLER3_VISIT_END
  103. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement