|
|
|
package main
|
|
|
|
|
|
|
|
type NodeType int
|
|
|
|
|
|
|
|
// This is a list of the possible node types
|
|
|
|
const (
|
|
|
|
CHARACTER NodeType = iota
|
|
|
|
|
|
|
|
PIPE
|
|
|
|
CONCATENATE
|
|
|
|
KLEENE
|
|
|
|
QUESTION
|
|
|
|
PLUS
|
|
|
|
)
|
|
|
|
|
|
|
|
// This represents a node in the postfix representation of the expression
|
|
|
|
type postfixNode struct {
|
|
|
|
nodetype NodeType
|
|
|
|
contents []rune // Contents of the node - the length of this would only be >1 if the node represents a character class
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates and returns a postfixNode based on the given contents
|
|
|
|
func newPostfixNode(contents ...rune) postfixNode {
|
|
|
|
if len(contents) < 1 {
|
|
|
|
panic("Empty node.")
|
|
|
|
}
|
|
|
|
to_return := postfixNode{}
|
|
|
|
if len(contents) > 1 { // If the node has more than element, it must be a character class - the type must be CHARACTER
|
|
|
|
to_return.nodetype = CHARACTER
|
|
|
|
to_return.contents = contents
|
|
|
|
} else { // Node has one element, could be anything
|
|
|
|
switch contents[0] {
|
|
|
|
case '+':
|
|
|
|
to_return.nodetype = PLUS
|
|
|
|
case '?':
|
|
|
|
to_return.nodetype = QUESTION
|
|
|
|
case '*':
|
|
|
|
to_return.nodetype = KLEENE
|
|
|
|
case '|':
|
|
|
|
to_return.nodetype = PIPE
|
|
|
|
case CONCAT:
|
|
|
|
to_return.nodetype = CONCATENATE
|
|
|
|
default:
|
|
|
|
to_return.nodetype = CHARACTER
|
|
|
|
}
|
|
|
|
to_return.contents = append(to_return.contents, contents...)
|
|
|
|
}
|
|
|
|
return to_return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a character node, regardless of the contents
|
|
|
|
func newPostfixCharNode(contents ...rune) postfixNode {
|
|
|
|
toReturn := postfixNode{}
|
|
|
|
toReturn.nodetype = CHARACTER
|
|
|
|
toReturn.contents = append(toReturn.contents, contents...)
|
|
|
|
return toReturn
|
|
|
|
}
|