<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'main/parse.go')
-rw-r--r--main/parse.go23
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}