compiler
This commit is contained in:
parent
e656cf75d1
commit
366a36df67
5 changed files with 226 additions and 0 deletions
|
|
@ -392,6 +392,8 @@ func compileStyleFieldInit(attrs *d2graph.Attributes, f *d2ir.Field) {
|
||||||
attrs.Style.Stroke = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
attrs.Style.Stroke = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
||||||
case "fill":
|
case "fill":
|
||||||
attrs.Style.Fill = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
attrs.Style.Fill = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
||||||
|
case "fill-pattern":
|
||||||
|
attrs.Style.FillPattern = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
||||||
case "stroke-width":
|
case "stroke-width":
|
||||||
attrs.Style.StrokeWidth = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
attrs.Style.StrokeWidth = &d2graph.Scalar{MapKey: f.LastPrimaryKey()}
|
||||||
case "stroke-dash":
|
case "stroke-dash":
|
||||||
|
|
|
||||||
|
|
@ -242,6 +242,25 @@ containers: {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "fill-pattern",
|
||||||
|
text: `x: {
|
||||||
|
style: {
|
||||||
|
fill-pattern: dots
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid-fill-pattern",
|
||||||
|
text: `x: {
|
||||||
|
style: {
|
||||||
|
fill-pattern: ddots
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expErr: `d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected "fill-pattern" to be one of: dots`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "shape_unquoted_hex",
|
name: "shape_unquoted_hex",
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,7 @@ type Style struct {
|
||||||
Opacity *Scalar `json:"opacity,omitempty"`
|
Opacity *Scalar `json:"opacity,omitempty"`
|
||||||
Stroke *Scalar `json:"stroke,omitempty"`
|
Stroke *Scalar `json:"stroke,omitempty"`
|
||||||
Fill *Scalar `json:"fill,omitempty"`
|
Fill *Scalar `json:"fill,omitempty"`
|
||||||
|
FillPattern *Scalar `json:"fillPattern,omitempty"`
|
||||||
StrokeWidth *Scalar `json:"strokeWidth,omitempty"`
|
StrokeWidth *Scalar `json:"strokeWidth,omitempty"`
|
||||||
StrokeDash *Scalar `json:"strokeDash,omitempty"`
|
StrokeDash *Scalar `json:"strokeDash,omitempty"`
|
||||||
BorderRadius *Scalar `json:"borderRadius,omitempty"`
|
BorderRadius *Scalar `json:"borderRadius,omitempty"`
|
||||||
|
|
@ -194,6 +195,14 @@ func (s *Style) Apply(key, value string) error {
|
||||||
return errors.New(`expected "fill" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
|
return errors.New(`expected "fill" to be a valid named color ("orange") or a hex code ("#f0ff3a")`)
|
||||||
}
|
}
|
||||||
s.Fill.Value = value
|
s.Fill.Value = value
|
||||||
|
case "fill-pattern":
|
||||||
|
if s.FillPattern == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if !go2.Contains(FillPatterns, strings.ToLower(value)) {
|
||||||
|
return fmt.Errorf(`expected "fill-pattern" to be one of: %s`, strings.Join(FillPatterns, ","))
|
||||||
|
}
|
||||||
|
s.FillPattern.Value = value
|
||||||
case "stroke-width":
|
case "stroke-width":
|
||||||
if s.StrokeWidth == nil {
|
if s.StrokeWidth == nil {
|
||||||
break
|
break
|
||||||
|
|
@ -1499,6 +1508,7 @@ var StyleKeywords = map[string]struct{}{
|
||||||
"opacity": {},
|
"opacity": {},
|
||||||
"stroke": {},
|
"stroke": {},
|
||||||
"fill": {},
|
"fill": {},
|
||||||
|
"fill-pattern": {},
|
||||||
"stroke-width": {},
|
"stroke-width": {},
|
||||||
"stroke-dash": {},
|
"stroke-dash": {},
|
||||||
"border-radius": {},
|
"border-radius": {},
|
||||||
|
|
@ -1540,6 +1550,11 @@ var NearConstantsArray = []string{
|
||||||
}
|
}
|
||||||
var NearConstants map[string]struct{}
|
var NearConstants map[string]struct{}
|
||||||
|
|
||||||
|
var FillPatterns = []string{
|
||||||
|
// TODO add lined later
|
||||||
|
"dots",
|
||||||
|
}
|
||||||
|
|
||||||
// BoardKeywords contains the keywords that create new boards.
|
// BoardKeywords contains the keywords that create new boards.
|
||||||
var BoardKeywords = map[string]struct{}{
|
var BoardKeywords = map[string]struct{}{
|
||||||
"layers": {},
|
"layers": {},
|
||||||
|
|
|
||||||
178
testdata/d2compiler/TestCompile/fill-pattern.exp.json
generated
vendored
Normal file
178
testdata/d2compiler/TestCompile/fill-pattern.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,178 @@
|
||||||
|
{
|
||||||
|
"graph": {
|
||||||
|
"name": "",
|
||||||
|
"isFolderOnly": false,
|
||||||
|
"ast": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-5:0:44",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"map_key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-4:1:43",
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "x",
|
||||||
|
"raw_string": "x"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {
|
||||||
|
"map": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:3:3-4:0:42",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"map_key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-3:3:41",
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:1:6-1:6:11",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "style",
|
||||||
|
"raw_string": "style"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {
|
||||||
|
"map": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,1:8:13-3:2:40",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"map_key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:22:37",
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:4:19-2:16:31",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "fill-pattern",
|
||||||
|
"raw_string": "fill-pattern"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,2:18:33-2:22:37",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "dots",
|
||||||
|
"raw_string": "dots"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"id": "",
|
||||||
|
"id_val": "",
|
||||||
|
"label_dimensions": {
|
||||||
|
"width": 0,
|
||||||
|
"height": 0
|
||||||
|
},
|
||||||
|
"attributes": {
|
||||||
|
"label": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"style": {},
|
||||||
|
"near_key": null,
|
||||||
|
"shape": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"direction": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"constraint": {
|
||||||
|
"value": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"zIndex": 0
|
||||||
|
},
|
||||||
|
"edges": null,
|
||||||
|
"objects": [
|
||||||
|
{
|
||||||
|
"id": "x",
|
||||||
|
"id_val": "x",
|
||||||
|
"label_dimensions": {
|
||||||
|
"width": 0,
|
||||||
|
"height": 0
|
||||||
|
},
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"key": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/fill-pattern.d2,0:0:0-0:1:1",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "x",
|
||||||
|
"raw_string": "x"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"key_path_index": 0,
|
||||||
|
"map_key_edge_index": -1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"attributes": {
|
||||||
|
"label": {
|
||||||
|
"value": "x"
|
||||||
|
},
|
||||||
|
"style": {},
|
||||||
|
"near_key": null,
|
||||||
|
"shape": {
|
||||||
|
"value": "rectangle"
|
||||||
|
},
|
||||||
|
"direction": {
|
||||||
|
"value": ""
|
||||||
|
},
|
||||||
|
"constraint": {
|
||||||
|
"value": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"zIndex": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"err": null
|
||||||
|
}
|
||||||
12
testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json
generated
vendored
Normal file
12
testdata/d2compiler/TestCompile/invalid-fill-pattern.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"graph": null,
|
||||||
|
"err": {
|
||||||
|
"ioerr": null,
|
||||||
|
"errs": [
|
||||||
|
{
|
||||||
|
"range": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2,2:18:33-2:23:38",
|
||||||
|
"errmsg": "d2/testdata/d2compiler/TestCompile/invalid-fill-pattern.d2:3:19: expected \"fill-pattern\" to be one of: dots"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue