From 33343f3d8ae4dffb6b343b95e5af0dff3bc07078 Mon Sep 17 00:00:00 2001 From: David McClosky Date: Tue, 11 Aug 2015 15:03:35 -0400 Subject: [PATCH] first-stage/PARSE: Remove two-part error messages These are messages where parts are printed to stderr followed by an assertion failure. Now there is only an assertion failure (which converts more cleanly into Python exceptions). --- first-stage/PARSE/InputTree.C | 21 ++++++++++----------- first-stage/PARSE/utils.C | 7 +++++++ first-stage/PARSE/utils.h | 5 +++-- python/tests/test_tree.py | 10 +++++----- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/first-stage/PARSE/InputTree.C b/first-stage/PARSE/InputTree.C index 394a183..fa76324 100644 --- a/first-stage/PARSE/InputTree.C +++ b/first-stage/PARSE/InputTree.C @@ -12,13 +12,14 @@ * under the License. */ +#include #include #include +#include +#include #include "InputTree.h" #include "headFinder.h" #include "utils.h" -#include -#include #include "Term.h" @@ -26,7 +27,7 @@ int InputTree::pageWidth = 75; //used for prettyPrinting ECString InputTree::tempword[MAXSENTLEN]; int InputTree::tempwordnum = 0; -// reset internal state after reporting an error +// reset internal state before reporting an error #define TREEREADINGERROR(message) \ { InputTree::init(); error(__FILE__, __LINE__, message); } @@ -93,8 +94,7 @@ readParse(istream& is) if(!is) return; if(temp != "(") { - cerr << "Saw " << temp << endl; - TREEREADINGERROR("Should have seen an open paren here."); + TREEREADINGERROR("Saw '" + temp + "' instead of open paren here."); } /* get annotated symbols like NP-OBJ. term_ = NP ntInfo_ = OBJ */ temp = readNext(is); @@ -110,8 +110,7 @@ readParse(istream& is) if(temp == ")") return; if(temp != "(") { - cerr << "Saw " << temp << endl; - TREEREADINGERROR("Should have seen second open paren here."); + TREEREADINGERROR("Saw '" + temp + "' instead of second open paren here."); } for (;;) @@ -125,8 +124,9 @@ readParse(istream& is) if (temp==")") break; if (temp!="(") { - cerr << *this << endl; - TREEREADINGERROR("Should have open or closed paren here."); + stringstream message("Saw '"); + message << *this << "' instead of open or closed paren here."; + TREEREADINGERROR(message.str()); } } } @@ -176,8 +176,7 @@ newParse(istream& is, int& strt, InputTree* par) const Term* ctrm = Term::get(trm); if(!ctrm) { - cerr << "No such term " << trm << endl; - assert(ctrm); + TREEREADINGERROR("No such term: " + trm); } if(wrd!="" && !(ctrm->terminal_p())) { diff --git a/first-stage/PARSE/utils.C b/first-stage/PARSE/utils.C index 22ae465..1e054ed 100644 --- a/first-stage/PARSE/utils.C +++ b/first-stage/PARSE/utils.C @@ -52,6 +52,13 @@ error(const char *s) ERROR( s ); } +void +error(const char *filename, int filelinenum, string str) +{ + const char* s = str.c_str(); + error(filename, filelinenum, s); +} + ECString langAwareToLower(ECString str) { diff --git a/first-stage/PARSE/utils.h b/first-stage/PARSE/utils.h index c38346d..67c131a 100644 --- a/first-stage/PARSE/utils.h +++ b/first-stage/PARSE/utils.h @@ -24,8 +24,9 @@ #define WARN( msg ) if (!Bchart::silent) { warn( __FILE__, __LINE__, msg ); } #define ERROR( msg ) error( __FILE__, __LINE__, msg ) -void warn( const char *filename, int filelinenum, const char *msg ); -void error( const char *filename, int filelinenum, const char *msg ); +void warn(const char *filename, int filelinenum, const char *msg); +void error(const char *filename, int filelinenum, const char *msg); +void error(const char *filename, int filelinenum, string str); void error(const char *s); // backwards compatibility ECString langAwareToLower(ECString str); diff --git a/python/tests/test_tree.py b/python/tests/test_tree.py index 0f71066..54ce594 100644 --- a/python/tests/test_tree.py +++ b/python/tests/test_tree.py @@ -14,7 +14,7 @@ def test_tree_errors(): bllipparser.Tree('(())') File "/usr/local/lib/python2.7/dist-packages/bllipparser/RerankingParser.py", line 63, in __init__ parser.inputTreeFromString(input_tree_or_string) - RuntimeError: [first-stage/PARSE/InputTree.C:260]: Saw paren rather than term + RuntimeError: [first-stage/PARSE/InputTree.C:259]: Saw paren rather than term >>> bllipparser.Tree('(S1 (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (ADJP (RB fairly) (JJ simple)) (NN parse) (NN tree))) (. .)))') Tree('(S1 (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (ADJP (RB fairly) (JJ simple)) (NN parse) (NN tree))) (. .)))') >>> bllipparser.Tree('(S1 (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (ADJP (RB fairly) (JJ simple)) (NN parse) (NN tree))) (. .)))') @@ -36,7 +36,7 @@ def test_tree_errors(): bllipparser.Tree('Does not start with a paren') File "/usr/local/lib/python2.7/dist-packages/bllipparser/RerankingParser.py", line 63, in __init__ parser.inputTreeFromString(input_tree_or_string) - RuntimeError: [first-stage/PARSE/InputTree.C:97]: Should have seen an open paren here. + RuntimeError: [first-stage/PARSE/InputTree.C:97]: Saw 'Does' instead of open paren here. >>> bllipparser.Tree('(S1 eh)') Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1315, in __run @@ -45,7 +45,7 @@ def test_tree_errors(): bllipparser.Tree('(S1 eh)') File "/usr/local/lib/python2.7/dist-packages/bllipparser/RerankingParser.py", line 63, in __init__ parser.inputTreeFromString(input_tree_or_string) - RuntimeError: [first-stage/PARSE/InputTree.C:114]: Should have seen second open paren here. + RuntimeError: [first-stage/PARSE/InputTree.C:113]: Saw 'eh' instead of second open paren here. >>> bllipparser.Tree('(S1') Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1315, in __run @@ -54,7 +54,7 @@ def test_tree_errors(): bllipparser.Tree('(S1') File "/usr/local/lib/python2.7/dist-packages/bllipparser/RerankingParser.py", line 63, in __init__ parser.inputTreeFromString(input_tree_or_string) - RuntimeError: [first-stage/PARSE/InputTree.C:114]: Should have seen second open paren here. + RuntimeError: [first-stage/PARSE/InputTree.C:113]: Saw '' instead of second open paren here. >>> bllipparser.Tree('(S1 ((') Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1315, in __run @@ -63,7 +63,7 @@ def test_tree_errors(): bllipparser.Tree('(S1 ((') File "/usr/local/lib/python2.7/dist-packages/bllipparser/RerankingParser.py", line 63, in __init__ parser.inputTreeFromString(input_tree_or_string) - RuntimeError: [first-stage/PARSE/InputTree.C:260]: Saw paren rather than term + RuntimeError: [first-stage/PARSE/InputTree.C:259]: Saw paren rather than term >>> bllipparser.Tree('(S1 (NP') Traceback (most recent call last): File "/usr/lib/python2.7/doctest.py", line 1315, in __run