-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
244 lines (199 loc) · 4.59 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bufio"
"fmt"
"os"
"slices"
"strconv"
"strings"
)
type Token struct {
token TokenType
literal string
// pos uint64
}
type Stack struct {
items []Token
}
func (s *Stack) Push(t Token) {
s.items = append(s.items, t);
}
func (s *Stack) Pop() Token {
lastItem := s.items[len(s.items)-1:];
s.items = s.items[0:len(s.items) - 1];
return lastItem[0];
}
func (s *Stack) PeekBack(steps int) Token {
lastItem := s.items[len(s.items)-steps:];
return lastItem[0];
}
func main() {
file, err := os.Open("./tests/step4/invalid.json")
if err != nil {
fmt.Println("error opening valid file in step 1")
}
tokens := scan(file);
fmt.Println("tokens: ", tokens);
valid := isValidJSON(tokens);
fmt.Println("is valid json?:", valid);
if valid {
os.Exit(0);
} else {
os.Exit(1);
}
}
func createToken(char string, token TokenType) Token {
return Token{ token: token, literal: char };
}
func scan(f *os.File) []Token {
var tokens []Token;
reader := bufio.NewReader(f)
// scanner.Split(bufio.ScanBytes)
for {
byte, err := reader.ReadByte();
if err != nil {
break;
}
char := string(byte);
switch char {
case "{":
tokens = append(tokens, createToken(char, RBRACE));
case "}":
tokens = append(tokens, createToken(char, LBRACE));
case "\"":
// continue scanning until you reach the corresponding "
literal := "";
for {
b, err := reader.ReadByte();
if err != nil {
fmt.Println("encountered error: ", err);
os.Exit(1);
}
c := string(b);
if c == "\"" {
break;
}
literal += c;
}
tokens = append(tokens, createToken(literal, STRING));
// booleans
case "t", "f":
literal := char;
for {
b, _ := reader.ReadByte();
c := string(b);
literal += c
if strings.Contains("true", literal) || strings.Contains("false", literal) {
if literal == "true" || literal == "false" {
tokens = append(tokens, createToken(literal, BOOL));
break;
}
} else {
fmt.Printf("Err: unknown token '%s'\n", literal);
os.Exit(1);
}
}
// null
case "n":
literal := char;
for {
b, _ := reader.ReadByte();
c := string(b);
literal += c
if strings.Contains("null", literal) {
if literal == "null" {
tokens = append(tokens, createToken(literal, NULL));
break;
}
} else {
fmt.Printf("Err: unknown token '%s'\n", literal);
os.Exit(1);
}
}
case ":":
tokens = append(tokens, createToken(char, COLON));
case ",":
tokens = append(tokens, createToken(char, COMMA));
case "[":
tokens = append(tokens, createToken(char, LBRACKET));
case "]":
tokens = append(tokens, createToken(char, RBRACKET));
case " ", "\n":
// keep going, dont care about spaces or newline characters (yet?)
default:
// numbers
_, err := strconv.Atoi(char);
createdNumericToken := false;
if err != nil {
fmt.Printf("Err: unknown token '%s'\n", char);
os.Exit(1);
}
literal := char;
for {
b, _ := reader.Peek(1);
c := string(b);
_, err := strconv.Atoi(c);
if err != nil {
tokens = append(tokens, createToken(literal, NUM));
createdNumericToken = true;
break;
} else {
bb, _ := reader.ReadByte();
cc := string(bb);
literal += cc;
}
}
if !createdNumericToken {
fmt.Printf("Err: unknown token '%s'\n", char);
os.Exit(1);
}
}
}
return tokens;
}
func isValidJSON(tokens []Token) bool {
var s Stack;
if len(tokens) == 0 {
return false;
}
valid := true;
validValues := []TokenType{STRING, NUM, BOOL, NULL, RBRACKET, RBRACE}
for i := 0; i < len(tokens); i ++ {
token := tokens[i];
if token.token == LBRACE {
// not handling nested objects yet
// left brace only appears as last token
peekedValue := s.PeekBack(1);
if !slices.Contains(validValues, peekedValue.token) {
valid = false;
break;
} else {
s.Push(token);
}
} else if token.token == STRING {
peekedValue := s.PeekBack(1);
if peekedValue.token == RBRACE || peekedValue.token == COMMA {
s.Push(token);
} else if peekedValue.token == COLON {
// its a string value
key := s.PeekBack(2);
if key.token != STRING {
valid = false;
} else {
s.Push(token);
}
}
} else if (token.token == BOOL || token.token == NULL || token.token == NUM) {
peekedValue := s.PeekBack(1);
if peekedValue.token != COLON {
valid = false;
break;
} else {
s.Push(token);
}
} else {
s.Push(token);
}
}
return valid;
}