Removed some panics with error throwing; changed some comments; removed use of mustPop() in parsing Kleene star - I use pop() and then throw an error if pop() returns an error

master
Aadhavan Srinivasan 5 days ago
parent 5e3801af7c
commit 20142e93c4

@ -344,7 +344,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
} }
if c == '\\' { // Escape character - invert special and non-special characters eg. \( is treated as a literal parentheses, \b is treated as word boundary if c == '\\' { // Escape character - invert special and non-special characters eg. \( is treated as a literal parentheses, \b is treated as word boundary
if i == len(re_postfix)-1 { // End of string - panic, because backslash is an escape character (something needs to come after it) if i == len(re_postfix)-1 { // End of string - throw error, because backslash is an escape character (something needs to come after it)
return nil, fmt.Errorf("backslash with no escape character") return nil, fmt.Errorf("backslash with no escape character")
} }
i++ i++
@ -426,7 +426,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
regex += string(re_postfix[i]) regex += string(re_postfix[i])
i++ i++
} }
if len(regex) <= 1 { // Nothing in regex - panic if len(regex) <= 1 { // Nothing in regex - throw error
return nil, fmt.Errorf("invalid lookaround. (too short?)") return nil, fmt.Errorf("invalid lookaround. (too short?)")
} }
// 'regex' should now contain the lookaround regex, plus the characters at the start (which indicate pos/neg, ahead/behind) // 'regex' should now contain the lookaround regex, plus the characters at the start (which indicate pos/neg, ahead/behind)
@ -660,7 +660,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
endOfRange = false // Reset the flag endOfRange = false // Reset the flag
} }
} }
if i == len(re_postfix) { // We have reached the end of the string, so we didn't encounter a closing brakcet. Panic. if i == len(re_postfix) { // We have reached the end of the string, so we didn't encounter a closing brakcet. Throw error.
return nil, fmt.Errorf("opening bracket without closing bracket") return nil, fmt.Errorf("opening bracket without closing bracket")
} }
@ -690,7 +690,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
startRangeNum, err := strconv.Atoi(string(startRange)) startRangeNum, err := strconv.Atoi(string(startRange))
if err != nil { if err != nil {
panic(err) return nil, fmt.Errorf("invalid numeric range")
} }
if re_postfix[i] == '}' { // Case 1 above if re_postfix[i] == '}' { // Case 1 above
@ -708,7 +708,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
return nil, fmt.Errorf("brace not closed") return nil, fmt.Errorf("brace not closed")
} }
if re_postfix[i] != '}' { if re_postfix[i] != '}' {
return nil, fmt.Errorf("invalid numeric specifier") return nil, fmt.Errorf("invalid start range for numeric specifier")
} }
if len(endRange) == 0 { // Case 3 above if len(endRange) == 0 { // Case 3 above
endRangeNum = INFINITE_REPS endRangeNum = INFINITE_REPS
@ -716,7 +716,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
var err error var err error
endRangeNum, err = strconv.Atoi(string(endRange)) endRangeNum, err = strconv.Atoi(string(endRange))
if err != nil { if err != nil {
panic(err) return nil, fmt.Errorf("invalid end range for numeric specifier")
} }
} }
} }
@ -737,7 +737,7 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) {
numOpenParens++ numOpenParens++
} }
if c == ')' { if c == ')' {
// Keep popping from opStack until we encounter an opening parantheses or a NONCAPLPAREN_CHAR. Panic if we reach the end of the stack. // Keep popping from opStack until we encounter an opening parantheses or a NONCAPLPAREN_CHAR. Throw error if we reach the end of the stack.
var val rune var val rune
var err error var err error
for val, err = peek(opStack); val != '(' && val != NONCAPLPAREN_CHAR; val, err = peek(opStack) { for val, err = peek(opStack); val != '(' && val != NONCAPLPAREN_CHAR; val, err = peek(opStack) {
@ -951,7 +951,10 @@ func thompson(re []postfixNode) (Reg, error) {
nfa = append(nfa, s1) nfa = append(nfa, s1)
} }
case KLEENE: // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state case KLEENE: // Create a 0-state, concat the popped state after it, concat the 0-state after the popped state
s1 := mustPop(&nfa) s1, err := pop(&nfa)
if err != nil {
return Reg{}, fmt.Errorf("error applying kleene star")
}
stateToAdd := kleene(*s1) stateToAdd := kleene(*s1)
nfa = append(nfa, stateToAdd) nfa = append(nfa, stateToAdd)
case PLUS: // a+ is equivalent to aa* case PLUS: // a+ is equivalent to aa*

Loading…
Cancel
Save