1 /* 2 Copyright 2020 Google LLC 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 https://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // An alternative implementation of Buildifier, on top of the Skylark parser. 18 // https://go.starlark.net/ 19 20 // This is experimental. 21 22 // If the experiment is successful, we might drop the AST defined in this 23 // package and use the AST from go.starlark.net/syntax. This will give 24 // us a much more precise AST and will allow us to share code with the 25 // Skylark interpreter. The end goal is to build a number of tools able 26 // to parse, analyze, format, refactor, evaluate Skylark code. 27 28 // Package main implements a buildifier on top of 'Skylark in Go'. 29 package main 30 31 import ( 32 "flag" 33 "fmt" 34 "log" 35 36 "github.com/bazelbuild/buildtools/build" 37 "github.com/bazelbuild/buildtools/convertast" 38 "go.starlark.net/syntax" 39 ) 40 41 func main() { 42 flag.Parse() 43 44 switch len(flag.Args()) { 45 case 0: 46 log.Fatal("Argument missing") 47 case 1: 48 filename := flag.Args()[0] 49 ast, err := syntax.Parse(filename, nil, syntax.RetainComments) 50 if err != nil { 51 log.Fatalf("%+v\n", err) 52 } 53 newAst := convertast.ConvFile(ast) 54 fmt.Print(build.FormatString(newAst)) 55 56 default: 57 log.Fatal("want at most one Skylark file name") 58 } 59 } 60