lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master e411ac4 09/10: Throw any parser errors from a


From: Greg Chicares
Subject: [lmi-commits] [lmi] master e411ac4 09/10: Throw any parser errors from all *_representation() functions
Date: Mon, 6 Feb 2017 04:10:56 +0000 (UTC)

branch: master
commit e411ac4975a2aac153f447633006ffca5a15a211
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Throw any parser errors from all *_representation() functions
    
    Now all InputSequence accessors throw an exception if the parser
    diagnosed any errors. Therefore, InputSequence objects are only
    accessed within try-blocks.
---
 gpt_input.cpp         |   34 +++++++----
 input_realization.cpp |  158 ++++++++++++++++++++++++++++++-------------------
 input_sequence.cpp    |    6 +-
 mec_input.cpp         |   34 +++++++----
 4 files changed, 148 insertions(+), 84 deletions(-)

diff --git a/gpt_input.cpp b/gpt_input.cpp
index 2c84413..4da87ff 100644
--- a/gpt_input.cpp
+++ b/gpt_input.cpp
@@ -36,6 +36,7 @@
 #include "miscellany.hpp"               // each_equal(), lmi_array_size()
 
 #include <algorithm>                    // std::max()
+#include <exception>
 #include <limits>
 #include <sstream>
 #include <utility>                      // std::pair
@@ -51,16 +52,29 @@ std::string realize_sequence_string
     ,datum_sequence const& sequence_string
     )
 {
-    InputSequence s
-        (sequence_string.value()
-        ,input.years_to_maturity()
-        ,input.issue_age        ()
-        ,input.maturity_age     () // This class has no "retirement age".
-        ,input.inforce_year     ()
-        ,input.effective_year   ()
-        );
-    detail::convert_vector(v, s.linear_number_representation());
-    return s.formatted_diagnostics(true);
+    try
+        {
+        InputSequence s
+            (sequence_string.value()
+            ,input.years_to_maturity()
+            ,input.issue_age        ()
+            ,input.maturity_age     () // This class has no "retirement age".
+            ,input.inforce_year     ()
+            ,input.effective_year   ()
+            );
+        detail::convert_vector(v, s.linear_number_representation());
+        }
+    catch(std::exception const& e)
+        {
+        std::string parser_diagnostics(e.what());
+        std::string::size_type z(parser_diagnostics.find('\n'));
+        if(std::string::npos != z)
+            {
+            parser_diagnostics.erase(z);
+            }
+        return parser_diagnostics;
+        }
+    return std::string();
 }
 } // Unnamed namespace.
 
diff --git a/input_realization.cpp b/input_realization.cpp
index 6335b31..20bbf99 100644
--- a/input_realization.cpp
+++ b/input_realization.cpp
@@ -50,16 +50,29 @@ std::string realize_sequence_string
     ,datum_sequence const& sequence_string
     )
 {
-    InputSequence s
-        (sequence_string.value()
-        ,input.years_to_maturity()
-        ,input.issue_age        ()
-        ,input.retirement_age   ()
-        ,input.inforce_year     ()
-        ,input.effective_year   ()
-        );
-    detail::convert_vector(v, s.linear_number_representation());
-    return s.formatted_diagnostics(true);
+    try
+        {
+        InputSequence s
+            (sequence_string.value()
+            ,input.years_to_maturity()
+            ,input.issue_age        ()
+            ,input.retirement_age   ()
+            ,input.inforce_year     ()
+            ,input.effective_year   ()
+            );
+        detail::convert_vector(v, s.linear_number_representation());
+        }
+    catch(std::exception const& e)
+        {
+        std::string parser_diagnostics(e.what());
+        std::string::size_type z(parser_diagnostics.find('\n'));
+        if(std::string::npos != z)
+            {
+            parser_diagnostics.erase(z);
+            }
+        return parser_diagnostics;
+        }
+    return std::string();
 }
 
 //============================================================================
@@ -73,24 +86,37 @@ std::string realize_sequence_string
     ,std::string       const& default_keyword
     )
 {
-    InputSequence s
-        (sequence_string.value()
-        ,input.years_to_maturity()
-        ,input.issue_age        ()
-        ,input.retirement_age   ()
-        ,input.inforce_year     ()
-        ,input.effective_year   ()
-        ,detail::extract_keys_from_string_map(keyword_dictionary)
-        ,true
-        ,default_keyword
-        );
-    detail::convert_vector
-        (v
-        ,s.linear_keyword_representation()
-        ,keyword_dictionary
-        ,default_keyword
-        );
-    return s.formatted_diagnostics(true);
+    try
+        {
+        InputSequence s
+            (sequence_string.value()
+            ,input.years_to_maturity()
+            ,input.issue_age        ()
+            ,input.retirement_age   ()
+            ,input.inforce_year     ()
+            ,input.effective_year   ()
+            ,detail::extract_keys_from_string_map(keyword_dictionary)
+            ,true
+            ,default_keyword
+            );
+        detail::convert_vector
+            (v
+            ,s.linear_keyword_representation()
+            ,keyword_dictionary
+            ,default_keyword
+            );
+        }
+    catch(std::exception const& e)
+        {
+        std::string parser_diagnostics(e.what());
+        std::string::size_type z(parser_diagnostics.find('\n'));
+        if(std::string::npos != z)
+            {
+            parser_diagnostics.erase(z);
+            }
+        return parser_diagnostics;
+        }
+    return std::string();
 }
 
 //============================================================================
@@ -105,25 +131,38 @@ std::string realize_sequence_string
     ,std::string         const& default_keyword
     )
 {
-    InputSequence s
-        (sequence_string.value()
-        ,input.years_to_maturity()
-        ,input.issue_age        ()
-        ,input.retirement_age   ()
-        ,input.inforce_year     ()
-        ,input.effective_year   ()
-        ,detail::extract_keys_from_string_map(keyword_dictionary)
-        ,false
-        ,default_keyword
-        );
-    detail::convert_vector(vn, s.linear_number_representation());
-    detail::convert_vector
-        (ve
-        ,s.linear_keyword_representation()
-        ,keyword_dictionary
-        ,default_keyword
-        );
-    return s.formatted_diagnostics(true);
+    try
+        {
+        InputSequence s
+            (sequence_string.value()
+            ,input.years_to_maturity()
+            ,input.issue_age        ()
+            ,input.retirement_age   ()
+            ,input.inforce_year     ()
+            ,input.effective_year   ()
+            ,detail::extract_keys_from_string_map(keyword_dictionary)
+            ,false
+            ,default_keyword
+            );
+        detail::convert_vector(vn, s.linear_number_representation());
+        detail::convert_vector
+            (ve
+            ,s.linear_keyword_representation()
+            ,keyword_dictionary
+            ,default_keyword
+            );
+        }
+    catch(std::exception const& e)
+        {
+        std::string parser_diagnostics(e.what());
+        std::string::size_type z(parser_diagnostics.find('\n'));
+        if(std::string::npos != z)
+            {
+            parser_diagnostics.erase(z);
+            }
+        return parser_diagnostics;
+        }
+    return std::string();
 }
 
 // SpecifiedAmount.allowed_keywords() does more or less the same
