Skip to content

Commit

Permalink
Optimized printcss
Browse files Browse the repository at this point in the history
  • Loading branch information
torbenschinke committed Sep 19, 2023
1 parent de55889 commit 584ce33
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 101 deletions.
107 changes: 107 additions & 0 deletions parser/annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package parser

import (
"fmt"
"strings"
)

type KeyValue struct {
node
Key *Name `@@ ( "="`
Value *Name `@@ )?`
}

func (n *KeyValue) String() string {
if n == nil {
return "nil"
}

key := ""
if n.Key != nil {
key = n.Key.Value
}

val := ""
if n.Value != nil {
val = n.Value.Value
}

return key + "=" + val
}

func (n *KeyValue) Children() []Node {
return sliceOf(n.Key, n.Value)
}

type Annotation struct {
node
Name *Name `@@`
KeyValues []*KeyValue `( "(" @@ ("," @@)* ")" )?`
}

func (n *Annotation) ExpectEmpty() error {
if len(n.KeyValues) != 0 {
var tmp []string
for _, value := range n.KeyValues {
tmp = append(tmp, value.String())
}
return fmt.Errorf("expected empty annotation map, but got (%s)", strings.Join(tmp, ","))
}

return nil
}

func (n *Annotation) ExpectKeysOf(possibleKeySet ...string) error {
tmp := map[string]bool{}
for _, kv := range n.KeyValues {
found := false
for _, s := range possibleKeySet {
if s == kv.Key.Value {
found = true
break
}
}

if !found {
return fmt.Errorf("unexpected key '%s', must be of (%s)", kv.Key.Value, strings.Join(possibleKeySet, "|"))
}

if tmp[kv.Key.Value] {
return fmt.Errorf("key has already been defined: '%s'", kv.Key.Value)
}

tmp[kv.Key.Value] = true
}

return nil
}

func (n *Annotation) FirstValue(possibleKeySet ...string) (value string, found bool) {
for _, kv := range n.KeyValues {
for _, s := range possibleKeySet {
if s == kv.Key.Value {
if kv.Value == nil {
return "", true
}

return kv.Value.Value, true
}
}

}

return "", false
}

func (n *Annotation) Children() []Node {
res := sliceOf(n.Name)
for _, value := range n.KeyValues {
res = append(res, value)
}

return res
}

type TypedAnnotation interface {
typedAnnotation()
}
5 changes: 5 additions & 0 deletions parser/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package parser

import "fmt"

func parseAnnotations(definition *TypeDefinition) []TypedAnnotation {
panic("todo")
}

type EventAnnotation struct {
TypedAnnotation
In bool
Out bool
}
Expand Down
114 changes: 14 additions & 100 deletions parser/typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,13 @@ import (
"strings"
)

type KeyValue struct {
node
Key *Name `@@ ( "="`
Value *Name `@@ )?`
}

func (n *KeyValue) String() string {
if n == nil {
return "nil"
}

key := ""
if n.Key != nil {
key = n.Key.Value
}

val := ""
if n.Value != nil {
val = n.Value.Value
}

return key + "=" + val
}

func (n *KeyValue) Children() []Node {
return sliceOf(n.Key, n.Value)
}

type Annotation struct {
node
Name *Name `@@`
KeyValues []*KeyValue `( "(" @@ ("," @@)* ")" )?`
}

func (n *Annotation) ExpectEmpty() error {
if len(n.KeyValues) != 0 {
var tmp []string
for _, value := range n.KeyValues {
tmp = append(tmp, value.String())
}
return fmt.Errorf("expected empty annotation map, but got (%s)", strings.Join(tmp, ","))
}

return nil
}

func (n *Annotation) ExpectKeysOf(possibleKeySet ...string) error {
tmp := map[string]bool{}
for _, kv := range n.KeyValues {
found := false
for _, s := range possibleKeySet {
if s == kv.Key.Value {
found = true
break
}
}

if !found {
return fmt.Errorf("unexpected key '%s', must be of (%s)", kv.Key.Value, strings.Join(possibleKeySet, "|"))
}

if tmp[kv.Key.Value] {
return fmt.Errorf("key has already been defined: '%s'", kv.Key.Value)
}

tmp[kv.Key.Value] = true
}

return nil
}

func (n *Annotation) FirstValue(possibleKeySet ...string) (value string, found bool) {
for _, kv := range n.KeyValues {
for _, s := range possibleKeySet {
if s == kv.Key.Value {
if kv.Value == nil {
return "", true
}

return kv.Value.Value, true
}
}

}

return "", false
}

func (n *Annotation) Children() []Node {
res := sliceOf(n.Name)
for _, value := range n.KeyValues {
res = append(res, value)
}

return res
}

type TypeDefinition struct {
node
// Description may be nil
Description *Literal `@@?`
Annotations []*Annotation `("@" @@)*`
Type NamedType `@@`
Description *Literal `@@?`
Annotations []*Annotation `("@" @@)*`
Type NamedType `@@`
parsedAnnotations []TypedAnnotation
}

func TypeDefinitionFrom(n Node) *TypeDefinition {
Expand All @@ -122,6 +26,16 @@ func TypeDefinitionFrom(n Node) *TypeDefinition {
return nil
}

// TypedAnnotations parses the Annotations once and caches them internally.
func (n *TypeDefinition) TypedAnnotations() []TypedAnnotation {
if n.parsedAnnotations == nil {
n.parsedAnnotations = make([]TypedAnnotation, 0, len(n.Annotations)) // optimize and allocate on 0 size

}

return n.parsedAnnotations
}

func (n *TypeDefinition) ExpectOnlyOf(names ...string) error {
tmp := map[string]int{}
for _, annotation := range n.Annotations {
Expand Down
5 changes: 5 additions & 0 deletions vsc-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog

## 0.0.27
Optimized print css.


## 0.0.26
Supporting markdown images.
Fixed soft-hard line break behavior.
Expand Down
2 changes: 1 addition & 1 deletion vsc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"publisher": "worldiety",
"description": "Fügt die Unterstützung für die worldiety domain driven design language hinzu.",
"version": "0.0.26",
"version": "0.0.27",
"engines": {
"vscode": "^1.67.0",
"node": ">=12.0.0"
Expand Down

0 comments on commit 584ce33

Please sign in to comment.