Help us shape the future of Go! Take the 2026 Go Developer Survey

parser

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package parser reads a Containerfile (a Dockerfile: same syntax) into a list of instructions, following the grammar of the Dockerfile reference and of BuildKit's frontend/dockerfile/parser: parser directives (syntax, escape, check) before anything else, comments, line continuation with the escape token, and per-instruction argument forms (JSON array or shell string, whitespace lists, key=value pairs, ONBUILD sub-instructions, HEALTHCHECK types). It does no variable expansion: that is package expand's, at build time.

BuildKit's parser itself is not imported: it would add 34 packages and 6.1 MB to a js/wasm build (measured in docs/REFERENCES.md), and the builder runs in the browser playground too.

The input is untrusted: its size, line length and instruction count are bounded.

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimits = Limits{MaxBytes: 1 << 20, MaxLineBytes: 256 << 10, MaxInstructions: 4096}

DefaultLimits are generous for any real Containerfile.

View Source
var ErrTooLarge = errors.New("containerfile too large")

ErrTooLarge reports input beyond the limits.

Functions

This section is empty.

Types

type Error

type Error struct {
	Line int
	Err  error
}

Error locates a parse error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Limits

type Limits struct {
	MaxBytes        int64 // whole file
	MaxLineBytes    int   // one physical line
	MaxInstructions int
}

Limits bound what Parse accepts.

type Node

type Node struct {
	// Cmd is the instruction, lowercased ("from", "run", ...).
	Cmd string
	// Original is the logical line as written (continuations joined).
	Original string
	// Flags are the leading --name[=value] words, unexpanded.
	Flags []string
	// Args are the arguments, in BuildKit's AST order: a JSON array's
	// elements; a shell-form string as one element; whitespace-separated
	// words; for ENV and LABEL, triplets key, value, separator ("=" or ""
	// for the legacy "ENV key value" form); for ARG, the raw words; for
	// HEALTHCHECK, the type then its command.
	Args []string
	// JSON reports the exec (JSON array) form.
	JSON bool
	// Sub is ONBUILD's instruction.
	Sub *Node
	// StartLine and EndLine are 1-based.
	StartLine, EndLine int
}

Node is one instruction.

func (*Node) Dump

func (n *Node) Dump() string

Dump renders a node as BuildKit's parser test results do, so its golden files apply unchanged.

type Result

type Result struct {
	Nodes []*Node
	// Escape is the escape token: '\\' or '`'.
	Escape rune
	// Directives found at the top ("syntax", "escape", "check").
	Directives map[string]string
	// Warnings are non-fatal findings (empty continuation lines).
	Warnings []string
}

Result is a parsed file.

func Parse

func Parse(r io.Reader, lim Limits) (*Result, error)

Parse reads a Containerfile.

func (*Result) Dump

func (r *Result) Dump() string

Dump renders a whole file, one "(instruction ...)" per line.

Jump to

Keyboard shortcuts

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