% crochet

// Matches wrap the underlying match object in JavaScript more or less
// directly, but here we don't really expose the `index` of the match
// because Crochet's text is supposed to be entirely opaque.

/// The entire text that was matched by the regular expression.
command regex-match matched-text =
  foreign re.matched-text(self.box);

/// The indexed capture at the given index.
command regex-match at: (Index is integer)
requires within-bounds :: (Index >= 1) and (Index <= self captures count)
  = foreign re.matched-index(self.box, Index);

/// The named capture with the given name.
command regex-match at: (Name is text)
requires group-defined :: self named-captures contains-key: Name
  = foreign re.matched-name(self.box, Name);

/// All indexed captures in the match.
command regex-match indexed-captures -> list<text> =
  foreign re.matched-groups(self.box);

/// All named captures in the match.
command regex-match named-captures -> record =
  foreign re.matched-named-groups(self.box);
