Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 1 | // Copyright 2018 The CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | "strings" |
| 21 | |
| 22 | "cuelang.org/go/cue/ast" |
| 23 | "cuelang.org/go/cue/token" |
| 24 | "cuelang.org/go/internal" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | internal.DebugStr = debugStr |
| 29 | } |
| 30 | |
| 31 | func debugStr(x interface{}) (out string) { |
| 32 | if n, ok := x.(ast.Node); ok { |
| 33 | comments := "" |
| 34 | for _, g := range n.Comments() { |
| 35 | comments += debugStr(g) |
| 36 | } |
| 37 | if comments != "" { |
| 38 | defer func() { out = "<" + comments + out + ">" }() |
| 39 | } |
| 40 | } |
| 41 | switch v := x.(type) { |
| 42 | case *ast.File: |
| 43 | out := "" |
| 44 | if v.Name != nil { |
| 45 | out += "package " |
| 46 | out += debugStr(v.Name) |
| 47 | out += ", " |
| 48 | } |
| 49 | out += debugStr(v.Decls) |
| 50 | return out |
| 51 | |
| 52 | case *ast.Alias: |
| 53 | out := debugStr(v.Ident) |
| 54 | out += " = " |
| 55 | out += debugStr(v.Expr) |
| 56 | return out |
| 57 | |
| 58 | case *ast.BottomLit: |
| 59 | return "_|_" |
| 60 | |
| 61 | case *ast.BasicLit: |
| 62 | return v.Value |
| 63 | |
| 64 | case *ast.Interpolation: |
| 65 | for _, e := range v.Elts { |
| 66 | out += debugStr(e) |
| 67 | } |
| 68 | return out |
| 69 | |
| 70 | case *ast.EmitDecl: |
| 71 | // out := "<" |
| 72 | out += debugStr(v.Expr) |
| 73 | // out += ">" |
| 74 | return out |
| 75 | |
| 76 | case *ast.ImportDecl: |
| 77 | out := "import " |
| 78 | if v.Lparen != token.NoPos { |
| 79 | out += "( " |
| 80 | out += debugStr(v.Specs) |
| 81 | out += " )" |
| 82 | } else { |
| 83 | out += debugStr(v.Specs) |
| 84 | } |
| 85 | return out |
| 86 | |
| 87 | case *ast.ComprehensionDecl: |
| 88 | out := debugStr(v.Field) |
| 89 | out += " " |
| 90 | out += debugStr(v.Clauses) |
| 91 | return out |
| 92 | |
| 93 | case *ast.StructLit: |
| 94 | out := "{" |
| 95 | out += debugStr(v.Elts) |
| 96 | out += "}" |
| 97 | return out |
| 98 | |
| 99 | case *ast.ListLit: |
| 100 | out := "[" |
| 101 | out += debugStr(v.Elts) |
| 102 | if v.Ellipsis != token.NoPos || v.Type != nil { |
| 103 | if out != "[" { |
| 104 | out += ", " |
| 105 | } |
| 106 | out += "..." |
| 107 | if v.Type != nil { |
| 108 | out += debugStr(v.Type) |
| 109 | } |
| 110 | } |
| 111 | out += "]" |
| 112 | return out |
| 113 | |
| 114 | case *ast.ListComprehension: |
| 115 | out := "[" |
| 116 | out += debugStr(v.Expr) |
| 117 | out += " " |
| 118 | out += debugStr(v.Clauses) |
| 119 | out += "]" |
| 120 | return out |
| 121 | |
| 122 | case *ast.ForClause: |
| 123 | out := "for " |
| 124 | if v.Key != nil { |
| 125 | out += debugStr(v.Key) |
| 126 | out += ": " |
| 127 | } |
| 128 | out += debugStr(v.Value) |
| 129 | out += " in " |
| 130 | out += debugStr(v.Source) |
| 131 | return out |
| 132 | |
| 133 | case *ast.IfClause: |
| 134 | out := "if " |
| 135 | out += debugStr(v.Condition) |
| 136 | return out |
| 137 | |
| 138 | case *ast.Field: |
| 139 | out := debugStr(v.Label) |
Marcel van Lohuizen | 08a0ef2 | 2019-03-28 09:12:19 +0100 | [diff] [blame] | 140 | if v.Optional != token.NoPos { |
| 141 | out += "?" |
| 142 | } |
Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 143 | if v.Value != nil { |
| 144 | out += ": " |
| 145 | out += debugStr(v.Value) |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 146 | for _, a := range v.Attrs { |
| 147 | out += " " |
| 148 | out += debugStr(a) |
| 149 | } |
Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 150 | } |
| 151 | return out |
| 152 | |
Marcel van Lohuizen | b9b62d3 | 2019-03-14 23:50:15 +0100 | [diff] [blame] | 153 | case *ast.Attribute: |
| 154 | return v.Text |
| 155 | |
Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 156 | case *ast.Ident: |
| 157 | return v.Name |
| 158 | |
Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 159 | case *ast.TemplateLabel: |
| 160 | out := "<" |
| 161 | out += debugStr(v.Ident) |
| 162 | out += ">" |
| 163 | return out |
| 164 | |
| 165 | case *ast.SelectorExpr: |
| 166 | return debugStr(v.X) + "." + debugStr(v.Sel) |
| 167 | |
| 168 | case *ast.CallExpr: |
| 169 | out := debugStr(v.Fun) |
| 170 | out += "(" |
| 171 | out += debugStr(v.Args) |
| 172 | out += ")" |
| 173 | return out |
| 174 | |
Marcel van Lohuizen | d96ad3d | 2018-12-10 15:30:20 +0100 | [diff] [blame] | 175 | case *ast.ParenExpr: |
| 176 | out := "(" |
| 177 | out += debugStr(v.X) |
| 178 | out += ")" |
| 179 | return out |
| 180 | |
| 181 | case *ast.UnaryExpr: |
| 182 | return v.Op.String() + debugStr(v.X) |
| 183 | |
| 184 | case *ast.BinaryExpr: |
| 185 | out := debugStr(v.X) |
| 186 | op := v.Op.String() |
| 187 | if 'a' <= op[0] && op[0] <= 'z' { |
| 188 | op = fmt.Sprintf(" %s ", op) |
| 189 | } |
| 190 | out += op |
| 191 | out += debugStr(v.Y) |
| 192 | return out |
| 193 | |
| 194 | case []*ast.CommentGroup: |
| 195 | var a []string |
| 196 | for _, c := range v { |
| 197 | a = append(a, debugStr(c)) |
| 198 | } |
| 199 | return strings.Join(a, "\n") |
| 200 | |
| 201 | case *ast.CommentGroup: |
| 202 | str := "[" |
| 203 | if v.Doc { |
| 204 | str += "d" |
| 205 | } |
| 206 | if v.Line { |
| 207 | str += "l" |
| 208 | } |
| 209 | str += strconv.Itoa(int(v.Position)) |
| 210 | var a = []string{} |
| 211 | for _, c := range v.List { |
| 212 | a = append(a, c.Text) |
| 213 | } |
| 214 | return str + strings.Join(a, " ") + "] " |
| 215 | |
| 216 | case *ast.IndexExpr: |
| 217 | out := debugStr(v.X) |
| 218 | out += "[" |
| 219 | out += debugStr(v.Index) |
| 220 | out += "]" |
| 221 | return out |
| 222 | |
| 223 | case *ast.SliceExpr: |
| 224 | out := debugStr(v.X) |
| 225 | out += "[" |
| 226 | out += debugStr(v.Low) |
| 227 | out += ":" |
| 228 | out += debugStr(v.High) |
| 229 | out += "]" |
| 230 | return out |
| 231 | |
| 232 | case *ast.ImportSpec: |
| 233 | out := "" |
| 234 | if v.Name != nil { |
| 235 | out += debugStr(v.Name) |
| 236 | out += " " |
| 237 | } |
| 238 | out += debugStr(v.Path) |
| 239 | return out |
| 240 | |
| 241 | case []ast.Decl: |
| 242 | if len(v) == 0 { |
| 243 | return "" |
| 244 | } |
| 245 | out := "" |
| 246 | for _, d := range v { |
| 247 | out += debugStr(d) |
| 248 | out += sep |
| 249 | } |
| 250 | return out[:len(out)-len(sep)] |
| 251 | |
| 252 | case []ast.Clause: |
| 253 | if len(v) == 0 { |
| 254 | return "" |
| 255 | } |
| 256 | out := "" |
| 257 | for _, c := range v { |
| 258 | out += debugStr(c) |
| 259 | out += " " |
| 260 | } |
| 261 | return out |
| 262 | |
| 263 | case []ast.Expr: |
| 264 | if len(v) == 0 { |
| 265 | return "" |
| 266 | } |
| 267 | out := "" |
| 268 | for _, d := range v { |
| 269 | out += debugStr(d) |
| 270 | out += sep |
| 271 | } |
| 272 | return out[:len(out)-len(sep)] |
| 273 | |
| 274 | case []*ast.ImportSpec: |
| 275 | if len(v) == 0 { |
| 276 | return "" |
| 277 | } |
| 278 | out := "" |
| 279 | for _, d := range v { |
| 280 | out += debugStr(d) |
| 281 | out += sep |
| 282 | } |
| 283 | return out[:len(out)-len(sep)] |
| 284 | |
| 285 | default: |
| 286 | if v == nil { |
| 287 | return "" |
| 288 | } |
| 289 | return fmt.Sprintf("<%T>", x) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | const sep = ", " |