From 1687d4ef3c739dbee4334c0eb7ed645523c49159 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20K=C4=99dzia?= <pawel.kedzia@pwr.wroc.pl>
Date: Wed, 8 Dec 2010 20:12:00 +0100
Subject: [PATCH] Included line/col/file in Parser exception messages

---
 libwccl/parser/Parser.cpp  | 55 +++++++++++++++++++++++++++++++++++---
 wcclparser/bool_main.cpp   | 29 ++++++++++----------
 wcclparser/main.cpp        |  9 +++++--
 wcclparser/strop_main.cpp  | 23 +++++++++-------
 wcclparser/tagset_main.cpp | 27 ++++++++++---------
 5 files changed, 100 insertions(+), 43 deletions(-)

diff --git a/libwccl/parser/Parser.cpp b/libwccl/parser/Parser.cpp
index 8d6cc0e..b713ae6 100644
--- a/libwccl/parser/Parser.cpp
+++ b/libwccl/parser/Parser.cpp
@@ -2,6 +2,9 @@
 #include "ANTLRLexer.hpp"
 #include "ANTLRParser.hpp"
 
+#include <antlr/NoViableAltException.hpp>
+#include <antlr/MismatchedTokenException.hpp>
+
 namespace Wccl {
 
 /**
@@ -177,8 +180,19 @@ boost::shared_ptr<ANTLRParserResultBase> Parser::parseAnyOperator(
 		ANTLRParser parser(lexer);
 		try {
 			result = parser.parse_symset_operator(tagset_);
+		} catch (antlr::MismatchedTokenException &e) {
+			errors << std::endl
+					<< "(as tset    ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
+		} catch(antlr::NoViableAltException &e) {
+			errors << std::endl
+					<< "(as tset    ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
 		} catch (antlr::ANTLRException& e) {
-			errors << "(as tset) " << e.getMessage() << "\n";
+			errors << std::endl
+					<< "(as tset    ) " << e.getMessage();
 			// ignore, try another type
 		}
 	}
@@ -189,8 +203,19 @@ boost::shared_ptr<ANTLRParserResultBase> Parser::parseAnyOperator(
 		ANTLRParser parser(lexer);
 		try {
 			result = parser.parse_strset_operator(tagset_);
+		} catch (antlr::MismatchedTokenException &e) {
+			errors << std::endl
+					<< "(as strset  ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
+		} catch(antlr::NoViableAltException &e) {
+			errors << std::endl
+					<< "(as strset  ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
 		} catch (antlr::ANTLRException& e) {
-			errors << "(as strset) " << e.getMessage() << "\n";
+			errors << std::endl
+					<< "(as strset  ) " << e.getMessage();
 			// ignore, try another type
 		}
 	}
@@ -201,8 +226,19 @@ boost::shared_ptr<ANTLRParserResultBase> Parser::parseAnyOperator(
 		ANTLRParser parser(lexer);
 		try {
 			result = parser.parse_bool_operator(tagset_);
+		} catch (antlr::MismatchedTokenException &e) {
+			errors << std::endl
+					<< "(as bool    ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
+		} catch(antlr::NoViableAltException &e) {
+			errors << std::endl
+					<< "(as bool    ) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage();
 		} catch (antlr::ANTLRException& e) {
-			errors << "(as predicate) " << e.getMessage() << "\n";
+			errors << std::endl
+					<< "(as bool    ) " << e.getMessage();
 			// ignore, try another type
 		}
 	}
@@ -213,8 +249,19 @@ boost::shared_ptr<ANTLRParserResultBase> Parser::parseAnyOperator(
 		ANTLRParser parser(lexer);
 		try {
 			result = parser.parse_position_operator(tagset_);
+		} catch (antlr::MismatchedTokenException &e) {
+			errors << std::endl
+					<< "(as position) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage() << std::endl;
+		} catch(antlr::NoViableAltException &e) {
+			errors << std::endl
+					<< "(as position) "
+					<< e.getFileLineColumnString()
+					<< " " << e.getMessage() << std::endl;
 		} catch (antlr::ANTLRException& e) {
-			errors << "(as position) " << e.getMessage() << "\n";
+			errors << std::endl
+					<< "(as position) " << e.getMessage() << std::endl;
 			// ignore, try another type
 		}
 	}
diff --git a/wcclparser/bool_main.cpp b/wcclparser/bool_main.cpp
index e63a5c3..c1570b0 100644
--- a/wcclparser/bool_main.cpp
+++ b/wcclparser/bool_main.cpp
@@ -1,7 +1,8 @@
 #include <cstdlib>
-
-#include <libwccl/values/strset.h>
+#include <libwccl/values/bool.h>
 #include <libwccl/parser/Parser.h>
+#include <libwccl/parser/ANTLRParserResult.h>
+#include <antlr/NoViableAltException.hpp>
 #include <antlr/MismatchedTokenException.hpp>
 
 // ----------------------------------------------------------------------------
@@ -50,30 +51,28 @@ int main()
 
 					if ((retVal = retOp->op->apply(cx)).get()) {
 						std::cerr << "Parsed expression: " << retVal->to_raw_string()
-											<< std::endl;
+								<< std::endl;
 					}
 					else {
-						std::cerr << "Problem while parsing -- "
-											<< "haven't got StrSet object in boost::shared_ptr!"
-											<< std::endl;
+						std::cerr << "Problem while parsing -- Bool is NULL!" << std::endl;
 					}
 				}
 				else {
 					std::cerr << "Problem while parsing -- "
-										<< "haven't got Function<Wccl::StrSet> object in "
-										<< "boost::shared_ptr!" << std::endl;
+							<< "Function<Wccl::Bool> is NULL!" << std::endl;
 				}
 			}
 			catch (antlr::MismatchedTokenException &e) {
-				std::cerr << e.getMessage() << std::endl;
-			}
-			catch (Wccl::InvalidVariableName &e) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch(antlr::NoViableAltException &e) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch (Wccl::InvalidVariableName &e) {
 				std::cerr << "Wccl::InvalidVariableName" << std::endl;
-			}
-			catch (Wccl::VariableTypeMismatch &e) {
+			} catch (Wccl::VariableTypeMismatch &e) {
 				std::cerr << "Wccl::VariableTypeMismatch" << std::endl;
-			}
-			catch (...) {
+			} catch (...) {
 				std::cerr << "[N] Syntax error!" << std::endl;
 			}
 		}
diff --git a/wcclparser/main.cpp b/wcclparser/main.cpp
index 4df9c59..41fd12a 100644
--- a/wcclparser/main.cpp
+++ b/wcclparser/main.cpp
@@ -11,6 +11,7 @@
 #include <boost/program_options.hpp>
 #include <libcorpus2/io/xcesreader.h>
 
+#include <antlr/NoViableAltException.hpp>
 #include <antlr/MismatchedTokenException.hpp>
 // ----------------------------------------------------------------------------
 
@@ -145,13 +146,17 @@ bool process_line(const std::string& line, Wccl::Parser& parser,
 				<< "parser returned NULL!" << std::endl;
 		}
 	} catch (antlr::MismatchedTokenException &e) {
-		std::cerr << e.getMessage() << std::endl;
+		std::cerr << e.getFileLineColumnString()
+				<< " " << e.getMessage() << std::endl;
+	} catch(antlr::NoViableAltException &e) {
+		std::cerr << e.getFileLineColumnString()
+				<< " " << e.getMessage() << std::endl;
 	} catch (Wccl::InvalidVariableName &e) {
 		std::cerr << "Wccl::InvalidVariableName " << e.info() << std::endl;
 	} catch (Wccl::VariableTypeMismatch &e) {
 		std::cerr << "Wccl::VariableTypeMismatch " << e.info() << std::endl;
 	} catch (Wccl::WcclError& e) {
-		std::cerr << "Wccl::WcclError " << e.info() << std::endl;
+		std::cerr << "Wccl::WcclError:" << e.info() << std::endl;
 	} catch (PwrNlp::PwrNlpError& e) {
 		std::cerr << "PwrNlp::PwrNlpError " << e.info() << std::endl;
 	} catch (antlr::ANTLRException& e) {
diff --git a/wcclparser/strop_main.cpp b/wcclparser/strop_main.cpp
index 96a837f..13be60b 100644
--- a/wcclparser/strop_main.cpp
+++ b/wcclparser/strop_main.cpp
@@ -1,9 +1,8 @@
 #include <cstdlib>
-#include <boost/shared_ptr.hpp>
-
 #include <libwccl/values/strset.h>
 #include <libwccl/parser/Parser.h>
 #include <libwccl/parser/ANTLRParserResult.h>
+#include <antlr/NoViableAltException.hpp>
 #include <antlr/MismatchedTokenException.hpp>
 
 // ----------------------------------------------------------------------------
@@ -54,21 +53,25 @@ int main()
 											<< std::endl;
 					}
 					else {
-						std::cerr << "Problem while parsing -- "
-											<< "haven't got StrSet object in boost::shared_ptr!"
-											<< std::endl;
+						std::cerr << "Problem while parsing -- StrSet is NULL!" << std::endl;
 					}
 				}
 				else {
 					std::cerr << "Problem while parsing -- "
-										<< "haven't got Function<Wccl::StrSet> object in "
-										<< "boost::shared_ptr!" << std::endl;
+							<< "Function<Wccl::StrSet> is NULL!" << std::endl;
 				}
 			}
 			catch (antlr::MismatchedTokenException &e) {
-				std::cerr << e.getMessage() << std::endl;
-			}
-			catch (...) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch(antlr::NoViableAltException &e) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch (Wccl::InvalidVariableName &e) {
+				std::cerr << "Wccl::InvalidVariableName" << std::endl;
+			} catch (Wccl::VariableTypeMismatch &e) {
+				std::cerr << "Wccl::VariableTypeMismatch" << std::endl;
+			} catch (...) {
 				std::cerr << "[N] Syntax error!" << std::endl;
 			}
 		}
diff --git a/wcclparser/tagset_main.cpp b/wcclparser/tagset_main.cpp
index 7229a73..ddfaaa4 100644
--- a/wcclparser/tagset_main.cpp
+++ b/wcclparser/tagset_main.cpp
@@ -1,13 +1,12 @@
 #include <cstdlib>
-
-#include <libcorpus2/tagsetmanager.h>
-
 #include <libwccl/values/tset.h>
-
 #include <libwccl/parser/Parser.h>
 #include <libwccl/parser/ANTLRParserResult.h>
+#include <antlr/NoViableAltException.hpp>
 #include <antlr/MismatchedTokenException.hpp>
 
+#include <libcorpus2/tagsetmanager.h>
+
 // ----------------------------------------------------------------------------
 
 /**
@@ -56,21 +55,25 @@ int main()
 											<< std::endl;
 					}
 					else {
-						std::cerr << "Problem while parsing -- "
-											<< "haven't got StrSet object in boost::shared_ptr!"
-											<< std::endl;
+						std::cerr << "Problem while parsing -- TSet is NULL!" << std::endl;
 					}
 				}
 				else {
 					std::cerr << "Problem while parsing -- "
-										<< "haven't got Function<Wccl::StrSet> object in "
-										<< "boost::shared_ptr!" << std::endl;
+							<< "Function<Wccl::TSet> is NULL!" << std::endl;
 				}
 			}
 			catch (antlr::MismatchedTokenException &e) {
-				std::cerr << e.getMessage() << std::endl;
-			}
-			catch (...) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch(antlr::NoViableAltException &e) {
+				std::cerr << e.getFileLineColumnString()
+						<< " " << e.getMessage() << std::endl;
+			} catch (Wccl::InvalidVariableName &e) {
+				std::cerr << "Wccl::InvalidVariableName" << std::endl;
+			} catch (Wccl::VariableTypeMismatch &e) {
+				std::cerr << "Wccl::VariableTypeMismatch" << std::endl;
+			} catch (...) {
 				std::cerr << "[N] Syntax error!" << std::endl;
 			}
 		}
-- 
GitLab