d2/d2ir/compile.go

423 lines
9.5 KiB
Go
Raw Normal View History

2023-01-16 11:52:37 +00:00
package d2ir
import (
"io/fs"
"strings"
2023-01-16 11:52:37 +00:00
"oss.terrastruct.com/d2/d2ast"
2023-03-03 01:56:32 +00:00
"oss.terrastruct.com/d2/d2format"
2023-01-16 11:52:37 +00:00
"oss.terrastruct.com/d2/d2parser"
)
type compiler struct {
err *d2parser.ParseError
fs fs.FS
// importStack is used to detect cyclic imports.
importStack []string
// importCache enables reuse of files imported multiple times.
importCache map[string]*Map
utf16 bool
}
type CompileOptions struct {
UTF16 bool
// Pass nil to disable imports.
FS fs.FS
2023-01-16 11:52:37 +00:00
}
func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, d2parser.Errorf(n, f, v...).(d2ast.Error))
2023-01-16 11:52:37 +00:00
}
func Compile(ast *d2ast.Map, opts *CompileOptions) (*Map, error) {
if opts == nil {
opts = &CompileOptions{}
}
c := &compiler{
err: &d2parser.ParseError{},
fs: opts.FS,
importCache: make(map[string]*Map),
utf16: opts.UTF16,
}
2023-01-24 06:45:21 +00:00
m := &Map{}
m.initRoot()
m.parent.(*Field).References[0].Context.Scope = ast
c.pushImportStack(&d2ast.Import{
Path: []*d2ast.StringBox{d2ast.RawStringBox(ast.GetRange().Path, true)},
})
defer c.popImportStack()
2023-01-18 15:15:16 +00:00
c.compileMap(m, ast)
2023-02-06 21:32:08 +00:00
c.compileClasses(m)
2023-01-16 11:52:37 +00:00
if !c.err.Empty() {
2023-01-18 01:39:31 +00:00
return nil, c.err
2023-01-16 11:52:37 +00:00
}
2023-01-18 11:51:16 +00:00
return m, nil
2023-01-18 01:39:31 +00:00
}
2023-02-06 21:32:08 +00:00
func (c *compiler) compileClasses(m *Map) {
classes := m.GetField("classes")
if classes == nil || classes.Map() == nil {
return
}
layersField := m.GetField("layers")
if layersField == nil {
return
}
layers := layersField.Map()
if layers == nil {
return
}
for _, lf := range layers.Fields {
if lf.Map() == nil || lf.Primary() != nil {
c.errorf(lf.References[0].Context.Key, "invalid layer")
continue
}
l := lf.Map()
lClasses := l.GetField("classes")
if lClasses == nil {
lClasses = classes.Copy(l).(*Field)
l.Fields = append(l.Fields, lClasses)
} else {
base := classes.Copy(l).(*Field)
OverlayMap(base.Map(), lClasses.Map())
l.DeleteField("classes")
l.Fields = append(l.Fields, base)
}
c.compileClasses(l)
}
}
2023-03-25 19:06:06 +00:00
func (c *compiler) overlay(base *Map, f *Field) {
if f.Map() == nil || f.Primary() != nil {
c.errorf(f.References[0].Context.Key, "invalid %s", NodeBoardKind(f))
2023-01-18 15:15:16 +00:00
return
}
2023-03-25 19:06:06 +00:00
base = base.CopyBase(f)
OverlayMap(base, f.Map())
f.Composite = base
2023-01-16 11:52:37 +00:00
}
func (c *compiler) compileMap(dst *Map, ast *d2ast.Map) {
for _, n := range ast.Nodes {
switch {
case n.MapKey != nil:
2023-01-24 05:48:43 +00:00
c.compileKey(&RefContext{
Key: n.MapKey,
Scope: ast,
ScopeMap: dst,
2023-01-18 05:28:33 +00:00
})
case n.Import != nil:
impn, ok := c._import(n.Import)
if !ok {
continue
}
if impn.Map() == nil {
c.errorf(n.Import, "cannot spread import non map into map")
continue
}
OverlayMap(dst, impn.Map())
2023-01-16 11:52:37 +00:00
case n.Substitution != nil:
panic("TODO")
}
}
}
2023-01-24 05:48:43 +00:00
func (c *compiler) compileKey(refctx *RefContext) {
2023-01-18 05:28:33 +00:00
if len(refctx.Key.Edges) == 0 {
2023-01-24 05:48:43 +00:00
c.compileField(refctx.ScopeMap, refctx.Key.Key, refctx)
2023-01-16 11:52:37 +00:00
} else {
2023-01-24 05:48:43 +00:00
c.compileEdges(refctx)
2023-01-16 11:52:37 +00:00
}
}
2023-01-18 10:06:44 +00:00
func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) {
f, err := dst.EnsureField(kp, refctx)
2023-01-16 11:52:37 +00:00
if err != nil {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
2023-01-16 11:52:37 +00:00
return
}
2023-01-18 10:06:44 +00:00
if refctx.Key.Primary.Unbox() != nil {
2023-01-18 15:15:16 +00:00
f.Primary_ = &Scalar{
2023-01-16 11:52:37 +00:00
parent: f,
2023-01-18 10:06:44 +00:00
Value: refctx.Key.Primary.Unbox(),
2023-01-16 11:52:37 +00:00
}
}
2023-01-18 10:06:44 +00:00
if refctx.Key.Value.Array != nil {
2023-01-16 11:52:37 +00:00
a := &Array{
parent: f,
}
2023-01-18 10:06:44 +00:00
c.compileArray(a, refctx.Key.Value.Array)
2023-01-16 11:52:37 +00:00
f.Composite = a
2023-01-18 10:06:44 +00:00
} else if refctx.Key.Value.Map != nil {
2023-01-18 15:15:16 +00:00
if f.Map() == nil {
f.Composite = &Map{
2023-01-16 11:52:37 +00:00
parent: f,
}
}
2023-03-25 19:06:06 +00:00
switch NodeBoardKind(f) {
case BoardScenario:
2023-04-07 04:02:12 +00:00
c.overlay(ParentBoard(f).Map(), f)
2023-03-25 19:06:06 +00:00
case BoardStep:
2023-04-07 04:02:12 +00:00
stepsMap := ParentMap(f)
2023-03-25 19:06:06 +00:00
for i := range stepsMap.Fields {
if stepsMap.Fields[i] == f {
if i == 0 {
2023-04-07 04:02:12 +00:00
c.overlay(ParentBoard(f).Map(), f)
2023-03-25 19:06:06 +00:00
} else {
c.overlay(stepsMap.Fields[i-1].Map(), f)
}
break
}
}
}
2023-01-18 15:15:16 +00:00
c.compileMap(f.Map(), refctx.Key.Value.Map)
2023-02-06 21:32:08 +00:00
switch NodeBoardKind(f) {
case BoardScenario, BoardStep:
c.compileClasses(f.Map())
}
} else if refctx.Key.Value.Import != nil {
n, ok := c._import(refctx.Key.Value.Import)
if !ok {
return
}
switch n := n.(type) {
case *Field:
if n.Primary_ != nil {
f.Primary_ = n.Primary_.Copy(f).(*Scalar)
}
if n.Composite != nil {
f.Composite = n.Composite.Copy(f).(Composite)
}
case *Map:
f.Composite = n.Copy(f).(Composite)
}
2023-01-18 10:06:44 +00:00
} else if refctx.Key.Value.ScalarBox().Unbox() != nil {
2023-03-03 01:56:32 +00:00
// If the link is a board, we need to transform it into an absolute path.
2023-03-01 22:53:58 +00:00
if f.Name == "link" {
2023-03-01 23:38:02 +00:00
c.compileLink(refctx)
2023-03-01 22:53:58 +00:00
}
2023-01-18 15:15:16 +00:00
f.Primary_ = &Scalar{
2023-01-16 11:52:37 +00:00
parent: f,
2023-01-18 10:06:44 +00:00
Value: refctx.Key.Value.ScalarBox().Unbox(),
2023-01-16 11:52:37 +00:00
}
}
}
2023-03-01 23:38:02 +00:00
func (c *compiler) compileLink(refctx *RefContext) {
val := refctx.Key.Value.ScalarBox().Unbox().ScalarString()
link, err := d2parser.ParseKey(val)
if err != nil {
return
}
2023-03-03 01:56:32 +00:00
scopeIDA := IDA(refctx.ScopeMap)
2023-03-01 23:38:02 +00:00
if len(scopeIDA) == 0 {
return
}
linkIDA := link.IDA()
if len(linkIDA) == 0 {
return
}
2023-03-03 02:01:51 +00:00
if linkIDA[0] == "root" {
c.errorf(refctx.Key.Key, "cannot refer to root in link")
return
}
2023-03-03 01:56:32 +00:00
// If it doesn't start with one of these reserved words, the link is definitely not a board link.
if !strings.EqualFold(linkIDA[0], "layers") && !strings.EqualFold(linkIDA[0], "scenarios") && !strings.EqualFold(linkIDA[0], "steps") && linkIDA[0] != "_" {
2023-03-01 23:38:02 +00:00
return
}
// Chop off the non-board portion of the scope, like if this is being defined on a nested object (e.g. `x.y.z`)
for i := len(scopeIDA) - 1; i > 0; i-- {
if strings.EqualFold(scopeIDA[i-1], "layers") || strings.EqualFold(scopeIDA[i-1], "scenarios") || strings.EqualFold(scopeIDA[i-1], "steps") {
2023-03-01 23:38:02 +00:00
scopeIDA = scopeIDA[:i+1]
break
}
if scopeIDA[i-1] == "root" {
scopeIDA = scopeIDA[:i]
break
}
}
// Resolve underscores
for len(linkIDA) > 0 && linkIDA[0] == "_" {
if len(scopeIDA) < 2 {
2023-03-01 23:49:47 +00:00
// IR compiler only validates bad underscore usage
// The compiler will validate if the target board actually exists
2023-03-03 01:56:32 +00:00
c.errorf(refctx.Key.Key, "invalid underscore usage")
2023-03-01 23:38:02 +00:00
return
}
// pop 2 off path per one underscore
scopeIDA = scopeIDA[:len(scopeIDA)-2]
linkIDA = linkIDA[1:]
}
if len(scopeIDA) == 0 {
scopeIDA = []string{"root"}
}
// Create the absolute path by appending scope path with value specified
scopeIDA = append(scopeIDA, linkIDA...)
2023-03-03 02:25:14 +00:00
kp := d2ast.MakeKeyPath(scopeIDA)
2023-03-03 01:56:32 +00:00
refctx.Key.Value = d2ast.MakeValueBox(d2ast.RawString(d2format.Format(kp), true))
2023-03-01 23:38:02 +00:00
}
2023-01-24 05:48:43 +00:00
func (c *compiler) compileEdges(refctx *RefContext) {
2023-01-18 10:06:44 +00:00
if refctx.Key.Key != nil {
2023-01-24 05:48:43 +00:00
f, err := refctx.ScopeMap.EnsureField(refctx.Key.Key, refctx)
2023-01-16 11:52:37 +00:00
if err != nil {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
2023-01-16 11:52:37 +00:00
return
}
2023-01-18 05:28:33 +00:00
if _, ok := f.Composite.(*Array); ok {
c.errorf(refctx.Key.Key, "cannot index into array")
return
}
2023-01-18 15:15:16 +00:00
if f.Map() == nil {
f.Composite = &Map{
2023-01-16 11:52:37 +00:00
parent: f,
}
}
2023-01-24 05:48:43 +00:00
refctx.ScopeMap = f.Map()
2023-01-16 11:52:37 +00:00
}
2023-01-18 05:28:33 +00:00
eida := NewEdgeIDs(refctx.Key)
2023-01-16 11:52:37 +00:00
for i, eid := range eida {
2023-01-18 10:06:44 +00:00
refctx = refctx.Copy()
refctx.Edge = refctx.Key.Edges[i]
2023-01-16 11:52:37 +00:00
var e *Edge
if eid.Index != nil {
2023-01-24 05:48:43 +00:00
ea := refctx.ScopeMap.GetEdges(eid)
2023-01-16 11:52:37 +00:00
if len(ea) == 0 {
2023-01-18 10:06:44 +00:00
c.errorf(refctx.Edge, "indexed edge does not exist")
2023-01-16 11:52:37 +00:00
continue
}
e = ea[0]
2023-01-22 08:59:02 +00:00
e.References = append(e.References, &EdgeReference{
2023-01-18 10:06:44 +00:00
Context: refctx,
})
2023-01-24 05:48:43 +00:00
refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Src, refctx)
refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Dst, refctx)
2023-01-16 11:52:37 +00:00
} else {
2023-01-24 05:48:43 +00:00
_, err := refctx.ScopeMap.EnsureField(refctx.Edge.Src, refctx)
2023-01-17 12:44:14 +00:00
if err != nil {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
2023-01-17 12:44:14 +00:00
continue
}
2023-01-24 05:48:43 +00:00
_, err = refctx.ScopeMap.EnsureField(refctx.Edge.Dst, refctx)
2023-01-17 12:44:14 +00:00
if err != nil {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
2023-01-17 12:44:14 +00:00
continue
}
2023-01-24 05:48:43 +00:00
e, err = refctx.ScopeMap.CreateEdge(eid, refctx)
2023-01-16 11:52:37 +00:00
if err != nil {
2023-01-18 10:06:44 +00:00
c.err.Errors = append(c.err.Errors, err.(d2ast.Error))
2023-01-16 11:52:37 +00:00
continue
}
}
2023-01-18 05:28:33 +00:00
if refctx.Key.EdgeKey != nil {
2023-01-18 15:15:16 +00:00
if e.Map_ == nil {
e.Map_ = &Map{
2023-01-16 11:52:37 +00:00
parent: e,
}
}
2023-01-18 15:15:16 +00:00
c.compileField(e.Map_, refctx.Key.EdgeKey, refctx)
2023-01-16 11:52:37 +00:00
} else {
2023-01-18 05:28:33 +00:00
if refctx.Key.Primary.Unbox() != nil {
2023-01-18 15:15:16 +00:00
e.Primary_ = &Scalar{
2023-01-16 11:52:37 +00:00
parent: e,
2023-01-18 05:28:33 +00:00
Value: refctx.Key.Primary.Unbox(),
2023-01-16 11:52:37 +00:00
}
2023-01-22 09:43:25 +00:00
}
if refctx.Key.Value.Array != nil {
c.errorf(refctx.Key.Value.Unbox(), "edges cannot be assigned arrays")
continue
2023-01-18 05:28:33 +00:00
} else if refctx.Key.Value.Map != nil {
2023-01-18 15:15:16 +00:00
if e.Map_ == nil {
e.Map_ = &Map{
2023-01-16 11:52:37 +00:00
parent: e,
}
}
2023-01-18 15:15:16 +00:00
c.compileMap(e.Map_, refctx.Key.Value.Map)
2023-01-22 09:43:25 +00:00
} else if refctx.Key.Value.ScalarBox().Unbox() != nil {
e.Primary_ = &Scalar{
parent: e,
Value: refctx.Key.Value.ScalarBox().Unbox(),
}
2023-01-16 11:52:37 +00:00
}
}
}
}
func (c *compiler) compileArray(dst *Array, a *d2ast.Array) {
2023-01-16 12:48:45 +00:00
for _, an := range a.Nodes {
var irv Value
switch v := an.Unbox().(type) {
case *d2ast.Array:
ira := &Array{
parent: dst,
}
c.compileArray(ira, v)
irv = ira
case *d2ast.Map:
irm := &Map{
parent: dst,
}
c.compileMap(irm, v)
irv = irm
case d2ast.Scalar:
irv = &Scalar{
parent: dst,
Value: v,
}
case *d2ast.Import:
n, ok := c._import(v)
if !ok {
continue
}
switch n := n.(type) {
case *Field:
if v.Spread {
a, ok := n.Composite.(*Array)
if !ok {
c.errorf(v, "can only spread import array into array")
continue
}
dst.Values = append(dst.Values, a.Values...)
continue
}
if n.Composite != nil {
irv = n.Composite
} else {
irv = n.Primary_
}
case *Map:
if v.Spread {
c.errorf(v, "cannot spread import map into array")
continue
}
irv = n
}
2023-01-16 15:45:13 +00:00
case *d2ast.Substitution:
2023-01-27 20:37:08 +00:00
// panic("TODO")
2023-01-16 12:48:45 +00:00
}
dst.Values = append(dst.Values, irv)
}
2023-01-16 11:52:37 +00:00
}