From 3636825c64bb6c172b0858d7a08c30acfcd68bdd Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 21 Sep 2022 20:44:56 +0100 Subject: Adds the or operator | --- main/main.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'main/main.go') diff --git a/main/main.go b/main/main.go index 7bb5152..08514ab 100644 --- a/main/main.go +++ b/main/main.go @@ -95,7 +95,7 @@ func parseRegex(l *RuneReader, minPower int) RegexAST { switch r { case eof: return nil - case ')', '*', '-': + case ')', '*', '-', '|': l.rewind() return nil case '(': @@ -122,6 +122,12 @@ func parseRegex(l *RuneReader, minPower int) RegexAST { lhs = RegexASTMaximise{lhs} case r == '-' && minPower <= 4: lhs = RegexASTMinimise{lhs} + case r == '|' && minPower <= 2: + rhs := parseRegex(l, 3) + if rhs == nil { + panic("Missing regex after |") + } + lhs = RegexASTOr{lhs, rhs} default: l.rewind() break loop @@ -141,7 +147,7 @@ func parseSubex(l *RuneReader, minPower int) TransducerAST { if !l.accept(")") { panic("Missing matching )") } - case ')', '*', '-': + case ')', '*', '-', '|': l.rewind() return nil case '$': @@ -179,6 +185,12 @@ func parseSubex(l *RuneReader, minPower int) TransducerAST { lhs = TransducerASTMaximise{lhs} case r == '-' && minPower <= 4: lhs = TransducerASTMinimise{lhs} + case r == '|' && minPower <= 2: + rhs := parseSubex(l, 3) + if rhs == nil { + panic("Missing subex after |") + } + lhs = TransducerASTOr{lhs, rhs} default: l.rewind() break loop @@ -290,6 +302,16 @@ func (ast RegexASTConcat) String() string { return fmt.Sprintf("Concat{%v, %v}", ast.first, ast.second) } +type RegexASTOr struct { + first, second RegexAST +} +func (ast RegexASTOr) compileWith(next RegexState) RegexState { + return RegexGroupState{ + ast.first.compileWith(next), + ast.second.compileWith(next), + } +} + type RegexASTMaximise struct { content RegexAST } @@ -470,6 +492,16 @@ func (ast TransducerASTStore) String() string { return fmt.Sprintf("$%c(%v)", ast.slot, ast.match) } +type TransducerASTOr struct { + first, second TransducerAST +} +func (ast TransducerASTOr) compileWithNext(next TransducerState) TransducerState { + return TransducerGroupState { + ast.first.compileWithNext(next), + ast.second.compileWithNext(next), + } +} + type TransducerASTMaximise struct { content TransducerAST } -- cgit v1.2.3