<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/parse.go
blob: e876010bafe59ec6e20cbe307fb55b7fa6346c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main

import (
	"strings"
	"strconv"
	"fmt"
)

type parser struct {
	tokenStream chan Token
	rewinds []Token
}
func (p *parser) next() Token {
	if len(p.rewinds) == 0 {
		return <- p.tokenStream
	}
	token := p.rewinds[len(p.rewinds)-1]
	p.rewinds = p.rewinds[:len(p.rewinds)-1]
	return token
}
func (p *parser) rewind(token Token) {
	p.rewinds = append(p.rewinds, token)
}
func (p *parser) peek() Token {
	token := p.next()
	p.rewind(token)
	return token
}

// TODO: make a pratt parser
func (p *parser) parsePathPatternFilter() PathFilterAST {
	var segments []PathFilterAST
	loop: for {
		token := p.next()
		switch token.typ {
			case TokenHash:
				stringIndex := p.next()
				if stringIndex.typ != TokenPatternStringIndex {
					panic("Expected string index after # in pattern")
				}
				segments = append(segments, 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")
				}
				segments = append(segments, IntegerSegmentPathFilterAST{index})
			case TokenDot:
				segments = append(segments, AnySegmentPathFilterAST{})
			case TokenAst:
				if len(segments) == 0 {
					panic("Invalid * in pattern, * must go after something")
				}
				segments[len(segments) - 1] = RepeatPathFilterAST {segments[len(segments)-1]}
			default:
				p.rewind(token)
				break loop
		}
	}
	return SequencePathFilterAST {segments}
}

// TODO: should only return a single filter
func (p *parser) parseFilter() []Filter {
	var filters []Filter
	token := p.next()
	switch token.typ {
		case TokenHash, TokenAt, TokenDot:
			p.rewind(token)
			filterAst := p.parsePathPatternFilter()
			filters = append(filters, compilePathFilterAST(filterAst))
			token = p.next()
	}
	if len(filters) == 0 {
		panic("Missing filter")
	}
	p.rewind(token)
	return filters
}

func (p *parser) parseBasicCommand(commandChar rune) Command {
	switch commandChar {
		case 'p':
			return PrintValueCommand{}
		case 'd':
			return DeleteAllCommand{}
		default:
			panic("Invalid command")
	}
}

func (p *parser) parseCommand() Command {
	token := p.next()
	switch token.typ {
		case TokenHash, TokenAt, TokenDot:
			p.rewind(token)
			filters := p.parseFilter()
			command := p.parseCommand()
			for _, filter := range filters {
				command = FilteredCommand {
					filter: filter,
					command: command,
				}
			}
			return command
		case TokenCommand:
			commandChar, _, err := strings.NewReader(token.val).ReadRune()
			if err != nil {
				panic("Error reading a command character!?")
			}
			return p.parseBasicCommand(commandChar)
		default:
			fmt.Println(token)
			panic("Invalid token, expected command")
	}
}

func (p *parser) parseCommands() []Command {
	var commands []Command
	for {
		nextToken := p.peek()
		if nextToken.typ == TokenEOF || nextToken.typ == TokenRBrace {
			return commands
		}
		commands = append(commands, p.parseCommand())
		semicolon := p.next()
		if semicolon.typ == TokenEOF || semicolon.typ == TokenRBrace {
			return commands
		}
		if semicolon.typ != TokenSemicolon {
			panic("Expected ; after command")
		}
	}
}

func Parse(tokens chan Token) []Command {
	p := parser {
		tokenStream: tokens,
	}
	return p.parseCommands()
}