concise error messages
Hello,
I wrote a small DSEL with proto. An example program can be found in
the main function below. I use BOOST_MPL_ASSERT_MSG to check an
expression against a grammar, and can make the compiler output a small
string if the syntax is wrong. But I can do it only for the entire
program. Is there a way to do finer grain grammar checking with proto?
For example, I haven't defined the "<" operator for expressions, but
when I use it in the DSEL code, I get 10 pages of error messages from
g++, with the SYNTAX_NOT_CORRECT string somewhere. I would like to get
somewhat more accurate than this. Any pointers?
Thanks,
Manjunath
#include
{ };
struct expr_grammar :
proto::or_<
proto::when
{ };
struct bb_grammar :
proto::or_<
proto::when
{ };
struct napl_grammar :
proto::or_<
proto::when
{ };
template<typename Expr>
void check_grammar(const Expr &e)
{
BOOST_MPL_ASSERT_MSG((proto::matches
On 1/23/2010 9:46 AM, Manjunath Kudlur wrote:
I wrote a small DSEL with proto. An example program can be found in the main function below. I use BOOST_MPL_ASSERT_MSG to check an expression against a grammar, and can make the compiler output a small string if the syntax is wrong. But I can do it only for the entire program. Is there a way to do finer grain grammar checking with proto? For example, I haven't defined the "<" operator for expressions, but when I use it in the DSEL code, I get 10 pages of error messages from g++, with the SYNTAX_NOT_CORRECT string somewhere. I would like to get somewhat more accurate than this. Any pointers?
There is currently no way to do error reporting at anything but the top-level with Proto. And you can only get a yes/no answer from proto::matches at that level, so you can't really provide users with feedback about what specifically is wrong with their expression. It's a known shortcoming. But see below for a suggestion about how to deal with gcc's error reporting. <snip>
template<typename Expr> void check_grammar(const Expr&e) { BOOST_MPL_ASSERT_MSG((proto::matches
::value), SYNTAX_NOT_CORRECT, (void));
OK, you detect and report errors ...
cout<< napl_grammar()(e)<< "\n"; }
... but then you unconditionally try to evaluate the expression anyway.
Gcc doesn't stop on the first error. If you pass this function an
invalid expression, gcc will issue the error caused by
BOOST_MPL_ASSERT_MSG in addition to any errors caused by then trying to
evaluate an invalid expression.
Instead, you should put the actual expression evaluation in a separate
function, as follows:
// called for valid expressions
template<typename Expr>
void check_grammar_impl(const Expr& e, mpl::true_)
{
cout<< napl_grammar()(e)<< "\n";
}
// called for invalid expressions
template<typename Expr>
void check_grammar_impl(const Expr& e, mpl::false_)
{
// no-op
}
template<typename Expr>
void check_grammar(const Expr&e)
{
BOOST_MPL_ASSERT_MSG((proto::matches
On 1/23/2010 12:45 PM, Eric Niebler wrote:
template<typename Expr> void check_grammar(const Expr&e) { BOOST_MPL_ASSERT_MSG((proto::matches
::value), SYNTAX_NOT_CORRECT, (void)); // Dispatch to the correct impl depending on whether we have // a valid expression or not. check_grammar_impl(e, proto::matches ()); } That should reduce the noise gcc emits when your users create invalid expressions.
Yep. This is essentially what Spirit does. It's not perfect, but it works. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net http://www.facebook.com/djowel Meet me at BoostCon http://www.boostcon.com/home http://www.facebook.com/boostcon
Joel de Guzman wrote:
Yep. This is essentially what Spirit does. It's not perfect, but it works. I do the same too. I also, sometimes, use the macth result as a enable_if parameter on the return type soi get an simpl "no such function call" error for some expression.
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
Thanks Eric, Joel and Joel. With the above suggestion, at least the
last message I see from g++ is "SYNTAX_NOT_CORRECT", which is better
than the string embedded in many pages of error messages.
Manjunath
On Sat, Jan 23, 2010 at 12:19 AM, joel falcou
Joel de Guzman wrote:
Yep. This is essentially what Spirit does. It's not perfect, but it works.
I do the same too. I also, sometimes, use the macth result as a enable_if parameter on the return type soi get an simpl "no such function call" error for some expression.
-- ___________________________________________ Joel Falcou - Assistant Professor PARALL Team - LRI - Universite Paris Sud XI Tel : (+33)1 69 15 66 35
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Eric Niebler
-
Joel de Guzman
-
joel falcou
-
Manjunath Kudlur