Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/internal/obj/stringer.go

Documentation: cmd/internal/obj

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

View as plain text