From 444413e1f75a69b1dd86547d04a1ae94cd81f9e6 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 28 Oct 2024 17:39:32 -0400 Subject: [PATCH] Added postfixNode type to represent a node in the postfix representation of the regex --- postfixNode.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 postfixNode.go diff --git a/postfixNode.go b/postfixNode.go new file mode 100644 index 0000000..c7f384a --- /dev/null +++ b/postfixNode.go @@ -0,0 +1,49 @@ +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 +}