@ -1,5 +1,7 @@
package main
package main
import "fmt"
type NodeType int
type NodeType int
// This is a slice containing all escapable characters that have special meaning.
// 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
// 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 := postfixNode { }
toReturn . startReps = 1
toReturn . startReps = 1
toReturn . endReps = 1
toReturn . endReps = 1
@ -86,8 +88,13 @@ func newEscapedNode(c rune) postfixNode {
toReturn = newPostfixDotNode ( )
toReturn = newPostfixDotNode ( )
toReturn . except = append ( [ ] postfixNode { } , newPostfixNode ( wordChars ... ) )
toReturn . except = append ( [ ] postfixNode { } , newPostfixNode ( wordChars ... ) )
case 'b' , 'B' :
case 'b' , 'B' :
if c == 'b' && inCharClass {
toReturn . nodetype = CHARACTER
toReturn . contents = append ( toReturn . contents , rune ( 8 ) )
} else {
toReturn . nodetype = ASSERTION
toReturn . nodetype = ASSERTION
toReturn . contents = append ( toReturn . contents , c )
toReturn . contents = append ( toReturn . contents , c )
}
case 'n' : // Newline character
case 'n' : // Newline character
toReturn . nodetype = CHARACTER
toReturn . nodetype = CHARACTER
toReturn . contents = append ( toReturn . contents , '\n' )
toReturn . contents = append ( toReturn . contents , '\n' )
@ -110,10 +117,13 @@ func newEscapedNode(c rune) postfixNode {
toReturn . nodetype = CHARACTER
toReturn . nodetype = CHARACTER
toReturn . contents = append ( toReturn . contents , rune ( 11 ) )
toReturn . contents = append ( toReturn . contents , rune ( 11 ) )
default : // None of the above - append it as a regular character
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 . nodetype = CHARACTER
toReturn . contents = append ( toReturn . contents , c )
toReturn . contents = append ( toReturn . contents , c )
}
}
return toReturn
return toReturn , nil
}
}
// Creates and returns a postfixNode based on the given contents
// Creates and returns a postfixNode based on the given contents