@@ -925,12 +964,9 @@ int Input::must_overwrite_specamt_with_obsolete_history
 {
     std::vector<tnr_unrestricted_double> u;
     std::vector<tnr_unrestricted_double> v;
-    try
-        {
-        realize_sequence_string(*this, u, numeric_sequence(specamt));
-        realize_sequence_string(*this, v, numeric_sequence(history));
-        }
-    catch(std::exception const& e)
+    std::string su = realize_sequence_string(*this, u, 
numeric_sequence(specamt));
+    std::string sv = realize_sequence_string(*this, v, 
numeric_sequence(history));
+    if(!su.empty() || !sv.empty())
         {
         if(!hide_errors)
             {
@@ -938,23 +974,23 @@ int Input::must_overwrite_specamt_with_obsolete_history
                 << "Possible conflict between specified amount and history."
                 << " Merge them manually into the specified-amount field."
                 << "\nSpecified amount: " << specamt
+                << "\nErrors: '" << su << "'"
                 << "\nHistory: " << history
-                << "\nError: " << e.what()
+                << "\nErrors: '" << sv << "'"
                 << LMI_FLUSH
                 ;
             }
         return 2;
         }
-    catch(...)
-        {
-        report_exception();
-        return 3;
-        }
 
     bool history_differs = false;
     bool future_differs  = false;
-    int const years_of_history = InforceYear.value() + (0 != 
InforceMonth.value());
-    for(int j = 0; j < years_of_history; ++j)
+
+    unsigned int const years_of_history = InforceYear.value() + (0 != 
InforceMonth.value());
+    LMI_ASSERT(years_of_history <= u.size());
+    LMI_ASSERT(years_of_history <= v.size());
+
+    for(unsigned int j = 0; j < years_of_history; ++j)
         {
         if(u[j] != v[j])
             {
diff --git a/input_sequence.cpp b/input_sequence.cpp
index f85bde3..fab69eb 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -921,7 +921,7 @@ std::vector<double> const& 
InputSequence::linear_number_representation() const
 {
     if(!formatted_diagnostics().empty())
         {
-        fatal_error() << formatted_diagnostics() << LMI_FLUSH;
+        throw std::runtime_error(formatted_diagnostics());
         }
 
     return number_result_;
@@ -931,7 +931,7 @@ std::vector<std::string> const& 
InputSequence::linear_keyword_representation() c
 {
     if(!formatted_diagnostics().empty())
         {
-        fatal_error() << formatted_diagnostics() << LMI_FLUSH;
+        throw std::runtime_error(formatted_diagnostics());
         }
 
     return keyword_result_;
@@ -958,7 +958,7 @@ std::string InputSequence::mathematical_representation() 
const
 {
     if(!formatted_diagnostics().empty())
         {
-        fatal_error() << formatted_diagnostics() << LMI_FLUSH;
+        throw std::runtime_error(formatted_diagnostics());
         }
 
     std::ostringstream oss;
diff --git a/mec_input.cpp b/mec_input.cpp
index 2f5e390..f3feff7 100644
--- a/mec_input.cpp
+++ b/mec_input.cpp
@@ -36,6 +36,7 @@
 #include "miscellany.hpp"               // each_equal(), lmi_array_size()
 
 #include <algorithm>                    // std::max()
+#include <exception>
 #include <limits>
 #include <sstream>
 #include <utility>                      // std::pair
@@ -51,16 +52,29 @@ std::string realize_sequence_string
     ,datum_sequence const& sequence_string
     )
 {
-    InputSequence s
-        (sequence_string.value()
-        ,input.years_to_maturity()
-        ,input.issue_age        ()
-        ,input.maturity_age     () // This class has no "retirement age".
-        ,input.inforce_year     ()
-        ,input.effective_year   ()
-        );
-    detail::convert_vector(v, s.linear_number_representation());
-    return s.formatted_diagnostics(true);
+    try
+        {
+        InputSequence s
+            (sequence_string.value()
+            ,input.years_to_maturity()
+            ,input.issue_age        ()
+            ,input.maturity_age     () // This class has no "retirement age".
+            ,input.inforce_year     ()
+            ,input.effective_year   ()
+            );
+        detail::convert_vector(v, s.linear_number_representation());
+        }
+    catch(std::exception const& e)
+        {
+        std::string parser_diagnostics(e.what());
+        std::string::size_type z(parser_diagnostics.find('\n'));
+        if(std::string::npos != z)
+            {
+            parser_diagnostics.erase(z);
+            }
+        return parser_diagnostics;
+        }
+    return std::string();
 }
 } // Unnamed namespace.
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]