aboutsummaryrefslogtreecommitdiff
path: root/libbuild2
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2022-09-28 15:21:47 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2022-09-28 15:21:47 +0200
commitf96b437a0c0ff0a62b16d425d500496ffb76e70e (patch)
tree555c3bf53807a6f5900c829b9ae58cdf0267691e /libbuild2
parent7ef61db09d651a1537a66404d1c74b07017116d1 (diff)
Add support for typed value subscript
Diffstat (limited to 'libbuild2')
-rw-r--r--libbuild2/parser.cxx42
1 files changed, 37 insertions, 5 deletions
diff --git a/libbuild2/parser.cxx b/libbuild2/parser.cxx
index 45b8cdd..5dbbfac 100644
--- a/libbuild2/parser.cxx
+++ b/libbuild2/parser.cxx
@@ -7457,12 +7457,44 @@ namespace build2
}
else
{
- // @@ TODO: we would want to return a value with element type.
+ // Similar logic to parse_for().
//
- //result_data = ...
- fail (l) << "typed value subscript not yet supported" <<
- info (bl) << "use the '\\[' escape sequence if this is a "
- << "wildcard pattern";
+ // @@ Maybe we should invent type-aware subscript? Could also
+ // be used for non-index subscripts (map keys etc).
+ //
+ const value_type* etype (result->type->element_type);
+
+ value val (result == &result_data
+ ? value (move (result_data))
+ : value (*result));
+
+ untypify (val);
+
+ names& ns (val.as<names> ());
+
+ // 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 (p)
+ ++i;
+ }
+
+ result_data = r.empty () ? value () : value (move (r));
+
+ if (etype != nullptr)
+ typify (result_data, *etype, nullptr /* var */);
}
result = &result_data;