github.com/bir3/gocompiler@v0.9.2202/src/cmd/internal/obj/stringer.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build ignore 6 7 // This is a mini version of the stringer tool customized for the Anames table 8 // in the architecture support for obj. 9 // This version just generates the slice of strings, not the String method. 10 11 package main 12 13 import ( 14 "bufio" 15 "flag" 16 "fmt" 17 "log" 18 "os" 19 "regexp" 20 "strings" 21 ) 22 23 var ( 24 input = flag.String("i", "", "input file name") 25 output = flag.String("o", "", "output file name") 26 pkg = flag.String("p", "", "package name") 27 ) 28 29 var Are = regexp.MustCompile(`^\tA([A-Za-z0-9]+)`) 30 31 func main() { 32 flag.Parse() 33 if *input == "" || *output == "" || *pkg == "" { 34 flag.Usage() 35 os.Exit(2) 36 } 37 in, err := os.Open(*input) 38 if err != nil { 39 log.Fatal(err) 40 } 41 fd, err := os.Create(*output) 42 if err != nil { 43 log.Fatal(err) 44 } 45 out := bufio.NewWriter(fd) 46 defer out.Flush() 47 var on = false 48 s := bufio.NewScanner(in) 49 first := true 50 for s.Scan() { 51 line := s.Text() 52 if !on { 53 // First relevant line contains "= obj.ABase". 54 // If we find it, delete the = so we don't stop immediately. 55 const prefix = "= obj.ABase" 56 index := strings.Index(line, prefix) 57 if index < 0 { 58 continue 59 } 60 // It's on. Start with the header. 61 fmt.Fprintf(out, header, *input, *output, *pkg, *pkg) 62 on = true 63 line = line[:index] 64 } 65 // Strip comments so their text won't defeat our heuristic. 66 index := strings.Index(line, "//") 67 if index > 0 { 68 line = line[:index] 69 } 70 index = strings.Index(line, "/*") 71 if index > 0 { 72 line = line[:index] 73 } 74 // Termination condition: Any line with an = changes the sequence, 75 // so stop there, and stop at a closing brace. 76 if strings.HasPrefix(line, "}") || strings.ContainsRune(line, '=') { 77 break 78 } 79 sub := Are.FindStringSubmatch(line) 80 if len(sub) < 2 { 81 continue 82 } 83 if first { 84 fmt.Fprintf(out, "\tobj.A_ARCHSPECIFIC: %q,\n", sub[1]) 85 first = false 86 } else { 87 fmt.Fprintf(out, "\t%q,\n", sub[1]) 88 } 89 } 90 fmt.Fprintln(out, "}") 91 if s.Err() != nil { 92 log.Fatal(err) 93 } 94 } 95 96 const header = `// Code generated by stringer -i %s -o %s -p %s; DO NOT EDIT. 97 98 package %s 99 100 import "cmd/internal/obj" 101 102 var Anames = []string{ 103 `