Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions compiler_dualcompiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ import (
func TestDualCompiler_ParseFilesMessageComments(t *testing.T) {
t.Parallel()

skip := dualcompiler.SkipConfig{
SkipNew: true,
SkipReason: "source code info not yet fully implemented in experimental compiler",
}

resolver := protocompile.WithStandardImports(&protocompile.SourceResolver{
ImportPaths: []string{"internal/testdata"},
})
Expand All @@ -46,7 +41,7 @@ func TestDualCompiler_ParseFilesMessageComments(t *testing.T) {

dualcompiler.RunWithBothCompilersIf(
t,
skip,
dualcompiler.SkipConfig{},
opts,
func(t *testing.T, compiler dualcompiler.CompilerInterface) {
ctx := t.Context()
Expand Down Expand Up @@ -115,11 +110,6 @@ func TestDualCompiler_ParseFilesWithImportsNoImportPath(t *testing.T) {
func TestDualCompiler_ParseCommentsBeforeDot(t *testing.T) {
t.Parallel()

skip := dualcompiler.SkipConfig{
SkipNew: true,
SkipReason: "source code info not yet fully implemented in experimental compiler",
}

accessor := protocompile.SourceAccessorFromMap(map[string]string{
"test.proto": `
syntax = "proto3";
Expand All @@ -137,7 +127,7 @@ message Foo {

dualcompiler.RunWithBothCompilersIf(
t,
skip,
dualcompiler.SkipConfig{},
opts,
func(t *testing.T, compiler dualcompiler.CompilerInterface) {
ctx := t.Context()
Expand Down
12 changes: 12 additions & 0 deletions experimental/ast/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func (f *File) Imports() iter.Seq[DeclImport] {
})
}

// Options returns an iterator over this file's option definitions.
func (f *File) Options() iter.Seq[DefOption] {
return iterx.FilterMap(seq.Values(f.Decls()), func(d DeclAny) (DefOption, bool) {
if def := d.AsDef(); !def.IsZero() {
if def.Classify() == DefKindOption {
return def.AsOption(), true
}
}
return DefOption{}, false
})
}

// Path returns the semantic import path of this file.
func (f *File) Path() string {
if f == nil {
Expand Down
15 changes: 15 additions & 0 deletions experimental/ast/decl_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package ast

import (
"iter"

"github.com/bufbuild/protocompile/experimental/id"
"github.com/bufbuild/protocompile/experimental/seq"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/bufbuild/protocompile/experimental/token"
"github.com/bufbuild/protocompile/internal/ext/iterx"
)

// DeclBody is the body of a [DeclBody], or the whole contents of a [File]. The
Expand Down Expand Up @@ -110,3 +113,15 @@ func (d DeclBody) Decls() seq.Inserter[DeclAny] {
},
)
}

// Options returns an iterator over the option definitions in this body.
func (d DeclBody) Options() iter.Seq[DefOption] {
return iterx.FilterMap(seq.Values(d.Decls()), func(d DeclAny) (DefOption, bool) {
if def := d.AsDef(); !def.IsZero() {
if def.Classify() == DefKindOption {
return def.AsOption(), true
}
}
return DefOption{}, false
})
}
2 changes: 1 addition & 1 deletion experimental/ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (e ExprAny) AsLiteral() ExprLiteral {
}

// AsPath converts a ExprAny into a ExprPath, if that is the type
// it contains.q
// it contains.
//
// Otherwise, returns zero.
func (e ExprAny) AsPath() ExprPath {
Expand Down
33 changes: 33 additions & 0 deletions experimental/ast/predeclared/predeclared.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"fmt"
"iter"

"google.golang.org/protobuf/types/descriptorpb"

"github.com/bufbuild/protocompile/experimental/token/keyword"
"github.com/bufbuild/protocompile/internal/ext/slicesx"
)

// Name is one of the built-in Protobuf names. These represent particular
Expand Down Expand Up @@ -63,6 +66,29 @@ const (
Float64 = Double
)

// predeclaredToFDPType maps the scalar predeclared [Name]s to their respective
// [descriptorpb.FieldDescriptorProto_Type].
var predeclaredToFDPType = []descriptorpb.FieldDescriptorProto_Type{
Int32: descriptorpb.FieldDescriptorProto_TYPE_INT32,
Int64: descriptorpb.FieldDescriptorProto_TYPE_INT64,
UInt32: descriptorpb.FieldDescriptorProto_TYPE_UINT32,
UInt64: descriptorpb.FieldDescriptorProto_TYPE_UINT64,
SInt32: descriptorpb.FieldDescriptorProto_TYPE_SINT32,
SInt64: descriptorpb.FieldDescriptorProto_TYPE_SINT64,

Fixed32: descriptorpb.FieldDescriptorProto_TYPE_FIXED32,
Fixed64: descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
SFixed32: descriptorpb.FieldDescriptorProto_TYPE_SFIXED32,
SFixed64: descriptorpb.FieldDescriptorProto_TYPE_SFIXED64,

Float32: descriptorpb.FieldDescriptorProto_TYPE_FLOAT,
Float64: descriptorpb.FieldDescriptorProto_TYPE_DOUBLE,

Bool: descriptorpb.FieldDescriptorProto_TYPE_BOOL,
String: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Bytes: descriptorpb.FieldDescriptorProto_TYPE_BYTES,
}

// FromKeyword performs a vast from a [keyword.Keyword], but also validates
// that it is in-range. If it isn't, returns [Unknown].
func FromKeyword(kw keyword.Keyword) Name {
Expand All @@ -89,6 +115,13 @@ func (n Name) GoString() string {
return keyword.Keyword(n).GoString()
}

// FDPType returns the [descriptorpb.FieldDescriptorProto_Type] for the predeclared name,
// if it is a scalar type. Otherwise, it returns 0.
func (n Name) FDPType() descriptorpb.FieldDescriptorProto_Type {
kind, _ := slicesx.Get(predeclaredToFDPType, n)
return kind
}

// InRange returns whether this name value is within the range of declared
// values.
func (n Name) InRange() bool {
Expand Down
33 changes: 18 additions & 15 deletions experimental/ast/predeclared/predeclared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/descriptorpb"

"github.com/bufbuild/protocompile/experimental/ast/predeclared"
)
Expand All @@ -28,27 +29,28 @@ func TestPredicates(t *testing.T) {
tests := []struct {
v predeclared.Name
scalar, key bool
fdpType descriptorpb.FieldDescriptorProto_Type
}{
{v: predeclared.Unknown},

{v: predeclared.Int32, scalar: true, key: true},
{v: predeclared.Int64, scalar: true, key: true},
{v: predeclared.UInt32, scalar: true, key: true},
{v: predeclared.UInt64, scalar: true, key: true},
{v: predeclared.SInt32, scalar: true, key: true},
{v: predeclared.SInt64, scalar: true, key: true},
{v: predeclared.Int32, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_INT32},
{v: predeclared.Int64, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_INT64},
{v: predeclared.UInt32, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_UINT32},
{v: predeclared.UInt64, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_UINT64},
{v: predeclared.SInt32, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_SINT32},
{v: predeclared.SInt64, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_SINT64},

{v: predeclared.Fixed32, scalar: true, key: true},
{v: predeclared.Fixed64, scalar: true, key: true},
{v: predeclared.SFixed32, scalar: true, key: true},
{v: predeclared.SFixed64, scalar: true, key: true},
{v: predeclared.Fixed32, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_FIXED32},
{v: predeclared.Fixed64, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_FIXED64},
{v: predeclared.SFixed32, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_SFIXED32},
{v: predeclared.SFixed64, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_SFIXED64},

{v: predeclared.Float, scalar: true},
{v: predeclared.Double, scalar: true},
{v: predeclared.Float, scalar: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_FLOAT},
{v: predeclared.Double, scalar: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_DOUBLE},

{v: predeclared.String, scalar: true, key: true},
{v: predeclared.Bytes, scalar: true},
{v: predeclared.Bool, scalar: true, key: true},
{v: predeclared.String, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_STRING},
{v: predeclared.Bytes, scalar: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_BYTES},
{v: predeclared.Bool, scalar: true, key: true, fdpType: descriptorpb.FieldDescriptorProto_TYPE_BOOL},

{v: predeclared.Map},
{v: predeclared.Max},
Expand All @@ -61,5 +63,6 @@ func TestPredicates(t *testing.T) {
for _, test := range tests {
assert.Equal(t, test.scalar, test.v.IsScalar())
assert.Equal(t, test.key, test.v.IsMapKey())
assert.Equal(t, test.fdpType, test.v.FDPType())
}
}
Loading
Loading