Source file src/simd/archsimd/_gen/specdoc/comment.go

     1  // Copyright 2026 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  package specdoc
     6  
     7  import (
     8  	"go/ast"
     9  	"simd/archsimd/_gen/specgen"
    10  	"strings"
    11  )
    12  
    13  // parseCommentGroup splits an AST comment group into classified paragraphs.
    14  func parseCommentGroup(cg *ast.CommentGroup) []specgen.Paragraph {
    15  	if cg == nil || len(cg.List) == 0 {
    16  		return nil
    17  	}
    18  
    19  	var paras []specgen.Paragraph
    20  	var para strings.Builder
    21  	flush := func() {
    22  		if para.Len() == 0 {
    23  			return
    24  		}
    25  		text := para.String()
    26  		para.Reset()
    27  
    28  		kind := specgen.SpecOwned
    29  		if specgen.HasNotePrefix(text) {
    30  			kind = specgen.ImplementationNote
    31  		}
    32  		paras = append(paras, specgen.Paragraph{Kind: kind, Text: text})
    33  	}
    34  	for _, c := range cg.List {
    35  		if isDirectiveLine(c.Text) {
    36  			flush()
    37  			paras = append(paras, specgen.Paragraph{Kind: specgen.DirectiveComment, Text: c.Text[2:]})
    38  			continue
    39  		}
    40  
    41  		// Drop leading "//" or "// ". If it's "//\t", we keep the "\t"
    42  		text, ok := strings.CutPrefix(c.Text, "// ")
    43  		if !ok {
    44  			text, ok = strings.CutPrefix(text, "//")
    45  			if !ok {
    46  				panic("comment does not start with //?")
    47  			}
    48  		}
    49  		if text == "" {
    50  			// Paragraph break
    51  			flush()
    52  			continue
    53  		}
    54  		if para.Len() > 0 {
    55  			para.WriteByte('\n')
    56  		}
    57  		para.WriteString(text)
    58  	}
    59  	flush()
    60  
    61  	return paras
    62  }
    63  
    64  // isDirectiveLine reports whether comment line is a compiler/tool directive
    65  // comment.
    66  func isDirectiveLine(line string) bool {
    67  	line = strings.TrimSpace(line)
    68  	if _, ok := ast.ParseDirective(0, line); ok {
    69  		return true
    70  	}
    71  	if strings.HasPrefix(line, "//line ") {
    72  		return true
    73  	}
    74  	return false
    75  }
    76  

View as plain text