-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
185 lines (145 loc) · 3.17 KB
/
parser.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package pgn
import (
"fmt"
"log"
"strconv"
)
type parser struct {
l *lexer
errors []string
currToken token
peekToken token
}
func newParser(l *lexer) *parser {
p := &parser{
l: l,
errors: []string{},
}
p.nextToken()
p.nextToken()
return p
}
func (p *parser) ParsePGN() (*Game, error) {
game := &Game{
tags: map[string]string{},
moves: map[int]*Move{},
}
for p.currToken.Type != EOF {
stmt := p.parseStatement()
if stmt != nil {
switch v := stmt.(type) {
case *TagPair:
game.SetTag(v.Name(), v.Value())
case *Move:
game.SetMove(v.Number(), v)
case *gameTermination:
if v.Value() != game.GetTag("Result") {
p.errors = append(p.errors, "Game termination marker does not match game result in tag pair")
}
game.SetResult(v.Value())
}
}
}
if len(p.Errors()) > 0 {
return nil, fmt.Errorf("parsing errors: %v", p.Errors())
}
return game, nil
}
func (p *parser) parseStatement() stmt {
switch p.currToken.Type {
case LBRACKET:
return p.parseTagPair()
case INTEGER:
return p.parseMove()
case SYMBOL:
if isGameResult(p.currToken.TokenLiteral()) {
gt := &gameTermination{TerminationValue: p.currToken.TokenLiteral()}
p.nextToken()
return gt
}
return nil
default:
return nil
}
}
func (p *parser) parseTagPair() *TagPair {
tp := &TagPair{
LBracket: p.currToken,
}
if !p.expectPeek(SYMBOL) {
return nil
}
tp.TagName = p.currToken.TokenLiteral()
if !p.expectPeek(STRING) {
return nil
}
tp.TagValue = p.currToken.TokenLiteral()
if p.expectPeek(RBRACKET) {
tp.RBracket = p.currToken
}
p.nextToken()
return tp
}
func (p *parser) parseMove() *Move {
moveNumInt, err := strconv.Atoi(p.currToken.TokenLiteral())
if err != nil {
log.Fatalf("Couldn't convert string to integer for moves: %s", p.currToken.TokenLiteral())
}
move := &Move{
MoveNumber: moveNumInt,
WhiteAnnotations: []string{},
BlackAnnotations: []string{},
}
//Zero or more periods
for p.peekTokenIs(PERIOD) {
p.nextToken()
}
if !p.expectPeek(SYMBOL) {
return nil
}
if isGameResult(p.currToken.TokenLiteral()) {
return move
}
move.MoveWhite = p.currToken.TokenLiteral()
for p.peekTokenIs(NAG) {
p.nextToken()
move.WhiteAnnotations = append(move.WhiteAnnotations, p.currToken.TokenLiteral())
}
p.nextToken()
if isGameResult(p.currToken.TokenLiteral()) {
return move
}
move.MoveBlack = p.currToken.TokenLiteral()
for p.peekTokenIs(NAG) {
p.nextToken()
move.BlackAnnotations = append(move.BlackAnnotations, p.currToken.TokenLiteral())
}
p.nextToken()
return move
}
func (p *parser) Errors() []string {
return p.errors
}
func (p *parser) nextToken() {
p.currToken = p.peekToken
p.peekToken = p.l.NextToken()
}
func (p *parser) currTokenIs(t tokenType) bool {
return p.currToken.Type == t
}
func (p *parser) peekTokenIs(t tokenType) bool {
return p.peekToken.Type == t
}
func (p *parser) peekError(t tokenType) {
msg := fmt.Sprintf("expected next token to be %s, got %s instead\n", t, p.peekToken.Type)
p.errors = append(p.errors, msg)
}
func (p *parser) expectPeek(t tokenType) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
} else {
p.peekError(t)
return false
}
}