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} {β : Type} [Inhabited β] (arr : Array α) :
ST.Ref β (Array α)

make reference of array

Equations
Instances For
    def BoundedBacktracker.Array.Ref.getRefValue {α : Type} {β : Type} [Inhabited β] (ref : ST.Ref β (Array α)) :

    get array of reference

    Equations
    Instances For
      def BoundedBacktracker.Array.Ref.modifyRefValue {α : Type} {β : 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
        Equations

        Char position in a substring

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

            create a CharPos from s and position «at»

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

                to next position CharPos of cp

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

                  is CharPos at start position

                  Equations
                  • cp.atStart = decide (cp.pos = cp.s.startPos)
                  Instances For

                    is CharPos at stop position

                    Equations
                    • cp.atStop = decide (cp.pos = cp.s.stopPos)
                    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]

                            Push frame to stack

                            Equations
                            • stack.push v = v :: stack
                            Instances For
                              @[inline]

                              pop head frame from stack

                              Equations
                              • stack.pop? = match stack with | [] => none | head :: tail => some (head, tail)
                              Instances For
                                @[inline]

                                append stacks

                                Equations
                                • stack1.append stack2 = match stack2 with | [v1, v2] => v2 :: v1 :: stack1 | x => (List.reverse stack2).append stack1
                                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
                                    theorem BoundedBacktracker.SearchState.ext_iff {n : Nat} {x : BoundedBacktracker.SearchState n} {y : BoundedBacktracker.SearchState n} :
                                    x = y x.stack = y.stack x.visited = y.visited x.countVisited = y.countVisited x.sid = y.sid x.input = y.input x.at = y.at x.slots = y.slots x.recentCaptures = y.recentCaptures x.logEnabled = y.logEnabled x.msgs = y.msgs x.halfMatch = y.halfMatch x.backtracks = y.backtracks
                                    theorem BoundedBacktracker.SearchState.ext {n : Nat} {x : BoundedBacktracker.SearchState n} {y : BoundedBacktracker.SearchState n} (stack : x.stack = y.stack) (visited : x.visited = y.visited) (countVisited : x.countVisited = y.countVisited) (sid : x.sid = y.sid) (input : x.input = y.input) (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

                                    State of the backtracking search

                                    Instances For

                                      create the SearchState from an NFA

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

                                        get encoded index of Fin n an CharPos in visited array

                                        Equations
                                        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

                                            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
                                              theorem BoundedBacktracker.withMsg_eq {nfa : NFA.Checked.NFA} {s : BoundedBacktracker.SearchState nfa.n} {s1 : BoundedBacktracker.SearchState nfa.n} {msg : UnitString} (h : BoundedBacktracker.withMsg msg s = s1) :
                                              s.countVisited = s1.countVisited s.stack = s1.stack
                                              theorem BoundedBacktracker.toNextStep'_eq {nfa : NFA.Checked.NFA} {state : NFA.Checked.State nfa.n} {s : BoundedBacktracker.SearchState nfa.n} {s1 : BoundedBacktracker.SearchState nfa.n} {msg : UnitString} (h : BoundedBacktracker.toNextStep' nfa state (BoundedBacktracker.withMsg msg s) = s1) :
                                              s.countVisited = s1.countVisited
                                              theorem BoundedBacktracker.toNextStepChecked_false_eq (nfa : NFA.Checked.NFA) (s : BoundedBacktracker.SearchState nfa.n) (s1 : BoundedBacktracker.SearchState nfa.n) (h : BoundedBacktracker.toNextStepChecked nfa s = (false, s1)) :
                                              s.countVisited = s1.countVisited s.stack = s1.stack

                                              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 (nfa : NFA.Checked.NFA) (s : BoundedBacktracker.SearchState nfa.n) (s1 : BoundedBacktracker.SearchState nfa.n) (h : BoundedBacktracker.steps.loop nfa s = s1) :
                                                  s.countVisited s1.countVisited
                                                  theorem BoundedBacktracker.steps_lt_or_eq_lt (nfa : NFA.Checked.NFA) (s : BoundedBacktracker.SearchState nfa.n) (s1 : BoundedBacktracker.SearchState nfa.n) (h : BoundedBacktracker.steps nfa s = s1) :
                                                  s.countVisited < s1.countVisited s.countVisited = s1.countVisited List.length s.stack = List.length s1.stack
                                                  theorem BoundedBacktracker.toNextFrameStep_true_lt_or_eq_lt (nfa : NFA.Checked.NFA) (s : BoundedBacktracker.SearchState nfa.n) (s1 : BoundedBacktracker.SearchState nfa.n) (h : BoundedBacktracker.toNextFrameStep nfa s = (true, s1)) :
                                                  s.countVisited < s1.countVisited s.countVisited = s1.countVisited List.length s.stack = List.length s1.stack
                                                  theorem BoundedBacktracker.toNextFrameRestoreCapture_true_lt_or_eq_lt {n : Nat} {s1 : BoundedBacktracker.SearchState n} (slot : Nat) (offset : Nat × Nat × Option String.Pos) (stack : BoundedBacktracker.Stack n) (s : BoundedBacktracker.SearchState n) (h : BoundedBacktracker.toNextFrameRestoreCapture slot offset stack s = (true, s1)) :
                                                  s.countVisited = s1.countVisited stack = s1.stack
                                                  theorem BoundedBacktracker.toNextFrame_true_lt (nfa : NFA.Checked.NFA) (s : BoundedBacktracker.SearchState nfa.n) (s1 : BoundedBacktracker.SearchState nfa.n) (h : BoundedBacktracker.toNextFrame nfa s = (true, s1)) :
                                                  s.countVisited < s1.countVisited s.countVisited = s1.countVisited List.length s1.stack < List.length s.stack

                                                  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 slots of each capture group.

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