parser

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

G++ extension nodes and entry points. This file is gpp's own (not vendored from GOROOT): the fork parses G++'s two grammar extensions — enum declarations and match statements — into the side-table types below, leaving stock placeholder nodes (*ast.BadExpr / *ast.BadStmt) in the *ast.File so every downstream go/ast consumer keeps working.

G++ grammar hooks. This file is gpp's own (not vendored). The vendored parser calls into it from five marked hunks; everything else lives here.

Package parser implements a parser for Go source files.

The ParseFile function reads file input from a string, []byte, or io.Reader, and produces an ast.File representing the complete abstract syntax tree of the file.

The ParseExprFrom function reads a single source-level expression and produces an ast.Expr, the syntax tree of the expression.

The parser accepts a larger language than is syntactically permitted by the Go spec, for simplicity, and for improved robustness in the presence of syntax errors. For instance, in method declarations, the receiver is treated like an ordinary parameter list and thus may contain multiple entries where the spec permits exactly one. Consequently, the corresponding field in the AST (ast.FuncDecl.Recv) field is not restricted to one entry.

Applications that need to parse one or more complete packages of Go source code may find it more convenient not to interact directly with the parser but instead to use the Load function in package golang.org/x/tools/go/packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDir deprecated

func ParseDir(fset *token.FileSet, path string, filter func(fs.FileInfo) bool, mode Mode) (pkgs map[string]*ast.Package, first error)

ParseDir calls ParseFile for all files with names ending in ".go" in the directory specified by path and returns a map of package name -> package AST with all the packages found.

If filter != nil, only the files with fs.FileInfo entries passing through the filter (and ending in ".go") are considered. The mode bits are passed to ParseFile unchanged. Position information is recorded in fset, which must not be nil.

If the directory couldn't be read, a nil map and the respective error are returned. If a parse error occurred, a non-nil but incomplete map and the first error encountered are returned.

Deprecated: ParseDir does not consider build tags when associating files with packages. For precise information about the relationship between packages and files, use golang.org/x/tools/go/packages, which can also optionally parse and type-check the files too.

func ParseExpr

func ParseExpr(x string) (ast.Expr, error)

ParseExpr is a convenience function for obtaining the AST of an expression x. The position information recorded in the AST is undefined. The filename used in error messages is the empty string.

