Documentation

Regex.Backtrack

BoundedBacktracker #

An NFA backed bounded backtracker for executing regex searches with capturing groups (BoundedBacktracker.slots).

This regex engine only implements leftmost-first match semantics and only supports leftmost searches. By design, this backtracking regex engine is bounded. This bound is implemented by not visiting any combination of NFA state ID and position in a haystack more than once.

All searches execute in linear time with respect to the size of the regular expression and search text.

def BoundedBacktracker.Array.Ref.mkRef {α β : Type} [Inhabited β] (arr : Array α) :
ST.Ref β (Array α)

make reference of array

Equations
Instances For

    get array of reference

    Equations
    Instances For
      def BoundedBacktracker.Array.Ref.modifyRefValue {α β : Type} [Inhabited β] [DecidableEq β] (ref : ST.Ref β (Array α)) (index : Nat) (value : α) :
      ST.Ref β (Array α)

      modify array, try to perform the update destructively

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        Char position in a slice of some underlying string.

        Instances For
          @[reducible, inline]
          Equations
          Instances For
            Equations
            • One or more equations did not get rendered due to their size.

            create a CharPos from s and position «at»

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For

              to prev position of cp

              Equations
              Instances For
                @[irreducible]
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  to next position of cp

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    is CharPos at start position

                    Equations
                    Instances For

                      is CharPos at stop position

                      Equations
                      Instances For

                        Represents a stack frame on the heap while doing backtracking.

                        Instances For
                          Equations
                          • One or more equations did not get rendered due to their size.

                          A representation of "half" of a match reported by a DFA.

                          Instances For
                            Equations
                            • One or more equations did not get rendered due to their size.
                            @[reducible, inline]

                            The stack of frames

                            Equations
                            Instances For
                              @[inline]
                              def BoundedBacktracker.Stack.push {n : Nat} {s : String.Slice} (stack : Stack n s) (v : Frame n s) :
                              Stack n s

                              Push frame to stack

                              Equations
                              Instances For
                                @[inline]
                                def BoundedBacktracker.Stack.pop? {n : Nat} {s : String.Slice} (stack : Stack n s) :
                                Option (Frame n s × Stack n s)

                                pop head frame from stack

                                Equations
                                Instances For
                                  theorem BoundedBacktracker.Stack.pop?_some_lt {n : Nat} {s : String.Slice} {a : Frame n s} {s1 : Stack n s} (s✝ : Stack n s) (h : s✝.pop? = some (a, s1)) :
                                  @[inline]
                                  def BoundedBacktracker.Stack.append {n : Nat} {s : String.Slice} (stack1 stack2 : Stack n s) :
                                  Stack n s

                                  append stacks

                                  Equations
                                  Instances For
                                    @[inline]
                                    Equations
                                    Instances For
                                      @[reducible, inline]

                                      The encoded set of (Fin n, HaystackOffset) pairs that have been visited by the backtracker within a single search. Optimization: encode as bits

                                      Equations
                                      Instances For
                                        @[reducible, inline]

                                        SlotEntry consists of slot, group and char pos in s

                                        Equations
                                        Instances For
                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            theorem BoundedBacktracker.valid_of_range_map (s : String.Slice) (slots : Array (SlotEntry s)) (f : NatSlotEntry s) (hf : f = fun (i : Nat) => (i, i.div 2, none)) (h : List.map f (List.range slots.size) = slots.toList) :
                                            theorem BoundedBacktracker.valid_map_of_valid (s : String.Slice) (captures : Array NFA.Capture) (h : NFA.Capture.Valid captures) (slots : Array (SlotEntry s)) (hs : slots = Array.map toSlotEntry captures) (slot : { slot : SlotEntry s // slot slots slot.fst = slot.snd.fst * 2 + 1 }) :
                                            (slot' : SlotEntry s), slot' slots slot.val.snd.fst = slot'.snd.fst slot'.fst = slot'.snd.fst * 2
                                            theorem BoundedBacktracker.mem_range_map_of_mem {s : String.Slice} (captures : Array NFA.Capture) (slots : Array (SlotEntry s)) (f : NatSlotEntry s) (hf : f = fun (i : Nat) => (i, i.div 2, none)) (heq : Array.map f (Array.range slots.size) = slots) (hsize : slots.size = match (Array.map Capture.toSlot captures).max? with | some m => m + 1 | none => 0) (capture : NFA.Capture) :
                                            capture capturestoSlotEntry capture slots

                                            State of the backtracking search

                                            • stack : Stack n input

                                              Stack used on the heap for doing backtracking

                                            • visited : ST.Ref Nat Visited

                                              The encoded set of (Fin n, HaystackOffset) pairs that have been visited by the backtracker within a single search.

                                            • countVisited : Nat

                                              count of pairs that have been visited

                                            • sid : Fin n

                                              current state

                                            • at : CharPos input

                                              position in input string

                                            • slots : Array (SlotEntry input)

                                              slots, positions of captures in input

                                            • slotsValid : Slots.Valid self.slots

                                              slots are valid

                                            • recentCaptures : Array (Option (input.Pos × input.Pos))

                                              recent captures of capture groups

                                            • logEnabled : Bool

                                              is logging enabled

                                            • msgs : Array String

                                              log msgs

                                            • halfMatch : Option (HalfMatch input)

                                              HalfMatch

                                            • backtracks : Nat

                                              count of backtracks

                                            Instances For
                                              theorem BoundedBacktracker.SearchState.ext {n : Nat} {input : String.Slice} {x y : SearchState n input} (stack : x.stack = y.stack) (visited : x.visited = y.visited) (countVisited : x.countVisited = y.countVisited) (sid : x.sid = y.sid) («at» : x.at = y.at) (slots : x.slots = y.slots) (recentCaptures : x.recentCaptures = y.recentCaptures) (logEnabled : x.logEnabled = y.logEnabled) (msgs : x.msgs = y.msgs) (halfMatch : x.halfMatch = y.halfMatch) (backtracks : x.backtracks = y.backtracks) :
                                              x = y
                                              def BoundedBacktracker.SearchState.fromNfa (nfa : NFA.Checked.NFA) (input : String.Slice) («at» : input.Pos) (logEnabled : Bool) (h : 0 < nfa.n) :
                                              SearchState nfa.n input

                                              create the SearchState from an NFA

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                theorem BoundedBacktracker.SearchState.slots_of_map_valid {s : String.Slice} (slots : Array (SlotEntry s)) (h : Slots.Valid slots) (f : SlotEntry sSlotEntry s) (hf : ∀ (x : SlotEntry s), (f x).fst = x.fst (f x).snd.fst = x.snd.fst) :
                                                def BoundedBacktracker.Visited.index {n : Nat} {s : String.Slice} (sid : Fin n) (cp : CharPos s) :

                                                get encoded index of Fin n and CharPos in visited array

                                                Equations
                                                Instances For
                                                  theorem BoundedBacktracker.Visited.eq_of_linear_eq {a1 b1 a2 b2 x : Nat} (hb1 : b1 < x) (hb2 : b2 < x) (h : a1 * x + b1 = a2 * x + b2) :
                                                  a1 = a2 b1 = b2
                                                  theorem BoundedBacktracker.Visited.index_injective {n : Nat} {s : String.Slice} (sid1 sid2 : Fin n) (cp1 cp2 : CharPos s) (heq : index sid1 cp1 = index sid2 cp2) :
                                                  sid1 = sid2 cp1.pos.offset.byteIdx = cp2.pos.offset.byteIdx

                                                  index is injective.

                                                  Check if current Fin n and CharPos in SearchState is already visited. If not visited mark pair as visited.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For

                                                    Check if current Fin n and CharPos in SearchState is already visited. If not visited mark pair as visited.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      execute next steps in NFA.

                                                      Equations
                                                      Instances For
                                                        @[irreducible]
                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          @[irreducible]
                                                          theorem BoundedBacktracker.steps_loop_le {input : String.Slice} (nfa : NFA.Checked.NFA) (s s1 : SearchState nfa.n input) (h : steps.loop nfa s = s1) :
                                                          theorem BoundedBacktracker.toNextFrameRestoreCapture_true_lt_or_eq_lt {s : String.Slice} {n : Nat} {s1 : SearchState n s} (slot : Nat) (offset : Option s.Pos) (stack : Stack n s) (s✝ : SearchState n s) (h : BoundedBacktracker.toNextFrameRestoreCapture✝ slot offset stack s✝ = (true, s1)) :

                                                          BoundedBacktrack search

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For
                                                            @[irreducible]
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              Search for the first match of this regex in the haystack given and return log msgs and the matches of each capture group.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For