Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into mcy/more-suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 27, 2025
2 parents f1bfdab + 3a19ea5 commit c476083
Show file tree
Hide file tree
Showing 24 changed files with 731 additions and 310 deletions.
40 changes: 25 additions & 15 deletions experimental/ast/decl_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ast
import (
"github.com/bufbuild/protocompile/experimental/report"
"github.com/bufbuild/protocompile/experimental/token"
"github.com/bufbuild/protocompile/experimental/token/keyword"
"github.com/bufbuild/protocompile/internal/arena"
)

Expand Down Expand Up @@ -87,7 +88,7 @@ type DeclDefArgs struct {
// introduced by a special keyword, this will be a [TypePath] whose single
// identifier is that keyword.
//
// See [DeclDef.Keyword].
// See [DeclDef.KeywordToken].
func (d DeclDef) Type() TypeAny {
if d.IsZero() {
return TypeAny{}
Expand All @@ -101,19 +102,28 @@ func (d DeclDef) SetType(ty TypeAny) {
d.raw.ty = ty.raw
}

// Keyword returns the introducing keyword for this definition, if
// KeywordToken returns the introducing keyword for this definition, if
// there is one.
//
// See [DeclDef.Type] for details on where this keyword comes from.
func (d DeclDef) Keyword() token.Token {
func (d DeclDef) Keyword() keyword.Keyword {
return d.KeywordToken().Keyword()
}

// KeywordToken returns the introducing keyword token for this definition, if
// there is one.
//
// See [DeclDef.Type] for details on where this keyword comes from.
func (d DeclDef) KeywordToken() token.Token {
path := d.Type().AsPath()
if path.IsZero() {
return token.Zero
}

ident := path.Path.AsIdent()
switch ident.Text() {
case "message", "enum", "service", "extend", "oneof", "group", "rpc", "option":
switch ident.Keyword() {
case keyword.Message, keyword.Enum, keyword.Service, keyword.Extend,
keyword.Oneof, keyword.Group, keyword.RPC, keyword.Option:
return ident
default:
return token.Zero
Expand Down Expand Up @@ -234,7 +244,7 @@ func (d DeclDef) Semicolon() token.Token {
// See [DeclDef.Classify].
func (d DeclDef) AsMessage() DefMessage {
return DefMessage{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Body: d.Body(),
Decl: d,
Expand All @@ -250,7 +260,7 @@ func (d DeclDef) AsMessage() DefMessage {
// See [DeclDef.Classify].
func (d DeclDef) AsEnum() DefEnum {
return DefEnum{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Body: d.Body(),
Decl: d,
Expand All @@ -266,7 +276,7 @@ func (d DeclDef) AsEnum() DefEnum {
// See [DeclDef.Classify].
func (d DeclDef) AsService() DefService {
return DefService{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Body: d.Body(),
Decl: d,
Expand All @@ -281,7 +291,7 @@ func (d DeclDef) AsService() DefService {
// See [DeclDef.Classify].
func (d DeclDef) AsExtend() DefExtend {
return DefExtend{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Extendee: d.Name(),
Body: d.Body(),
Decl: d,
Expand Down Expand Up @@ -316,7 +326,7 @@ func (d DeclDef) AsField() DefField {
// See [DeclDef.Classify].
func (d DeclDef) AsOneof() DefOneof {
return DefOneof{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Body: d.Body(),
Decl: d,
Expand All @@ -332,7 +342,7 @@ func (d DeclDef) AsOneof() DefOneof {
// See [DeclDef.Classify].
func (d DeclDef) AsGroup() DefGroup {
return DefGroup{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Equals: d.Equals(),
Tag: d.Value(),
Expand Down Expand Up @@ -368,7 +378,7 @@ func (d DeclDef) AsEnumValue() DefEnumValue {
// See [DeclDef.Classify].
func (d DeclDef) AsMethod() DefMethod {
return DefMethod{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Name: d.Name().AsIdent(),
Signature: d.Signature(),
Body: d.Body(),
Expand All @@ -384,7 +394,7 @@ func (d DeclDef) AsMethod() DefMethod {
// See [DeclDef.Classify].
func (d DeclDef) AsOption() DefOption {
return DefOption{
Keyword: d.Keyword(),
Keyword: d.KeywordToken(),
Option: Option{
Path: d.Name(),
Equals: d.Equals(),
Expand All @@ -399,7 +409,7 @@ func (d DeclDef) AsOption() DefOption {
// definition it's supposed to represent.
//
// To select which definition this probably is, this function looks at
// [DeclDef.Keyword]. If there is no keyword or it isn't something that it
// [DeclDef.KeywordToken]. If there is no keyword or it isn't something that it
// recognizes, it is classified as either an enum value or a field, depending on
// whether this definition has a type.
//
Expand All @@ -411,7 +421,7 @@ func (d DeclDef) Classify() DefKind {
return DefKindInvalid
}

switch d.Keyword().Text() {
switch d.KeywordToken().Text() {
case "message":
if !d.Body().IsZero() {
return DefKindMessage
Expand Down
65 changes: 43 additions & 22 deletions experimental/ast/decl_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/bufbuild/protocompile/experimental/report"
"github.com/bufbuild/protocompile/experimental/seq"
"github.com/bufbuild/protocompile/experimental/token"
"github.com/bufbuild/protocompile/experimental/token/keyword"
"github.com/bufbuild/protocompile/internal/arena"
"github.com/bufbuild/protocompile/internal/iter"
)
Expand All @@ -35,7 +36,7 @@ type File struct {
DeclBody
}

// Syntax returns this file's pragma, if it has one.
// Syntax returns this file's declaration, if it has one.
func (f File) Syntax() (syntax DeclSyntax) {
seq.Values(f.Decls())(func(d DeclAny) bool {
if s := d.AsSyntax(); !s.IsZero() {
Expand Down Expand Up @@ -76,7 +77,7 @@ func (f File) Imports() iter.Seq2[int, DeclImport] {
}
}

// DeclSyntax represents a language pragma, such as the syntax or edition
// DeclSyntax represents a language declaration, such as the syntax or edition
// keywords.
//
// # Grammar
Expand All @@ -103,23 +104,28 @@ type DeclSyntaxArgs struct {
Semicolon token.Token
}

// Keyword returns the keyword for this pragma.
func (d DeclSyntax) Keyword() token.Token {
// Keyword returns the keyword for this declaration.
func (d DeclSyntax) Keyword() keyword.Keyword {
return d.KeywordToken().Keyword()
}

// KeywordToken returns the keyword token for this declaration.
func (d DeclSyntax) KeywordToken() token.Token {
if d.IsZero() {
return token.Zero
}

return d.raw.keyword.In(d.Context())
}

// IsSyntax checks whether this is an OG syntax pragma.
// IsSyntax checks whether this is an OG syntax declaration.
func (d DeclSyntax) IsSyntax() bool {
return d.Keyword().Text() == "syntax"
return d.Keyword() == keyword.Syntax
}

// IsEdition checks whether this is a new-style edition pragma.
// IsEdition checks whether this is a new-style edition declaration.
func (d DeclSyntax) IsEdition() bool {
return d.Keyword().Text() == "edition"
return d.Keyword() == keyword.Edition
}

// Equals returns the equals sign after the keyword.
Expand All @@ -133,7 +139,7 @@ func (d DeclSyntax) Equals() token.Token {
return d.raw.equals.In(d.Context())
}

// Value returns the value expression of this pragma.
// Value returns the value expression of this declaration.
//
// May be zero, if the user wrote something like syntax;. It can also be
// a number or an identifier, for cases like edition = 2024; or syntax = proto2;.
Expand All @@ -145,7 +151,7 @@ func (d DeclSyntax) Value() ExprAny {
return newExprAny(d.Context(), d.raw.value)
}

// SetValue sets the expression for this pragma's value.
// SetValue sets the expression for this declaration's value.
//
// If passed zero, this clears the value (e.g., for syntax = ;).
func (d DeclSyntax) SetValue(expr ExprAny) {
Expand All @@ -170,7 +176,7 @@ func (d DeclSyntax) SetOptions(opts CompactOptions) {
d.raw.options = d.Context().Nodes().options.Compress(opts.raw)
}

// Semicolon returns this pragma's ending semicolon.
// Semicolon returns this declaration's ending semicolon.
//
// May be zero, if the user forgot it.
func (d DeclSyntax) Semicolon() token.Token {
Expand All @@ -187,7 +193,7 @@ func (d DeclSyntax) Span() report.Span {
return report.Span{}
}

return report.Join(d.Keyword(), d.Equals(), d.Value(), d.Semicolon())
return report.Join(d.KeywordToken(), d.Equals(), d.Value(), d.Semicolon())
}

func wrapDeclSyntax(c Context, ptr arena.Pointer[rawDeclSyntax]) DeclSyntax {
Expand Down Expand Up @@ -219,8 +225,13 @@ type DeclPackageArgs struct {
Semicolon token.Token
}

// Keyword returns the "package" keyword for this declaration.
func (d DeclPackage) Keyword() token.Token {
// Keyword returns the keyword for this declaration.
func (d DeclPackage) Keyword() keyword.Keyword {
return d.KeywordToken().Keyword()
}

// KeywordToken returns the "package" token for this declaration.
func (d DeclPackage) KeywordToken() token.Token {
if d.IsZero() {
return token.Zero
}
Expand Down Expand Up @@ -274,7 +285,7 @@ func (d DeclPackage) Span() report.Span {
return report.Span{}
}

return report.Join(d.Keyword(), d.Path(), d.Semicolon())
return report.Join(d.KeywordToken(), d.Path(), d.Semicolon())
}

func wrapDeclPackage(c Context, ptr arena.Pointer[rawDeclPackage]) DeclPackage {
Expand Down Expand Up @@ -306,19 +317,29 @@ type DeclImportArgs struct {
Semicolon token.Token
}

// Keyword returns the "import" keyword for this pragma.
func (d DeclImport) Keyword() token.Token {
// Keyword returns the keyword for this declaration.
func (d DeclImport) Keyword() keyword.Keyword {
return d.KeywordToken().Keyword()
}

// KeywordToken returns the "import" keyword for this declaration.
func (d DeclImport) KeywordToken() token.Token {
if d.IsZero() {
return token.Zero
}

return d.raw.keyword.In(d.Context())
}

// Keyword returns the modifier keyword for this pragma.
// Modifier returns the modifier keyword for this declaration.
func (d DeclImport) Modifier() keyword.Keyword {
return d.ModifierToken().Keyword()
}

// ModifierToken returns the modifier token for this declaration.
//
// May be zero if there is no modifier.
func (d DeclImport) Modifier() token.Token {
func (d DeclImport) ModifierToken() token.Token {
if d.IsZero() {
return token.Zero
}
Expand All @@ -328,12 +349,12 @@ func (d DeclImport) Modifier() token.Token {

// IsSyntax checks whether this is an "import public".
func (d DeclImport) IsPublic() bool {
return d.Modifier().Text() == "public"
return d.Modifier() == keyword.Public
}

// IsEdition checks whether this is an "import weak".
func (d DeclImport) IsWeak() bool {
return d.Modifier().Text() == "weak"
return d.Modifier() == keyword.Weak
}

// ImportPath returns the file path for this import as a string.
Expand Down Expand Up @@ -389,7 +410,7 @@ func (d DeclImport) Span() report.Span {
return report.Span{}
}

return report.Join(d.Keyword(), d.Modifier(), d.ImportPath(), d.Semicolon())
return report.Join(d.KeywordToken(), d.ModifierToken(), d.ImportPath(), d.Semicolon())
}

func wrapDeclImport(c Context, ptr arena.Pointer[rawDeclImport]) DeclImport {
Expand Down
16 changes: 11 additions & 5 deletions experimental/ast/decl_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/bufbuild/protocompile/experimental/report"
"github.com/bufbuild/protocompile/experimental/seq"
"github.com/bufbuild/protocompile/experimental/token"
"github.com/bufbuild/protocompile/experimental/token/keyword"
"github.com/bufbuild/protocompile/internal/arena"
)

Expand All @@ -44,7 +45,12 @@ type DeclRangeArgs struct {
}

// Keyword returns the keyword for this range.
func (d DeclRange) Keyword() token.Token {
func (d DeclRange) Keyword() keyword.Keyword {
return d.KeywordToken().Keyword()
}

// KeywordToken returns the keyword token for this range.
func (d DeclRange) KeywordToken() token.Token {
if d.IsZero() {
return token.Zero
}
Expand All @@ -54,12 +60,12 @@ func (d DeclRange) Keyword() token.Token {

// IsExtensions checks whether this is an extension range.
func (d DeclRange) IsExtensions() bool {
return d.Keyword().Text() == "extensions"
return d.Keyword() == keyword.Extensions
}

// IsReserved checks whether this is a reserved range.
func (d DeclRange) IsReserved() bool {
return d.Keyword().Text() == "reserved"
return d.Keyword() == keyword.Reserved
}

// Ranges returns the sequence of expressions denoting the ranges in this
Expand Down Expand Up @@ -118,10 +124,10 @@ func (d DeclRange) Span() report.Span {
case d.IsZero():
return report.Span{}
case r.Len() == 0:
return report.Join(d.Keyword(), d.Semicolon(), d.Options())
return report.Join(d.KeywordToken(), d.Semicolon(), d.Options())
default:
return report.Join(
d.Keyword(), d.Semicolon(), d.Options(),
d.KeywordToken(), d.Semicolon(), d.Options(),
r.At(0),
r.At(r.Len()-1),
)
Expand Down
Loading

0 comments on commit c476083

Please sign in to comment.