d2/d2ir/compile.go

217 lines
4.3 KiB
Go
Raw Normal View History

2023-01-16 11:52:37 +00:00
package d2ir
import (
"fmt"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2parser"
)
type compiler struct {
err d2parser.ParseError
}
func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) {
f = "%v: " + f
v = append([]interface{}{n.GetRange()}, v...)
c.err.Errors = append(c.err.Errors, d2ast.Error{
Range: n.GetRange(),
Message: fmt.Sprintf(f, v...),
})
}
2023-01-18 01:39:31 +00:00
func Compile(ast *d2ast.Map) (*IR, error) {
ir := &IR{
AST: ast,
Map: &Map{},
}
c := &compiler{}
c.compile(ir)
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 01:39:31 +00:00
return ir, nil
}
func (c *compiler) compile(ir *IR) {
c.compileMap(ir.Map, ir.AST)
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-18 05:28:33 +00:00
c.compileKey(dst, &RefContext{
Key: n.MapKey,
Scope: ast,
})
2023-01-16 11:52:37 +00:00
case n.Substitution != nil:
panic("TODO")
}
}
}
2023-01-18 05:28:33 +00:00
func (c *compiler) compileKey(dst *Map, refctx *RefContext) {
if len(refctx.Key.Edges) == 0 {
c.compileField(dst, refctx.Key)
dst.appendFieldReferences(0, refctx.Key.Key, refctx)
2023-01-16 11:52:37 +00:00
} else {
2023-01-18 05:28:33 +00:00
c.compileEdges(dst, refctx)
2023-01-16 11:52:37 +00:00
}
}
func (c *compiler) compileField(dst *Map, k *d2ast.Key) {
2023-01-17 12:44:14 +00:00
f, err := dst.EnsureField(d2format.KeyPath(k.Key))
2023-01-16 11:52:37 +00:00
if err != nil {
c.errorf(k, err.Error())
return
}
if k.Primary.Unbox() != nil {
f.Primary = &Scalar{
parent: f,
Value: k.Primary.Unbox(),
}
}
if k.Value.Array != nil {
a := &Array{
parent: f,
}
c.compileArray(a, k.Value.Array)
f.Composite = a
} else if k.Value.Map != nil {
2023-01-18 05:28:33 +00:00
f_m := ToMap(f)
if f_m == nil {
f_m = &Map{
2023-01-16 11:52:37 +00:00
parent: f,
}
2023-01-18 05:28:33 +00:00
f.Composite = f_m
2023-01-16 11:52:37 +00:00
}
2023-01-18 05:28:33 +00:00
c.compileMap(f_m, k.Value.Map)
2023-01-16 11:52:37 +00:00
} else if k.Value.ScalarBox().Unbox() != nil {
f.Primary = &Scalar{
parent: f,
Value: k.Value.ScalarBox().Unbox(),
}
}
}
2023-01-18 05:28:33 +00:00
func (c *compiler) compileEdges(dst *Map, refctx *RefContext) {
if refctx.Key.Key != nil && len(refctx.Key.Key.Path) > 0 {
f, err := dst.EnsureField(d2format.KeyPath(refctx.Key.Key))
2023-01-16 11:52:37 +00:00
if err != nil {
2023-01-18 05:28:33 +00:00
c.errorf(refctx.Key.Key, err.Error())
2023-01-16 11:52:37 +00:00
return
}
2023-01-18 05:28:33 +00:00
dst.appendFieldReferences(0, refctx.Key.Key, refctx)
if _, ok := f.Composite.(*Array); ok {
c.errorf(refctx.Key.Key, "cannot index into array")
return
}
f_m := ToMap(f)
if f_m == nil {
f_m = &Map{
2023-01-16 11:52:37 +00:00
parent: f,
}
2023-01-18 05:28:33 +00:00
f.Composite = f_m
2023-01-16 11:52:37 +00:00
}
2023-01-18 05:28:33 +00:00
dst = f_m
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 {
var e *Edge
if eid.Index != nil {
ea := dst.GetEdges(eid)
if len(ea) == 0 {
2023-01-18 05:28:33 +00:00
c.errorf(refctx.Key.Edges[i], "indexed edge does not exist")
2023-01-16 11:52:37 +00:00
continue
}
e = ea[0]
} else {
2023-01-17 12:44:14 +00:00
_, err := dst.EnsureField(eid.SrcPath)
if err != nil {
2023-01-18 05:28:33 +00:00
c.errorf(refctx.Key.Edges[i].Src, err.Error())
2023-01-17 12:44:14 +00:00
continue
}
_, err = dst.EnsureField(eid.DstPath)
if err != nil {
2023-01-18 05:28:33 +00:00
c.errorf(refctx.Key.Edges[i].Dst, err.Error())
2023-01-17 12:44:14 +00:00
continue
}
2023-01-16 11:52:37 +00:00
e, err = dst.EnsureEdge(eid)
if err != nil {
2023-01-18 05:28:33 +00:00
c.errorf(refctx.Key.Edges[i], err.Error())
2023-01-16 11:52:37 +00:00
continue
}
}
2023-01-18 05:28:33 +00:00
refctx = refctx.Copy()
refctx.Edge = refctx.Key.Edges[i]
dst.appendEdgeReferences(e, refctx)
if refctx.Key.EdgeKey != nil {
2023-01-16 11:52:37 +00:00
if e.Map == nil {
e.Map = &Map{
parent: e,
}
}
tmpk := &d2ast.Key{
2023-01-18 05:28:33 +00:00
Range: refctx.Key.EdgeKey.Range,
Key: refctx.Key.EdgeKey,
2023-01-16 11:52:37 +00:00
}
c.compileField(e.Map, tmpk)
2023-01-18 05:28:33 +00:00
e.Map.appendFieldReferences(0, 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-16 11:52:37 +00:00
e.Primary = &Scalar{
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-18 05:28:33 +00:00
} else if refctx.Key.Value.Map != nil {
2023-01-16 11:52:37 +00:00
if e.Map == nil {
e.Map = &Map{
parent: e,
}
}
2023-01-18 05:28:33 +00:00
c.compileMap(e.Map, refctx.Key.Value.Map)
} else if refctx.Key.Value.Unbox() != nil {
c.errorf(refctx.Key.Value.Unbox(), "edges cannot be assigned arrays")
2023-01-16 11:52:37 +00:00
continue
}
}
}
}
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,
}
2023-01-16 15:45:13 +00:00
case *d2ast.Substitution:
panic("TODO")
2023-01-16 12:48:45 +00:00
}
dst.Values = append(dst.Values, irv)
}
2023-01-16 11:52:37 +00:00
}