<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2022-09-21 20:44:56 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2022-09-21 20:44:56 +0100
commit3636825c64bb6c172b0858d7a08c30acfcd68bdd (patch)
tree7e42fca1224f17eecdb54d779ecacc53ae406712
parentb81f564164a512fc3dba155c7bd25613b201c070 (diff)
downloadstred-go-3636825c64bb6c172b0858d7a08c30acfcd68bdd.tar
Adds the or operator |
-rw-r--r--main/main.go36
1 files changed, 34 insertions, 2 deletions
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
}