...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package fix
16
17 import (
18 "cuelang.org/go/cue/ast"
19 "cuelang.org/go/cue/ast/astutil"
20 "cuelang.org/go/cue/token"
21 )
22
23 func simplify(f *ast.File) *ast.File {
24
25 f = astutil.Apply(f, nil, func(c astutil.Cursor) bool {
26 if x, ok := c.Node().(ast.Expr); ok {
27 if y := elideTop(x); x != y {
28 c.Replace(y)
29 }
30 }
31 return true
32 }).(*ast.File)
33
34 return f
35 }
36
37 func elideTop(x ast.Expr) ast.Expr {
38 switch x := x.(type) {
39 case *ast.BinaryExpr:
40 switch x.Op {
41 case token.OR:
42 if isTop(x.X) {
43 return x.X
44 }
45 if isTop(x.Y) {
46 ast.SetRelPos(x.Y, token.NoRelPos)
47 return x.Y
48 }
49
50 case token.AND:
51 if isTop(x.X) {
52 ast.SetRelPos(x.Y, token.NoRelPos)
53 return x.Y
54 }
55 if isTop(x.Y) {
56 return x.X
57 }
58 }
59
60 case *ast.ParenExpr:
61 switch x.X.(type) {
62 case *ast.BinaryExpr, *ast.UnaryExpr:
63 default:
64 return x.X
65 }
66 }
67 return x
68 }
69
70 func isTop(x ast.Expr) bool {
71 v, ok := x.(*ast.Ident)
72 return ok && v.Name == "_"
73 }
74
View as plain text