From 685dcfe6329479f1e19313a30409ba9f6e113f7d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Nov 2016 08:30:43 +0200 Subject: Allow customizing names parsing diagnostics --- build2/parser | 24 +++++++++++++++++------- build2/parser.cxx | 18 +++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/build2/parser b/build2/parser index eeaec7b..bb6bc17 100644 --- a/build2/parser +++ b/build2/parser @@ -154,13 +154,18 @@ namespace build2 // If chunk is true, then parse the smallest but complete, name-wise, // chunk of input. Note that in this case you may still end up with - // multiple names, for example, {foo bar}. + // multiple names, for example, {foo bar} or $foo. + // + // The what argument is used in diagnostics (e.g., "expected + // instead of ...". // names_type - names (token& t, token_type& tt, bool chunk = false) + names (token& t, token_type& tt, + bool chunk = false, + const char* what = "name") { names_type ns; - names (t, tt, ns, chunk, 0, nullptr, nullptr, nullptr); + names (t, tt, ns, chunk, what, 0, nullptr, nullptr, nullptr); return ns; } @@ -170,10 +175,10 @@ namespace build2 // context evaluation. // value - names_value (token& t, token_type& tt) + names_value (token& t, token_type& tt, const char* what = "name") { names_type ns; - return names (t, tt, ns, false, 0, nullptr, nullptr, nullptr) + return names (t, tt, ns, false, what, 0, nullptr, nullptr, nullptr) ? value (move (ns)) : value (nullptr); } @@ -181,15 +186,19 @@ namespace build2 // Append names and return true if the parsed value is NOT NULL. // bool - names (token& t, token_type& tt, names_type& ns, bool chunk = false) + names (token& t, token_type& tt, + names_type& ns, + bool chunk = false, + const char* what = "name") { - return names (t, tt, ns, chunk, 0, nullptr, nullptr, nullptr); + return names (t, tt, ns, chunk, what, 0, nullptr, nullptr, nullptr); } bool names (token&, token_type&, names_type&, bool chunk, + const char* what, size_t pair, const string* prj, const dir_path* dir, @@ -198,6 +207,7 @@ namespace build2 size_t names_trailer (token&, token_type&, names_type&, + const char* what, size_t pair, const string* prj, const dir_path* dir, diff --git a/build2/parser.cxx b/build2/parser.cxx index a2d9d1b..ba6b957 100644 --- a/build2/parser.cxx +++ b/build2/parser.cxx @@ -1885,6 +1885,7 @@ namespace build2 size_t parser:: names_trailer (token& t, type& tt, names_type& ns, + const char* what, size_t pair, const string* pp, const dir_path* dp, @@ -1896,6 +1897,7 @@ namespace build2 names (t, tt, ns, false, + what, (pair != 0 ? pair : (ns.empty () || ns.back ().pair ? ns.size () : 0)), @@ -1913,7 +1915,7 @@ namespace build2 const location loc (get_location (t)); names_type x; // Parse into a separate list of names. - names_trailer (t, tt, x, 0, nullptr, nullptr, nullptr); + names_trailer (t, tt, x, what, 0, nullptr, nullptr, nullptr); if (size_t n = x.size ()) { @@ -2000,6 +2002,7 @@ namespace build2 names (token& t, type& tt, names_type& ns, bool chunk, + const char* what, size_t pair, const string* pp, const dir_path* dp, @@ -2153,7 +2156,7 @@ namespace build2 tp1 = &t1; } - count = names_trailer (t, tt, ns, pair, pp1, dp1, tp1); + count = names_trailer (t, tt, ns, what, pair, pp1, dp1, tp1); tt = peek (); continue; } @@ -2228,7 +2231,7 @@ namespace build2 }; location loc; - const char* what; // Variable or evaluation context. + const char* what; // Variable, function, or evaluation context. value result; // Holds function call/eval context result. if (tt == type::dollar) @@ -2538,7 +2541,7 @@ namespace build2 // if (tt == type::lcbrace) { - count = names_trailer (t, tt, ns, pair, pp, dp, tp); + count = names_trailer (t, tt, ns, what, pair, pp, dp, tp); tt = peek (); continue; } @@ -2568,7 +2571,8 @@ namespace build2 count = 1; } else if (count > 1) - fail (t) << "multiple names on the left hand side of a pair"; + fail (t) << "multiple " << what << "s on the left hand side " + << "of a pair"; ns.back ().pair = lexer_->pair_separator (); tt = peek (); @@ -2610,9 +2614,9 @@ namespace build2 break; } else - // Our caller expected this to be a name. + // Our caller expected this to be something. // - fail (t) << "expected name instead of " << t; + fail (t) << "expected " << what << " instead of " << t; } // Handle the empty RHS in a pair, (e.g., y@). -- cgit v1.1