aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/parser.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-02-06 15:35:07 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-02-06 15:35:07 +0200
commitf7cd640bcc59f64e37648487391989d245ed8f0f (patch)
tree336820da30501602ae7c27ef20e0cb39ea13acad /libbuild2/parser.cxx
parent2269a611da40bd7242dbd1a3204c212ac6091fd7 (diff)
Add support for nested subscript, use for json accessjson-type-review
Diffstat (limited to 'libbuild2/parser.cxx')
-rw-r--r--libbuild2/parser.cxx184
1 files changed, 95 insertions, 89 deletions
diff --git a/libbuild2/parser.cxx b/libbuild2/parser.cxx
index 1ac159b..7cc01e3 100644
--- a/libbuild2/parser.cxx
+++ b/libbuild2/parser.cxx
@@ -8589,126 +8589,132 @@ namespace build2
// Handle value subscript.
//
- if (tt == type::lsbrace && mode () == lexer_mode::eval)
+ if (mode () == lexer_mode::eval) // Note: not if(sub)!
{
- location bl (get_location (t));
- next (t, tt); // `[`
- mode (lexer_mode::subscript, '\0' /* pair */);
- next (t, tt);
-
- location l (get_location (t));
- value v (
- tt != type::rsbrace
- ? parse_value (t, tt, pattern_mode::ignore, "value subscript")
- : value (names ()));
-
- if (tt != type::rsbrace)
+ while (tt == type::lsbrace)
{
- // Note: wildcard pattern should have `]` as well so no escaping
- // suggestion.
- //
- fail (t) << "expected ']' instead of " << t;
- }
+ location bl (get_location (t));
+ next (t, tt); // `[`
+ mode (lexer_mode::subscript, '\0' /* pair */);
+ next (t, tt);
- if (!pre_parse_)
- {
- // For type-specific subscript implementations we pass the
- // subscript value as is.
- //
- if (auto f = (result->type != nullptr
- ? result->type->subscript
- : nullptr))
+ location l (get_location (t));
+ value v (
+ tt != type::rsbrace
+ ? parse_value (t, tt, pattern_mode::ignore, "value subscript")
+ : value (names ()));
+
+ if (tt != type::rsbrace)
{
- result_data = f (*result, &result_data, move (v), l, bl);
+ // Note: wildcard pattern should have `]` as well so no escaping
+ // suggestion.
+ //
+ fail (t) << "expected ']' instead of " << t;
}
- else
+
+ if (!pre_parse_)
{
- uint64_t j;
- try
- {
- j = convert<uint64_t> (move (v));
- }
- catch (const invalid_argument& e)
+ // For type-specific subscript implementations we pass the
+ // subscript value as is.
+ //
+ if (auto f = (result->type != nullptr
+ ? result->type->subscript
+ : nullptr))
{
- fail (l) << "invalid value subscript: " << e <<
- info (bl) << "use the '\\[' escape sequence if this is a "
- << "wildcard pattern" << endf;
+ result_data = f (*result, &result_data, move (v), l, bl);
}
-
- // Similar to expanding an undefined variable, we return NULL if
- // the index is out of bounds.
- //
- // Note that result may or may not point to result_data.
- //
- if (result->null)
- result_data = value ();
- else if (result->type == nullptr)
+ else
{
- const names& ns (result->as<names> ());
+ uint64_t j;
+ try
+ {
+ j = convert<uint64_t> (move (v));
+ }
+ catch (const invalid_argument& e)
+ {
+ fail (l) << "invalid value subscript: " << e <<
+ info (bl) << "use the '\\[' escape sequence if this is a "
+ << "wildcard pattern" << endf;
+ }
- // Pair-aware subscript.
+ // Similar to expanding an undefined variable, we return NULL
+ // if the index is out of bounds.
//
- names r;
- for (auto i (ns.begin ()); i != ns.end (); ++i, --j)
+ // Note that result may or may not point to result_data.
+ //
+ if (result->null)
+ result_data = value ();
+ else if (result->type == nullptr)
{
- if (j == 0)
+ const names& ns (result->as<names> ());
+
+ // Pair-aware subscript.
+ //
+ names r;
+ for (auto i (ns.begin ()); i != ns.end (); ++i, --j)
{
- r.push_back (*i);
+ if (j == 0)
+ {
+ r.push_back (*i);
+ if (i->pair)
+ r.push_back (*++i);
+ break;
+ }
+
if (i->pair)
- r.push_back (*++i);
- break;
+ ++i;
}
- if (i->pair)
- ++i;
+ result_data = r.empty () ? value () : value (move (r));
}
+ else
+ {
+ // Similar logic to parse_for().
+ //
+ const value_type* etype (result->type->element_type);
- result_data = r.empty () ? value () : value (move (r));
- }
- else
- {
- // Similar logic to parse_for().
- //
- const value_type* etype (result->type->element_type);
+ value val (result == &result_data
+ ? value (move (result_data))
+ : value (*result));
- value val (result == &result_data
- ? value (move (result_data))
- : value (*result));
+ untypify (val, false /* reduce */);
- untypify (val, false /* reduce */);
+ names& ns (val.as<names> ());
- names& ns (val.as<names> ());
+ // Pair-aware subscript.
+ //
+ names r;
+ for (auto i (ns.begin ()); i != ns.end (); ++i, --j)
+ {
+ bool p (i->pair);
- // Pair-aware subscript.
- //
- names r;
- for (auto i (ns.begin ()); i != ns.end (); ++i, --j)
- {
- bool p (i->pair);
+ if (j == 0)
+ {
+ r.push_back (move (*i));
+ if (p)
+ r.push_back (move (*++i));
+ break;
+ }
- if (j == 0)
- {
- r.push_back (move (*i));
if (p)
- r.push_back (move (*++i));
- break;
+ ++i;
}
- if (p)
- ++i;
- }
-
- result_data = r.empty () ? value () : value (move (r));
+ result_data = r.empty () ? value () : value (move (r));
- if (etype != nullptr)
- typify (result_data, *etype, nullptr /* var */);
+ if (etype != nullptr)
+ typify (result_data, *etype, nullptr /* var */);
+ }
}
+
+ result = &result_data;
}
- result = &result_data;
+ // See if we have another subscript.
+ //
+ enable_subscript ();
+ tt = peek ();
}
-
- tt = peek ();
}
if (pre_parse_)