github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/parser/errorformat.go (about)

     1  package parser
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/reviewdog/errorformat"
     9  
    10  	"github.com/mistwind/reviewdog/proto/rdf"
    11  )
    12  
    13  var _ Parser = &ErrorformatParser{}
    14  
    15  // ErrorformatParser is errorformat parser.
    16  type ErrorformatParser struct {
    17  	efm *errorformat.Errorformat
    18  }
    19  
    20  // NewErrorformatParser returns a new ErrorformatParser.
    21  func NewErrorformatParser(efm *errorformat.Errorformat) *ErrorformatParser {
    22  	return &ErrorformatParser{efm: efm}
    23  }
    24  
    25  // NewErrorformatParserString returns a new ErrorformatParser from errorformat
    26  // in string representation.
    27  func NewErrorformatParserString(efms []string) (*ErrorformatParser, error) {
    28  	efm, err := errorformat.NewErrorformat(efms)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return NewErrorformatParser(efm), nil
    33  }
    34  
    35  func (p *ErrorformatParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
    36  	s := p.efm.NewScanner(r)
    37  	var ds []*rdf.Diagnostic
    38  	for s.Scan() {
    39  		e := s.Entry()
    40  		fmt.Printf("entry: %#v\n", e)
    41  		if e.Valid {
    42  			d := &rdf.Diagnostic{
    43  				Location: &rdf.Location{
    44  					Path: e.Filename,
    45  					Range: &rdf.Range{
    46  						Start: &rdf.Position{
    47  							Line:   int32(e.Lnum),
    48  							Column: int32(e.Col),
    49  						},
    50  					},
    51  				},
    52  				Message:        e.Text,
    53  				Severity:       severity(string(e.Type)),
    54  				OriginalOutput: strings.Join(e.Lines, "\n"),
    55  			}
    56  			if e.Nr != 0 {
    57  				d.Code = &rdf.Code{Value: fmt.Sprintf("%d", e.Nr)}
    58  			}
    59  			ds = append(ds, d)
    60  		}
    61  	}
    62  	fmt.Printf("ErrorformatParser Parse : %d\n", len(ds))
    63  	return ds, nil
    64  }