Documentation
¶
Overview ¶
Package source owns immutable physical Go source units and their lexical reconstruction data.
Index ¶
- Constants
- Variables
- func ReadAll(reader io.Reader) ([]byte, error)
- func ReadFile(path string) ([]byte, error)
- func ValidateEquivalent(before, after *File) error
- func ValidateFragmentEquivalent(before, after *Fragment) error
- func ValidateSize(size int64) error
- type Comment
- type CommentID
- type Digest
- type Directive
- type DirectiveKind
- type File
- func (f *File) Bytes() []byte
- func (f *File) CanFormat() bool
- func (f *File) Comments() []Comment
- func (f *File) Digest() Digest
- func (f *File) Directives() []Directive
- func (f *File) Metadata() Metadata
- func (f *File) Path() string
- func (f *File) PhysicalOffset(position token.Pos) (int, bool)
- func (f *File) Pieces() []Piece
- func (f *File) Position(offset int) (Position, bool)
- func (f *File) PreviousSignificantToken(position token.Pos) (Token, bool)
- func (f *File) RawToken(position token.Pos) (string, bool)
- func (f *File) ReadSyntax(read func(*ast.File) error) error
- func (f *File) ReadSyntaxView(read func(*token.FileSet, *ast.File) error) error
- func (f *File) Slice(sourceRange Range) (string, bool)
- func (f *File) TokenRangeAtOffset(offset int) (Range, bool)
- func (f *File) Tokens() []Token
- func (f *File) Trivia() []Trivia
- type Fragment
- func (f *Fragment) Bytes() []byte
- func (f *Fragment) CanFormat() bool
- func (f *Fragment) Comments() []Comment
- func (f *Fragment) Digest() Digest
- func (f *Fragment) Directives() []Directive
- func (f *Fragment) Kind() FragmentKind
- func (f *Fragment) Metadata() Metadata
- func (f *Fragment) Path() string
- func (f *Fragment) PhysicalOffset(position token.Pos) (int, bool)
- func (f *Fragment) Pieces() []Piece
- func (f *Fragment) RawToken(position token.Pos) (string, bool)
- func (f *Fragment) ReadSyntax(read func(FragmentSyntax) error) error
- func (f *Fragment) Slice(sourceRange Range) (string, bool)
- func (f *Fragment) Tokens() []Token
- func (f *Fragment) Trivia() []Trivia
- type FragmentError
- type FragmentKind
- type FragmentSyntax
- type Metadata
- type NewlineStyle
- type Piece
- type PieceKind
- type Position
- type Range
- type SemicolonKind
- type Token
- type Trivia
- type TriviaKind
Constants ¶
const MaxFileSize int64 = 64 << 20
MaxFileSize is the largest complete file or physical fragment accepted by the source frontend.
Variables ¶
var ErrTooLarge = errors.New("Go source is too large")
ErrTooLarge reports source input that exceeds MaxFileSize.
Functions ¶
func ValidateEquivalent ¶
ValidateEquivalent verifies the normalized syntax and source-accounting invariants required before formatted output can be accepted.
func ValidateFragmentEquivalent ¶
ValidateFragmentEquivalent verifies normalized syntax and physical source-accounting invariants for two fragments of the same kind.
func ValidateSize ¶
ValidateSize rejects a complete source file or physical fragment that exceeds the shared frontend limit.
Types ¶
type Directive ¶
type Directive struct {
Kind DirectiveKind
Range Range
Raw string
}
Directive is a classified physical source comment.
type DirectiveKind ¶
type DirectiveKind uint8
DirectiveKind identifies comments whose placement has tool or language semantics.
const ( DirectiveBuildConstraint DirectiveKind = iota + 1 DirectiveGoGenerate DirectiveGoEmbed DirectiveCompiler DirectiveLine DirectiveGenerated DirectiveGoxSuppression DirectiveExternalSuppression DirectiveCgoPreamble )
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is an immutable physical source unit.
func Load ¶
Load constructs a physical source unit. Invalid Go returns the lossless diagnostic-only File together with the parse or scan error.
func (*File) CanFormat ¶
CanFormat reports whether parsing and physical reconstruction accepted the complete file.
func (*File) Directives ¶
Directives returns classified directive comments in physical order.
func (*File) PhysicalOffset ¶
PhysicalOffset maps a parsed position to the exact source byte offset.
func (*File) PreviousSignificantToken ¶
PreviousSignificantToken returns the lexical token immediately before a parsed position, ignoring comments and inserted semicolons.
func (*File) ReadSyntax ¶
ReadSyntax provides an isolated parsed syntax view to a run-owned consumer.
func (*File) ReadSyntaxView ¶
ReadSyntaxView provides one isolated syntax tree and its matching file set.
func (*File) TokenRangeAtOffset ¶
TokenRangeAtOffset returns the exact lexical token beginning at a physical byte offset without exposing the immutable token ledger.
type Fragment ¶
type Fragment struct {
// contains filtered or unexported fields
}
Fragment is an immutable physical source fragment parsed through a fixed synthetic Go file. Synthetic wrapper bytes never enter its physical ledger.
func LoadFragment ¶
func LoadFragment(path string, kind FragmentKind, input []byte) (*Fragment, error)
LoadFragment constructs a physical source fragment using the fixed wrapper for kind. Invalid fragments retain their physical ledger for diagnostics.
func (*Fragment) CanFormat ¶
CanFormat reports whether parsing and physical reconstruction accepted the selected fragment boundary.
func (*Fragment) Comments ¶
Comments returns exact physical comments with stable fragment identities.
func (*Fragment) Directives ¶
Directives returns classified physical comment directives.
func (*Fragment) Kind ¶
func (f *Fragment) Kind() FragmentKind
Kind returns the explicit grammar boundary used to parse the fragment.
func (*Fragment) Path ¶
Path returns the normalized physical source identity supplied to LoadFragment.
func (*Fragment) PhysicalOffset ¶
PhysicalOffset maps a selected synthetic AST position to the exact fragment byte offset. Wrapper positions are deliberately unmappable.
func (*Fragment) RawToken ¶
RawToken returns the exact physical token spelling at a synthetic AST position owned by the fragment.
func (*Fragment) ReadSyntax ¶
func (f *Fragment) ReadSyntax(read func(FragmentSyntax) error) error
ReadSyntax provides an isolated selected AST boundary to a run-owned consumer.
type FragmentError ¶
FragmentError reports one syntax failure at a physical fragment byte offset.
func (*FragmentError) Error ¶
func (e *FragmentError) Error() string
type FragmentKind ¶
type FragmentKind uint8
FragmentKind identifies one explicitly selected standard-input grammar boundary.
const ( FragmentDeclaration FragmentKind = iota + 1 FragmentStatement FragmentExpression )
type FragmentSyntax ¶
FragmentSyntax is the single AST boundary selected from a synthetic parse. The field corresponding to the fragment kind is selected; an empty declaration or statement list may be nil.
type Metadata ¶
type Metadata struct {
HasBOM bool
Newlines NewlineStyle
FinalNewline bool
Generated bool
}
Metadata describes physical file properties relevant to formatting.
type NewlineStyle ¶
type NewlineStyle uint8
NewlineStyle is the physical line-ending policy observed in a source file.
const ( NewlineNone NewlineStyle = iota NewlineLF NewlineCRLF NewlineMixed )
type PieceKind ¶
type PieceKind uint8
PieceKind identifies the role of a physical reconstruction segment.
type SemicolonKind ¶
type SemicolonKind uint8
SemicolonKind records whether a semicolon was present in source or inserted by the Go scanner.
const ( SemicolonNone SemicolonKind = iota SemicolonExplicit SemicolonInserted )
type Token ¶
type Token struct {
Kind token.Token
Range Range
Raw string
Semicolon SemicolonKind
}
Token is one physical lexical token.
type Trivia ¶
type Trivia struct {
Kind TriviaKind
Range Range
Raw string
}
Trivia is one exact physical token gap or accepted byte-order mark.
type TriviaKind ¶
type TriviaKind uint8
TriviaKind identifies physical bytes between lexical tokens.
const ( TriviaWhitespace TriviaKind = iota TriviaBOM )