<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/parse.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-04-21 17:16:01 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2024-04-21 17:16:01 +0100
commit1e66aaece6ea7cd3c705ca56ce5558e8f87681b8 (patch)
tree2fff87e4abfb0727e028854c006577eccfeee370 /main/parse.go
parent7162ae8c641314846f0b565d7614ac8d71dbd628 (diff)
downloadstred-go-1e66aaece6ea7cd3c705ca56ce5558e8f87681b8.tar
Add substitute next commands
Diffstat (limited to 'main/parse.go')
-rw-r--r--main/parse.go24
1 files changed, 15 insertions, 9 deletions
diff --git a/main/parse.go b/main/parse.go
index 3c24e6c..36917ac 100644
--- a/main/parse.go
+++ b/main/parse.go
@@ -44,13 +44,7 @@ func (p *parser) parseSubex() subex.SubexAST {
if subexProgramToken.typ != TokenSubex {
panic("Missing subex from substitution")
}
- var subexProgram string
- if delim.val == "=" || delim.val == "~" || delim.val == "\"" || delim.val == "`" || delim.val == "^" {
- subexProgram = delim.val + subexProgramToken.val + delim.val
- } else {
- subexProgram = subexProgramToken.val
- }
- reader := subex.NewStringRuneReader(subexProgram)
+ reader := subex.NewStringRuneReader(subexProgramToken.val)
subexAST := subex.Parse(reader)
delim = p.next()
if delim.typ != TokenSubstituteDelimiter {
@@ -66,9 +60,21 @@ func (p *parser) parseBasicCommand(commands []Command, commandChar rune) []Comma
case 'd':
return append(commands, DeleteValueCommand{})
case 'n':
- return append(commands, NextCommand{})
+ delim := p.peek()
+ if delim.typ != TokenSubstituteDelimiter {
+ return append(commands, NextCommand{})
+ }
+ ast := p.parseSubex()
+ subex := subex.CompileTransducer(ast)
+ return append(commands, SubstituteNextCommand {subex}, JumpCommand {len(commands) + 3})
case 'N':
- return append(commands, AppendNextCommand{})
+ delim := p.peek()
+ if delim.typ != TokenSubstituteDelimiter {
+ return append(commands, AppendNextCommand{})
+ }
+ ast := p.parseSubex()
+ subex := subex.CompileTransducer(ast)
+ return append(commands, SubstituteAppendNextCommand {subex}, JumpCommand {len(commands) + 3})
case 'm':
return append(commands, MergeCommand{})
case 's':