Wrote function to provide correct node for escaped character

master
Aadhavan Srinivasan 2 months ago
parent 1bafdcdb7e
commit 656c506aa8

@ -1,16 +1,18 @@
package main
import "slices"
type NodeType int
// This is a list of the possible node types
const (
CHARACTER NodeType = iota
PIPE
CONCATENATE
KLEENE
QUESTION
PLUS
ASSERTION
)
// This represents a node in the postfix representation of the expression
@ -19,6 +21,41 @@ type postfixNode struct {
contents []rune // Contents of the node - the length of this would only be >1 if the node represents a character class
}
// Creates a new escaped node - the given character is assumed to have been preceded by a backslash
func newEscapedNode(c rune) postfixNode {
toReturn := postfixNode{}
switch c {
case 's': // Whitespace
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, whitespaceChars...)
case 'S': // Non-whitespace - I am doing this in a fancy way, generating all dot characters, then removing whitespace characters from it
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, slices.DeleteFunc(dotChars(), func(r rune) bool {
return slices.Contains(whitespaceChars, r)
})...)
case 'd': // Digits
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, digitChars...)
case 'D': // Non-digits - same fancy way as 'S'
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, slices.DeleteFunc(dotChars(), func(r rune) bool {
return slices.Contains(digitChars, r)
})...)
case 'w': // word character
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, wordChars...)
case 'W': // Non-word character - same fancy way as 'S' and 'D'
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, slices.DeleteFunc(dotChars(), func(r rune) bool {
return slices.Contains(wordChars, r)
})...)
default: // None of the above - append it as a regular character
toReturn.nodetype = CHARACTER
toReturn.contents = append(toReturn.contents, c)
}
return toReturn
}
// Creates and returns a postfixNode based on the given contents
func newPostfixNode(contents ...rune) postfixNode {
if len(contents) < 1 {

Loading…
Cancel
Save