1 #include <utl/UnitEvaluator.h>
2 #include <utl/ErrorLogger.h>
3 #include <utl/AugerException.h>
5 #include <boost/version.hpp>
6 #if BOOST_VERSION >= 103800
7 # define BOOST_SPIRIT_USE_OLD_NAMESPACE
8 # include <boost/spirit/include/classic_parser.hpp>
10 # include <boost/spirit/core/parser.hpp>
24 UnitEvaluator::GetError()
27 ostringstream errStream;
28 errStream <<
"Unit parsing failed: ";
30 case UnitGrammar::eLookup:
31 errStream <<
"Unknown unit in the expression.";
33 case UnitGrammar::eBracket:
34 errStream <<
"Not a well-formed expression. (e.g.: brackets)";
36 case UnitGrammar::eBadSymbol:
37 errStream <<
"Invalid symbol in the expression. (e.g.: + or -)";
39 case UnitGrammar::eParsing:
40 errStream <<
"Boost spirit parsing error.";
43 errStream <<
"Unknown error.";
46 errStream <<
'\n' << fExpression <<
'\n'
47 << setw(fErrPosition) <<
" "
48 <<
"^ failed at the " << fErrPosition+1 <<
". position\n";
49 return errStream.str();
54 UnitEvaluator::Evaluate(
const string& expression)
57 fExpression = expression;
58 fError = UnitGrammar::eOk;
61 string::iterator firstChar = fExpression.begin();
62 const string::iterator lastChar = fExpression.end();
64 boost::spirit::parse_info<string::iterator> info;
67 info = boost::spirit::parse(
71 boost::spirit::space_p
75 fError = UnitGrammar::eParsing;
76 fErrPosition = info.stop - firstChar;
77 const char errChar = fExpression[fErrPosition];
78 if (errChar ==
'-' || errChar ==
'+')
79 fError = UnitGrammar::eBadSymbol;
80 if (errChar ==
'(' || errChar ==
')')
81 fError = UnitGrammar::eBracket;
82 firstChar = info.stop;
83 }
else if (!info.full) {
84 fError = UnitGrammar::eBracket;
87 }
while(!info.full && info.hit);
89 if (fError != UnitGrammar::eOk) {
90 const string err = GetError();
Exception for errors encountered when parsing XML.
#define ERROR(message)
Macro for logging error messages.