From f96b437a0c0ff0a62b16d425d500496ffb76e70e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 28 Sep 2022 15:21:47 +0200 Subject: Add support for typed value subscript --- libbuild2/parser.cxx | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'libbuild2') 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 ()); + + // 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; -- cgit v1.1