collectionLiteral method

Parser<CollectionLiteralNode> collectionLiteral()
inherited

List, Set, or Map literal ([1, 2], {'a': 1}, <int>{1, 2}).

Implementation

Parser<CollectionLiteralNode> collectionLiteral() =>
    seq3(
      ref0(constToken).optional(),
      ref0(typeArguments)
          .optional()
          .map((list) => list ?? const <TypeNode>[]),
      [
        // List literal [...]
        seq3(
          ref1(token, '['),
          ref0(collectionElement)
              .plusSeparated(ref1(token, ','))
              .map((l) => l.elements)
              .optionalWith(const <CollectionElementNode>[]),
          seq2(ref1(token, ',').optional(), ref1(token, ']')),
        ).map3((_, elems, _) => elems),
        // Set / Map literal {...}
        seq3(
          ref1(token, '{'),
          ref0(collectionElement)
              .plusSeparated(ref1(token, ','))
              .map((l) => l.elements)
              .optionalWith(const <CollectionElementNode>[]),
          seq2(ref1(token, ',').optional(), ref1(token, '}')),
        ).map3((_, elems, _) => elems),
      ].toChoiceParser(),
    ).map3(
      (constKw, typeArgs, elements) => CollectionLiteralNode(
        typeArguments: typeArgs,
        elements: elements,
        isConst: constKw != null,
      ),
    );