If syntax errors were found, the result is a partial AST (with ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseExprFrom

func ParseExprFrom(fset *token.FileSet, filename string, src any, mode Mode) (expr ast.Expr, err error)

ParseExprFrom is a convenience function for parsing an expression. The arguments have the same meaning as for ParseFile, but the source must be a valid Go (type or value) expression. Specifically, fset must not be nil.

If the source couldn't be read, the returned AST is nil and the error indicates the specific failure. If the source was read but syntax errors were found, the result is a partial AST (with ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

func ParseFile

func ParseFile(fset *token.FileSet, filename string, src any, mode Mode) (f *ast.File, err error)

ParseFile parses the source code of a single Go source file and returns the corresponding ast.File node. The source code may be provided via the filename of the source file, or via the src parameter.

If src != nil, ParseFile parses the source from src and the filename is only used when recording position information. The type of the argument for the src parameter must be string, []byte, or io.Reader. If src == nil, ParseFile parses the file specified by filename.

The mode parameter controls the amount of source text parsed and other optional parser functionality. If the SkipObjectResolution mode bit is set (recommended), the object resolution phase of parsing will be skipped, causing File.Scope, File.Unresolved, and all Ident.Obj fields to be nil. Those fields are deprecated; see ast.Object for details.

Position information is recorded in the file set fset, which must not be nil.

If the source couldn't be read, the returned AST is nil and the error indicates the specific failure. If the source was read but syntax errors were found, the result is a partial AST (with ast.Bad* nodes representing the fragments of erroneous source code). Multiple errors are returned via a scanner.ErrorList which is sorted by source position.

Types

type CaseClause

type CaseClause struct {
	Case    token.Pos
	Binder  *ast.Ident // c in `case c := Circle(r):`; nil if absent
	Define  token.Pos  // position of ":="; NoPos if absent
	Pattern Pattern    // WildcardPattern for `case _:`
	Alts    []Pattern  // additional alternatives of a multi-pattern arm (v0.12.0); nil otherwise
	Colon   token.Pos
	Body    []ast.Stmt // stock statements; nested matches appear as *ast.BadStmt
}

CaseClause is one `case [binder :=] pattern:` arm.

type ClassDecl added in v0.5.0

type ClassDecl struct {
	Gen      *ast.GenDecl  // enclosing declaration; filled by syntax.ParseFile
	Spec     *ast.TypeSpec // Name/TypeParams are real; Spec.Type is an *ast.BadExpr spanning the class body
	ClassPos token.Pos     // position of the `class` keyword
	Lbrace   token.Pos
	Members  []*ClassMember
	Rbrace   token.Pos
}

ClassDecl is one `type Name[T any] class { … }` declaration (v0.5.0).

type ClassMember added in v0.5.0

type ClassMember struct {
	Doc     *ast.CommentGroup
	LawPos  token.Pos      // position of `law`; NoPos for ops and embeds
	Embed   ast.Expr       // Semigroup[T] / pkg.Semigroup[T]; nil for ops/laws
	Name    *ast.Ident     // op or law name; nil for embeds
	Params  *ast.FieldList // ops/laws; nil for embeds
	Result  ast.Expr       // op result type; nil for void ops and laws
	Body    *ast.BlockStmt // law: required; op: optional default; embed: nil
	Comment *ast.CommentGroup
}

ClassMember is one embed, operation, or law inside a class body. Exactly one of Embed / Name is set: embeds carry only Embed; ops and laws carry Name (+ Params, and for ops an optional Result and optional default Body; laws always have a Body and an implicit bool result).

type ComposeExpr added in v0.3.0

type ComposeExpr struct {
	Bad   *ast.BadExpr
	Fns   []ast.Expr    // len >= 2; operands may be *ast.BadExpr
	OpPos []token.Pos   // first char of each operator; len == len(Fns)-1
	Ops   []ComposeKind // operator of each link; len == len(Fns)-1
}

ComposeExpr is one `f >>> g >=> h` chain (left-associative, flattened; the two operators share a precedence level and mix freely).

type ComposeKind added in v0.4.0

type ComposeKind int

ComposeKind discriminates the operator of one composition link.

const (
	ComposeFn      ComposeKind = iota // >>> — one-track composition
	ComposeKleisli                    // >=> — railway (Kleisli) composition
)

type ConstructorPattern

type ConstructorPattern struct {
	Name   ast.Expr // *ast.Ident or *ast.SelectorExpr (qualified)
	Lparen token.Pos
	Args   []Pattern
	Rparen token.Pos
}

ConstructorPattern is `Name(args…)` or a bare `Name`. With Lparen == token.NoPos it is a bare name: a nullary constructor or a field binder — the parser does not decide; resolution does (a constructor name shadows binding).

func (*ConstructorPattern) End

func (p *ConstructorPattern) End() token.Pos

func (*ConstructorPattern) Pos

func (p *ConstructorPattern) Pos() token.Pos

type DelegateField added in v0.6.0

type DelegateField struct {
	Field       *ast.Field
	DelegatePos token.Pos
}

DelegateField is one struct field marked with the trailing `delegate` contextual keyword (v0.6.0): the outer type gains generated forwarders for the field's interface methods.

type EnumDecl

type EnumDecl struct {
	Gen      *ast.GenDecl  // enclosing declaration; filled by syntax.ParseFile
	Spec     *ast.TypeSpec // Name/TypeParams are real; Spec.Type is an *ast.BadExpr spanning the enum body
	EnumPos  token.Pos     // position of the `enum` keyword
	Lbrace   token.Pos
	Variants []*Variant
	Rbrace   token.Pos
}

EnumDecl is one `type Name [TypeParams] enum { … }` declaration.

type Extensions

type Extensions struct {
	Enums    []*EnumDecl
	Matches  []*MatchStmt // pre-order; includes matches nested inside arms
	Pipes    []*PipeExpr
	Composes []*ComposeExpr
	// v0.4.0 — creation order; resolve placeholders by pointer.
	Tries       []*TryExpr
	IfExprs     []*IfExpr // roots only; else-if links via ElseIf
	SwitchExprs []*SwitchExpr
	MatchExprs  []*MatchExpr
	// v0.5.0 — source order.
	Classes   []*ClassDecl
	Instances []*InstanceDecl
	// v0.6.0 — source order.
	Delegates []*DelegateField
	// v0.7.0 — source order.
	Quantities []*QuantityParam
	Totals     []*TotalFunc
}

Extensions collects a file's G++ constructs.

Pipes and Composes are in CREATION order: a node registers when its first operator token is claimed, so extensions nested in stages/right operands follow their encloser, while extensions nested in the head/left operand precede it. Downstream phases must resolve placeholders by pointer (File.PipeFor / File.ComposeFor), never by slice position.

func ParseFileExt

func ParseFileExt(fset *token.FileSet, filename string, src []byte, mode Mode) (*ast.File, *Extensions, error)

ParseFileExt parses G++ source: stock Go grammar plus enum declarations, match statements, and type parameters on methods.

type IfExpr added in v0.4.0

type IfExpr struct {
	Bad        *ast.BadExpr // nil on else-if links
	If         token.Pos
	Cond       ast.Expr
	Lbrace     token.Pos
	Then       ast.Expr
	Rbrace     token.Pos
	ElsePos    token.Pos
	ElseIf     *IfExpr   // `else if …`; nil if braced else
	ElseLbrace token.Pos // braced else only
	Else       ast.Expr  // braced else only; nil when ElseIf != nil
	ElseRbrace token.Pos
}

IfExpr is one `if cond { e } else …` expression (v0.4.0). Only the root of an else-if chain carries Bad and registers in Extensions.IfExprs; else-if links hang off ElseIf with Bad == nil.

type InstanceDecl added in v0.5.0

type InstanceDecl struct {
	Decl        *ast.BadDecl // placeholder occupying this instance's slot in File.Decls
	Doc         *ast.CommentGroup
	InstancePos token.Pos
	Name        *ast.Ident
	TParams     *ast.FieldList // generic instances (SliceConcat[T any]); nil otherwise
	Class       ast.Expr       // IndexExpr/IndexListExpr over Ident or SelectorExpr
	Lbrace      token.Pos
	Members     []*InstanceMember
	Rbrace      token.Pos
}

InstanceDecl is one top-level `instance Name [TParams] Class[Args] { … }` declaration (v0.5.0).

type InstanceMember added in v0.5.0

type InstanceMember struct {
	Doc     *ast.CommentGroup
	Name    *ast.Ident
	Params  *ast.FieldList
	Result  ast.Expr       // nil for void ops
	Body    *ast.BlockStmt // required
	Comment *ast.CommentGroup
}

InstanceMember is one operation implementation inside an instance body.

type MatchExpr added in v0.4.0

type MatchExpr struct {
	Bad     *ast.BadExpr
	Match   token.Pos
	Subject ast.Expr
	Lbrace  token.Pos
	Arms    []*MatchExprArm
	Rbrace  token.Pos
}

MatchExpr is one `match subject { case pattern: expr … }` expression.

type MatchExprArm added in v0.4.0

type MatchExprArm struct {
	Case    token.Pos
	Binder  *ast.Ident // nil if absent
	Define  token.Pos  // NoPos if absent
	Pattern Pattern
	Colon   token.Pos
	Value   ast.Expr
}

MatchExprArm is one `case [binder :=] pattern: expr` arm.

type MatchStmt

type MatchStmt struct {
	Stmt    *ast.BadStmt // placeholder occupying this match's slot in the enclosing block
	Match   token.Pos    // position of the `match` keyword
	Subject ast.Expr
	Lbrace  token.Pos
	Cases   []*CaseClause
	Rbrace  token.Pos
}

MatchStmt is one `match subject { case … }` statement.

type Mode

type Mode uint

A Mode value is a set of flags (or 0). They control the amount of source code parsed and other optional parser functionality.

const (
	PackageClauseOnly    Mode             = 1 << iota // stop parsing after package clause
	ImportsOnly                                       // stop parsing after import declarations
	ParseComments                                     // parse comments and add them to AST
	Trace                                             // print a trace of parsed productions
	DeclarationErrors                                 // report declaration errors
	SpuriousErrors                                    // same as AllErrors, for backward-compatibility
	SkipObjectResolution                              // skip deprecated identifier resolution; see ParseFile
	AllErrors            = SpuriousErrors             // report all errors (not just the first 10 on different lines)
)

type Pattern

type Pattern interface {
	Pos() token.Pos
	End() token.Pos
	// contains filtered or unexported methods
}

Pattern is a match pattern: wildcard or (possibly nested) constructor.

type PipeExpr added in v0.3.0

type PipeExpr struct {
	Bad    *ast.BadExpr // placeholder occupying the pipeline's span
	Head   ast.Expr     // real expression; may itself be a *ast.BadExpr
	Stages []*PipeStage // one per |>, source order; len >= 1
}

PipeExpr is one `head |> stage |> stage` pipeline expression (v0.3.0).

type PipeStage added in v0.3.0

type PipeStage struct {
	OpPos token.Pos // position of `|` in `|>`
	Dot   token.Pos // position of `.` for a dot-segment; NoPos otherwise
	// Expr is a stock expression. Dot-segment: rooted at the post-dot
	// name, with arbitrary selector/call/index suffixes (`.A().B(c)`).
	// Plain segment: any expression; nested pipes/composes appear as
	// *ast.BadExpr resolvable via the File lookup maps.
	Expr ast.Expr
}

PipeStage is one `|> segment`.

type QuantityParam added in v0.7.0

type QuantityParam struct {
	Quantity string     // "0", "1", or a multiplicity variable name
	QPos     token.Pos  // start of the quantity token
	Name     *ast.Ident // the parameter name the quantity annotates
}

QuantityParam is one parameter carrying a QTT quantity prefix (v0.7.0): `0 n int` (erased), `1 f *os.File` (linear), or `m x T` where m names a multiplicity type parameter. The prefix spans [QPos, Name.Pos) in the source and is stripped by lowering.

type SwitchExpr added in v0.4.0

type SwitchExpr struct {
	Bad    *ast.BadExpr
	Switch token.Pos
	Tag    ast.Expr // nil for a tag-less switch
	Lbrace token.Pos
	Arms   []*SwitchExprArm
	Rbrace token.Pos
}

SwitchExpr is one `switch [tag] { case …: e … }` expression (v0.4.0).

type SwitchExprArm added in v0.4.0

type SwitchExprArm struct {
	Case   token.Pos  // position of `case` or `default`
	Values []ast.Expr // nil ⇒ default arm
	Colon  token.Pos
	Value  ast.Expr
}

SwitchExprArm is one `case v1, v2: expr` or `default: expr` arm.

type TotalFunc added in v0.7.0

type TotalFunc struct {
	Decl     *ast.FuncDecl
	TotalPos token.Pos // position of the `total` keyword
}

TotalFunc is one `total func` declaration (v0.7.0): the function is checked for structural termination and becomes callable in types.

type TryExpr added in v0.4.0

type TryExpr struct {
	Bad  *ast.BadExpr // placeholder spanning X.Pos()..QPos+1
	X    ast.Expr     // the fallible expression; may itself be a placeholder
	QPos token.Pos    // position of '?'
}

TryExpr is one postfix `expr?` failure-propagation suffix (v0.4.0).

type Variant

type Variant struct {
	Doc     *ast.CommentGroup
	Name    *ast.Ident
	TParams *ast.FieldList // bounded existential type parameters (v0.6.0); nil otherwise
	Params  *ast.FieldList // nil for a bare variant (Point); (…) may be empty
	Result  ast.Expr       // GADT result type; nil ⇒ enum applied to its own type parameters
	Comment *ast.CommentGroup
}

Variant is one constructor declaration inside an enum body.

type WildcardPattern

type WildcardPattern struct{ UnderscorePos token.Pos }

WildcardPattern is `_`.

func (*WildcardPattern) End

func (p *WildcardPattern) End() token.Pos

func (*WildcardPattern) Pos

func (p *WildcardPattern) Pos() token.Pos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL