diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 15:19:42 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 15:19:42 +0100 |
commit | dae74317d84471f6a859117fcbba1cf546be8ea1 (patch) | |
tree | db8516ba8a5a208d16aaa0e6da439541dd496b94 /main/parse.go | |
parent | 10f847acc7087317b0fbe20b7cf3307a0fafab8a (diff) | |
download | stred-go-dae74317d84471f6a859117fcbba1cf546be8ea1.tar |
Adds parsing substitute commands, though executing them currently does nothing
Diffstat (limited to 'main/parse.go')
-rw-r--r-- | main/parse.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/main/parse.go b/main/parse.go index 73c7913..bf2f0ac 100644 --- a/main/parse.go +++ b/main/parse.go @@ -5,6 +5,7 @@ import ( "strconv" "fmt" "main/walk" + "main/subex" ) type parser struct { @@ -202,6 +203,25 @@ func (p *parser) parseLiterals() (items []walk.WalkItem) { return items } +func (p *parser) parseSubex() subex.SubexState { + delim := p.next() + if delim.typ != TokenSubstituteDelimiter { + panic("Missing substitute delimiter") + } + subexProgramToken := p.next() + if subexProgramToken.typ != TokenSubex { + panic("Missing subex from substitution") + } + reader := subex.NewStringRuneReader(subexProgramToken.val) + subexAST := subex.Parse(reader) + subex := subex.CompileTransducer(subexAST) + delim = p.next() + if delim.typ != TokenSubstituteDelimiter { + panic("Missing end substitute delimiter") + } + return subex +} + func (p *parser) parseBasicCommand(commandChar rune) Command { switch commandChar { case 'p': @@ -212,6 +232,9 @@ func (p *parser) parseBasicCommand(commandChar rune) Command { return NextCommand{} case 'N': return AppendNextCommand{} + case 's': + subex := p.parseSubex() + return SubstituteCommand {subex: subex} case 'i': items := p.parseLiterals() return PrintLiteralsCommand {items: items} |