-
Notifications
You must be signed in to change notification settings - Fork 171
/
base_helper.go
66 lines (52 loc) · 1.35 KB
/
base_helper.go
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
package parser
import (
"fmt"
"github.com/goby-lang/goby/token"
)
func (p *Parser) peekPrecedence() int {
if p, ok := precedence[p.peekToken.Type]; ok {
return p
}
return LOWEST
}
func (p *Parser) curPrecedence() int {
if p, ok := precedence[p.curToken.Type]; ok {
return p
}
return LOWEST
}
func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}
func (p *Parser) curTokenIs(t token.Type) bool {
return p.curToken.Type == t
}
func (p *Parser) peekTokenIs(t token.Type) bool {
return p.peekToken.Type == t
}
func (p *Parser) peekError(t token.Type) {
msg := fmt.Sprintf("expected next token to be %s, got %s instead. Line: %d", t, p.peekToken.Type, p.peekToken.Line)
p.errors = append(p.errors, msg)
}
func (p *Parser) expectPeek(t token.Type) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
}
p.peekError(t)
return false
}
func (p *Parser) registerPrefix(tokenType token.Type, fn prefixParseFn) {
p.prefixParseFns[tokenType] = fn
}
func (p *Parser) registerInfix(tokenType token.Type, fn infixParseFn) {
p.infixParseFns[tokenType] = fn
}
func (p *Parser) noPrefixParseFnError(t token.Type) {
msg := fmt.Sprintf("no prefix function for %s. Line: %d", t, p.curToken.Line)
p.errors = append(p.errors, msg)
}
func (p *Parser) peekTokenAtSameLine() bool {
return p.curToken.Line == p.peekToken.Line
}