From 184118c1522ee4e78a0588fcac8eb235f512b599 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Fri, 21 Apr 2023 11:19:18 +0100 Subject: Add :xyz: replacement syntax that removes whatever is before it and inserts whatever is inside it --- subex/parse.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'subex/parse.go') 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) -- cgit v1.2.3