From 00081d77200a08baac2e62cc2797c7530caacea3 Mon Sep 17 00:00:00 2001 From: Mark Spruiell Date: Fri, 16 Jan 2004 22:22:44 +0000 Subject: initial check-in --- cpp/src/FreezeScript/Error.cpp | 193 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 cpp/src/FreezeScript/Error.cpp (limited to 'cpp/src/FreezeScript/Error.cpp') diff --git a/cpp/src/FreezeScript/Error.cpp b/cpp/src/FreezeScript/Error.cpp new file mode 100644 index 00000000000..7262827c1d6 --- /dev/null +++ b/cpp/src/FreezeScript/Error.cpp @@ -0,0 +1,193 @@ +// ********************************************************************** +// +// Copyright (c) 2004 +// ZeroC, Inc. +// Billerica, MA, USA +// +// All Rights Reserved. +// +// Ice is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License version 2 as published by +// the Free Software Foundation. +// +// ********************************************************************** + +#include +#include +#include + +using namespace std; + +// +// ErrorReporter +// +FreezeScript::ErrorReporter::ErrorReporter(ostream& out, bool suppress) : + _out(out), _suppress(suppress) +{ +} + +ostream& +FreezeScript::ErrorReporter::stream() const +{ + return _out; +} + +void +FreezeScript::ErrorReporter::warning(const string& msg) +{ + ostringstream ostr; + if(!_descName.empty()) + { + ostr << "warning in <" << _descName << "> descriptor, line " << _descLine << ": " << msg << endl; + } + else + { + ostr << "warning: " << msg << endl; + } + string warn = ostr.str(); + if(_suppress) + { + map::iterator p = _warningHistory.find(warn); + if(p != _warningHistory.end()) + { + return; + } + _warningHistory.insert(map::value_type(warn, true)); + } + _out << warn; +} + +void +FreezeScript::ErrorReporter::error(const string& msg) +{ + ostringstream ostr; + if(!_descName.empty()) + { + ostr << "error in <" << _descName << "> descriptor, line " << _descLine << ": " << msg << endl; + } + else + { + ostr << "error: " << msg << endl; + } + throw Exception(__FILE__, __LINE__, ostr.str()); +} + +void +FreezeScript::ErrorReporter::typeMismatchError(const Slice::TypePtr& expected, const Slice::TypePtr& received, + bool fatal) +{ + ostringstream ostr; + ostr << "type mismatch: expected " << typeToString(expected) << " but received " << typeToString(received); + + if(fatal) + { + error(ostr.str()); + } + else + { + warning(ostr.str()); + } +} + +void +FreezeScript::ErrorReporter::conversionError(const string& value, const Slice::TypePtr& type, bool fatal) +{ + ostringstream ostr; + ostr << "unable to convert `" << value << "' to " << typeToString(type); + + if(fatal) + { + error(ostr.str()); + } + else + { + warning(ostr.str()); + } +} + +void +FreezeScript::ErrorReporter::rangeError(const string& value, const Slice::TypePtr& type, bool fatal) +{ + ostringstream ostr; + ostr << "value `" << value << "' is out of range for type " << typeToString(type); + + if(fatal) + { + error(ostr.str()); + } + else + { + warning(ostr.str()); + } +} + +void +FreezeScript::ErrorReporter::expressionSyntaxError(const string& msg) +{ + assert(!_expression.empty()); + ostringstream ostr; + ostr << "syntax error in expression `" << _expression << "': " << msg; + error(ostr.str()); +} + +void +FreezeScript::ErrorReporter::descriptorError(const string& msg, int line) +{ + ostringstream ostr; + ostr << "XML error on line " << line << ":" << endl << msg; + error(ostr.str()); +} + +void +FreezeScript::ErrorReporter::setDescriptor(const string& name, int line) +{ + _descName = name; + _descLine = line; +} + +void +FreezeScript::ErrorReporter::getDescriptor(string& name, int& line) +{ + name = _descName; + line = _descLine; +} + +void +FreezeScript::ErrorReporter::clearDescriptor() +{ + _descName.clear(); +} + +void +FreezeScript::ErrorReporter::setExpression(const string& expr) +{ + _expression = expr; +} + +void +FreezeScript::ErrorReporter::clearExpression() +{ + _expression.clear(); +} + +// +// DescriptorErrorContext +// +FreezeScript::DescriptorErrorContext::DescriptorErrorContext(const ErrorReporterPtr& errorReporter, const string& name, + int line) : + _errorReporter(errorReporter) +{ + // + // Save the existing descriptor information before changing it. + // + _errorReporter->getDescriptor(_name, _line); + _errorReporter->setDescriptor(name, line); +} + +FreezeScript::DescriptorErrorContext::~DescriptorErrorContext() +{ + // + // Restore the original descriptor information. + // + _errorReporter->setDescriptor(_name, _line); +} -- cgit v1.2.3