dmitri.shuralyov.com/go/generated

update implementation for 2020 spec changes dmitri.shuralyov.com/go/generated#1

Mergeddmitshur opened this change 3 years ago
Patch Set 3
dmitshur committed 2 years ago commit 07d13072b9a381af233fb200457b989dceb4dcc7
Collapse all
Commit Message
FileFile
@@ -1,10 +1,10 @@
11
Parent:     b1254a4 (remove comment about API being undecided)
22
Author:     Dmitri Shuralyov <dmitri@shuralyov.com>
33
AuthorDate: Mon Jun 21 00:40:49 2021 -0400
44
Commit:     Dmitri Shuralyov <dmitri@shuralyov.com>
5
CommitDate: Sun Dec 12 15:01:05 2021 -0500
5
CommitDate: Tue Dec 14 01:49:51 2021 -0500
66

77
update implementation for 2020 spec changes
88

99
With the new specification, it's possible to stop reading a file early
1010
even when the generated comment is absent, since the generated comment
fuzz_test.go
FileFile
@@ -1,7 +1,7 @@
1
//go:build gofuzzbeta
2
// +build gofuzzbeta
1
//go:build go1.18
2
// +build go1.18
33

44
package generated_test
55

66
import (
77
	"regexp"
generated.go
FileFile
@@ -31,11 +31,13 @@ import (
3131
//
3232
// 	This line must appear before the first non-comment, non-blank
3333
// 	text in the file.
3434
func Parse(src io.Reader) (hasGeneratedComment bool, err error) {
3535
	br := bufio.NewReader(src)
36
	var inBlock bool // Whether we're inside a multi-line /* */ comment block.
36
	// Use inBlock to track whether we're inside a multi-line
37
	// /* */ comment block across calls to containsNonComment.
38
	var inBlock bool
3739
	for {
3840
		s, err := br.ReadBytes('\n')
3941
		if err == io.EOF {
4042
			return containsGenComment(s), nil
4143
		} else if err != nil {
@@ -54,12 +56,10 @@ func Parse(src io.Reader) (hasGeneratedComment bool, err error) {
5456
	}
5557
}
5658

5759
// containsNonComment reports whether a line of source code s (without newline)
5860
// contains something other than a line comment, block comment, or white space.
59
// *inBlock, given at the start of the line and updated at the end of the line,
60
// represents whether we're inside a multi-line /* */ comment block.
6161
func containsNonComment(s []byte, inBlock *bool) bool {
6262
	type state int
6363
	const (
6464
		normal state = iota
6565
		normalSlash