[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 762cc15 4/4: Treat errors found by InputSeque
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 762cc15 4/4: Treat errors found by InputSequence::realize_vector() uniformly |
Date: |
Thu, 2 Feb 2017 17:51:56 +0000 (UTC) |
branch: master
commit 762cc156ea1e44f3b93d8ec0bdd125247557573a
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Treat errors found by InputSequence::realize_vector() uniformly
Where realize_vector() detects errors, an exception (via fatal_error())
is thrown in most cases; however, in one case, a message was formerly
appended to the 'diagnostics' stream...and mark_diagnostic_context() was
called, which logically but fatuously appended:
Current token 'end of input' at position -1.
because this occurs outside the recursive-descent parser, so there is no
actual parser context. Now, an exception is thrown for any error found
by realize_vector().
This particular test detects out-of-order intervals. The former method
was perhaps appropriate before the recent decision to require intervals
to be specified in order, but now an exception is clearly wanted.
In the unit test, check() has been rewritten to compare expected and
observed diagnostics, whether they're observed by catching an exception
or (regrettably) by calling formatted_diagnostics(). [Use
git diff --ignore-all-space
to understand this change.]
This commit is a preparatory step that makes ensuing commits simpler and
clearer by confining these members:
mark_diagnostic_context()
std::ostringstream diagnostics
to the distinct parser class that aches to be factored out.
---
input_seq_test.cpp | 112 +++++++++++++++++++++++++++++++---------------------
input_sequence.cpp | 6 +--
2 files changed, 71 insertions(+), 47 deletions(-)
diff --git a/input_seq_test.cpp b/input_seq_test.cpp
index ebefe70..6d1d95e 100644
--- a/input_seq_test.cpp
+++ b/input_seq_test.cpp
@@ -41,55 +41,80 @@ void check
,bool o = false
)
{
- InputSequence const seq(e, n, 90, 95, 0, 2002, k, w, o);
-
- std::vector<double> const& v(seq.linear_number_representation());
- bool const bv = v == std::vector<double>(d, d + n);
- if(!bv)
+ try
{
- using osid = std::ostream_iterator<double>;
- std::cout << "\nExpression: '" << e << "'";
- std::cout << "\n observed numbers: ";
- std::copy(v.begin(), v.end(), osid(std::cout, " "));
- std::cout << "\n differ from expected: ";
- std::copy(d , d + n , osid(std::cout, " "));
- std::cout << std::endl;
+ InputSequence const seq(e, n, 90, 95, 0, 2002, k, w, o);
+
+ std::vector<double> const& v(seq.linear_number_representation());
+ bool const bv = v == std::vector<double>(d, d + n);
+ if(!bv)
+ {
+ using osid = std::ostream_iterator<double>;
+ std::cout << "\nExpression: '" << e << "'";
+ std::cout << "\n observed numbers: ";
+ std::copy(v.begin(), v.end(), osid(std::cout, " "));
+ std::cout << "\n differ from expected: ";
+ std::copy(d , d + n , osid(std::cout, " "));
+ std::cout << std::endl;
+ }
+
+ std::vector<std::string> const& s(seq.linear_keyword_representation());
+ std::vector<std::string> const t =
+ ( std::vector<std::string>() == c)
+ ? std::vector<std::string>(n)
+ : c
+ ;
+ bool const bs = s == t;
+ if(!bs)
+ {
+ using osis = std::ostream_iterator<std::string>;
+ std::cout << "\nExpression: '" << e << "'";
+ std::cout << "\n observed keywords: ";
+ std::copy(s.begin(), s.end(), osis(std::cout, "|"));
+ std::cout << "\n differ from expected: ";
+ std::copy(t.begin(), t.end(), osis(std::cout, "|"));
+ std::cout << std::endl;
+ }
+
+ std::string const& x(seq.formatted_diagnostics());
+ std::string const y = (nullptr == m) ? std::string() : std::string(m);
+ bool const by = x == y;
+ if(!by)
+ {
+ std::cout
+ << "\nObserved diagnostics:"
+ << "\n\n'" << x << "'"
+ << "\n\ndiffer from expected:"
+ << "\n\n'" << y << "'"
+ << std::endl
+ ;
+ }
+
+ bool const b = bv && bs && by;
+ INVOKE_BOOST_TEST(b, file, line);
}
-
- std::vector<std::string> const& s(seq.linear_keyword_representation());
- std::vector<std::string> const t =
- ( std::vector<std::string>() == c)
- ? std::vector<std::string>(n)
- : c
- ;
- bool const bs = s == t;
- if(!bs)
+ catch(std::exception const& x)
{
- using osis = std::ostream_iterator<std::string>;
- std::cout << "\nExpression: '" << e << "'";
- std::cout << "\n observed keywords: ";
- std::copy(s.begin(), s.end(), osis(std::cout, "|"));
- std::cout << "\n differ from expected: ";
- std::copy(t.begin(), t.end(), osis(std::cout, "|"));
- std::cout << std::endl;
+ std::string const f = x.what();
+ std::string const g = f.substr(0, f.find("\n[file "));
+ std::string const h = (nullptr == m) ? std::string() : std::string(m);
+ bool const b = g == h;
+ if(!b)
+ {
+ std::cout
+ << "\nObserved exception:"
+ << "\n\n'" << g << "'"
+ << "\n\ndiffers from expected:"
+ << "\n\n'" << h << "'"
+ << std::endl
+ ;
+ }
+ INVOKE_BOOST_TEST(b, file, line);
}
-
- std::string const& x(seq.formatted_diagnostics());
- std::string const y = (nullptr == m) ? std::string() : std::string(m);
- bool const by = x == y;
- if(!by)
+ catch(...)
{
- std::cout
- << "\nObserved diagnostics:"
- << "\n\n'" << x << "'"
- << "\n\ndiffer from expected:"
- << "\n\n'" << y << "'"
- << std::endl
- ;
+ throw std::runtime_error("Unexpected exception");
}
-
- bool const b = bv && bs && by;
- INVOKE_BOOST_TEST(b, file, line);
}
int test_main(int, char*[])
@@ -265,7 +290,6 @@ int test_main(int, char*[])
char const* m =
"Previous interval began at duration 5;"
" current interval [ 2, 5 ) would begin before that."
- " Current token 'end of input' at position -1.\n"
;
check(__FILE__, __LINE__, d, n, e, m);
}
diff --git a/input_sequence.cpp b/input_sequence.cpp
index 76a2bd8..18d0d39 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -320,15 +320,15 @@ void InputSequence::realize_vector()
}
if(interval_i.begin_duration < prior_begin_duration)
{
- diagnostics
+ fatal_error()
<< "Previous interval began at duration "
<< prior_begin_duration
<< "; current interval "
<< "[ " << interval_i.begin_duration << ", "
<< interval_i.end_duration << " )"
- << " would begin before that. "
+ << " would begin before that."
+ << LMI_FLUSH
;
- mark_diagnostic_context();
return;
}
prior_begin_duration = interval_i.begin_duration;