aGrUM  0.16.0
DSLReader_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
26 
27 namespace gum {
28 
29  template < typename GUM_SCALAR >
30  DSLReader< GUM_SCALAR >::DSLReader(BayesNet< GUM_SCALAR >* bn,
31  const std::string& filename) :
32  BNReader< GUM_SCALAR >(bn, filename) {
33  GUM_CONSTRUCTOR(DSLReader);
34  __bn = bn;
35  __streamName = filename;
36  __parseDone = false;
37 
38  __factory = new BayesNetFactory< GUM_SCALAR >(__bn);
39 
40  __ioerror = false;
41 
42  try {
43  __scanner = new DSL::Scanner(__streamName.c_str());
44  __parser = new DSL::Parser(__scanner);
45  __parser->setFactory((IBayesNetFactory*)__factory);
46  } catch (IOError&) { __ioerror = true; }
47  }
48 
49  template < typename GUM_SCALAR >
51  GUM_DESTRUCTOR(DSLReader);
52 
53  if (!__ioerror) {
54  // this could lead to memory leak !!
55  if (__parser) delete (__parser);
56 
57  if (__scanner) delete (__scanner);
58  }
59 
60  if (__factory) delete (__factory);
61  }
62 
63  template < typename GUM_SCALAR >
64  INLINE DSL::Scanner& DSLReader< GUM_SCALAR >::scanner() {
65  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
66 
67  return *__scanner;
68  }
69 
70  template < typename GUM_SCALAR >
71  INLINE const std::string& DSLReader< GUM_SCALAR >::streamName() const {
72  return __streamName;
73  }
74 
75  template < typename GUM_SCALAR >
76  INLINE bool DSLReader< GUM_SCALAR >::trace() const {
77  return __traceScanning;
78  }
79 
80  template < typename GUM_SCALAR >
81  INLINE void DSLReader< GUM_SCALAR >::trace(bool b) {
82  __traceScanning = b;
83  scanner().setTrace(b);
84  }
85 
86  template < typename GUM_SCALAR >
88  if (__ioerror) { GUM_ERROR(gum::IOError, "No such file " + streamName()); }
89 
90  if (!__parseDone) {
91  try {
92  __parser->Parse();
93  __parseDone = true;
94  } catch (gum::Exception& e) {
95  GUM_SHOWERROR(e);
96  return 1 + __parser->errors().error_count;
97  }
98  }
99 
100  return (__parser->errors().error_count);
101  }
102 
105  template < typename GUM_SCALAR >
107  if (__parseDone)
108  return __parser->errors().error(i).line;
109  else {
110  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
111  }
112  }
113 
114  template < typename GUM_SCALAR >
116  if (__parseDone)
117  return __parser->errors().error(i).column;
118  else {
119  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
120  }
121  }
122 
123  template < typename GUM_SCALAR >
125  if (__parseDone)
126  return __parser->errors().error(i).is_error;
127  else {
128  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
129  }
130  }
131 
132  template < typename GUM_SCALAR >
133  INLINE std::string DSLReader< GUM_SCALAR >::errMsg(Idx i) {
134  if (__parseDone)
135  return __parser->errors().error(i).msg;
136  else {
137  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
138  }
139  }
140 
141  template < typename GUM_SCALAR >
142  INLINE void DSLReader< GUM_SCALAR >::showElegantErrors(std::ostream& o) {
143  if (__parseDone)
144  __parser->errors().elegantErrors(o);
145  else {
146  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
147  }
148  }
149 
150  template < typename GUM_SCALAR >
151  INLINE void
153  if (__parseDone)
154  __parser->errors().elegantErrorsAndWarnings(o);
155  else {
156  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
157  }
158  }
159 
160  template < typename GUM_SCALAR >
161  INLINE void DSLReader< GUM_SCALAR >::showErrorsAndWarnings(std::ostream& o) {
162  if (__parseDone)
163  __parser->errors().simpleErrorsAndWarnings(o);
164  else {
165  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
166  }
167  }
168 
169  template < typename GUM_SCALAR >
170  INLINE void DSLReader< GUM_SCALAR >::showErrorCounts(std::ostream& o) {
171  if (__parseDone)
172  __parser->errors().syntheticResults(o);
173  else {
174  GUM_ERROR(OperationNotAllowed, "DSL file not parsed yet");
175  }
176  }
177 
178  template < typename GUM_SCALAR >
180  return (!__parseDone) ? (Size)0 : __parser->errors().error_count;
181  }
182 
183  template < typename GUM_SCALAR >
185  return (!__parseDone) ? (Size)0 : __parser->errors().warning_count;
186  }
187 
189 } // namespace gum
190 
191 #endif // DOXYGEN_SHOULD_SKIP_THIS
Size errors()
publishing Errors API
DSL::Parser * __parser
Definition: DSLReader.h:120
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:61
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Idx errLine(Idx i)
line of ith error or warning
bool __traceScanning
Definition: DSLReader.h:123
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:119
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:106
~DSLReader() final
Default destructor.
std::string __streamName
Definition: DSLReader.h:122
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:124
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:117
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
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:55
BayesNetFactory< GUM_SCALAR > * __factory
Definition: DSLReader.h:118