Documentation
¶
Overview ¶
d2ast implements the d2 language's abstract syntax tree.
Special characters to think about in parser:
# """ ; [] {} | $ ' " \ : . -- <> * & ()
Index ¶
- Variables
- type Array
- type ArrayNode
- type ArrayNodeBox
- type BlockComment
- type BlockString
- type Boolean
- type Comment
- type DoubleQuotedString
- type Edge
- type EdgeIndex
- type Error
- type InterpolationBox
- type Key
- type KeyPath
- type Map
- type MapNode
- type MapNodeBox
- type Node
- type Null
- type Number
- type Position
- func (p Position) Advance(r rune, byUTF16 bool) Position
- func (p *Position) From(src *Position)
- func (p Position) MarshalText() ([]byte, error)
- func (p Position) String() string
- func (p Position) Subtract(r rune, byUTF16 bool) Position
- func (p Position) SubtractString(s string, byUTF16 bool) Position
- func (p *Position) UnmarshalText(b []byte) (err error)
- type Range
- type Scalar
- type ScalarBox
- type SingleQuotedString
- type String
- type StringBox
- type Substitution
- type UnquotedString
- type Value
- type ValueBox
Constants ¶
This section is empty.
Variables ¶
var UnquotedKeySpecials = string([]rune{'#', ';', '\n', '\\', '{', '}', '[', ']', '\'', '"', '|', ':', '.', '-', '<', '>', '*', '&', '(', ')'})
& is only special if it begins a key. - is only special if followed by another - in a key. ' " and | are only special if they begin an unquoted key or value.
var UnquotedValueSpecials = string([]rune{'#', ';', '\n', '\\', '{', '}', '[', ']', '\'', '"', '|', '$'})
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct { Range Range `json:"range"` Nodes []ArrayNodeBox `json:"nodes"` }
type ArrayNode ¶
type ArrayNode interface { Node // contains filtered or unexported methods }
ArrayNode is implemented by nodes that may be children of Arrays.
type ArrayNodeBox ¶
type ArrayNodeBox struct { Comment *Comment `json:"comment,omitempty"` BlockComment *BlockComment `json:"block_comment,omitempty"` Substitution *Substitution `json:"substitution,omitempty"` Null *Null `json:"null,omitempty"` Boolean *Boolean `json:"boolean,omitempty"` Number *Number `json:"number,omitempty"` UnquotedString *UnquotedString `json:"unquoted_string,omitempty"` DoubleQuotedString *DoubleQuotedString `json:"double_quoted_string,omitempty"` SingleQuotedString *SingleQuotedString `json:"single_quoted_string,omitempty"` BlockString *BlockString `json:"block_string,omitempty"` Array *Array `json:"array,omitempty"` Map *Map `json:"map,omitempty"` }
ArrayNodeBox is used to box ArrayNode for JSON persistence.
func (ArrayNodeBox) Unbox ¶
func (ab ArrayNodeBox) Unbox() ArrayNode
type BlockComment ¶
func (*BlockComment) GetRange ¶
func (c *BlockComment) GetRange() Range
func (*BlockComment) Type ¶
func (c *BlockComment) Type() string
type BlockString ¶
type BlockString struct { Range Range `json:"range"` // Quote contains the pipe delimiter for the block string. // e.g. if 5 pipes were used to begin a block string, then Quote == "||||". // The tag is not included. Quote string `json:"quote"` Tag string `json:"tag"` Value string `json:"value"` }
func (*BlockString) Copy ¶
func (s *BlockString) Copy() String
func (*BlockString) GetRange ¶
func (s *BlockString) GetRange() Range
func (*BlockString) ScalarString ¶
func (s *BlockString) ScalarString() string
func (*BlockString) SetString ¶
func (s *BlockString) SetString(s2 string)
func (*BlockString) Type ¶
func (s *BlockString) Type() string
type Boolean ¶
func (*Boolean) ScalarString ¶
type DoubleQuotedString ¶
type DoubleQuotedString struct { Range Range `json:"range"` Value []InterpolationBox `json:"value"` }
func FlatDoubleQuotedString ¶
func FlatDoubleQuotedString(s string) *DoubleQuotedString
func (*DoubleQuotedString) Copy ¶
func (s *DoubleQuotedString) Copy() String
func (*DoubleQuotedString) GetRange ¶
func (s *DoubleQuotedString) GetRange() Range
func (*DoubleQuotedString) ScalarString ¶
func (s *DoubleQuotedString) ScalarString() string
func (*DoubleQuotedString) SetString ¶
func (s *DoubleQuotedString) SetString(s2 string)
func (*DoubleQuotedString) Type ¶
func (s *DoubleQuotedString) Type() string
type Edge ¶
type EdgeIndex ¶
type Error ¶
TODO: Right now this is here to be available in both the Parser and Compiler but eventually we should make this a real part of the AST so that autofmt works on files with parse errors and semantically it makes more sense. Compile would continue to maintain a separate set of errors and then we'd do a merge & sort to get the final list of errors for user display.
type InterpolationBox ¶
type InterpolationBox struct { String *string `json:"string,omitempty"` StringRaw *string `json:"raw_string,omitempty"` Substitution *Substitution `json:"substitution,omitempty"` }
InterpolationBox is used to select between strings and substitutions in unquoted and double quoted strings. There is no corresponding interface to avoid unnecessary abstraction.
type Key ¶
type Key struct { Range Range `json:"range"` // Indicates this MapKey is an override selector. Ampersand bool `json:"ampersand,omitempty"` // At least one of Key and Edges will be set but all four can also be set. // The following are all valid MapKeys: // Key: // x // Edges: // x -> y // Edges and EdgeIndex: // (x -> y)[*] // Edges and EdgeKey: // (x -> y).label // Key and Edges: // container.(x -> y) // Key, Edges and EdgeKey: // container.(x -> y -> z).label // Key, Edges, EdgeIndex EdgeKey: // container.(x -> y -> z)[4].label Key *KeyPath `json:"key,omitempty"` Edges []*Edge `json:"edges,omitempty"` EdgeIndex *EdgeIndex `json:"edge_index,omitempty"` EdgeKey *KeyPath `json:"edge_key,omitempty"` Primary ScalarBox `json:"primary,omitempty"` Value ValueBox `json:"value"` }
TODO: require @ on import values for readability
type Map ¶
type Map struct { Range Range `json:"range"` Nodes []MapNodeBox `json:"nodes"` }
func (*Map) InsertAfter ¶
func (*Map) InsertBefore ¶
type MapNode ¶
type MapNode interface { Node // contains filtered or unexported methods }
MapNode is implemented by nodes that may be children of Maps.
type MapNodeBox ¶
type MapNodeBox struct { Comment *Comment `json:"comment,omitempty"` BlockComment *BlockComment `json:"block_comment,omitempty"` Substitution *Substitution `json:"substitution,omitempty"` MapKey *Key `json:"map_key,omitempty"` }
MapNodeBox is used to box MapNode for JSON persistence.
func MakeMapNodeBox ¶
func MakeMapNodeBox(n MapNode) MapNodeBox
func (MapNodeBox) Unbox ¶
func (mb MapNodeBox) Unbox() MapNode
type Node ¶
type Node interface { // Type returns the user friendly name of the node. Type() string // GetRange returns the range a node occupies in its file. GetRange() Range // contains filtered or unexported methods }
Node is the base interface implemented by all d2 AST nodes. TODO: add error node for autofmt of incomplete AST
type Number ¶
type Number struct { Range Range `json:"range"` Raw string `json:"raw"` Value *big.Rat `json:"value"` }
func (*Number) ScalarString ¶
type Position ¶
Position represents a line:column and byte position in a file.
note: Line and Column are zero indexed. note: Column and Byte are UTF-8 byte indexes unless byUTF16 was passed to Position.Advance in
which they are UTF-16 code unit indexes. If intended for Javascript consumption like in the browser or via LSP, byUTF16 is set to true.
func (Position) Advance ¶
Advance advances p's Line, Column and Byte by r and returns the new Position. Set byUTF16 to advance the position as though r represents a UTF-16 codepoint.
func (*Position) From ¶
From copies src into p. It's used in the d2parser package to set a node's Range.End to the parser's current pos on all return paths with defer.
func (Position) String ¶
String returns a line:column representation of the position suitable for error messages.
note: Should not normally be used directly, see Range.String()
func (*Position) UnmarshalText ¶
See docs on Range.
type Range ¶
Range represents a range between Start and End in Path. It's also used in the d2parser package to represent the range of an error.
note: See docs on Position.
It has a custom JSON string encoding with encoding.TextMarshaler and encoding.TextUnmarshaler to keep it compact as the JSON struct encoding is too verbose, especially with json.MarshalIndent.
It looks like path,start-end
func (Range) String ¶
String returns a string representation of the range including only the path and start.
If path is empty, it will be omitted.
The format is path:start
func (*Range) UnmarshalText ¶
See docs on Range.
type ScalarBox ¶
type ScalarBox struct { Null *Null `json:"null,omitempty"` Boolean *Boolean `json:"boolean,omitempty"` Number *Number `json:"number,omitempty"` UnquotedString *UnquotedString `json:"unquoted_string,omitempty"` DoubleQuotedString *DoubleQuotedString `json:"double_quoted_string,omitempty"` SingleQuotedString *SingleQuotedString `json:"single_quoted_string,omitempty"` BlockString *BlockString `json:"block_string,omitempty"` }
ScalarBox is used to box Scalar for JSON persistence. TODO: implement ScalarString()
type SingleQuotedString ¶
type SingleQuotedString struct { Range Range `json:"range"` Raw string `json:"raw"` Value string `json:"value"` }
func (*SingleQuotedString) Copy ¶
func (s *SingleQuotedString) Copy() String
func (*SingleQuotedString) GetRange ¶
func (s *SingleQuotedString) GetRange() Range
func (*SingleQuotedString) ScalarString ¶
func (s *SingleQuotedString) ScalarString() string
func (*SingleQuotedString) SetString ¶
func (s *SingleQuotedString) SetString(s2 string)
func (*SingleQuotedString) Type ¶
func (s *SingleQuotedString) Type() string
type String ¶
type String interface { Scalar SetString(string) Copy() String // contains filtered or unexported methods }
String is implemented by nodes that represent strings.
type StringBox ¶
type StringBox struct { UnquotedString *UnquotedString `json:"unquoted_string,omitempty"` DoubleQuotedString *DoubleQuotedString `json:"double_quoted_string,omitempty"` SingleQuotedString *SingleQuotedString `json:"single_quoted_string,omitempty"` BlockString *BlockString `json:"block_string,omitempty"` }
StringBox is used to box String for JSON persistence.
type Substitution ¶
type Substitution struct { Range Range `json:"range"` Spread bool `json:"spread"` Path []*StringBox `json:"path"` }
func (*Substitution) GetRange ¶
func (s *Substitution) GetRange() Range
func (*Substitution) Type ¶
func (s *Substitution) Type() string
type UnquotedString ¶
type UnquotedString struct { Range Range `json:"range"` Value []InterpolationBox `json:"value"` }
func FlatUnquotedString ¶
func FlatUnquotedString(s string) *UnquotedString
func (*UnquotedString) Copy ¶
func (s *UnquotedString) Copy() String
func (*UnquotedString) GetRange ¶
func (s *UnquotedString) GetRange() Range
func (*UnquotedString) ScalarString ¶
func (s *UnquotedString) ScalarString() string
func (*UnquotedString) SetString ¶
func (s *UnquotedString) SetString(s2 string)
func (*UnquotedString) Type ¶
func (s *UnquotedString) Type() string
type Value ¶
type Value interface { ArrayNode // contains filtered or unexported methods }
Value is implemented by nodes that may be values of a key.
type ValueBox ¶
type ValueBox struct { Null *Null `json:"null,omitempty"` Boolean *Boolean `json:"boolean,omitempty"` Number *Number `json:"number,omitempty"` UnquotedString *UnquotedString `json:"unquoted_string,omitempty"` DoubleQuotedString *DoubleQuotedString `json:"double_quoted_string,omitempty"` SingleQuotedString *SingleQuotedString `json:"single_quoted_string,omitempty"` BlockString *BlockString `json:"block_string,omitempty"` Array *Array `json:"array,omitempty"` Map *Map `json:"map,omitempty"` }
ValueBox is used to box Value for JSON persistence.