aGrUM  0.13.2
DSLReader_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef DOXYGEN_SHOULD_SKIP_THIS
22 
24 
25 namespace gum {
26 
27  template < typename GUM_SCALAR >
28  DSLReader< GUM_SCALAR >::DSLReader(BayesNet< GUM_SCALAR >* bn,
29  const std::string& filename) :
30  BNReader< GUM_SCALAR >(bn, filename) {
31  GUM_CONSTRUCTOR(DSLReader);
32  __bn = bn;
33  __streamName = filename;
34  __parseDone = false;
35 
36  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
37 
38  __ioerror = false;
39 
40  try {
41  __scanner = new DSL::Scanner(__streamName.c_str());
42  __parser = new DSL::Parser(__scanner);
43  __parser->setFactory((IBayesNetFactory*)__factory);
44  } catch (IOError& e) { __ioerror = true; }
45  }
46 
47  template < typename GUM_SCALAR >
49  GUM_DESTRUCTOR(DSLReader);
50 
51  if (!__ioerror) {
52  // this could lead to memory leak !!
53  if (__parser) delete (__parser);
54 
55  if (__scanner) delete (__scanner);
56  }
57 
58  if (__factory) delete (__factory);
59  }
60 
61  template < typename GUM_SCALAR >
62  INLINE DSL::Scanner& DSLReader< GUM_SCALAR >::scanner() {
63  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
64 
65  return *__scanner;
66  }
67 
68  template < typename GUM_SCALAR >
69  INLINE const std::string& DSLReader< GUM_SCALAR >::streamName() const {
70  return __streamName;
71  }
72 
73  template < typename GUM_SCALAR >
74  INLINE bool DSLReader< GUM_SCALAR >::trace() const {
75  return __traceScanning;
76  }
77 
78  template < typename GUM_SCALAR >
79  INLINE void DSLReader< GUM_SCALAR >::trace(bool b) {
80  __traceScanning = b;
81  scanner().setTrace(b);
82  }
83 
84  template < typename GUM_SCALAR >
86  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
87 
88  if (!__parseDone) {
89  try {
90  __parser->Parse();
91  } catch (gum::Exception& e) {
92  GUM_SHOWERROR(e);
93  return 1 + __parser->errors().error_count;
94  }
95 
96  __parseDone = true;
97  }
98 
99  return (__parser->errors().error_count);
100  }
101 
104  template < typename GUM_SCALAR >
106  if (__parseDone)
107  return __parser->errors().error(i).line;
108  else {
109  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
110  }
111  }
112 
113  template < typename GUM_SCALAR >
115  if (__parseDone)
116  return __parser->errors().error(i).column;
117  else {
118  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
119  }
120  }
121 
122  template < typename GUM_SCALAR >
124  if (__parseDone)
125  return __parser->errors().error(i).is_error;
126  else {
127  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
128  }
129  }
130 
131  template < typename GUM_SCALAR >
132  INLINE std::string DSLReader< GUM_SCALAR >::errMsg(Idx i) {
133  if (__parseDone)
134  return __parser->errors().error(i).msg;
135  else {
136  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
137  }
138  }
139 
140  template < typename GUM_SCALAR >
141  INLINE void DSLReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
142  if (__parseDone)
143  __parser->errors().elegantErrors(o);
144  else {
145  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
146  }
147  }
148 
149  template < typename GUM_SCALAR >
150  INLINE void
152  if (__parseDone)
153  __parser->errors().elegantErrorsAndWarnings(o);
154  else {
155  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
156  }
157  }
158 
159  template < typename GUM_SCALAR >
160  INLINE void DSLReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
161  if (__parseDone)
162  __parser->errors().simpleErrorsAndWarnings(o);
163  else {
164  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
165  }
166  }
167 
168  template < typename GUM_SCALAR >
169  INLINE void DSLReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
170  if (__parseDone)
171  __parser->errors().syntheticResults(o);
172  else {
173  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
174  }
175  }
176 
177  template < typename GUM_SCALAR >
179  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
180  }
181 
182  template < typename GUM_SCALAR >
184  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
185  }
186 
188 } // namespace gum
189 
190 #endif // DOXYGEN_SHOULD_SKIP_THIS
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
Size errors()
publishing Errors API
DSL::Parser * __parser
Definition: DSLReader.h:117
void showErrorCounts(std::ostream &o=std::cerr)
send on std::cerr the number of errors and the number of warnings
bool errIsError(Idx i)
type of ith error or warning
#define GUM_SHOWERROR(e)
Definition: exceptions.h:73
const std::string & streamName() const
name of readen file
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Idx errLine(Idx i)
line of ith error or warning
bool __traceScanning
Definition: DSLReader.h:120
std::string errMsg(Idx i)
message of ith error or warning
DSL::Scanner * __scanner
Definition: DSLReader.h:116
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:103
~DSLReader() final
Default destructor.
std::string __streamName
Definition: DSLReader.h:119
void showErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings
DSL::Scanner & scanner()
Direct access to DSL scanner (mandatory for listener connection)
bool __parseDone
Definition: DSLReader.h:121
Idx errCol(Idx i)
col of ith error or warning
Size proceed() final
parse.
bool trace() const
accessor to trace function (just write the number of parser line)
void showElegantErrorsAndWarnings(std::ostream &o=std::cerr)
send on std::cerr the list of errors or warnings with contents
BayesNet< GUM_SCALAR > * __bn
Definition: DSLReader.h:114
Size warnings()
of errors
unsigned long Idx
Type for indexes.
Definition: types.h:43
DSLReader(BayesNet< GUM_SCALAR > *bn, const std::string &filename)
Constructor A reader is defined for reading a defined file.
void showElegantErrors(std::ostream &o=std::cerr)
send on std::cerr the list of errorswith contents
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
BayesNetFactory< GUM_SCALAR > * __factory
Definition: DSLReader.h:115