You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
2 months ago
|
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
|
||
|
}
|