d2parser

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MPL-2.0 Imports: 16 Imported by: ? modules, 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

Index

Constants

View Source
const MaxNestingDepth = 128

MaxNestingDepth is the maximum number of nested maps and arrays accepted by the parser.

Variables

This section is empty.

Functions

func Errorf

func Errorf(n d2ast.Node, f string, v ...interface{}) error

func Parse

func Parse(path string, r io.Reader, opts *ParseOptions) (*d2ast.Map, error)

Parse parses a .d2 Map in r.

The returned Map always represents a valid .d2 file. All encountered errors will be in []error.

The map may be compiled via Compile even if there are errors to keep language tooling operational. Though autoformat should not run.

If UTF16Pos is true, positions will be recorded in UTF-16 codeunits as required by LSP and browser clients. See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocuments TODO: update godocs

func ParseContext added in v0.10.0

func ParseContext(ctx context.Context, path string, r io.Reader, opts *ParseOptions) (*d2ast.Map, error)

ParseContext parses a .d2 Map in r and stops promptly when ctx is canceled.

func ParseKey

func ParseKey(key string) (*d2ast.KeyPath, error)

ParseKey parses a single key path.

func ParseMapKey

func ParseMapKey(mapKey string) (*d2ast.Key, error)

ParseMapKey parses one map entry, including its optional value.

func ParseValue

func ParseValue(value string) (d2ast.Value, error)

ParseValue parses a single scalar, map, array or import.

Types

type ParseError

type ParseError struct {
	// Errors from globs need to be deduplicated
	ErrorsLookup map[d2ast.Error]struct{} `json:"-"`
	Errors       []d2ast.Error            `json:"errs"`
}

TODO: rename to Error and make existing Error a private type errorWithRange

func (*ParseError) Empty

func (pe *ParseError) Empty() bool

func (*ParseError) Error

func (pe *ParseError) Error() string

type ParseOptions

type ParseOptions struct {
	// UTF16Pos would be used with input received from a browser where the browser will send the text as UTF-8 but
	// JavaScript keeps strings in memory as UTF-16 and so needs UTF-16 indexes into the text to line up errors correctly.
	// So you want to read UTF-8 still but adjust the indexes to pretend the input is utf16.
	UTF16Pos bool

	ParseError *ParseError
}

Jump to

Keyboard shortcuts

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