stred

Stred: Streaming Tree Editor. Like sed but for JSON
git clone http://shtanton.xyz/git/repo/stred
Log | Files | Refs

commit 8a9d6ef5970eea2a42ff9eb537f2a3f5e56aec2c
parent 1830133215449ebd32751aca7deb9b66663563bd
Author: Charlie Stanton <charlie@shtanton.xyz>
Date:   Wed, 19 Apr 2023 16:54:53 +0100

Upgrades the substitute command to also act as a filter

Substitute now captures the command after it and only runs it if the substitution is a success

Diffstat:
Mmain/command.go | 13+++++++++----
Mmain/parse.go | 13++++++++++++-
2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/main/command.go b/main/command.go @@ -107,6 +107,7 @@ func (cmd DeleteAllCommand) exec(state *ProgramState) { type SubstituteCommand struct { subex subex.SubexState + next Command } func (cmd SubstituteCommand) exec(state *ProgramState) { valueStream := make(chan walk.WalkValue) @@ -119,15 +120,19 @@ func (cmd SubstituteCommand) exec(state *ProgramState) { atomStream := walk.Atomise(valueStream) atomsOut, error := subex.RunTransducer(cmd.subex, atomStream) if error { - panic("Error running subex") + return } valuesOut, err := walk.MemoryCompound(atomsOut) if err != nil { - panic("Error compounding atoms") + return } state.value = valuesOut + cmd.next.exec(state) } type Command interface { exec(*ProgramState) -} -\ No newline at end of file +} + +type NoopCommand struct {} +func (cmd NoopCommand) exec(state *ProgramState) {} +\ No newline at end of file diff --git a/main/parse.go b/main/parse.go @@ -234,7 +234,18 @@ func (p *parser) parseBasicCommand(commandChar rune) Command { return AppendNextCommand{} case 's': subex := p.parseSubex() - return SubstituteCommand {subex: subex} + var next Command + token := p.peek() + switch token.typ { + case TokenEOF, TokenRBrace: + next = NoopCommand{} + default: + next = p.parseCommand() + } + return SubstituteCommand { + subex: subex, + next: next, + } case 'i': items := p.parseLiterals() return PrintLiteralsCommand {items: items}