/
parse_groups.go
54 lines (47 loc) · 1.19 KB
/
parse_groups.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
package main
import "fmt"
func parseGroups(ts []Token, ew ErrorWriter) []Token {
if DEBUG_PARSER {
fmt.Printf("parseGroups IN: %s\n", DumpTokens(ts))
}
res := []Token{}
for _, t := range ts {
switch {
case IsGroupSymbol(t):
// should've been grouped during the tokenization step
ew.Add(t.Context().Error("invalid syntax"))
case IsSymbol(t, ","):
// should've been grouped during the tokenization step
ew.Add(t.Context().Error("invalid syntax"))
case IsParens(t):
p := AssertParens(t)
innerExpr := ParseExpr(p.Content(), ew)
if innerExpr == nil {
ew.Add(p.Context().Error("empty parentheses"))
} else {
res = append(res, innerExpr)
}
case IsEscParens(t):
innerExpr := ParseAnon(AssertEscParens(t), ew)
if innerExpr != nil {
res = append(res, innerExpr)
}
case IsBrackets(t):
innerExpr := ParseList(AssertBrackets(t), ew)
if innerExpr != nil {
res = append(res, innerExpr)
}
case IsBraces(t):
innerExpr := ParseDict(AssertBraces(t), ew)
if innerExpr != nil {
res = append(res, innerExpr)
}
default:
res = append(res, t)
}
}
if DEBUG_PARSER {
fmt.Printf("parseGroups OUT: %s\n", DumpTokens(res))
}
return res
}