Implement PCRE Matching (prefer left-branch) #2

Merged
Rockingcool merged 48 commits from implementPCREMatchingRules into master 2025-02-09 15:24:29 -06:00
Showing only changes of commit fbc9dfcc95 - Show all commits

View File

@@ -15,8 +15,8 @@ const (
func getPriority(state *nfaState) int {
if state.isKleene {
return kleene_priority
} else if state.isQuestion || state.isAlternation {
return zerostate_priority
} else if state.isAlternation {
return alternation_priority
} else {
if state.isEmpty {
@@ -33,6 +33,14 @@ type priorQueueItem struct {
index int
}
func newPriorQueueItem(state *nfaState) *priorQueueItem {
return &priorQueueItem{
state: state,
index: -1,
priority: getPriority(state),
}
}
type priorityQueue []*priorQueueItem
func (pq priorityQueue) Len() int {
@@ -41,7 +49,7 @@ func (pq priorityQueue) Len() int {
func (pq priorityQueue) Less(i, j int) bool {
if pq[i].priority == pq[j].priority {
return pq[i].index > pq[j].index
return pq[i].index < pq[j].index
}
return pq[i].priority > pq[j].priority // We want max-heap, so we use greater-than
}
@@ -68,6 +76,11 @@ func (pq *priorityQueue) Pop() any {
*pq = old[0 : n-1]
return item
}
func (pq *priorityQueue) peek() any {
queue := *pq
n := len(queue)
return queue[n-1]
}
func (pq *priorityQueue) update(item *priorQueueItem, value *nfaState, priority int) {
item.state = value