Documentation
¶
Overview ¶
Package ast is a high-performance implementation of a Protobuf IDL abstract syntax tree. It is intended to be end-all, be-all AST for the following use-cases:
1. Parse target for a Protobuf compiler.
2. Incremental, fault-tolerant parsing for a Protobuf language server.
3. In-place rewriting of the AST to implement refactoring tools.
4. Formatting, even of partially-invalid code.
"High-performance" means that the AST is optimized to minimize resident memory use, pointer nesting (to minimize GC pause contribution), and maximize locality. This AST is suitable for hydrating large Buf modules in a long-lived process without exhausting memory or introducing unreasonable GC latency.
In general, if an API tradeoff is necessary for satisfying any of the above goals, we make the tradeoff, but we attempt to maintain a high degree of ease of use.
Node Types ¶
Most types in this package represent AST nodes of some kind. Each such type contains a grammar for the lenient extension of the Protobuf grammar that it represents. The grammar is notated using regular expression syntax, with the following modifications:
- Whitespace is ignored.
- Literal strings (except in character classes) are represented with Go strings.
- Unquoted names refer to other productions.
Productions that represent a type from this package, a token.Kind, or a production used by a different type, are in PascalCase; all other productions are in camelCase and are scoped to that type.
The parser (and other parts of the compiler) will diagnose invalid Protobuf syntax, but this AST is able to represent the extended syntax it documents.
These grammars are provided for illustration only: the complete grammar is ambiguous in the absence of greediness decisions, which, except where otherwise noted, neither affect the parsing of correct Protobuf files, nor are they specified and are subject to change.
AST Context ¶
Virtually all operations in this package involve a [Context] (no, not a context.Context). This struct acts as an arena that enables the highly compressed, memory-friendly representation this package uses for the AST.
Using a [Context], you can create new tokens and new AST nodes. Types that represent AST nodes, such as [DeclMessage], are thin wrappers over a pointer to a [Context] and index into one of its tables. They are intended to be passed by value, because they are essentially pointers (and, in fact, expose a IsZero function for checking if they refer to a nil Context pointer).
Pointer-like Types ¶
Virtually all AST nodes in this library are "pointer-like" types, in that although they are not Go pointers, they do refer to something stored in a [Context] somewhere. Internally, they contain a pointer to a [Context] and a pointer to the compressed node representation inside the [Context]. This is done so that we can avoid spending an extra eight or twelve bytes per node-at-rest, and to minimize GC churn by avoiding pointer cycles in the in-memory representation of the AST.
All pointer-like types have a bool-returning IsZero method, which checks for the zero value. Pointer-like types should generally be passed by value, not by pointer; all of them have value receivers.
In some places, the zero value of a pointer-like type is (incorrectly) referred to as nil. This is a documentation bug; instead, it should say zero.
Index ¶
- type AnnotationUseArg
- func (a AnnotationUseArg) Equals() token.Token
- func (a AnnotationUseArg) IsNamed() bool
- func (a AnnotationUseArg) IsZero() bool
- func (a AnnotationUseArg) MessageType() Path
- func (a AnnotationUseArg) Name() token.Token
- func (a AnnotationUseArg) Span() source.Span
- func (a AnnotationUseArg) Value() ExprAny
- func (a AnnotationUseArg) ValueEnd() token.Token
- func (a AnnotationUseArg) ValueSpan() source.Span
- func (a AnnotationUseArg) ValueStart() token.Token
- type AnnotationUseArgSpec
- type Commas
- type CompactOptions
- type DeclAnnotation
- func (d DeclAnnotation) Annotations() seq.Inserter[DeclAnnotationUse]
- func (d DeclAnnotation) AsAny() DeclAny
- func (d DeclAnnotation) Keyword() keyword.Keyword
- func (d DeclAnnotation) KeywordToken() token.Token
- func (d DeclAnnotation) Name() token.Token
- func (d DeclAnnotation) Params() Commas[DeclAnnotationParam]
- func (d DeclAnnotation) Parens() token.Token
- func (d DeclAnnotation) Semicolon() token.Token
- func (d DeclAnnotation) Span() source.Span
- type DeclAnnotationArgs
- type DeclAnnotationParam
- func (p DeclAnnotationParam) Colon() token.Token
- func (p DeclAnnotationParam) Default() ExprAny
- func (p DeclAnnotationParam) DefaultEnd() token.Token
- func (p DeclAnnotationParam) DefaultSpan() source.Span
- func (p DeclAnnotationParam) DefaultStart() token.Token
- func (p DeclAnnotationParam) Equals() token.Token
- func (p DeclAnnotationParam) HasDefault() bool
- func (p DeclAnnotationParam) Name() token.Token
- func (p DeclAnnotationParam) SetDefault(expr ExprAny)
- func (p DeclAnnotationParam) SetType(ty TypeAny)
- func (p DeclAnnotationParam) Span() source.Span
- func (p DeclAnnotationParam) Type() TypeAny
- type DeclAnnotationParamArgs
- type DeclAnnotationUse
- func (d DeclAnnotationUse) Args() Commas[AnnotationUseArg]
- func (d DeclAnnotationUse) AsAny() DeclAny
- func (d DeclAnnotationUse) At() token.Token
- func (d DeclAnnotationUse) Name() Path
- func (d DeclAnnotationUse) NewArg(spec AnnotationUseArgSpec) AnnotationUseArg
- func (d DeclAnnotationUse) Parens() token.Token
- func (d DeclAnnotationUse) Span() source.Span
- type DeclAnnotationUseArgs
- type DeclAny
- func (d DeclAny) AsAnnotation() DeclAnnotation
- func (d DeclAny) AsAnnotationUse() DeclAnnotationUse
- func (d DeclAny) AsBody() DeclBody
- func (d DeclAny) AsDef() DeclDef
- func (d DeclAny) AsEmpty() DeclEmpty
- func (d DeclAny) AsFunction() DeclFunction
- func (d DeclAny) AsImport() DeclImport
- func (d DeclAny) AsPackage() DeclPackage
- func (d DeclAny) AsRange() DeclRange
- func (d DeclAny) AsSyntax() DeclSyntax
- func (d DeclAny) AsType() DeclType
- func (d DeclAny) Span() source.Span
- type DeclBody
- type DeclDef
- func (d DeclDef) Annotations() seq.Inserter[DeclAnnotationUse]
- func (d DeclDef) AsAny() DeclAny
- func (d DeclDef) AsEnum() DefEnum
- func (d DeclDef) AsEnumValue() DefEnumValue
- func (d DeclDef) AsExtend() DefExtend
- func (d DeclDef) AsField() DefField
- func (d DeclDef) AsGroup() DefGroup
- func (d DeclDef) AsMessage() DefMessage
- func (d DeclDef) AsMethod() DefMethod
- func (d DeclDef) AsOneof() DefOneof
- func (d DeclDef) AsOption() DefOption
- func (d DeclDef) AsService() DefService
- func (d DeclDef) Body() DeclBody
- func (d DeclDef) Classify() DefKind
- func (d DeclDef) Equals() token.Token
- func (d DeclDef) IsCorrupt() bool
- func (d DeclDef) Keyword() keyword.Keyword
- func (d DeclDef) KeywordToken() token.Token
- func (d DeclDef) MarkCorrupt()
- func (d DeclDef) Name() Path
- func (d DeclDef) Options() CompactOptions
- func (d DeclDef) Prefixes() iter.Seq[TypePrefixed]
- func (d DeclDef) Semicolon() token.Token
- func (d DeclDef) SetBody(b DeclBody)
- func (d DeclDef) SetOptions(opts CompactOptions)
- func (d DeclDef) SetType(ty TypeAny)
- func (d DeclDef) SetValue(expr ExprAny)
- func (d DeclDef) Signature() Signature
- func (d DeclDef) Span() source.Span
- func (d DeclDef) Stem() source.Span
- func (d DeclDef) Type() TypeAny
- func (d DeclDef) Value() ExprAny
- func (d DeclDef) WithSignature() Signature
- type DeclDefArgs
- type DeclEmpty
- type DeclFunction
- func (d DeclFunction) Annotations() seq.Inserter[DeclAnnotationUse]
- func (d DeclFunction) AsAny() DeclAny
- func (d DeclFunction) Keyword() keyword.Keyword
- func (d DeclFunction) KeywordToken() token.Token
- func (d DeclFunction) Name() token.Token
- func (d DeclFunction) Options() CompactOptions
- func (d DeclFunction) Params() Commas[DeclFunctionParam]
- func (d DeclFunction) Parens() token.Token
- func (d DeclFunction) Semicolon() token.Token
- func (d DeclFunction) SetOptions(opts CompactOptions)
- func (d DeclFunction) Span() source.Span
- type DeclFunctionArgs
- type DeclFunctionParam
- type DeclFunctionParamArgs
- type DeclImport
- func (d DeclImport) AsAny() DeclAny
- func (d DeclImport) ImportPath() ExprAny
- func (d DeclImport) IsOption() bool
- func (d DeclImport) IsPublic() bool
- func (d DeclImport) IsWeak() bool
- func (d DeclImport) Keyword() keyword.Keyword
- func (d DeclImport) KeywordToken() token.Token
- func (d DeclImport) ModifierTokens() seq.Inserter[token.Token]
- func (d DeclImport) Modifiers() seq.Indexer[keyword.Keyword]
- func (d DeclImport) Options() CompactOptions
- func (d DeclImport) Semicolon() token.Token
- func (d DeclImport) SetImportPath(expr ExprAny)
- func (d DeclImport) SetOptions(opts CompactOptions)
- func (d DeclImport) Span() source.Span
- type DeclImportArgs
- type DeclKind
- type DeclPackage
- func (d DeclPackage) AsAny() DeclAny
- func (d DeclPackage) Keyword() keyword.Keyword
- func (d DeclPackage) KeywordToken() token.Token
- func (d DeclPackage) Options() CompactOptions
- func (d DeclPackage) Path() Path
- func (d DeclPackage) Semicolon() token.Token
- func (d DeclPackage) SetOptions(opts CompactOptions)
- func (d DeclPackage) Span() source.Span
- type DeclPackageArgs
- type DeclRange
- func (d DeclRange) AsAny() DeclAny
- func (d DeclRange) IsExtensions() bool
- func (d DeclRange) IsReserved() bool
- func (d DeclRange) Keyword() keyword.Keyword
- func (d DeclRange) KeywordToken() token.Token
- func (d DeclRange) Options() CompactOptions
- func (d DeclRange) Ranges() Commas[ExprAny]
- func (d DeclRange) Semicolon() token.Token
- func (d DeclRange) SetOptions(opts CompactOptions)
- func (d DeclRange) Span() source.Span
- type DeclRangeArgs
- type DeclSyntax
- func (d DeclSyntax) AsAny() DeclAny
- func (d DeclSyntax) Equals() token.Token
- func (d DeclSyntax) IsEdition() bool
- func (d DeclSyntax) IsSyntax() bool
- func (d DeclSyntax) Keyword() keyword.Keyword
- func (d DeclSyntax) KeywordToken() token.Token
- func (d DeclSyntax) Options() CompactOptions
- func (d DeclSyntax) Semicolon() token.Token
- func (d DeclSyntax) SetOptions(opts CompactOptions)
- func (d DeclSyntax) SetValue(expr ExprAny)
- func (d DeclSyntax) Span() source.Span
- func (d DeclSyntax) Value() ExprAny
- type DeclSyntaxArgs
- type DeclType
- func (d DeclType) Annotations() seq.Inserter[DeclAnnotationUse]
- func (d DeclType) AsAny() DeclAny
- func (d DeclType) Equals() token.Token
- func (d DeclType) Keyword() keyword.Keyword
- func (d DeclType) KeywordToken() token.Token
- func (d DeclType) Name() token.Token
- func (d DeclType) Semicolon() token.Token
- func (d DeclType) SetValue(expr ExprAny)
- func (d DeclType) Span() source.Span
- func (d DeclType) Value() ExprAny
- type DeclTypeArgs
- type Def
- type DefEnum
- type DefEnumValue
- type DefExtend
- type DefField
- type DefGroup
- type DefKind
- type DefMessage
- type DefMethod
- type DefOneof
- type DefOption
- type DefService
- type ExprAny
- func (e ExprAny) AsArray() ExprArray
- func (e ExprAny) AsDict() ExprDict
- func (e ExprAny) AsError() ExprError
- func (e ExprAny) AsField() ExprField
- func (e ExprAny) AsLiteral() ExprLiteral
- func (e ExprAny) AsPath() ExprPath
- func (e ExprAny) AsPrefixed() ExprPrefixed
- func (e ExprAny) AsRange() ExprRange
- func (e ExprAny) Span() source.Span
- type ExprArray
- type ExprDict
- type ExprError
- type ExprField
- type ExprFieldArgs
- type ExprKind
- type ExprLiteral
- type ExprPath
- type ExprPrefixed
- type ExprPrefixedArgs
- type ExprRange
- type ExprRangeArgs
- type File
- func (f *File) Decls() seq.Inserter[DeclAny]
- func (f *File) FromID(id uint64, want any) any
- func (f *File) Imports() iter.Seq[DeclImport]
- func (f *File) Nodes() *Nodes
- func (f *File) Options() iter.Seq[DefOption]
- func (f *File) Package() DeclPackage
- func (f *File) Path() string
- func (f *File) Span() source.Span
- func (f *File) Stream() *token.Stream
- func (f *File) Syntax() DeclSyntax
- type HasBody
- type Nodes
- func (n *Nodes) File() *File
- func (n *Nodes) NewCompactOptions(brackets token.Token) CompactOptions
- func (n *Nodes) NewDeclAnnotation(args DeclAnnotationArgs) DeclAnnotation
- func (n *Nodes) NewDeclAnnotationParam(args DeclAnnotationParamArgs) DeclAnnotationParam
- func (n *Nodes) NewDeclAnnotationUse(args DeclAnnotationUseArgs) DeclAnnotationUse
- func (n *Nodes) NewDeclBody(braces token.Token) DeclBody
- func (n *Nodes) NewDeclDef(args DeclDefArgs) DeclDef
- func (n *Nodes) NewDeclEmpty(semicolon token.Token) DeclEmpty
- func (n *Nodes) NewDeclFunction(args DeclFunctionArgs) DeclFunction
- func (n *Nodes) NewDeclFunctionParam(args DeclFunctionParamArgs) DeclFunctionParam
- func (n *Nodes) NewDeclImport(args DeclImportArgs) DeclImport
- func (n *Nodes) NewDeclPackage(args DeclPackageArgs) DeclPackage
- func (n *Nodes) NewDeclRange(args DeclRangeArgs) DeclRange
- func (n *Nodes) NewDeclSyntax(args DeclSyntaxArgs) DeclSyntax
- func (n *Nodes) NewDeclType(args DeclTypeArgs) DeclType
- func (n *Nodes) NewExprArray(brackets token.Token) ExprArray
- func (n *Nodes) NewExprDict(braces token.Token) ExprDict
- func (n *Nodes) NewExprField(args ExprFieldArgs) ExprField
- func (n *Nodes) NewExprPrefixed(args ExprPrefixedArgs) ExprPrefixed
- func (n *Nodes) NewExprRange(args ExprRangeArgs) ExprRange
- func (n *Nodes) NewExtensionComponent(separator token.Token, path Path) PathComponent
- func (n *Nodes) NewPath(components ...PathComponent) Path
- func (n *Nodes) NewPathComponent(separator, name token.Token) PathComponent
- func (n *Nodes) NewTypeGeneric(args TypeGenericArgs) TypeGeneric
- func (n *Nodes) NewTypePrefixed(args TypePrefixedArgs) TypePrefixed
- func (n *Nodes) NewTypedExprDict(typeName Path, braces token.Token) ExprDict
- type Option
- type Path
- func (p Path) Absolute() bool
- func (p Path) AsIdent() token.Token
- func (p Path) AsKeyword() keyword.Keyword
- func (p Path) AsPredeclared() predeclared.Name
- func (p Path) Canonicalized() string
- func (p Path) Components() iter.Seq[PathComponent]
- func (p Path) ID() PathID
- func (p Path) IsIdents(idents ...string) bool
- func (p Path) IsSynthetic() bool
- func (p Path) Span() source.Span
- func (p Path) Split(n int) (prefix, suffix Path)
- func (p Path) ToRelative() Path
- type PathComponent
- func (p PathComponent) AsExtension() Path
- func (p PathComponent) AsIdent() token.Token
- func (p PathComponent) IsEmpty() bool
- func (p PathComponent) IsFirst() bool
- func (p PathComponent) IsLast() bool
- func (p PathComponent) Name() token.Token
- func (p PathComponent) Next() PathComponent
- func (p PathComponent) Path() Path
- func (p PathComponent) Separator() token.Token
- func (p PathComponent) Span() source.Span
- func (p PathComponent) SplitAfter() (before, after Path)
- func (p PathComponent) SplitBefore() (before, after Path)
- type PathID
- type Signature
- type TypeAny
- type TypeError
- type TypeGeneric
- type TypeGenericArgs
- type TypeKind
- type TypeList
- func (d TypeList) AppendComma(value TypeAny, comma token.Token)
- func (d TypeList) At(n int) TypeAny
- func (d TypeList) Brackets() token.Token
- func (d TypeList) Comma(n int) token.Token
- func (d TypeList) Delete(n int)
- func (d TypeList) Insert(n int, ty TypeAny)
- func (d TypeList) InsertComma(n int, ty TypeAny, comma token.Token)
- func (d TypeList) Len() int
- func (d TypeList) SetAt(n int, ty TypeAny)
- func (d TypeList) SetBrackets(brackets token.Token)
- func (d TypeList) SetComma(n int, comma token.Token)
- func (d TypeList) Span() source.Span
- type TypePath
- type TypePrefixed
- type TypePrefixedArgs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnnotationUseArg ¶
type AnnotationUseArg struct {
// contains filtered or unexported fields
}
AnnotationUseArg is a single argument of a DeclAnnotationUse, following RFC-001 §5.1 capture-then-classify parsing.
Every argument records its captured token range (AnnotationUseArg.ValueStart through AnnotationUseArg.ValueEnd; AnnotationUseArg.ValueSpan for the raw text). Arguments that re-parse under the §5.1 value grammar `literal | qualifiedIdent | listLiteral | messageLiteral` also carry the parsed expression in AnnotationUseArg.Value; opaque engine-expression fragments leave Value zero. Classification against the annotation's parameter types happens at link time.
func (AnnotationUseArg) Equals ¶
func (a AnnotationUseArg) Equals() token.Token
Equals returns the `=` token of the `name = value` form. Zero for positional arguments.
func (AnnotationUseArg) IsNamed ¶
func (a AnnotationUseArg) IsNamed() bool
IsNamed reports whether this is a named argument (`name = value`).
func (AnnotationUseArg) IsZero ¶
func (a AnnotationUseArg) IsZero() bool
IsZero reports whether this is the zero value.
func (AnnotationUseArg) MessageType ¶
func (a AnnotationUseArg) MessageType() Path
MessageType returns the leading type name of a typed message-literal argument (the `Money` of `Money{currency: "USD"}`), in which case AnnotationUseArg.Value holds the braced initializer as an ExprDict. Zero for every other argument shape.
func (AnnotationUseArg) Name ¶
func (a AnnotationUseArg) Name() token.Token
Name returns the named-argument identifier for the `name = value` form. Zero for positional arguments.
func (AnnotationUseArg) Span ¶
func (a AnnotationUseArg) Span() source.Span
Span implements source.Spanner.
func (AnnotationUseArg) Value ¶
func (a AnnotationUseArg) Value() ExprAny
Value returns the argument re-parsed under the RFC-001 §5.1 value grammar `literal | qualifiedIdent | listLiteral | messageLiteral`.
Returns a zero ExprAny when the capture did not re-parse under that grammar — i.e. the argument is an opaque engine-expression fragment, legal only when bound to an `expression`-typed parameter (diagnosed at link time).
func (AnnotationUseArg) ValueEnd ¶
func (a AnnotationUseArg) ValueEnd() token.Token
ValueEnd returns the last token of the captured value (inclusive).
func (AnnotationUseArg) ValueSpan ¶
func (a AnnotationUseArg) ValueSpan() source.Span
ValueSpan returns the span of the captured value — the argument without the `name =` prefix. Its Text() is the §5.1 verbatim capture (before whitespace trimming).
func (AnnotationUseArg) ValueStart ¶
func (a AnnotationUseArg) ValueStart() token.Token
ValueStart returns the first token of the captured value.
type AnnotationUseArgSpec ¶
type AnnotationUseArgSpec struct {
// Name is the named-argument identifier for the `name = value`
// form; zero for positional arguments.
Name token.Token
// Equals is the `=` token of the `name = value` form; zero for
// positional arguments.
Equals token.Token
// Start and End delimit the captured value: every token from Start
// to End inclusive, at the argument's nesting depth. Both are
// always set (the capture must be non-empty).
Start, End token.Token
// MessageType is the leading type name of a typed message literal
// (`Money{...}`); zero otherwise.
MessageType Path
// Value is the argument re-parsed under the RFC-001 §5.1 value
// grammar `literal | qualifiedIdent | listLiteral | messageLiteral`;
// zero when the capture is an opaque engine-expression fragment.
Value ExprAny
}
AnnotationUseArgSpec is the arguments for DeclAnnotationUse.NewArg.
type Commas ¶
type Commas[T any] interface { seq.Inserter[T] // Comma is like [seq.Indexer.At] but returns the comma that follows the nth // element. // // May be [token.Zero], either because it's the last element // (a common situation where there is no comma) or it was added with // Insert() rather than InsertComma(). Comma(n int) token.Token // AppendComma is like [seq.Append], but includes an explicit comma. AppendComma(value T, comma token.Token) // InsertComma is like [seq.Inserter.Insert], but includes an explicit comma. InsertComma(n int, value T, comma token.Token) // SetComma sets the comma that follows the nth element. SetComma(n int, comma token.Token) }
Commas is like [Slice], but it's for a comma-delimited list of some kind.
This makes it easy to work with the list as though it's a slice, while also allowing access to the commas.
type CompactOptions ¶
type CompactOptions id.Node[CompactOptions, *File, *rawCompactOptions]
CompactOptions represents the collection of options attached to a DeclAny, contained within square brackets.
Grammar ¶
CompactOptions := `[` (option `,`?)? `]` option := Path [:=]? Expr?
func (CompactOptions) Brackets ¶
func (o CompactOptions) Brackets() token.Token
Brackets returns the token tree corresponding to the whole [...].
func (CompactOptions) Entries ¶
func (o CompactOptions) Entries() Commas[Option]
Entries returns the sequence of options in this CompactOptions.
func (CompactOptions) Span ¶
func (o CompactOptions) Span() source.Span
Span implements source.Spanner.
type DeclAnnotation ¶
type DeclAnnotation id.Node[DeclAnnotation, *File, *rawDeclAnnotation]
DeclAnnotation is an annotation-declaration introduced by the protowire v1.2 schema extensions (RFC-001).
An annotation declaration names a metadata attachment usable at use sites via the `@name(args)` syntax.
Grammar ¶
DeclAnnotation := `annotation` Ident (`(` (DeclAnnotationParam `,`?)* `)`)? `;`?
The parameter list is optional in the source: both `annotation required;` (no parens) and `annotation required();` (empty parens) are accepted forms. When parens are absent, DeclAnnotation.Parens returns zero. Parameters are appended to the returned node via DeclAnnotation.Params.
func (DeclAnnotation) Annotations ¶
func (d DeclAnnotation) Annotations() seq.Inserter[DeclAnnotationUse]
Annotations returns this declaration's annotation use sites.
For DeclAnnotation, annotations attach as trailing metadata (between the closing paren and the semicolon). The seq is empty unless the parser has successfully attached at least one annotation.
func (DeclAnnotation) AsAny ¶
func (d DeclAnnotation) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclAnnotation) Keyword ¶
func (d DeclAnnotation) Keyword() keyword.Keyword
Keyword returns the keyword for this declaration.
func (DeclAnnotation) KeywordToken ¶
func (d DeclAnnotation) KeywordToken() token.Token
KeywordToken returns the "annotation" token for this declaration.
func (DeclAnnotation) Name ¶
func (d DeclAnnotation) Name() token.Token
Name returns the identifier token naming the declared annotation.
May be zero, if the user forgot it.
func (DeclAnnotation) Params ¶
func (d DeclAnnotation) Params() Commas[DeclAnnotationParam]
Params returns the parameter list for this declaration.
func (DeclAnnotation) Parens ¶
func (d DeclAnnotation) Parens() token.Token
Parens returns the fused parentheses token wrapping the parameter list, or zero when the source omitted the parameter list entirely.
func (DeclAnnotation) Semicolon ¶
func (d DeclAnnotation) Semicolon() token.Token
Semicolon returns this declaration's ending semicolon.
May be zero, if the user forgot it.
func (DeclAnnotation) Span ¶
func (d DeclAnnotation) Span() source.Span
Span implements source.Spanner.
type DeclAnnotationArgs ¶
type DeclAnnotationArgs struct {
Keyword token.Token
Name token.Token
Parens token.Token
Semicolon token.Token
}
DeclAnnotationArgs is arguments for Nodes.NewDeclAnnotation.
Parameters are added after construction with DeclAnnotation.Params. Set Parens to token.Zero for the parameterless form.
type DeclAnnotationParam ¶
type DeclAnnotationParam id.Node[DeclAnnotationParam, *File, *rawDeclAnnotationParam]
DeclAnnotationParam is one parameter in a DeclAnnotation declaration.
The default value is optional:
rule: expression code: string = "" value: any
When the default value is absent, DeclAnnotationParam.Equals and DeclAnnotationParam.Default both return zero.
A default on an `expression`-typed parameter is captured the way a use-site argument is (RFC-001 §5.1): every token to the next top-level comma, recorded as DeclAnnotationParam.DefaultStart through DeclAnnotationParam.DefaultEnd. Default then holds the capture re-parsed under the value grammar, and is zero for an engine-expression fragment such as `size(this) > 0`; use DeclAnnotationParam.HasDefault to ask whether there is a default.
Grammar ¶
DeclAnnotationParam := Ident `:` Type (`=` Expr)?
func (DeclAnnotationParam) Colon ¶
func (p DeclAnnotationParam) Colon() token.Token
Colon returns the colon separating the name from the type.
May be zero, if the user forgot it.
func (DeclAnnotationParam) Default ¶
func (p DeclAnnotationParam) Default() ExprAny
Default returns the parameter's default value expression, or zero when the parameter has no default.
func (DeclAnnotationParam) DefaultEnd ¶ added in v0.36.0
func (p DeclAnnotationParam) DefaultEnd() token.Token
DefaultEnd returns the last token of an `expression`-typed parameter's captured default, or zero.
func (DeclAnnotationParam) DefaultSpan ¶ added in v0.36.0
func (p DeclAnnotationParam) DefaultSpan() source.Span
DefaultSpan returns the source span of an `expression`-typed parameter's captured default, or zero.
func (DeclAnnotationParam) DefaultStart ¶ added in v0.36.0
func (p DeclAnnotationParam) DefaultStart() token.Token
DefaultStart returns the first token of an `expression`-typed parameter's captured default, or zero.
func (DeclAnnotationParam) Equals ¶
func (p DeclAnnotationParam) Equals() token.Token
Equals returns the equals sign before the default value, or zero when the parameter has no default.
func (DeclAnnotationParam) HasDefault ¶ added in v0.36.0
func (p DeclAnnotationParam) HasDefault() bool
HasDefault reports whether the parameter declares a default value.
func (DeclAnnotationParam) Name ¶
func (p DeclAnnotationParam) Name() token.Token
Name returns the parameter's name token.
May be zero, if the user forgot it.
func (DeclAnnotationParam) SetDefault ¶
func (p DeclAnnotationParam) SetDefault(expr ExprAny)
SetDefault sets the parameter's default value expression.
If passed zero, this clears the default.
func (DeclAnnotationParam) SetType ¶
func (p DeclAnnotationParam) SetType(ty TypeAny)
SetType sets the parameter's declared type.
If passed zero, this clears the type.
func (DeclAnnotationParam) Span ¶
func (p DeclAnnotationParam) Span() source.Span
Span implements source.Spanner.
func (DeclAnnotationParam) Type ¶
func (p DeclAnnotationParam) Type() TypeAny
Type returns the parameter's declared type.
May be zero, if the user forgot it.
type DeclAnnotationParamArgs ¶
type DeclAnnotationParamArgs struct {
Name token.Token
Colon token.Token
Type TypeAny
Equals token.Token
Default ExprAny
// DefaultStart and DefaultEnd bound the captured default of an
// `expression`-typed parameter, inclusive. Leave them zero for every
// other parameter.
DefaultStart, DefaultEnd token.Token
}
DeclAnnotationParamArgs is arguments for Nodes.NewDeclAnnotationParam.
Set Equals to token.Zero and Default to a zero ExprAny when the parameter has no default value.
type DeclAnnotationUse ¶
type DeclAnnotationUse id.Node[DeclAnnotationUse, *File, *rawDeclAnnotationUse]
DeclAnnotationUse is a `@name(args)` annotation use site introduced by the protowire v1.2 schema extensions (RFC-001).
Use sites attach to other declarations as leading metadata (on block decls like messages, enums, services) or as trailing metadata (on single-line decls like type, function, annotation, and eventually field and enum-value). The owning declaration's `LeadingAnnotations()` accessor exposes the seq.
A use site may also appear as a top-level DeclAny when the parser recovers from an orphaned annotation (an annotation that did not successfully bind to a following declaration). In that case the linker is expected to diagnose it.
Grammar ¶
DeclAnnotationUse := `@` Path (`(` (Arg `,`?)* `)`)? Arg := (Ident `=`)? Capture
Three forms are accepted:
@Name — no args, no parens @Name() — empty parens @Name(arg1, name = arg2, ...) — one or more args
Arguments follow RFC-001 §5.1 capture-then-classify: each argument is captured as the raw token range extending to the next `,` or `)` at zero delimiter depth (see AnnotationUseArg). Arguments that re-parse under the value grammar `literal | qualifiedIdent | listLiteral | messageLiteral` additionally carry the parsed ExprAny; arguments that do not (engine-expression fragments) carry only the capture.
func (DeclAnnotationUse) Args ¶
func (d DeclAnnotationUse) Args() Commas[AnnotationUseArg]
Args returns the argument list for this annotation use site.
Returns an empty seq for the bare `@Name` and empty-parens `@Name()` forms.
func (DeclAnnotationUse) AsAny ¶
func (d DeclAnnotationUse) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclAnnotationUse) At ¶
func (d DeclAnnotationUse) At() token.Token
At returns the leading `@` token for this annotation use site.
May be zero, if the user forgot it.
func (DeclAnnotationUse) Name ¶
func (d DeclAnnotationUse) Name() Path
Name returns the annotation's name path. The path may be a single identifier (e.g. `@validate`) or a qualified name (e.g. `@myco.validate`).
func (DeclAnnotationUse) NewArg ¶
func (d DeclAnnotationUse) NewArg(spec AnnotationUseArgSpec) AnnotationUseArg
NewArg assembles an AnnotationUseArg for this use site from spec. The returned value is appended to the use site via DeclAnnotationUse.Args.
func (DeclAnnotationUse) Parens ¶
func (d DeclAnnotationUse) Parens() token.Token
Parens returns the fused parentheses token wrapping the argument list, or zero when the source omitted the argument list entirely (the bare `@Name` form).
func (DeclAnnotationUse) Span ¶
func (d DeclAnnotationUse) Span() source.Span
Span implements source.Spanner.
type DeclAnnotationUseArgs ¶
DeclAnnotationUseArgs is arguments for Nodes.NewDeclAnnotationUse.
Set Parens to token.Zero for the bare `@Name` form. Arguments are added after construction via DeclAnnotationUse.Args.
type DeclAny ¶
DeclAny is any Decl* type in this package.
Values of this type can be obtained by calling an AsAny method on a Decl* type, such as DeclSyntax.AsAny. It can be type-asserted back to any of the concrete Decl* types using its own As* methods.
This type is used in lieu of a putative Decl interface type to avoid heap allocations in functions that would return one of many different Decl* types.
Grammar ¶
DeclAny := DeclEmpty | DeclSyntax | DeclPackage | DeclImport | DeclDef | DeclBody | DeclRange
Note that this grammar is highly ambiguous. TODO: document the rules under which parse DeclSyntax, DeclPackage, DeclImport, and DeclRange.
func (DeclAny) AsAnnotation ¶
func (d DeclAny) AsAnnotation() DeclAnnotation
AsAnnotation converts a DeclAny into a DeclAnnotation, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsAnnotationUse ¶
func (d DeclAny) AsAnnotationUse() DeclAnnotationUse
AsAnnotationUse converts a DeclAny into a DeclAnnotationUse, if that is the declaration it contains.
A DeclAnnotationUse may appear at the top level as an orphan use site (the parser produces one when a `@name(args)` does not successfully bind to a following declaration). When a use site is correctly attached to a declaration, it lives in that declaration's [DeclX.Annotations] sequence and does not appear in DeclAny.
Otherwise, returns zero.
func (DeclAny) AsBody ¶
AsBody converts a DeclAny into a DeclBody, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsDef ¶
AsDef converts a DeclAny into a DeclDef, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsEmpty ¶
AsEmpty converts a DeclAny into a DeclEmpty, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsFunction ¶
func (d DeclAny) AsFunction() DeclFunction
AsFunction converts a DeclAny into a DeclFunction, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsImport ¶
func (d DeclAny) AsImport() DeclImport
AsImport converts a DeclAny into a DeclImport, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsPackage ¶
func (d DeclAny) AsPackage() DeclPackage
AsPackage converts a DeclAny into a DeclPackage, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsRange ¶
AsRange converts a DeclAny into a DeclRange, if that is the declaration it contains.
Otherwise, returns zero.
func (DeclAny) AsSyntax ¶
func (d DeclAny) AsSyntax() DeclSyntax
AsSyntax converts a DeclAny into a DeclSyntax, if that is the declaration it contains.
Otherwise, returns zero.
type DeclBody ¶
DeclBody is the body of a DeclBody, or the whole contents of a File. The protocompile AST is very lenient, and allows any declaration to exist anywhere, for the benefit of rich diagnostics and refactorings. For example, it is possible to represent an "orphaned" field or oneof outside of a message, or an RPC method inside of an enum, and so on.
Grammar ¶
DeclBody := `{` DeclAny* `}`
Note that a File is simply a DeclBody that is delimited by the bounds of the source file, rather than braces.
func (DeclBody) Decls ¶
Decls returns a seq.Inserter over the declarations in this body.
type DeclDef ¶
DeclDef is a general Protobuf definition.
This [Decl] represents the union of several similar AST nodes, to aid in permissive parsing and precise diagnostics.
This node represents messages, enums, services, extend blocks, fields, enum values, oneofs, groups, service methods, and options. It also permits nonsensical syntax, such as a message with a tag number.
Generally, you should not need to work with DeclDef directly; instead, use the As* methods to access the correct concrete syntax production a DeclDef represents.
Grammar ¶
DeclDef := (Type Path | Type | Ident) followers* (`;` | DeclBody)? followers := inputs | outputs | value | CompactOptions inputs := `(` (Type `,`?)* `)` outputs := `returns` (Type | inputs)? value := (`=` Expr) | ExprPath | ExprLiteral | ExprRange | ExprField
Note that this type will only record the first appearance of any follower.
func (DeclDef) Annotations ¶
func (d DeclDef) Annotations() seq.Inserter[DeclAnnotationUse]
Annotations returns this definition's annotation use sites.
For DeclDef, annotations attach as leading metadata (before the keyword and type) on block-shaped definitions like messages, enums, services, oneofs, and methods. The seq is empty unless the parser has successfully attached at least one annotation.
Trailing-annotation placement on field and enum-value definitions will land in a follow-up PR; for now those reuse the same slot.
func (DeclDef) AsEnum ¶
AsEnum extracts the fields from this definition relevant to interpreting it as an enum.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsEnumValue ¶
func (d DeclDef) AsEnumValue() DefEnumValue
AsEnumValue extracts the fields from this definition relevant to interpreting it as an enum value.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsExtend ¶
AsExtend extracts the fields from this definition relevant to interpreting it as a service.
The return value's fields may be zero if they are not present.
See DeclDef.Classify.
func (DeclDef) AsField ¶
AsField extracts the fields from this definition relevant to interpreting it as a message field.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsGroup ¶
AsGroup extracts the fields from this definition relevant to interpreting it as a group.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsMessage ¶
func (d DeclDef) AsMessage() DefMessage
AsMessage extracts the fields from this definition relevant to interpreting it as a message.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsMethod ¶
AsMethod extracts the fields from this definition relevant to interpreting it as a service method.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsOneof ¶
AsOneof extracts the fields from this definition relevant to interpreting it as a oneof.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) AsOption ¶
AsMethod extracts the fields from this definition relevant to interpreting it as an option.
The return value's fields may be zero if they are not present.
See DeclDef.Classify.
func (DeclDef) AsService ¶
func (d DeclDef) AsService() DefService
AsService extracts the fields from this definition relevant to interpreting it as a service.
The return value's fields may be zero if they are not present (in particular, Name will be zero if d.Name() is not an identifier).
See DeclDef.Classify.
func (DeclDef) Classify ¶
Classify looks at all the fields in this definition and decides what kind of definition it's supposed to represent.
To select which definition this probably is, this function looks at 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.
The correct way to use this function is as the input value for a switch. The cases of the switch should then use the As* methods, such as DeclDef.AsMessage, to extract the relevant fields.
func (DeclDef) IsCorrupt ¶
IsCorrupt reports whether or not some part of the parser decided that this definition is not interpretable as any specific kind of definition.
func (DeclDef) Keyword ¶
KeywordToken returns the introducing keyword for this definition, if there is one.
See DeclDef.Type for details on where this keyword comes from.
func (DeclDef) KeywordToken ¶
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 (DeclDef) MarkCorrupt ¶
func (d DeclDef) MarkCorrupt()
the compiler to ignore it. See DeclDef.IsCorrupt.
func (DeclDef) Options ¶
func (d DeclDef) Options() CompactOptions
Options returns the compact options list for this definition.
func (DeclDef) Prefixes ¶
func (d DeclDef) Prefixes() iter.Seq[TypePrefixed]
Prefixes returns an iterator over the modifiers on this def, expressed as TypePrefixed nodes.
func (DeclDef) Semicolon ¶
Semicolon returns the ending semicolon token for this definition. May be zero.
func (DeclDef) SetOptions ¶
func (d DeclDef) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this definition.
Setting it to a zero Options clears it.
func (DeclDef) Signature ¶
Signature returns this definition's type signature, if it has one.
Note that this is distinct from the type returned by DeclDef.Type, which is the "prefix" type for the definition (such as for a field). This is a signature for e.g. a method.
Not all defs have a signature, so this function may return a zero Signature.q If you want to add one, use DeclDef.WithSignature.
func (DeclDef) Stem ¶
Stem returns a span that contains both this definition's type and name.
For e.g. a message, this is the "message Foo" part.
func (DeclDef) Type ¶
Type returns the "prefix" type of this definition.
This type may coexist with a Signature in this definition.
May be zero, such as for enum values. For messages and other productions introduced by a special keyword, this will be a TypePath whose single identifier is that keyword.
See DeclDef.KeywordToken.
func (DeclDef) Value ¶
Value returns this definition's value. For a field, this will be the tag number, while for an option, this will be the complex expression representing its value.
func (DeclDef) WithSignature ¶
WithSignature is like Signature, but it adds an empty signature if it would return zero.
type DeclDefArgs ¶
type DeclDefArgs struct {
// If both Keyword and Type are set, Type will be prioritized.
Keyword token.Token
Type TypeAny
Name Path
// NOTE: the values for the type signature are not provided at
// construction time, and should be added by mutating through
// DeclDef.Signature.
Returns token.Token
Equals token.Token
Value ExprAny
Options CompactOptions
Body DeclBody
Semicolon token.Token
}
DeclDefArgs is arguments for creating a DeclDef with [Context.NewDeclDef].
type DeclEmpty ¶
DeclEmpty is an empty declaration, a lone ;.
Grammar ¶
DeclEmpty := `;`
func (DeclEmpty) AsAny ¶
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclEmpty) Semicolon ¶
Semicolon returns this field's ending semicolon.
May be token.Zero, if not present.
type DeclFunction ¶
type DeclFunction id.Node[DeclFunction, *File, *rawDeclFunction]
DeclFunction is a function-signature declaration introduced by the protowire v1.2 schema extensions (RFC-001).
A function declaration names a signature for a validation predicate. The body is implemented by the engine runtime; this node only carries the signature.
Grammar ¶
DeclFunction := `function` Ident `(` (DeclFunctionParam `,`?)* `)` CompactOptions? `;`?
Parameters are appended to the returned node via DeclFunction.Params.
func (DeclFunction) Annotations ¶
func (d DeclFunction) Annotations() seq.Inserter[DeclAnnotationUse]
Annotations returns this declaration's annotation use sites.
For DeclFunction, annotations attach as trailing metadata (between the closing paren and the semicolon, after any compact options). The seq is empty unless the parser has successfully attached at least one annotation.
func (DeclFunction) AsAny ¶
func (d DeclFunction) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclFunction) Keyword ¶
func (d DeclFunction) Keyword() keyword.Keyword
Keyword returns the keyword for this declaration.
func (DeclFunction) KeywordToken ¶
func (d DeclFunction) KeywordToken() token.Token
KeywordToken returns the "function" token for this declaration.
func (DeclFunction) Name ¶
func (d DeclFunction) Name() token.Token
Name returns the identifier token naming the declared function.
May be zero, if the user forgot it.
func (DeclFunction) Options ¶
func (d DeclFunction) Options() CompactOptions
Options returns the compact options list for this declaration.
func (DeclFunction) Params ¶
func (d DeclFunction) Params() Commas[DeclFunctionParam]
Params returns the parameter list for this declaration.
func (DeclFunction) Parens ¶
func (d DeclFunction) Parens() token.Token
Parens returns the fused parentheses token wrapping this declaration's parameter list.
May be zero, if the user forgot it.
func (DeclFunction) Semicolon ¶
func (d DeclFunction) Semicolon() token.Token
Semicolon returns this declaration's ending semicolon.
May be zero, if the user forgot it.
func (DeclFunction) SetOptions ¶
func (d DeclFunction) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this declaration.
Setting it to a zero Options clears it.
type DeclFunctionArgs ¶
type DeclFunctionArgs struct {
Keyword token.Token
Name token.Token
Parens token.Token
Options CompactOptions
Semicolon token.Token
}
DeclFunctionArgs is arguments for Nodes.NewDeclFunction.
Parameters are added after construction with DeclFunction.Params.
type DeclFunctionParam ¶
type DeclFunctionParam id.Node[DeclFunctionParam, *File, *rawDeclFunctionParam]
DeclFunctionParam is one parameter in a DeclFunction declaration:
value: string pattern: string msg: myco.commons.Address
Grammar ¶
DeclFunctionParam := Ident `:` Type
func (DeclFunctionParam) Colon ¶
func (p DeclFunctionParam) Colon() token.Token
Colon returns the colon separating the name from the type.
May be zero, if the user forgot it.
func (DeclFunctionParam) Name ¶
func (p DeclFunctionParam) Name() token.Token
Name returns the parameter's name token.
May be zero, if the user forgot it.
func (DeclFunctionParam) SetType ¶
func (p DeclFunctionParam) SetType(ty TypeAny)
SetType sets the parameter's declared type.
If passed zero, this clears the type.
func (DeclFunctionParam) Span ¶
func (p DeclFunctionParam) Span() source.Span
Span implements source.Spanner.
func (DeclFunctionParam) Type ¶
func (p DeclFunctionParam) Type() TypeAny
Type returns the parameter's declared type.
May be zero, if the user forgot it.
type DeclFunctionParamArgs ¶
DeclFunctionParamArgs is arguments for Nodes.NewDeclFunctionParam.
type DeclImport ¶
type DeclImport id.Node[DeclImport, *File, *rawDeclImport]
DeclImport is an import declaration within a file.
Grammar ¶
DeclImport := `import` (`weak` | `public`)? Expr? CompactOptions? `;`?
Note: options are not permitted on import declarations in Protobuf, but we parse them for diagnosis.
func (DeclImport) AsAny ¶
func (d DeclImport) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclImport) ImportPath ¶
func (d DeclImport) ImportPath() ExprAny
ImportPath returns the file path for this import as a string.
May be zero, if the user forgot it.
func (DeclImport) IsOption ¶
func (d DeclImport) IsOption() bool
IsOption checks whether this is an "import option".
func (DeclImport) IsPublic ¶
func (d DeclImport) IsPublic() bool
IsPublic checks whether this is an "import public".
func (DeclImport) IsWeak ¶
func (d DeclImport) IsWeak() bool
IsWeak checks whether this is an "import weak".
func (DeclImport) Keyword ¶
func (d DeclImport) Keyword() keyword.Keyword
Keyword returns the keyword for this declaration.
func (DeclImport) KeywordToken ¶
func (d DeclImport) KeywordToken() token.Token
KeywordToken returns the "import" keyword for this declaration.
func (DeclImport) ModifierTokens ¶
func (d DeclImport) ModifierTokens() seq.Inserter[token.Token]
ModifierTokens returns the modifier tokens for this declaration.
func (DeclImport) Modifiers ¶
func (d DeclImport) Modifiers() seq.Indexer[keyword.Keyword]
Modifiers returns the modifiers for this declaration.
func (DeclImport) Options ¶
func (d DeclImport) Options() CompactOptions
Options returns the compact options list for this declaration.
Imports cannot have options, but we parse them anyways.
func (DeclImport) Semicolon ¶
func (d DeclImport) Semicolon() token.Token
Semicolon returns this import's ending semicolon.
May be zero, if the user forgot it.
func (DeclImport) SetImportPath ¶
func (d DeclImport) SetImportPath(expr ExprAny)
SetValue sets the expression for this import's file path.
If passed zero, this clears the path expression.
func (DeclImport) SetOptions ¶
func (d DeclImport) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this declaration.
Setting it to a zero Options clears it.
func (DeclImport) Span ¶
func (d DeclImport) Span() source.Span
source.Span implements source.Spanner.
type DeclImportArgs ¶
type DeclImportArgs struct {
Keyword token.Token
Modifiers []token.Token
ImportPath ExprAny
Options CompactOptions
Semicolon token.Token
}
DeclImportArgs is arguments for [Context.NewDeclImport].
type DeclKind ¶
type DeclKind int8
DeclKind is a kind of declaration. There is one value of DeclKind for each Decl* type in this package.
func (DeclKind) DecodeDynID ¶
type DeclPackage ¶
type DeclPackage id.Node[DeclPackage, *File, *rawDeclPackage]
DeclPackage is the package declaration for a file.
Grammar ¶
DeclPackage := `package` Path? CompactOptions? `;`?
Note: options are not permitted on package declarations in Protobuf, but we parse them for diagnosis.
func (DeclPackage) AsAny ¶
func (d DeclPackage) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclPackage) Keyword ¶
func (d DeclPackage) Keyword() keyword.Keyword
Keyword returns the keyword for this declaration.
func (DeclPackage) KeywordToken ¶
func (d DeclPackage) KeywordToken() token.Token
KeywordToken returns the "package" token for this declaration.
func (DeclPackage) Options ¶
func (d DeclPackage) Options() CompactOptions
Options returns the compact options list for this declaration.
Package declarations cannot have options, but we parse them anyways.
func (DeclPackage) Path ¶
func (d DeclPackage) Path() Path
Path returns this package's path.
May be zero, if the user wrote something like package;.
func (DeclPackage) Semicolon ¶
func (d DeclPackage) Semicolon() token.Token
Semicolon returns this package's ending semicolon.
May be zero, if the user forgot it.
func (DeclPackage) SetOptions ¶
func (d DeclPackage) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this declaration.
Setting it to a zero Options clears it.
func (DeclPackage) Span ¶
func (d DeclPackage) Span() source.Span
source.Span implements source.Spanner.
type DeclPackageArgs ¶
type DeclPackageArgs struct {
Keyword token.Token
Path Path
Options CompactOptions
Semicolon token.Token
}
DeclPackageArgs is arguments for [Context.NewDeclPackage].
type DeclRange ¶
DeclRange represents an extension or reserved range declaration. They are almost identical syntactically so they use the same AST node.
Grammar ¶
DeclRange := (`extensions` | `reserved`) (Expr `,`)* Expr? CompactOptions? `;`?
func (DeclRange) AsAny ¶
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclRange) IsExtensions ¶
IsExtensions checks whether this is an extension range.
func (DeclRange) IsReserved ¶
IsReserved checks whether this is a reserved range.
func (DeclRange) KeywordToken ¶
KeywordToken returns the keyword token for this range.
func (DeclRange) Options ¶
func (d DeclRange) Options() CompactOptions
Options returns the compact options list for this range.
func (DeclRange) Ranges ¶
Ranges returns the sequence of expressions denoting the ranges in this range declaration.
func (DeclRange) Semicolon ¶
Semicolon returns this range's ending semicolon.
May be nil, if not present.
func (DeclRange) SetOptions ¶
func (d DeclRange) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this definition.
Setting it to a nil Options clears it.
type DeclRangeArgs ¶
type DeclRangeArgs struct {
Keyword token.Token
Options CompactOptions
Semicolon token.Token
}
DeclRangeArgs is arguments for [Context.NewDeclRange].
type DeclSyntax ¶
type DeclSyntax id.Node[DeclSyntax, *File, *rawDeclSyntax]
DeclSyntax represents a language declaration, such as the syntax or edition keywords.
Grammar ¶
DeclSyntax := (`syntax` | `edition`) (`=`? Expr)? CompactOptions? `;`?
Note: options are not permitted on syntax declarations in Protobuf, but we parse them for diagnosis.
func (DeclSyntax) AsAny ¶
func (d DeclSyntax) AsAny() DeclAny
AsAny type-erases this declaration value.
See DeclAny for more information.
func (DeclSyntax) Equals ¶
func (d DeclSyntax) Equals() token.Token
Equals returns the equals sign after the keyword.
May be zero, if the user wrote something like syntax "proto2";.
func (DeclSyntax) IsEdition ¶
func (d DeclSyntax) IsEdition() bool
IsEdition checks whether this is a new-style edition declaration.
func (DeclSyntax) IsSyntax ¶
func (d DeclSyntax) IsSyntax() bool
IsSyntax checks whether this is an OG syntax declaration.
func (DeclSyntax) Keyword ¶
func (d DeclSyntax) Keyword() keyword.Keyword
Keyword returns the keyword for this declaration.
func (DeclSyntax) KeywordToken ¶
func (d DeclSyntax) KeywordToken() token.Token
KeywordToken returns the keyword token for this declaration.
func (DeclSyntax) Options ¶
func (d DeclSyntax) Options() CompactOptions
Options returns the compact options list for this declaration.
Syntax declarations cannot have options, but we parse them anyways.
func (DeclSyntax) Semicolon ¶
func (d DeclSyntax) Semicolon() token.Token
Semicolon returns this declaration's ending semicolon.
May be zero, if the user forgot it.
func (DeclSyntax) SetOptions ¶
func (d DeclSyntax) SetOptions(opts CompactOptions)
SetOptions sets the compact options list for this declaration.
Setting it to a zero Options clears it.
func (DeclSyntax) SetValue ¶
func (d DeclSyntax) SetValue(expr ExprAny)
SetValue sets the expression for this declaration's value.
If passed zero, this clears the value (e.g., for syntax = ;).
func (DeclSyntax) Span ¶
func (d DeclSyntax) Span() source.Span
source.Span implements source.Spanner.
func (DeclSyntax) Value ¶
func (d DeclSyntax) Value() ExprAny
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;.
type DeclSyntaxArgs ¶
type DeclSyntaxArgs struct {
// Must be "syntax" or "edition".
Keyword token.Token
Equals token.Token
Value ExprAny
Options CompactOptions
Semicolon token.Token
}
DeclSyntaxArgs is arguments for [Context.NewDeclSyntax].
type DeclType ¶
DeclType is a type-alias declaration introduced by the protowire v1.2 schema extensions (RFC-001).
Grammar ¶
DeclType := `type` Ident `=` Expr `;`?
The value expression is the base type the alias refines. In well-formed input it is a path expression naming a Protobuf scalar or another declared type; the parser accepts the broader ExprAny grammar so the linker can issue precise diagnostics on malformed input.
func (DeclType) Annotations ¶
func (d DeclType) Annotations() seq.Inserter[DeclAnnotationUse]
Annotations returns this declaration's annotation use sites.
For DeclType, annotations attach as trailing metadata (between the value and the semicolon). The seq is empty unless the parser has successfully attached at least one annotation.
func (DeclType) Equals ¶
Equals returns the equals sign between the name and the value.
May be zero, if the user forgot it.
func (DeclType) KeywordToken ¶
KeywordToken returns the "type" token for this declaration.
func (DeclType) Name ¶
Name returns the identifier token naming the declared type.
May be zero, if the user forgot it.
func (DeclType) Semicolon ¶
Semicolon returns this declaration's ending semicolon.
May be zero, if the user forgot it.
type DeclTypeArgs ¶
type DeclTypeArgs struct {
Keyword token.Token
Name token.Token
Equals token.Token
Value ExprAny
Semicolon token.Token
}
DeclTypeArgs is arguments for Nodes.NewDeclType.
type Def ¶
Def is the return type of DeclDef.Classify.
This interface is implemented by all the Def* types in this package, and can be type-asserted to any of them, usually in a type switch.
A DeclDef can't be mutated through a Def; instead, you will need to mutate the general structure instead.
type DefEnumValue ¶
type DefEnumValue struct {
Name token.Token
Equals token.Token
Tag ExprAny
Options CompactOptions
Semicolon token.Token
Decl DeclDef
}
DefEnumValue is a DeclDef projected into an enum value definition.
See DeclDef.Classify.
func (DefEnumValue) Context ¶
func (d DefEnumValue) Context() *File
func (DefEnumValue) Span ¶
func (d DefEnumValue) Span() source.Span
type DefExtend ¶
DefExtend is a DeclDef projected into an extension definition.
See DeclDef.Classify.
type DefField ¶
type DefField struct {
Type TypeAny
Name token.Token
Equals token.Token
Tag ExprAny
Options CompactOptions
Semicolon token.Token
Decl DeclDef
}
DefField is a DeclDef projected into a field definition.
See DeclDef.Classify.
type DefGroup ¶
type DefGroup struct {
Keyword token.Token
Name token.Token
Equals token.Token
Tag ExprAny
Options CompactOptions
Body DeclBody
Decl DeclDef
}
DefGroup is a DeclDef projected into a group definition.
See DeclDef.Classify.
type DefKind ¶
type DefKind int8
DefKind is the kind of definition a DeclDef contains.
See DeclDef.Classify.
type DefMessage ¶
DefMessage is a DeclDef projected into a message definition.
See DeclDef.Classify.
func (DefMessage) Context ¶
func (d DefMessage) Context() *File
func (DefMessage) Span ¶
func (d DefMessage) Span() source.Span
type DefMethod ¶
type DefMethod struct {
Keyword token.Token
Name token.Token
Signature Signature
Body DeclBody
Decl DeclDef
}
DefMethod is a DeclDef projected into a method definition.
See DeclDef.Classify.
type DefOption ¶
DefOption is a DeclDef projected into a method definition.
Yes, an option is technically not defining anything, just setting a value. However, it's syntactically analogous to a definition!
See DeclDef.Classify.
type DefService ¶
DefService is a DeclDef projected into a service definition.
See DeclDef.Classify.
func (DefService) Context ¶
func (d DefService) Context() *File
func (DefService) Span ¶
func (d DefService) Span() source.Span
type ExprAny ¶
ExprAny is any ExprAny* type in this package.
Values of this type can be obtained by calling an AsAny method on a ExprAny* type, such as ExprPath.AsAny. It can be type-asserted back to any of the concrete ExprAny* types using its own As* methods.
This type is used in lieu of a putative ExprAny interface type to avoid heap allocations in functions that would return one of many different ExprAny* types.
Grammar ¶
In addition to the Expr type, we define some exported productions for handling operator precedence.
Expr := ExprField | ExprOp ExprJuxta := ExprFieldWithColon | ExprOp ExprOp := ExprRange | ExprPrefix | ExprSolo ExprSolo := ExprLiteral | ExprPath | ExprArray | ExprDict
Note: ExprJuxta is the expression production that is unambiguous when expressions are juxtaposed with each other; i.e., ExprJuxta* does not make e.g. "foo {}" ambiguous between an ExprField or an ExprPath followed by an ExprDict.
func (ExprAny) AsArray ¶
AsArray converts a ExprAny into a ExprArray, if that is the type it contains.
Otherwise, returns zero.
func (ExprAny) AsDict ¶
AsDict converts a ExprAny into a ExprDict, if that is the type it contains.
Otherwise, returns zero.
func (ExprAny) AsError ¶
AsError converts a ExprAny into a ExprError, if that is the type it contains.
Otherwise, returns nil.
func (ExprAny) AsField ¶
AsField converts a ExprAny into a ExprKV, if that is the type it contains.
Otherwise, returns zero.
func (ExprAny) AsLiteral ¶
func (e ExprAny) AsLiteral() ExprLiteral
AsLiteral converts a ExprAny into a ExprLiteral, if that is the type it contains.
Otherwise, returns zero.
func (ExprAny) AsPath ¶
AsPath converts a ExprAny into a ExprPath, if that is the type it contains.
Otherwise, returns zero.
func (ExprAny) AsPrefixed ¶
func (e ExprAny) AsPrefixed() ExprPrefixed
AsPrefixed converts a ExprAny into a ExprPrefixed, if that is the type it contains.
Otherwise, returns zero.
type ExprArray ¶
ExprArray represents an array of expressions between square brackets.
Grammar ¶
ExprArray := `[` (ExprJuxta `,`?)*`]`
func (ExprArray) Brackets ¶
Brackets returns the token tree corresponding to the whole [...].
May be missing for a synthetic expression.
type ExprDict ¶
ExprDict represents a an array of message fields between curly braces.
Grammar ¶
ExprDict := `{` fields `}` | `<` fields `>`
fields := (Expr (`,` | `;`)?)*
Note that if a non-ExprField occurs as a field of a dict, the parser will rewrite it into an ExprField with a missing key.
func (ExprDict) Braces ¶
Braces returns the token tree corresponding to the whole {...}.
May be missing for a synthetic expression.
func (ExprDict) TypeName ¶ added in v0.22.0
TypeName returns the leading type name of a typed message literal (the `Money` of `Money{currency: "USD"}`).
Only the RFC-001 §5.1 annotation-argument classifier attaches type names, wherever the literal is written typed — at argument level (where DeclAnnotationUse arguments also expose it as AnnotationUseArg.MessageType) and in list-element position, where this accessor is the only carrier. Zero for dicts from every other grammar position (standard option values, text format).
type ExprError ¶
ExprError represents an unrecoverable parsing error in an expression context.
type ExprField ¶
ExprField is a key-value pair within an ExprDict.
It implements ExprAny, since it can appear inside of e.g. an array if the user incorrectly writes [foo: bar].
Grammar ¶
ExprField := ExprFieldWithColon | Expr (ExprDict | ExprArray) ExprFieldWithColon := Expr (`:` | `=`) Expr
Note: ExprFieldWithColon appears in ExprJuxta, the expression production that is unambiguous when expressions are juxtaposed with each other.
func (ExprField) Colon ¶
Colon returns the colon between Key() and Value().
May be zero: it is valid for a field name to be immediately followed by its value and be syntactically valid (unlike most "optional" punctuation, this is permitted by Protobuf, not just our permissive AST).
func (ExprField) Key ¶
Key returns the key for this field.
May be zero if the parser encounters a message expression with a missing field, e.g. {foo, bar: baz}.
type ExprFieldArgs ¶
ExprFieldArgs is arguments for [Context.NewExprKV].
type ExprKind ¶
type ExprKind int8
ExprKind is a kind of expression. There is one value of ExprKind for each Expr* type in this package.
func (ExprKind) DecodeDynID ¶
type ExprLiteral ¶
type ExprLiteral struct {
File *File
// The token backing this expression. Must be [token.String] or [token.Number].
token.Token
}
ExprLiteral is an expression corresponding to a string or number literal.
Grammar ¶
ExprLiteral := token.Number | token.String
func (ExprLiteral) AsAny ¶
func (e ExprLiteral) AsAny() ExprAny
AsAny type-erases this type value.
See TypeAny for more information.
func (ExprLiteral) Context ¶
func (e ExprLiteral) Context() *File
Context returns this literal's context.
This returns a File rather than a token.Stream, which would otherwise be returned because ExprLiteral embeds token.Token.
type ExprPath ¶
type ExprPath struct {
// The path backing this expression.
Path
}
ExprPath is a simple path reference in expression position.
Grammar ¶
ExprPath := Path
type ExprPrefixed ¶
type ExprPrefixed id.Node[ExprPrefixed, *File, *rawExprPrefixed]
ExprPrefixed is an expression prefixed with an operator.
Grammar ¶
ExprPrefix := `-` ExprSolo
func (ExprPrefixed) AsAny ¶
func (e ExprPrefixed) AsAny() ExprAny
AsAny type-erases this expression value.
See ExprAny for more information.
func (ExprPrefixed) Expr ¶
func (e ExprPrefixed) Expr() ExprAny
Expr returns the expression the prefix is applied to.
func (ExprPrefixed) Prefix ¶
func (e ExprPrefixed) Prefix() keyword.Keyword
Prefix returns this expression's prefix.
Returns keyword.Unknown if TypePrefixed.PrefixToken does not contain a known prefix.
func (ExprPrefixed) PrefixToken ¶
func (e ExprPrefixed) PrefixToken() token.Token
PrefixToken returns the token representing this expression's prefix.
func (ExprPrefixed) SetExpr ¶
func (e ExprPrefixed) SetExpr(expr ExprAny)
SetExpr sets the expression that the prefix is applied to.
If passed zero, this clears the expression.
func (ExprPrefixed) Span ¶
func (e ExprPrefixed) Span() source.Span
source.Span implements source.Spanner.
type ExprPrefixedArgs ¶
ExprPrefixedArgs is arguments for [Context.NewExprPrefixed].
type ExprRange ¶
ExprRange represents a range of values, such as 1 to 4 or 5 to max.
Note that max is not special syntax; it will appear as an ExprPath with the name "max".
Grammar ¶
ExprRange := ExprPrefixed `to` ExprOp
type ExprRangeArgs ¶
ExprRangeArgs is arguments for [Context.NewExprRange].
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is the top-level AST node for a Protobuf file.
A file is a list of declarations (in other words, it is a DeclBody). The File type provides convenience functions for extracting salient elements, such as the DeclSyntax and the DeclPackage.
Grammar ¶
File := DeclAny*
func New ¶
New creates a fresh context for a file.
path is the semantic import path of this file, which may not be the same as file.Path, which is used for diagnostics.
func (*File) Imports ¶
func (f *File) Imports() iter.Seq[DeclImport]
Imports returns an iterator over this file's import declarations.
func (*File) Nodes ¶
Nodes returns the node arena for this file, which can be used to allocate new AST nodes.
func (*File) Package ¶
func (f *File) Package() DeclPackage
Package returns this file's package declaration, if it has one.
func (*File) Syntax ¶
func (f *File) Syntax() DeclSyntax
Syntax returns this file's declaration, if it has one.
type Nodes ¶
type Nodes File
Nodes provides storage for the various AST node types, and can be used to construct new ones.
func (*Nodes) NewCompactOptions ¶
func (n *Nodes) NewCompactOptions(brackets token.Token) CompactOptions
NewCompactOptions creates a new CompactOptions node.
func (*Nodes) NewDeclAnnotation ¶
func (n *Nodes) NewDeclAnnotation(args DeclAnnotationArgs) DeclAnnotation
NewDeclAnnotation creates a new DeclAnnotation node.
To add parameters to the returned declaration, use DeclAnnotation.Params. Set args.Parens to token.Zero for the parameterless form (`annotation Name;`).
func (*Nodes) NewDeclAnnotationParam ¶
func (n *Nodes) NewDeclAnnotationParam(args DeclAnnotationParamArgs) DeclAnnotationParam
NewDeclAnnotationParam creates a new DeclAnnotationParam node.
Set args.Equals to token.Zero and args.Default to a zero ExprAny when the parameter has no default value.
func (*Nodes) NewDeclAnnotationUse ¶
func (n *Nodes) NewDeclAnnotationUse(args DeclAnnotationUseArgs) DeclAnnotationUse
NewDeclAnnotationUse creates a new DeclAnnotationUse node.
Set args.Parens to token.Zero for the bare `@Name` form. Arguments are added after construction via DeclAnnotationUse.Args.
func (*Nodes) NewDeclBody ¶
NewDeclBody creates a new DeclBody node.
To add declarations to the returned body, use [DeclBody.Append].
func (*Nodes) NewDeclDef ¶
func (n *Nodes) NewDeclDef(args DeclDefArgs) DeclDef
NewDeclDef creates a new DeclDef node.
func (*Nodes) NewDeclEmpty ¶
NewDeclEmpty creates a new DeclEmpty node.
func (*Nodes) NewDeclFunction ¶
func (n *Nodes) NewDeclFunction(args DeclFunctionArgs) DeclFunction
NewDeclFunction creates a new DeclFunction node.
To add parameters to the returned declaration, use DeclFunction.Params.
func (*Nodes) NewDeclFunctionParam ¶
func (n *Nodes) NewDeclFunctionParam(args DeclFunctionParamArgs) DeclFunctionParam
NewDeclFunctionParam creates a new DeclFunctionParam node.
func (*Nodes) NewDeclImport ¶
func (n *Nodes) NewDeclImport(args DeclImportArgs) DeclImport
NewDeclImport creates a new DeclImport node.
func (*Nodes) NewDeclPackage ¶
func (n *Nodes) NewDeclPackage(args DeclPackageArgs) DeclPackage
NewDeclPackage creates a new DeclPackage node.
func (*Nodes) NewDeclRange ¶
func (n *Nodes) NewDeclRange(args DeclRangeArgs) DeclRange
NewDeclRange creates a new DeclRange node.
To add ranges to the returned declaration, use [DeclRange.Append].
func (*Nodes) NewDeclSyntax ¶
func (n *Nodes) NewDeclSyntax(args DeclSyntaxArgs) DeclSyntax
NewDeclSyntax creates a new DeclSyntax node.
func (*Nodes) NewDeclType ¶
func (n *Nodes) NewDeclType(args DeclTypeArgs) DeclType
NewDeclType creates a new DeclType node.
func (*Nodes) NewExprArray ¶
NewExprArray creates a new ExprArray node.
To add elements to the returned expression, use [ExprArray.Append].
func (*Nodes) NewExprDict ¶
NewExprDict creates a new ExprDict node.
To add elements to the returned expression, use [ExprDict.Append].
func (*Nodes) NewExprField ¶
func (n *Nodes) NewExprField(args ExprFieldArgs) ExprField
NewExprField creates a new ExprPrefixed node.
func (*Nodes) NewExprPrefixed ¶
func (n *Nodes) NewExprPrefixed(args ExprPrefixedArgs) ExprPrefixed
NewExprPrefixed creates a new ExprPrefixed node.
func (*Nodes) NewExprRange ¶
func (n *Nodes) NewExprRange(args ExprRangeArgs) ExprRange
NewExprRange creates a new ExprRange node.
func (*Nodes) NewExtensionComponent ¶
func (n *Nodes) NewExtensionComponent(separator token.Token, path Path) PathComponent
NewExtensionComponent returns a new extension path component containing the given path.
func (*Nodes) NewPath ¶
func (n *Nodes) NewPath(components ...PathComponent) Path
NewPath creates a new synthetic Path.
func (*Nodes) NewPathComponent ¶
func (n *Nodes) NewPathComponent(separator, name token.Token) PathComponent
NewPathComponent returns a new path component with the given separator and name.
sep must be a token.Keyword whose value is either '.' or '/'. name must be a token.Ident. This function will panic if either condition does not hold.
To create a path component with an extension value, see Nodes.NewExtensionComponent.
func (*Nodes) NewTypeGeneric ¶
func (n *Nodes) NewTypeGeneric(args TypeGenericArgs) TypeGeneric
NewTypeGeneric creates a new TypeGeneric node.
To add arguments to the returned type, use [TypeGeneric.Append].
func (*Nodes) NewTypePrefixed ¶
func (n *Nodes) NewTypePrefixed(args TypePrefixedArgs) TypePrefixed
NewTypePrefixed creates a new TypePrefixed node.
func (*Nodes) NewTypedExprDict ¶ added in v0.22.0
NewTypedExprDict creates a new ExprDict node carrying the leading type name of a typed message literal (`Money{...}`) — see ExprDict.TypeName. typeName may be zero.
To add elements to the returned expression, use [ExprDict.Append].
type Option ¶
Option is a key-value pair inside of a CompactOptions or a DefOption.
type Path ¶
type Path struct {
// contains filtered or unexported fields
}
Path represents a multi-part identifier.
This includes single identifiers like foo, references like foo.bar, and fully-qualified names like .foo.bar.
Grammar ¶
Path := `.`? component (sep component)* component := token.Ident | `(` Path `)` sep := `.` | `/`
func (Path) AsIdent ¶
AsIdent returns the single identifier that comprises this path, or the zero token.
func (Path) AsKeyword ¶
AsKeyword returns the keyword.Keyword that this path represents.
If this path does not represent a builtin, returns keyword.Unknown.
func (Path) AsPredeclared ¶
func (p Path) AsPredeclared() predeclared.Name
AsPredeclared returns the predeclared.Name that this path represents.
If this path does not represent a builtin, returns predeclared.Unknown.
func (Path) Canonicalized ¶
Canonicalized returns a string containing this path's value after canonicalization.
Canonicalization converts a path into something that can be used for name resolution. This includes removing extra separators and deleting whitespace and comments.
func (Path) Components ¶
func (p Path) Components() iter.Seq[PathComponent]
Components is an iter.Seq that ranges over each component in this path. Specifically, it yields the (possibly zero) dot that precedes the component, and the identifier token.
func (Path) IsSynthetic ¶
IsSynthetic returns whether this path was created with Nodes.NewPath.
func (Path) Split ¶
Split splits a path at the given path component index, producing two new paths where the first contains the first n components and the second contains the rest. If n is negative or greater than the number of components in p, both returned paths will be zero.
The suffix will be absolute, except in the following cases: 1. n == 0 and p is not absolute (prefix will be zero and suffix will be p). 2. n is equal to the length of p (suffix will be zero and prefix will be p).
This operation runs in O(n) time.
func (Path) ToRelative ¶
ToRelative converts this path into a relative path, by deleting all leading separators. In particular, the path "..foo", which contains empty components, will be converted into "foo".
If called on zero or a relative path, returns p.
type PathComponent ¶
type PathComponent struct {
// contains filtered or unexported fields
}
PathComponent is a piece of a path. This is either an identifier or a nested path (for an extension name).
func (PathComponent) AsExtension ¶
func (p PathComponent) AsExtension() Path
AsExtension returns the Path inside of this path component, if it is an extension path component, i.e. (a.b.c).
This is unrelated to the foo.bar/my.Type URL-like Any paths that appear in some expressions. Those are represented by allowing / as an alternative separator to . in paths.
func (PathComponent) AsIdent ¶
func (p PathComponent) AsIdent() token.Token
AsIdent returns the single identifier that makes up this path component, if it is not an extension path component.
May be zero, in the case of e.g. the second component of foo..bar.
func (PathComponent) IsEmpty ¶
func (p PathComponent) IsEmpty() bool
Returns whether this is an empty path component. Such components are not allowed in the grammar but may occur in invalid inputs nonetheless.
func (PathComponent) IsFirst ¶
func (p PathComponent) IsFirst() bool
IsFirst returns whether this is the first component of its path.
func (PathComponent) IsLast ¶
func (p PathComponent) IsLast() bool
IsLast returns whether this is the last component of its path.
func (PathComponent) Name ¶
func (p PathComponent) Name() token.Token
Name is the token that represents this component's name. THis is either an identifier or a (...) token containing a path.
func (PathComponent) Next ¶
func (p PathComponent) Next() PathComponent
Next returns the next path component after this one, if there is one.
func (PathComponent) Path ¶
func (p PathComponent) Path() Path
Path returns the path that this component is part of.
func (PathComponent) Separator ¶
func (p PathComponent) Separator() token.Token
Separator is the token that separates this component from the previous one, if any. This may be a dot or a slash.
func (PathComponent) Span ¶
func (p PathComponent) Span() source.Span
Span implements source.Spanner.
func (PathComponent) SplitAfter ¶
func (p PathComponent) SplitAfter() (before, after Path)
SplitAfter splits the path that this component came from around the component boundary after this component.
before's last component will be this component.
func (PathComponent) SplitBefore ¶
func (p PathComponent) SplitBefore() (before, after Path)
SplitBefore splits the path that this component came from around the component boundary before this component.
after's first component will be this component.
Not currently implemented for synthetic paths.
type PathID ¶
type PathID struct {
// contains filtered or unexported fields
}
PathID identifies a Path in a [Context].
type Signature ¶
type Signature struct {
// contains filtered or unexported fields
}
Signature is a type signature of the form (types) returns (types).
Signatures may have multiple inputs and outputs.
type TypeAny ¶
TypeAny is any Type* type in this package.
Values of this type can be obtained by calling an AsAny method on a Type* type, such as TypePath.AsAny. It can be type-asserted back to any of the concrete Type* types using its own As* methods.
This type is used in lieu of a putative Type interface type to avoid heap allocations in functions that would return one of many different Type* types.
Grammar ¶
Type := TypePath | TypePrefixed | TypeGeneric
Note that parsing a type cannot always be greedy. Consider that, if parsed as a type, "optional optional foo" could be parsed as:
TypePrefix{Optional, TypePrefix{Optional, TypePath("foo")}}
However, if we want to parse a type followed by a Path, it needs to parse as follows:
TypePrefix{Optional, TypePath("optional")}, Path("foo")
Thus, parsing a type is greedy except when the containing production contains "Type Path?" or similar, in which case parsing must be greedy up to the last Path it would otherwise consume.
func (TypeAny) AsError ¶
AsError converts a TypeAny into a TypeError, if that is the type it contains.
Otherwise, returns nil.
func (TypeAny) AsGeneric ¶
func (t TypeAny) AsGeneric() TypeGeneric
AsGeneric converts a TypeAny into a TypePrefix, if that is the type it contains.
Otherwise, returns zero.
func (TypeAny) AsPath ¶
AsPath converts a TypeAny into a TypePath, if that is the type it contains.
Otherwise, returns zero.
func (TypeAny) AsPrefixed ¶
func (t TypeAny) AsPrefixed() TypePrefixed
AsPrefixed converts a TypeAny into a TypePrefix, if that is the type it contains.
Otherwise, returns zero.
func (TypeAny) Prefixes ¶
func (t TypeAny) Prefixes() iter.Seq[TypePrefixed]
Prefixes is an iterator over all [TypePrefix]es wrapping this type.
func (TypeAny) RemovePrefixes ¶
RemovePrefixes removes all [TypePrefix] values wrapping this type.
type TypeError ¶
TypeError represents an unrecoverable parsing error in a type context.
This type is so named to adhere to package ast's naming convention. It does not represent a "type error" as in "type-checking failure".
type TypeGeneric ¶
type TypeGeneric id.Node[TypeGeneric, *File, *rawTypeGeneric]
TypeGeneric is a type with generic arguments.
Protobuf does not have generics... mostly. It has the map<K, V> production, which looks like something that generalizes, but doesn't. It is useful to parse when users mistakenly think this generalizes or provide the incorrect number of arguments.
You will usually want to immediately call [TypeGeneric.Map] to codify the assumption that all generic types understood by your code are maps.
TypeGeneric implements [Commas[TypeAny]] for accessing its arguments.
Grammar ¶
TypeGeneric := TypePath `<` (Type `,`?`)* `>`
func (TypeGeneric) Args ¶
func (t TypeGeneric) Args() TypeList
Args returns the argument list for this generic type.
func (TypeGeneric) AsAny ¶
func (t TypeGeneric) AsAny() TypeAny
AsAny type-erases this type value.
See TypeAny for more information.
func (TypeGeneric) AsMap ¶
func (t TypeGeneric) AsMap() (key, value TypeAny)
AsMap extracts the key/value types out of this generic type, checking that it's actually a map<K, V>. This is intended for asserting the extremely common case of "the only generic type is map".
Returns zeros if this is not a map, or it has the wrong number of generic arguments.
func (TypeGeneric) Path ¶
func (t TypeGeneric) Path() Path
Path returns the path of the "type constructor". For example, for my.Map<K, V>, this would return the path my.Map.
type TypeGenericArgs ¶
TypeGenericArgs is the arguments for [Context.NewTypeGeneric].
Generic arguments should be added after construction with [TypeGeneric.AppendComma].
type TypeKind ¶
type TypeKind int8
TypeKind is a kind of type. There is one value of TypeKind for each Type* type in this package.
func (TypeKind) DecodeDynID ¶
type TypeList ¶
type TypeList struct {
// contains filtered or unexported fields
}
TypeList is a Commas over a list of types surrounded by some kind of brackets.
Despite the name, TypeList does not implement TypeAny because it is not a type.
func (TypeList) AppendComma ¶
AppendComma implements Commas.
func (TypeList) Brackets ¶
Brackets returns the token tree for the brackets wrapping the argument list.
May be zero, if the user forgot to include brackets.
func (TypeList) Insert ¶
Insert implements seq.Inserter.
func (TypeList) InsertComma ¶
InsertComma implements Commas.
func (TypeList) SetBrackets ¶
SetBrackets sets the token tree for the brackets wrapping the argument list.
type TypePath ¶
type TypePath struct {
// The path that refers to this type.
Path
}
TypePath is a simple path reference as a type.
Grammar ¶
TypePath := Path
type TypePrefixed ¶
type TypePrefixed id.Node[TypePrefixed, *File, *rawTypePrefixed]
TypePrefixed is a type with a [TypePrefix].
Unlike in ordinary Protobuf, the Protocompile AST permits arbitrary nesting of modifiers.
Grammar ¶
TypePrefixed := (`optional` | `repeated` | `required` | `stream`) Type
Note that there are ambiguities when Type is an absolute TypePath. The source "optional .foo" names the type "optional.foo" only when inside of a TypeGeneric's brackets or a Signature's method parameters.
Also, the `stream` prefix may only occur inside of a Signature.
func (TypePrefixed) AsAny ¶
func (t TypePrefixed) AsAny() TypeAny
AsAny type-erases this type value.
See TypeAny for more information.
func (TypePrefixed) Prefix ¶
func (t TypePrefixed) Prefix() keyword.Keyword
Prefix extracts the modifier out of this type.
Returns keyword.Unknown if TypePrefixed.PrefixToken does not contain a known prefix.
func (TypePrefixed) PrefixToken ¶
func (t TypePrefixed) PrefixToken() token.Token
PrefixToken returns the token representing this type's prefix.
func (TypePrefixed) SetType ¶
func (t TypePrefixed) SetType(ty TypeAny)
SetType sets the expression that is being prefixed.
If passed zero, this clears the type.
func (TypePrefixed) Type ¶
func (t TypePrefixed) Type() TypeAny
Type returns the type that is being prefixed.
type TypePrefixedArgs ¶
TypePrefixedArgs is the arguments for [Context.NewTypePrefixed].
Source Files
¶
- commas.go
- context.go
- decl.go
- decl_annotation.go
- decl_annotation_use.go
- decl_body.go
- decl_def.go
- decl_empty.go
- decl_file.go
- decl_function.go
- decl_range.go
- decl_type.go
- doc.go
- enums.go
- expr.go
- expr_array.go
- expr_dict.go
- expr_literal.go
- expr_prefixed.go
- expr_range.go
- nodes.go
- options.go
- path.go
- type.go
- type_generic.go
- type_prefixed.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package edit applies declaration-level mutations to a Protobuf AST before it is rendered.
|
Package edit applies declaration-level mutations to a Protobuf AST before it is rendered. |
|
Package predeclared provides all of the identifiers with a special meaning in Protobuf.
|
Package predeclared provides all of the identifiers with a special meaning in Protobuf. |
|
Package printer renders an ast.File (or an individual ast.DeclAny) back to protobuf source text.
|
Package printer renders an ast.File (or an individual ast.DeclAny) back to protobuf source text. |
|
Package syntax specifies all of the syntax pragmas (including editions) that Protocompile understands.
|
Package syntax specifies all of the syntax pragmas (including editions) that Protocompile understands. |