unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.69k stars 265 forks source link

`Pattern.run.indexed : Pattern a -> a -> Optional ([(a, Nat)], a)` #3669

Open pchiusano opened 1 year ago

pchiusano commented 1 year ago

Idea for new builtin, discussed with @runarorama

Returns the starting index of each capture.

Also possible: returns [(Nat,Nat)] for the captures (start index and length).

pchiusano commented 1 year ago

Some implementation notes of how this could be done. In Util.Text.Pattern.hs, I'd just change:

-- Stack used to track captures and to support backtracking.
-- A `try` will push a `Mark` that allows the old state
-- (both the list of captures and the current remainder)
-- to be restored on failure.
data Stack = Empty !Captures | Mark !Captures !Text !Stack

-- A difference list for representing the captures of a pattern.
-- So that capture lists can be appended in O(1).
type Captures = [Text] -> [Text]

Change Captures to [(Text,Word64)] -> [(Text,Word64)], then get everything typechecking in that file.

Then tweak the existing Pattern.run builtin to throw out the starting indices. Then introduce Pattern.run.indexed.

aryairani commented 1 year ago

@runarorama We are re-triaging this and wanted to check the current status.

runarorama commented 1 year ago

Have not gotten around to this due to Unison library work