NCBI C++ ToolKit
macro_ex.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OBJUTILS___MACRO_EX__HPP
2 #define GUI_OBJUTILS___MACRO_EX__HPP
3 
4 /* $Id: macro_ex.hpp 46859 2021-11-10 21:18:16Z asztalos $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Anatoly Osipov
30  *
31  * File Description: Macro language processing exception class
32  *
33  */
34 
35 /// @file macro_ex.hpp
36 /// Macro exceptions
37 
38 #include <sstream>
39 
40 #include <corelib/ncbiexpt.hpp>
42 #include <serial/serialbase.hpp>
43 #include <gui/gui_export.h>
44 /** @addtogroup GUI_MACRO_SCRIPTS_UTIL
45  *
46  * @{
47  */
48 
50 BEGIN_SCOPE(macro)
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 /// Macro exception
54 ///
56 public:
57  // Error codes for parsing the macro script
58  //
59  enum EErrCode {
64 
73 
76 
83 
90 
94  eParseCommaExpected
95  };
96 
97  // Convertion of error code to user domain message
98  //
99  virtual const char* GetErrCodeString(void) const override {
100  switch (GetErrCode()) {
101  case eParseKeywordExpected: return "eKeywordExpected";
102  case eParseAsnSelectorExpected: return "eSelectorExpected";
103 
104  case eParseMacroNameExpected: return "eMacroNameExpected";
105 
106  case eParseVariableDefined: return "eVarDefined";
107  case eParseVarDefinitionExpected: return "eVarDefinitionExpected";
108  case eParseUndefinedVariable: return "eUndefinedVar";
109  case eParseIdentifierNameExpected: return "eIdentifierNameExpected";
110  case eParseWrongVariableUsage: return "eWrongVariableUsage";
111 
112  case eParseAssignmentExpected: return "eAssignmentExpected";
113  case eParseAssignmentToConst: return "eWrongVarAssignment";
114 
115 
116  case eParseLeftBraceExpected: return "eLeftBraceExpected";
117  case eParseCommaOrRightBraceExpected: return "eCommaOrRightBraceExpected";
118  case eParseLeftParenthesisExpected: return "eLeftParanthExpected";
119  case eParseRightParenthesisExpected: return "eRightParanthExpected";
120 
121  case eParseWhereClauseExpected: return "eWhereSectionExpected";
122  case eParseWhereClauseInvalid: return "eInvalidWhereSection";
123 
124  case eParseFunctionOrObjectNameExpected: return "eFncOrObjectexpected";
125  case eParseFunctionExpected: return "eFncNameExpected";
126  case eParseFunctionOrDoneExpected: return "eFncOrDONEExpected";
127  case eParseEndOfFunction: return "eEndOfFunction";
128 
129  case eParseNothingExpected: return "eNoTokenExpected";
130  case eParseSemicolonExpected: return "eSemicolonExpected";
131  case eParseSeparatorExpected: return "eSeparatorExpected";
132 
133  default: return CException::GetErrCodeString();
134  }
135  }
136 
138 };
139 
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 /// class CMacroExecException
143 ///
144 /// Exception that carries information about the data, that caused the error in the form of CSerialObject
146 {
147 public:
148  /// Constructor.
150  const CException* prev_exception, EErrCode err_code,
151  const string& message, CConstRef<CSerialObject> data,
152  EDiagSev severity = eDiag_Error)
153  : CException(info, prev_exception, message, severity),
154  m_Data(data)
156 
157 public:
158  virtual void ReportExtra(ostream& out) const override
159  {
160  if (!m_Data)
161  return;
162 
163  out << "Data context:" << endl;
164  out << MSerial_AsnText << *(m_Data.GetPointer()) << endl;
165  }
166 
167  /// Returns the ASN.1 data that caused the error
168  CConstRef<CSerialObject> GetData() const throw() { return m_Data; }
169 
170  /// Returns the text encoded ASN.1 data that caused the error
171  string GetDataAsString() const throw()
172  {
173  if (!m_Data)
174  return "";
175 
176  stringstream strm;
177  strm << MSerial_AsnText << *(m_Data.GetPointer()) << endl;
178  return strm.str();
179  }
180 
181 protected:
182  virtual void x_Assign(const CException& src) override
183  {
185  m_Data = dynamic_cast<const CMacroDataException&>(src).m_Data;
186  }
187 
188 private:
190 };
191 
192 
193 ///////////////////////////////////////////////////////////////////////////////
194 /// class CMacroExecException
195 ///
197 public:
198  enum EErrCode {
199  eInternalError, // Internal error, usually indicates attempt to derefence
200  // non-initialized pointer
201  eWrongArguments, // Wrong argument count or types
202  eFloatNotSupported, // Float is not yet supported
203  eIncorrectIdentifier, // identifier could not be resolved (i.e. it is undefined)
204  eIncorrectNamedAnnot, // NA number could not be resolved
205  eFunctionNotImplemented,// function not implemented
206  eWrongFunctionScope, // function is used in the wrong do/where clause scope
207  eInvalidQuery, // a given query or part of query coming from qparser library is invalid
208  eIncorrectNodeType, // a given query node is incorrect, e.g. "abc"
209  eVarNotResolved, // Variable not resolved
210  eInvalidData, // Indicates errors, caused by invalid data
211  eFileNotFound, // File does not exist
212  eMultipleEntries, // Multiple entries in the table file
213  eSynonymsNotFound // Synonyms of a phrase are not found
214  };
215 
217  const CException *prev_exception, EErrCode err_code,
218  const string &message, unsigned lineNo = 0, unsigned columnNo = 0, EDiagSev severity = eDiag_Error)
219  : CException(info, prev_exception, (CException::EErrCode) CException::eInvalid, (message)),
220  m_LineNo(lineNo),
221  m_ColumnNo(columnNo)
223 
224 public:
225  virtual const char* GetErrCodeString(void) const override
226  {
227  switch (GetErrCode())
228  {
229  case eInternalError: return "Internal error";
230  case eWrongArguments: return "Wrong arguments";
231  case eIncorrectIdentifier: return "Incorrect identifier";
232  case eFunctionNotImplemented: return "Function was not implemented";
233  case eWrongFunctionScope: return "Wrong function scope";
234  case eInvalidQuery: return "Invalid query";
235  case eIncorrectNodeType: return "Incorrect node type";
236  case eVarNotResolved: return "Variable not resolved";
237  case eInvalidData: return "Invalid data";
238  case eFileNotFound: return "File not found";
239  case eMultipleEntries: return "Multiple entries in the table file";
240  case eSynonymsNotFound: return "Synonyms of a phrase not found";
241 
242  default: return "";
243  }
244  }
245 
246  /// Returns the line number where error occurred
247  unsigned GetLineNo(void) const throw() { return m_LineNo; }
248 
249  /// Sets the line number where error occurred
250  void SetLineNo(unsigned lineNo) throw() { m_LineNo = lineNo; }
251 
252  /// Returns the column number where error occurred
253  unsigned GetColumnNo(void) const throw() { return m_ColumnNo; }
254  /// Sets the column number where error occurred
255  void SetColumnNo(unsigned columnNo) throw() { m_ColumnNo = columnNo; }
256 
257  virtual void ReportExtra(ostream& out) const override
258  {
259  out << "[Error] Line " << m_LineNo << ", Pos " << m_ColumnNo << endl;
260  }
261 
262 protected:
263  virtual void x_Assign(const CException& src) override
264  {
266  const CMacroExecException& src_macro = dynamic_cast<const CMacroExecException&>(src);
267  m_LineNo = src_macro.m_LineNo;
268  m_ColumnNo = src_macro.m_ColumnNo;
269  }
270 
271 protected:
272  /// Line where the error occured
273  unsigned m_LineNo;
274  /// Column where the error occured
275  unsigned m_ColumnNo;
276 };
277 
278 /// Throws CMacroExecException with the specified message and error location from the TNode
279 inline void ThrowCMacroExecException(const CDiagCompileInfo &info, CMacroExecException::EErrCode code, const string &message, const CQueryParseTree::TNode *treeNode, const CException* previous = nullptr)
280 {
281  unsigned lineNo(0);
282  unsigned columnNo(0);
283  if (treeNode) {
284  const CQueryParseNode::SSrcLoc &loc = treeNode->GetValue().GetLoc();
285  // SSrcLoc positions are 0 based
286  lineNo = loc.line + 1;
287  columnNo = loc.pos + 1;
288  }
289  throw CMacroExecException(info, previous, code, message, lineNo, columnNo);
290 }
291 
292 END_SCOPE(macro)
294 
295 /* @} */
296 
297 #endif // GUI_OBJUTILS___MACRO_EX__HPP
Incapsulate compile time information such as __FILE__, __LINE__, NCBI_MODULE, current function.
Definition: ncbidiag.hpp:65
class CMacroExecException
Definition: macro_ex.hpp:146
class CMacroExecException
Definition: macro_ex.hpp:196
Macro exception.
Definition: macro_ex.hpp:55
definition of a Culling tree
Definition: ncbi_tree.hpp:100
std::ofstream out("events_result.xml")
main entry point for tests
char data[12]
Definition: iconv.c:80
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
@ eDiag_Error
Error message.
Definition: ncbidiag.hpp:653
TErrCode GetErrCode(void) const
Get error code.
Definition: ncbiexpt.cpp:453
virtual void x_Assign(const CException &src)
Helper method for copying exception data.
Definition: ncbiexpt.cpp:536
#define NCBI_EXCEPTION_DEFAULT_IMPLEMENTATION(exception_class, base_class)
Helper macro for default exception implementation.
Definition: ncbiexpt.hpp:1300
virtual const char * GetErrCodeString(void) const
Get error code interpreted as text.
Definition: ncbiexpt.cpp:444
#define EXCEPTION_VIRTUAL_BASE
Do not use virtual base classes in exception declaration at all, because in this case derived class s...
Definition: ncbiexpt.hpp:1388
CException(void)
Constructor with no arguments.
Definition: ncbiexpt.cpp:244
virtual void x_Assign(const CException &src) override
Definition: macro_ex.hpp:263
virtual void ReportExtra(ostream &out) const override
Definition: macro_ex.hpp:257
virtual void ReportExtra(ostream &out) const override
Constructor.
Definition: macro_ex.hpp:158
void ThrowCMacroExecException(const CDiagCompileInfo &info, CMacroExecException::EErrCode code, const string &message, const CQueryParseTree::TNode *treeNode, const CException *previous=nullptr)
Throws CMacroExecException with the specified message and error location from the TNode.
Definition: macro_ex.hpp:279
CConstRef< CSerialObject > GetData() const
Returns the ASN.1 data that caused the error.
Definition: macro_ex.hpp:168
void SetColumnNo(unsigned columnNo)
Sets the column number where error occurred.
Definition: macro_ex.hpp:255
unsigned GetColumnNo(void) const
Returns the column number where error occurred.
Definition: macro_ex.hpp:253
virtual const char * GetErrCodeString(void) const override
Definition: macro_ex.hpp:225
virtual const char * GetErrCodeString(void) const override
Definition: macro_ex.hpp:99
virtual void x_Assign(const CException &src) override
Definition: macro_ex.hpp:182
void SetLineNo(unsigned lineNo)
Sets the line number where error occurred.
Definition: macro_ex.hpp:250
unsigned GetLineNo(void) const
Returns the line number where error occurred.
Definition: macro_ex.hpp:247
NCBI_EXCEPTION_DEFAULT(CMacroParseException, CException)
string GetDataAsString() const
Returns the text encoded ASN.1 data that caused the error.
Definition: macro_ex.hpp:171
unsigned m_ColumnNo
Column where the error occured.
Definition: macro_ex.hpp:275
unsigned m_LineNo
Line where the error occured.
Definition: macro_ex.hpp:273
CConstRef< CSerialObject > m_Data
Definition: macro_ex.hpp:189
@ eParseFunctionOrObjectNameExpected
Definition: macro_ex.hpp:86
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
unsigned pos
Position in the src line.
unsigned line
Src line number.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
const TValue & GetValue(void) const
Return node's value.
Definition: ncbi_tree.hpp:184
#define NCBI_GUIOBJUTILS_EXPORT
Definition: gui_export.h:512
Defines to provide correct exporting from DLLs in Windows.
static MDB_envinfo info
Definition: mdb_load.c:37
Defines NCBI C++ exception handling.
Query string parsing components.
Source location (points to the position in the original src) All positions are 0 based.
Definition: inftrees.h:24
Modified on Fri Sep 20 14:58:19 2024 by modify_doxy.py rev. 669887