<- 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.go181
1 files changed, 0 insertions, 181 deletions
diff --git a/main/parse.go b/main/parse.go
index e0272e8..e9dd012 100644
--- a/main/parse.go
+++ b/main/parse.go
@@ -2,9 +2,7 @@ package main
import (
"strings"
- "strconv"
"fmt"
- "main/walk"
"main/subex"
)
@@ -42,167 +40,6 @@ var segmentTokens map[TokenType]bool = map[TokenType]bool {
TokenLBrack: true,
}
-func (p *parser) parsePathPatternFilter(minPower int) PathFilterAST {
- var lhs PathFilterAST
- token := p.next()
- switch token.typ {
- case TokenHash:
- stringIndex := p.next()
- if stringIndex.typ != TokenPatternStringIndex {
- panic("Expected string index after # in pattern")
- }
- lhs = StringSegmentPathFilterAST{stringIndex.val}
- case TokenAt:
- intIndex := p.next()
- if intIndex.typ != TokenPatternIntegerIndex {
- panic("Expected integer index after @ in pattern")
- }
- index, err := strconv.Atoi(intIndex.val)
- if err != nil {
- panic("Expected integer index after @ in pattern")
- }
- lhs = IntegerSegmentPathFilterAST{index}
- case TokenDot:
- lhs = AnySegmentPathFilterAST{}
- case TokenLBrack:
- lhs = p.parsePathPatternFilter(0)
- if p.next().typ != TokenRBrack {
- panic("Expected ] in path filter")
- }
- default:
- panic("Expected path pattern filter segment")
- }
- loop: for {
- token = p.next()
- switch {
- case token.typ == TokenAst && 10 >= minPower:
- lhs = RepeatPathFilterAST {lhs}
- case token.typ == TokenQuestion && 10 >= minPower:
- lhs = OrPathFilterAST{lhs, NonePathFilterAST{}}
- case token.typ == TokenBar && 0 >= minPower:
- lhs = OrPathFilterAST{lhs, p.parsePathPatternFilter(1)}
- case segmentTokens[token.typ] && 2 >= minPower:
- p.rewind(token)
- lhs = SequencePathFilterAST {lhs, p.parsePathPatternFilter(3)}
- default:
- p.rewind(token)
- break loop
- }
- }
- return lhs
-}
-
-func (p *parser) parseFilter(minPower int) Filter {
- var lhs Filter
- token := p.next()
- switch token.typ {
- case TokenHash, TokenAt, TokenDot, TokenLBrack:
- p.rewind(token)
- filterAst := p.parsePathPatternFilter(0)
- lhs = compilePathFilterAST(filterAst)
- case TokenHat:
- lhs = BeginTerminalFilter{}
- case TokenDollar:
- lhs = EndTerminalFilter{}
- case TokenHatDollar:
- lhs = TerminalFilter{}
- case TokenTilde:
- lhs = RootFilter{}
- case TokenLParen:
- lhs = p.parseFilter(0)
- rParen := p.next()
- if rParen.typ != TokenRParen {
- panic("Missing ) in filter")
- }
- default:
- panic("Expected filter")
- }
- loop: for {
- token = p.next()
- switch {
- case token.typ == TokenAnd && 2 >= minPower:
- lhs = AndFilter {lhs, p.parseFilter(3)}
- case token.typ == TokenOr && 0 >= minPower:
- lhs = OrFilter {lhs, p.parseFilter(1)}
- default:
- p.rewind(token)
- break loop
- }
- }
- return lhs
-}
-
-func (p *parser) parseLiterals() (items []walk.WalkItem) {
- var path walk.Path
- var value walk.WalkValue
- loop: for {
- token := p.next()
- switch token.typ {
- case TokenSemicolon, TokenEOF:
- p.rewind(token)
- break loop
- case TokenComma:
- case TokenNullLiteral:
- value = walk.ValueNull{}
- case TokenTrueLiteral:
- value = walk.ValueBool(true)
- case TokenFalseLiteral:
- value = walk.ValueBool(false)
- case TokenNumberLiteral:
- numberLiteral, err := strconv.ParseFloat(token.val, 64)
- if err != nil {
- panic("Error parsing number literal to float64")
- }
- value = walk.ValueNumber(numberLiteral)
- case TokenDoubleQuote:
- stringToken := p.next()
- if stringToken.typ != TokenStringLiteral {
- panic("Expected string literal after \"")
- }
- // TODO: resolve escape characters
- stringLiteral := stringToken.val
- if p.next().typ != TokenDoubleQuote {
- panic("Expected \" after string literal")
- }
- colon := p.next()
- if colon.typ == TokenColon {
- if path != nil {
- panic("Expected value after path:")
- }
- path = walk.Path{stringLiteral}
- } else {
- p.rewind(colon)
- value = walk.ValueString(stringLiteral)
- }
- case TokenTerminalLiteral:
- switch token.val {
- case "{":
- value = walk.MapBegin
- case "}":
- value = walk.MapEnd
- case "[":
- value = walk.ArrayBegin
- case "]":
- value = walk.ArrayEnd
- default:
- panic("Invalid terminal token")
- }
- }
- if value != nil {
- items = append(items, walk.WalkItem {
- Path: path,
- Value: value,
- })
- path = nil
- value = nil
- }
- }
- if path != nil {
- panic("Expected value after path:")
- }
- return items
-}
-
func (p *parser) parseSubex() subex.SubexState {
delim := p.next()
if delim.typ != TokenSubstituteDelimiter {
@@ -246,9 +83,6 @@ func (p *parser) parseBasicCommand(commandChar rune) Command {
subex: subex,
next: next,
}
- case 'i':
- items := p.parseLiterals()
- return PrintLiteralsCommand {items: items}
case 'o':
return NoopCommand{}
case 'x':
@@ -267,21 +101,6 @@ func (p *parser) parseBasicCommand(commandChar rune) Command {
func (p *parser) parseCommand() Command {
token := p.next()
switch token.typ {
- case TokenHash, TokenAt, TokenDot, TokenLParen, TokenHat, TokenDollar, TokenHatDollar, TokenTilde:
- p.rewind(token)
- filter := p.parseFilter(0)
- notToken := p.next()
- if notToken.typ == TokenExclamation {
- filter = NotFilter {filter}
- } else {
- p.rewind(notToken)
- }
- command := p.parseCommand()
- command = FilteredCommand {
- filter: filter,
- command: command,
- }
- return command
case TokenLBrace:
commands := p.parseCommands()
if p.next().typ != TokenRBrace {