parser

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package parser builds the syntax tree of an Ant module. It implements sections 2, 5, 6, and 10 of the specification with one token of lookahead and no feedback from the checker.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTestFile

func IsTestFile(path string) bool

IsTestFile reports whether path names a test file, whose name ends in _test.ant (section 10, Tests).

func Parse

func Parse(path, src string, mode Mode) (f *ast.File, err error)

Parse parses one module. path only labels the result.

func ParseExpr

func ParseExpr(src string) (e ast.Expr, err error)

ParseExpr parses a single expression, in spec mode.

func ParseRecover

func ParseRecover(path, src string) (*ast.File, *Layout, []*Error)

ParseRecover parses one module in the mode of its path (ModeOf) as ParseLayout does, but goes on past its syntax errors, for an editor. It returns the tree of what parsed, with a BadStmt, BadExpr, or BadType standing where a statement, an expression, or a type could not be read, the layout, and every error in source order: the lexer's, then the parser's as it met them. The error Parse reports is among them, and the rest are those Parse would report once the earlier ones were fixed, as far as the parser can tell. A Bad token, which the lexer has reported, ends a statement for the parser, as a line break would, so that it is reported once. The tree is for analysis only and must not be run.

Types

type Error

type Error struct {
	Pos        ast.Pos
	Msg        string
	Incomplete bool
	// Help says how to fix the error, when the message does not.
	Help string
}

Error is a syntax error. Incomplete is set when the input ended too early, which a REPL uses to keep reading.

func (*Error) Error

func (e *Error) Error() string

type Layout

type Layout struct {
	Tokens   []token.Token
	Comments []token.Comment
	Roles    map[int]Role
	// Prefix holds the offsets of the - and ! tokens the parser read as prefix operators.
	Prefix map[int]bool
}

Layout is what the formatter needs beyond the tree: the tokens with their spans, the comments, and the role of every opening bracket, keyed by its byte offset.

func ParseLayout

func ParseLayout(path, src string) (f *ast.File, l *Layout, err error)

ParseLayout parses one module, as Parse does in the mode of its path (ModeOf), and also returns its Layout.

type Mode

type Mode int

Mode selects what a source may contain at the top level.

const (
	// Program is a module: imports followed by declarations and statements (section 10).
	Program Mode = iota
	// Spec also allows top-level statements and "..." placeholders, for the specification's
	// own examples.
	Spec
	// Session also allows top-level statements, for a REPL.
	Session
	// Declarations is a declaration file (section 10, Declaration files): declarations only,
	// each qualified by its owner, a func without a body, a type without one, and a var as a
	// typed constant; the prelude and a host namespace's signatures.
	Declarations
)

func ModeOf

func ModeOf(path string) Mode

ModeOf is the mode a file's path chooses: Declarations for a declaration file, whose name ends in .d.ant, and Program otherwise.

type Role

type Role struct {
	Block bool // a block or a lambda body, in which newlines separate statements
	Typed bool // the { of a record or map literal, directly after its type
	Comma bool // a list that allows a comma after its last item
}

Role is what an opening bracket is, as the parser decided while parsing it.

Jump to

Keyboard shortcuts

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