d2compiler: Integrate d2ir (wip)
This commit is contained in:
parent
b900f63414
commit
566ea11db7
4 changed files with 61 additions and 27 deletions
|
|
@ -123,7 +123,7 @@ func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
|
||||||
|
|
||||||
func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
|
func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
|
||||||
keyword := strings.ToLower(f.Name)
|
keyword := strings.ToLower(f.Name)
|
||||||
_, isReserved := d2graph.ReservedKeywords[keyword]
|
_, isReserved := d2graph.SimpleReservedKeywords[keyword]
|
||||||
if isReserved {
|
if isReserved {
|
||||||
c.compileReserved(obj.Attributes, f)
|
c.compileReserved(obj.Attributes, f)
|
||||||
return
|
return
|
||||||
|
|
@ -142,6 +142,18 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
|
||||||
if f.Map() != nil {
|
if f.Map() != nil {
|
||||||
c.compileMap(obj, f.Map())
|
c.compileMap(obj, f.Map())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, er := range f.References {
|
||||||
|
obj.References = append(obj.References, d2graph.Reference{
|
||||||
|
Key: er.KeyPath,
|
||||||
|
KeyPathIndex: er.KeyPathIndex(),
|
||||||
|
|
||||||
|
MapKey: er.Context.Key,
|
||||||
|
MapKeyEdgeIndex: er.Context.EdgeIndex(),
|
||||||
|
Scope: er.Context.Scope,
|
||||||
|
ScopeObj: obj.Parent,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
|
func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
|
||||||
|
|
@ -246,13 +258,16 @@ func (c *compiler) compileStyleField(attrs *d2graph.Attributes, f *d2ir.Field) {
|
||||||
if f.Primary() == nil {
|
if f.Primary() == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
compileStyleFieldInit(attrs, f)
|
||||||
scalar := f.Primary().Value
|
scalar := f.Primary().Value
|
||||||
err := attrs.Style.Apply(f.Name, scalar.ScalarString())
|
err := attrs.Style.Apply(f.Name, scalar.ScalarString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.errorf(scalar, err.Error())
|
c.errorf(scalar, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) {
|
||||||
switch f.Name {
|
switch f.Name {
|
||||||
case "opacity":
|
case "opacity":
|
||||||
attrs.Style.Opacity = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
attrs.Style.Opacity = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
||||||
|
|
@ -315,11 +330,21 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
|
||||||
c.compileEdgeField(edge, f)
|
c.compileEdgeField(edge, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, er := range e.References {
|
||||||
|
edge.References = append(edge.References, d2graph.EdgeReference{
|
||||||
|
Edge: er.Context.Edge,
|
||||||
|
MapKey: er.Context.Key,
|
||||||
|
MapKeyEdgeIndex: er.Context.EdgeIndex(),
|
||||||
|
Scope: er.Context.Scope,
|
||||||
|
ScopeObj: obj,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compiler) compileEdgeField(edge *d2graph.Edge, f *d2ir.Field) {
|
func (c *compiler) compileEdgeField(edge *d2graph.Edge, f *d2ir.Field) {
|
||||||
keyword := strings.ToLower(f.Name)
|
keyword := strings.ToLower(f.Name)
|
||||||
_, isReserved := d2graph.ReservedKeywords[keyword]
|
_, isReserved := d2graph.SimpleReservedKeywords[keyword]
|
||||||
if isReserved {
|
if isReserved {
|
||||||
c.compileReserved(edge.Attributes, f)
|
c.compileReserved(edge.Attributes, f)
|
||||||
return
|
return
|
||||||
|
|
@ -354,7 +379,7 @@ func (c *compiler) compileArrowheads(edge *d2graph.Edge, f *d2ir.Field) {
|
||||||
|
|
||||||
for _, f2 := range f.Map().Fields {
|
for _, f2 := range f.Map().Fields {
|
||||||
keyword := strings.ToLower(f2.Name)
|
keyword := strings.ToLower(f2.Name)
|
||||||
_, isReserved := d2graph.ReservedKeywords[keyword]
|
_, isReserved := d2graph.SimpleReservedKeywords[keyword]
|
||||||
if isReserved {
|
if isReserved {
|
||||||
c.compileReserved(attrs, f2)
|
c.compileReserved(attrs, f2)
|
||||||
continue
|
continue
|
||||||
|
|
@ -475,7 +500,7 @@ func (c *compiler) validateKeys(obj *d2graph.Object, m *d2ir.Map) {
|
||||||
|
|
||||||
func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) {
|
func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) {
|
||||||
keyword := strings.ToLower(f.Name)
|
keyword := strings.ToLower(f.Name)
|
||||||
_, isReserved := d2graph.ReservedKeywords[keyword]
|
_, isReserved := d2graph.SimpleReservedKeywords[keyword]
|
||||||
if isReserved {
|
if isReserved {
|
||||||
switch obj.Attributes.Shape.Value {
|
switch obj.Attributes.Shape.Value {
|
||||||
case d2target.ShapeSQLTable, d2target.ShapeClass:
|
case d2target.ShapeSQLTable, d2target.ShapeClass:
|
||||||
|
|
|
||||||
|
|
@ -987,7 +987,7 @@ func addSQLTableColumnIndexes(e *Edge, srcID, dstID []string, obj, src, dst *Obj
|
||||||
}
|
}
|
||||||
objAbsID := obj.AbsIDArray()
|
objAbsID := obj.AbsIDArray()
|
||||||
srcAbsID := src.AbsIDArray()
|
srcAbsID := src.AbsIDArray()
|
||||||
if len(objAbsID) + len(srcID) > len(srcAbsID) {
|
if len(objAbsID)+len(srcID) > len(srcAbsID) {
|
||||||
for i, d2col := range src.SQLTable.Columns {
|
for i, d2col := range src.SQLTable.Columns {
|
||||||
if d2col.Name.Label == srcID[len(srcID)-1] {
|
if d2col.Name.Label == srcID[len(srcID)-1] {
|
||||||
d2col.Reference = dst.AbsID()
|
d2col.Reference = dst.AbsID()
|
||||||
|
|
@ -1001,7 +1001,7 @@ func addSQLTableColumnIndexes(e *Edge, srcID, dstID []string, obj, src, dst *Obj
|
||||||
if dst.Attributes.Shape.Value == d2target.ShapeSQLTable {
|
if dst.Attributes.Shape.Value == d2target.ShapeSQLTable {
|
||||||
objAbsID := obj.AbsIDArray()
|
objAbsID := obj.AbsIDArray()
|
||||||
dstAbsID := dst.AbsIDArray()
|
dstAbsID := dst.AbsIDArray()
|
||||||
if len(objAbsID) + len(dstID) > len(dstAbsID) {
|
if len(objAbsID)+len(dstID) > len(dstAbsID) {
|
||||||
for i, d2col := range dst.SQLTable.Columns {
|
for i, d2col := range dst.SQLTable.Columns {
|
||||||
if d2col.Name.Label == dstID[len(dstID)-1] {
|
if d2col.Name.Label == dstID[len(dstID)-1] {
|
||||||
d2col.Reference = dst.AbsID()
|
d2col.Reference = dst.AbsID()
|
||||||
|
|
@ -1284,7 +1284,11 @@ func Key(k *d2ast.KeyPath) []string {
|
||||||
return d2format.KeyPath(k)
|
return d2format.KeyPath(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ReservedKeywords = map[string]struct{}{
|
// All reserved keywords. See init below.
|
||||||
|
var ReservedKeywords map[string]struct{}
|
||||||
|
|
||||||
|
// Non Style/Holder keywords.
|
||||||
|
var SimpleReservedKeywords = map[string]struct{}{
|
||||||
"label": {},
|
"label": {},
|
||||||
"desc": {},
|
"desc": {},
|
||||||
"shape": {},
|
"shape": {},
|
||||||
|
|
@ -1351,6 +1355,10 @@ var NearConstantsArray = []string{
|
||||||
var NearConstants map[string]struct{}
|
var NearConstants map[string]struct{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
ReservedKeywords = make(map[string]struct{})
|
||||||
|
for k, v := range SimpleReservedKeywords {
|
||||||
|
ReservedKeywords[k] = v
|
||||||
|
}
|
||||||
for k, v := range StyleKeywords {
|
for k, v := range StyleKeywords {
|
||||||
ReservedKeywords[k] = v
|
ReservedKeywords[k] = v
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,10 @@ func (c *compiler) compileEdges(dst *Map, refctx *RefContext) {
|
||||||
parent: e,
|
parent: e,
|
||||||
Value: refctx.Key.Primary.Unbox(),
|
Value: refctx.Key.Primary.Unbox(),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if refctx.Key.Value.Array != nil {
|
||||||
|
c.errorf(refctx.Key.Value.Unbox(), "edges cannot be assigned arrays")
|
||||||
|
continue
|
||||||
} else if refctx.Key.Value.Map != nil {
|
} else if refctx.Key.Value.Map != nil {
|
||||||
if e.Map_ == nil {
|
if e.Map_ == nil {
|
||||||
e.Map_ = &Map{
|
e.Map_ = &Map{
|
||||||
|
|
@ -213,9 +217,11 @@ func (c *compiler) compileEdges(dst *Map, refctx *RefContext) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.compileMap(e.Map_, refctx.Key.Value.Map)
|
c.compileMap(e.Map_, refctx.Key.Value.Map)
|
||||||
} else if refctx.Key.Value.Unbox() != nil {
|
} else if refctx.Key.Value.ScalarBox().Unbox() != nil {
|
||||||
c.errorf(refctx.Key.Value.Unbox(), "edges cannot be assigned arrays")
|
e.Primary_ = &Scalar{
|
||||||
continue
|
parent: e,
|
||||||
|
Value: refctx.Key.Value.ScalarBox().Unbox(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
d2ir/d2ir.go
19
d2ir/d2ir.go
|
|
@ -213,7 +213,7 @@ func NodeLayerKind(n Node) LayerKind {
|
||||||
var f *Field
|
var f *Field
|
||||||
switch n := n.(type) {
|
switch n := n.(type) {
|
||||||
case *Field:
|
case *Field:
|
||||||
f = ParentField(n)
|
f = n
|
||||||
case *Map:
|
case *Map:
|
||||||
f = ParentField(n)
|
f = ParentField(n)
|
||||||
}
|
}
|
||||||
|
|
@ -540,7 +540,7 @@ func (rc *RefContext) EdgeIndex() int {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("d2ir.RefContext.EdgeIndex: Edge not in Key.Edges?")
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Map) FieldCountRecursive() int {
|
func (m *Map) FieldCountRecursive() int {
|
||||||
|
|
@ -909,7 +909,6 @@ func ParentMap(n Node) *Map {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParentField(n Node) *Field {
|
func ParentField(n Node) *Field {
|
||||||
|
|
@ -922,20 +921,17 @@ func ParentField(n Node) *Field {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParentLayer(n Node) *Map {
|
func ParentLayer(n Node) Node {
|
||||||
for {
|
for {
|
||||||
// ParentMap and not ParentField so we get the root layer too.
|
n = n.Parent()
|
||||||
m := ParentMap(n)
|
if n == nil {
|
||||||
if m == nil {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if NodeLayerKind(m) != "" {
|
if NodeLayerKind(n) != "" {
|
||||||
return m
|
return n
|
||||||
}
|
}
|
||||||
n = m
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -949,7 +945,6 @@ func ParentEdge(n Node) *Edge {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func countUnderscores(p []string) int {
|
func countUnderscores(p []string) int {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue