diff --git a/postfixNode.go b/postfixNode.go index 68f589d..b8318f8 100644 --- a/postfixNode.go +++ b/postfixNode.go @@ -1,5 +1,7 @@ package main +import "fmt" + type NodeType int // This is a slice containing all escapable characters that have special meaning. @@ -62,7 +64,7 @@ func newCharClassNode(nodes []postfixNode, negated bool) postfixNode { } // Creates a new escaped node - the given character is assumed to have been preceded by a backslash -func newEscapedNode(c rune) postfixNode { +func newEscapedNode(c rune, inCharClass bool) (postfixNode, error) { toReturn := postfixNode{} toReturn.startReps = 1 toReturn.endReps = 1 @@ -86,8 +88,13 @@ func newEscapedNode(c rune) postfixNode { toReturn = newPostfixDotNode() toReturn.except = append([]postfixNode{}, newPostfixNode(wordChars...)) case 'b', 'B': - toReturn.nodetype = ASSERTION - toReturn.contents = append(toReturn.contents, c) + if c == 'b' && inCharClass { + toReturn.nodetype = CHARACTER + toReturn.contents = append(toReturn.contents, rune(8)) + } else { + toReturn.nodetype = ASSERTION + toReturn.contents = append(toReturn.contents, c) + } case 'n': // Newline character toReturn.nodetype = CHARACTER toReturn.contents = append(toReturn.contents, '\n') @@ -110,10 +117,13 @@ func newEscapedNode(c rune) postfixNode { toReturn.nodetype = CHARACTER toReturn.contents = append(toReturn.contents, rune(11)) default: // None of the above - append it as a regular character + if isNormalChar(c) { // Normal characters cannot be escaped + return postfixNode{}, fmt.Errorf("Invalid escape character.") + } toReturn.nodetype = CHARACTER toReturn.contents = append(toReturn.contents, c) } - return toReturn + return toReturn, nil } // Creates and returns a postfixNode based on the given contents