diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 11:19:18 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 11:19:18 +0100 |
commit | 184118c1522ee4e78a0588fcac8eb235f512b599 (patch) | |
tree | cf32b7fff7e820e0b81e6975fee661f554c5c717 /subex/parse.go | |
parent | 80e7fd0626bfb98f8c1b7f69726d88f8cfa3e4fc (diff) | |
download | stred-go-184118c1522ee4e78a0588fcac8eb235f512b599.tar |
Add :xyz: replacement syntax that removes whatever is before it and inserts whatever is inside it
Diffstat (limited to 'subex/parse.go')
-rw-r--r-- | subex/parse.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/subex/parse.go b/subex/parse.go index 52488a7..4e7a3f6 100644 --- a/subex/parse.go +++ b/subex/parse.go @@ -166,7 +166,7 @@ func parseReplacement(l RuneReader) (output []OutputContent) { switch r { case eof: panic("Missing closing \"") - case '=', '^': + case '=', '^', ':': break loop case '$': slot := l.Next() @@ -281,7 +281,7 @@ func parseSubex(l RuneReader, minPower int) SubexAST { case '[': rangeParts := parseRangeSubex(l) lhs = SubexASTRange {rangeParts} - case ')', '|', ';', '{', '+', '-', '*', '/', '!', '$': + case ')', '|', ';', '{', '+', '-', '*', '/', '!', '$', ':': l.Rewind() return nil case '=': @@ -358,9 +358,19 @@ func parseSubex(l RuneReader, minPower int) SubexAST { if slot == eof { panic("Missing slot character") } - lhs = SubexASTStore{ - Match: lhs, - Slot: slot, + if slot == '_' { + lhs = SubexASTDiscard {lhs} + } else { + lhs = SubexASTStore{ + Match: lhs, + Slot: slot, + } + } + case r == ':' && minPower <= 4: + replacement := parseReplacement(l) + lhs = SubexASTConcat { + SubexASTDiscard {lhs}, + SubexASTOutput {replacement}, } case r == '|' && minPower <= 8: rhs := parseSubex(l, 9) |