Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 25, 2025
1 parent 709b42c commit a88cee7
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion experimental/ast/syntax/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// Editions returns an iterator over all the editions in this package.
func Editions() iter.Seq[Syntax] {
return func(yield func(Syntax) bool) {
for i := 0; i < totalEditions; i++ {
for i := range totalEditions {
if !yield(Syntax(i + int(Edition2023))) {
break
}
Expand Down
3 changes: 3 additions & 0 deletions experimental/internal/taxa/noun.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions experimental/internal/taxa/noun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
- {name: Braces, string: "`{...}`"}
- {name: Angles, string: "`<...>`"}

- {name: ReturnsParens, string: "`returns (...)`"}

- {name: KeywordSyntax, string: "`syntax`"}
- {name: KeywordEdition, string: "`edition`"}
- {name: KeywordImport, string: "`import`"}
Expand Down
17 changes: 12 additions & 5 deletions experimental/parser/diagnostics_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package parser

import (
"fmt"

"github.com/bufbuild/protocompile/experimental/ast"
"github.com/bufbuild/protocompile/experimental/internal/taxa"
"github.com/bufbuild/protocompile/experimental/report"
Expand All @@ -37,7 +39,10 @@ type errUnexpected struct {
// shown, but if it's not set, we call describe(what) to get a user-visible
// description.
want taxa.Set
got any
// If set and want is empty, the snippet will repeat the "unexpected foo"
// text under the snippet.
repeatUnexpected bool
got any
}

func (e errUnexpected) Diagnose(d *report.Diagnostic) {
Expand All @@ -49,20 +54,22 @@ func (e errUnexpected) Diagnose(d *report.Diagnostic) {
}
}

var message report.DiagnosticOption
var message string
if e.where.Subject() == taxa.Unknown {
message = report.Message("unexpected %v", got)
message = fmt.Sprintf("unexpected %v", got)
} else {
message = report.Message("unexpected %v %v", got, e.where)
message = fmt.Sprintf("unexpected %v %v", got, e.where)
}

snippet := report.Snippet(e.what)
if e.want.Len() > 0 {
snippet = report.Snippetf(e.what, "expected %v", e.want.Join("or"))
} else if e.repeatUnexpected {
snippet = report.Snippetf(e.what, "%v", message)
}

d.Apply(
message,
report.Message("%v", message),
snippet,
report.Snippetf(e.prev, "previous %v is here", e.where.Subject()),
)
Expand Down
4 changes: 1 addition & 3 deletions experimental/parser/legalize_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ func legalizeRange(p *parser, parent classified, decl ast.DeclRange) {
}
case ast.ExprKindPrefixed:
expr := expr.AsPrefixed()
//nolint:gocritic // Intentional single-case switch.
switch expr.Prefix() {
case ast.ExprPrefixMinus:
if expr.Prefix() == ast.ExprPrefixMinus {
lit := expr.Expr().AsLiteral()
if lit.Kind() != token.Number || strings.Contains(lit.Text(), ".") {
p.Error(errUnexpected{
Expand Down
11 changes: 8 additions & 3 deletions experimental/parser/legalize_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func legalizeTypeDefLike(p *parser, what taxa.Noun, def ast.DeclDef) {
err = errUnexpected{
what: pc.Separator(),
where: taxa.Ident.In(),
want: taxa.Ident.AsSet(),

repeatUnexpected: true,
}
return false
})
Expand Down Expand Up @@ -253,7 +254,7 @@ func legalizeMethod(p *parser, def ast.DeclDef) {
if sig.Inputs().Span().IsZero() {
def.MarkCorrupt()
p.Errorf("missing %v in %v", taxa.MethodIns, taxa.Method).Apply(
report.Snippetf(def.Name(), "expected type in %s after this", taxa.Parens),
report.Snippetf(def.Name(), "expected argument type in %s after this", taxa.Parens),
)
} else {
legalizeMethodParams(p, sig.Inputs(), taxa.MethodIns)
Expand All @@ -262,17 +263,21 @@ func legalizeMethod(p *parser, def ast.DeclDef) {
if sig.Outputs().Span().IsZero() {
def.MarkCorrupt()
var after report.Spanner
var expected taxa.Noun
switch {
case !sig.Returns().IsZero():
after = sig.Returns()
expected = taxa.Parens
case !sig.Inputs().IsZero():
after = sig.Inputs()
expected = taxa.ReturnsParens
default:
after = def.Name()
expected = taxa.ReturnsParens
}

p.Errorf("missing %v in %v", taxa.MethodOuts, taxa.Method).Apply(
report.Snippetf(after, "expected type in %s after this", taxa.Parens),
report.Snippetf(after, "expected return type in %s after this", expected),
)
} else {
legalizeMethodParams(p, sig.Outputs(), taxa.MethodOuts)
Expand Down
1 change: 1 addition & 0 deletions experimental/parser/legalize_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func legalizeOptionValue(p *parser, decl report.Span, parent ast.ExprAny, value
}
fallthrough
default:
// TODO: generate a suggestion for this.
err.Apply(report.Helpf("break this %s into one per element", taxa.Option))
}

Expand Down
4 changes: 3 additions & 1 deletion experimental/parser/parse_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func (defOutputs) parse(p *defParser) report.Span {
// Note that the inputs and outputs of a method are parsed
// separately, so foo(bar) and foo returns (bar) are both possible.
returns := p.c.Next()
if p.args.Returns.IsZero() {
p.args.Returns = returns
}

var ty ast.TypeAny
list, err := p.Punct(p.c, "(", taxa.KeywordReturns.After())
Expand All @@ -220,7 +223,6 @@ func (defOutputs) parse(p *defParser) report.Span {
}

if p.outputs.IsZero() && p.outputTy.IsZero() {
p.args.Returns = returns
if !list.IsZero() {
p.outputs = list
} else {
Expand Down
9 changes: 8 additions & 1 deletion internal/ext/mapsx/mapsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ func Keys[M ~map[K]V, K comparable, V any](m M) iter.Seq[K] {

// KeySet returns a copy of m, with its values replaced with empty structs.
func KeySet[M ~map[K]V, K comparable, V any](m M) map[K]struct{} {
return CollectSet(Keys(m))
// return CollectSet(Keys(m))
// Instead of going through an iterator, inline the loop so that
// we can preallocate and avoid rehashes.
keys := make(map[K]struct{}, len(m))
for k := range m {
keys[k] = struct{}{}
}
return keys
}
4 changes: 2 additions & 2 deletions internal/ext/stringsx/stringsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/bufbuild/protocompile/internal/iter"
)

// Rune returns the rune at the given index.
// Rune returns the rune at the given byte index.
//
// Returns 0, false if out of bounds. Returns U+FFFD, false if rune decoding fails.
func Rune[I slicesx.SliceIndex](s string, idx I) (rune, bool) {
Expand All @@ -37,7 +37,7 @@ func Rune[I slicesx.SliceIndex](s string, idx I) (rune, bool) {
return r, r != utf8.RuneError
}

// Rune returns the previous rune at the given index.
// Rune returns the previous rune at the given byte index.
//
// Returns 0, false if out of bounds. Returns U+FFFD, false if rune decoding fails.
func PrevRune[I slicesx.SliceIndex](s string, idx I) (rune, bool) {
Expand Down
10 changes: 0 additions & 10 deletions internal/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,12 @@ require (
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
<<<<<<< HEAD
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
golang.org/x/exp/typeparams v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.36.4-0.20250116160514-2005adbe0cf6 // indirect
=======
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
>>>>>>> origin/main
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down

0 comments on commit a88cee7

Please sign in to comment.