github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/dbc/analysis/analysis.go (about)

     1  package analysis
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"text/scanner"
     7  
     8  	"github.com/blueinnovationsgroup/can-go/pkg/dbc"
     9  )
    10  
    11  // An Analyzer describes an analysis function and its options.
    12  type Analyzer struct {
    13  	// Name of the analyzer.
    14  	Name string
    15  
    16  	// Doc is the documentation for the analyzer.
    17  	Doc string
    18  
    19  	// Run the analyzer.
    20  	Run func(*Pass) error
    21  }
    22  
    23  // Title is the part before the first "\n\n" of the documentation.
    24  func (a *Analyzer) Title() string {
    25  	return strings.SplitN(a.Doc, "\n\n", 2)[0]
    26  }
    27  
    28  // Validate the analyzer metadata.
    29  func (a *Analyzer) Validate() error {
    30  	if a.Doc == "" {
    31  		return fmt.Errorf("missing doc")
    32  	}
    33  	return nil
    34  }
    35  
    36  // A Diagnostic is a message associated with a source location.
    37  type Diagnostic struct {
    38  	Pos     scanner.Position
    39  	Message string
    40  }
    41  
    42  // Pass is the interface to the run function that analyzes DBC definitions.
    43  type Pass struct {
    44  	Analyzer    *Analyzer
    45  	File        *dbc.File
    46  	Diagnostics []*Diagnostic
    47  }
    48  
    49  // Reportf reports a diagnostic by building a message from the provided format and args.
    50  func (pass *Pass) Reportf(pos scanner.Position, format string, args ...interface{}) {
    51  	msg := fmt.Sprintf(format, args...)
    52  	pass.Diagnostics = append(pass.Diagnostics, &Diagnostic{Pos: pos, Message: msg})
    53  }