github.com/goplus/llgo@v0.8.3/chore/clangast/clangast.go (about) 1 /* 2 * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved. 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 * http://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 package main 18 19 import ( 20 "encoding/json" 21 "flag" 22 "fmt" 23 "os" 24 25 "github.com/goplus/llgo/xtool/clang/parser" 26 ) 27 28 var ( 29 dump = flag.Bool("dump", false, "dump AST") 30 ) 31 32 func usage() { 33 fmt.Fprintf(os.Stderr, "Usage: clangast [-dump] source.i\n") 34 flag.PrintDefaults() 35 } 36 37 func main() { 38 flag.Usage = usage 39 flag.Parse() 40 if flag.NArg() < 1 { 41 usage() 42 return 43 } 44 var file = flag.Arg(0) 45 var err error 46 if *dump { 47 doc, _, e := parser.DumpAST(file, nil) 48 if e == nil { 49 os.Stdout.Write(doc) 50 return 51 } 52 err = e 53 } else { 54 doc, _, e := parser.ParseFile(file, 0) 55 if e == nil { 56 enc := json.NewEncoder(os.Stdout) 57 enc.SetIndent("", " ") 58 check(enc.Encode(doc)) 59 return 60 } 61 err = e 62 } 63 fmt.Fprintln(os.Stderr, err) 64 os.Exit(1) 65 } 66 67 func check(err error) { 68 if err != nil { 69 panic(err) 70 } 71 }