aGrUM  0.14.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&) { __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  __parseDone = true;
92  } catch (gum::Exception& e) {
93  GUM_SHOWERROR(e);
94  return 1 + __parser->errors().error_count;
95  }
96  }
97 
98  return (__parser->errors().error_count);
99  }
100 
103  template < typename GUM_SCALAR >
105  if (__parseDone)
106  return __parser->errors().error(i).line;
107  else {
108  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
109  }
110  }
111 
112  template < typename GUM_SCALAR >
114  if (__parseDone)
115  return __parser->errors().error(i).column;
116  else {
117  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
118  }
119  }
120 
121  template < typename GUM_SCALAR >
123  if (__parseDone)
124  return __parser->errors().error(i).is_error;
125  else {
126  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
127  }
128  }
129 
130  template < typename GUM_SCALAR >
131  INLINE std::string DSLReader< GUM_SCALAR >::errMsg(Idx i) {
132  if (__parseDone)
133  return __parser->errors().error(i).msg;
134  else {
135  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
136  }
137  }
138 
139  template < typename GUM_SCALAR >
140  INLINE void DSLReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
141  if (__parseDone)
142  __parser->errors().elegantErrors(o);
143  else {
144  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
145  }
146  }
147 
148  template < typename GUM_SCALAR >
149  INLINE void
151  if (__parseDone)
152  __parser->errors().elegantErrorsAndWarnings(o);
153  else {
154  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
155  }
156  }
157 
158  template < typename GUM_SCALAR >
159  INLINE void DSLReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
160  if (__parseDone)
161  __parser->errors().simpleErrorsAndWarnings(o);
162  else {
163  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
164  }
165  }
166 
167  template < typename GUM_SCALAR >
168  INLINE void DSLReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
169  if (__parseDone)
170  __parser->errors().syntheticResults(o);
171  else {
172  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
173  }
174  }
175 
176  template < typename GUM_SCALAR >
178  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
179  }
180 
181  template < typename GUM_SCALAR >
183  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
184  }
185 
187 } // namespace gum
188 
189 #endif // DOXYGEN_SHOULD_SKIP_THIS
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:58
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
const std::string & streamName() const
name of readen file
std::string errMsg(Idx i)
message of ith error or warning
DSL::Scanner * __scanner
Definition: DSLReader.h:116
bool trace() const
accessor to trace function (just write the number of parser line)
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.
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
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Size warnings()
of errors
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:52
BayesNetFactory< GUM_SCALAR > * __factory
Definition: DSLReader.h:115