Skip to content

Commit

Permalink
update variable names and some other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaeguard committed Sep 30, 2023
1 parent 9a27432 commit b3d82bb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 106 deletions.
50 changes: 24 additions & 26 deletions nfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const (
newline = 10
)

func toNfa(memory *parsingContext) (*State, *RegexError) {
func toNfa(parseContext *parsingContext) (*State, *RegexError) {
startFrom := 0
endAt := len(memory.tokens) - 1
endAt := len(parseContext.tokens) - 1

token := memory.tokens[startFrom]
startState, endState, err := tokenToNfa(token, memory, &State{
token := parseContext.tokens[startFrom]
startState, endState, err := tokenToNfa(token, parseContext, &State{
transitions: map[uint8][]*State{},
})

Expand All @@ -47,7 +47,7 @@ func toNfa(memory *parsingContext) (*State, *RegexError) {
}

for i := startFrom + 1; i <= endAt; i++ {
_, endNext, err := tokenToNfa(memory.tokens[i], memory, endState)
_, endNext, err := tokenToNfa(parseContext.tokens[i], parseContext, endState)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func toNfa(memory *parsingContext) (*State, *RegexError) {
return start, nil
}

func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*State, *State, *RegexError) {
func tokenToNfa(token regexToken, parseContext *parsingContext, startFrom *State) (*State, *State, *RegexError) {
switch token.tokenType {
case literal:
value := token.value.(uint8)
Expand All @@ -93,7 +93,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
startFrom.transitions[value] = []*State{to}
return startFrom, to, nil
case quantifier:
return handleQuantifierToToken(token, memory, startFrom)
return handleQuantifierToToken(token, parseContext, startFrom)
case wildcard:
to := &State{
transitions: map[uint8][]*State{},
Expand All @@ -104,11 +104,11 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
return startFrom, to, nil
case or:
values := token.value.([]regexToken)
_, end1, err := tokenToNfa(values[0], memory, startFrom)
_, end1, err := tokenToNfa(values[0], parseContext, startFrom)
if err != nil {
return nil, nil, err
}
_, end2, err := tokenToNfa(values[1], memory, startFrom)
_, end2, err := tokenToNfa(values[1], parseContext, startFrom)
if err != nil {
return nil, nil, err
}
Expand All @@ -125,7 +125,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
v := token.value.(groupTokenPayload)

// concatenate all the elements in the group
start, end, err := tokenToNfa(v.tokens[0], memory, &State{
start, end, err := tokenToNfa(v.tokens[0], parseContext, &State{
transitions: map[uint8][]*State{},
})

Expand All @@ -134,22 +134,22 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
}

for i := 1; i < len(v.tokens); i++ {
_, endNext, err := tokenToNfa(v.tokens[i], memory, end)
_, endNext, err := tokenToNfa(v.tokens[i], parseContext, end)
if err != nil {
return nil, nil, err
}
end = endNext
}
// concatenation ends

groupNameNumeric := fmt.Sprintf("%d", memory.nextGroup())
groupNameNumeric := fmt.Sprintf("%d", parseContext.nextGroup())
groupNameUserSet := v.name

groupNames := []string{groupNameNumeric}
memory.capturedGroups[groupNameNumeric] = true
parseContext.capturedGroups[groupNameNumeric] = true
if groupNameUserSet != "" {
groupNames = append(groupNames, groupNameUserSet)
memory.capturedGroups[groupNameUserSet] = true
parseContext.capturedGroups[groupNameUserSet] = true
}

if startFrom.groups != nil {
Expand Down Expand Up @@ -190,7 +190,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
return startFrom, end, nil
}

start, end, err := tokenToNfa(values[0], memory, &State{
start, end, err := tokenToNfa(values[0], parseContext, &State{
transitions: map[uint8][]*State{},
})

Expand All @@ -199,7 +199,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
}

for i := 1; i < len(values); i++ {
_, endNext, err := tokenToNfa(values[i], memory, end)
_, endNext, err := tokenToNfa(values[i], parseContext, end)

if err != nil {
return nil, nil, err
Expand All @@ -211,20 +211,19 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
startFrom.transitions[epsilonChar] = append(startFrom.transitions[epsilonChar], start)
return startFrom, end, nil
case bracket:
constructTokens := token.value.([]regexToken)
constructTokens := token.value.(map[uint8]bool)

to := &State{
transitions: map[uint8][]*State{},
}

for _, construct := range constructTokens {
ch := construct.value.(uint8)
for ch := range constructTokens {
startFrom.transitions[ch] = []*State{to}
}

return startFrom, to, nil
case bracketNot:
constructTokens := token.value.([]regexToken)
constructTokens := token.value.(map[uint8]bool)

to := &State{
transitions: map[uint8][]*State{},
Expand All @@ -234,8 +233,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
transitions: map[uint8][]*State{},
}

for _, construct := range constructTokens {
ch := construct.value.(uint8)
for ch := range constructTokens {
startFrom.transitions[ch] = []*State{deadEnd}
}
startFrom.transitions[anyChar] = []*State{to}
Expand All @@ -253,7 +251,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
return startFrom, startFrom, nil
case backReference:
groupName := token.value.(string)
if _, ok := memory.capturedGroups[groupName]; !ok {
if _, ok := parseContext.capturedGroups[groupName]; !ok {
return nil, nil, &RegexError{
Code: CompilationError,
Message: fmt.Sprintf("Group (%s) does not exist", groupName),
Expand All @@ -277,7 +275,7 @@ func tokenToNfa(token regexToken, memory *parsingContext, startFrom *State) (*St
}
}

func handleQuantifierToToken(token regexToken, memory *parsingContext, startFrom *State) (*State, *State, *RegexError) {
func handleQuantifierToToken(token regexToken, parseContext *parsingContext, startFrom *State) (*State, *State, *RegexError) {
payload := token.value.(quantifierPayload)
// the minimum amount of time the NFA needs to repeat
min := payload.min
Expand Down Expand Up @@ -310,7 +308,7 @@ func handleQuantifierToToken(token regexToken, memory *parsingContext, startFrom
} else {
value = token.value.([]regexToken)[0]
}
previousStart, previousEnd, err := tokenToNfa(value, memory, &State{
previousStart, previousEnd, err := tokenToNfa(value, parseContext, &State{
transitions: map[uint8][]*State{},
})

Expand All @@ -323,7 +321,7 @@ func handleQuantifierToToken(token regexToken, memory *parsingContext, startFrom
// starting from 2, because the one above is the first one
for i := 2; i <= total; i++ {
// the same NFA needs to be generated 'total' times
start, end, err := tokenToNfa(value, memory, &State{
start, end, err := tokenToNfa(value, parseContext, &State{
transitions: map[uint8][]*State{},
})

Expand Down
Loading

0 comments on commit b3d82bb

Please sign in to comment.