|
| 1 | +package dotdsl |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// Properties holds the properties of a node or edge. |
| 10 | +// The values can be int, bool or string. |
| 11 | +type Properties map[string]any |
| 12 | + |
| 13 | +// Graph stores the parts of a dot graph. |
| 14 | +// All entities are stored as a Properties map (`nil` Properties when none set) |
| 15 | +// attrs is the Properties for the entire Graph, vs a specific node or edge. |
| 16 | +type Graph struct { |
| 17 | + nodes map[string]Properties |
| 18 | + edges map[string]Properties |
| 19 | + attrs Properties |
| 20 | +} |
| 21 | + |
| 22 | +// extractPropSection splits a line like "a -- b [prop=val]" into "a -- b" and "prop=val". |
| 23 | +func extractPropSection(line string) (string, string, error) { |
| 24 | + // Parse the property, if any. |
| 25 | + if !strings.ContainsAny(line, "[]") { |
| 26 | + return line, "", nil |
| 27 | + } |
| 28 | + if strings.Count(line, "[") != 1 || strings.Count(line, "]") != 1 { |
| 29 | + return "", "", fmt.Errorf("incorrect number of []") |
| 30 | + } |
| 31 | + if !strings.HasSuffix(line, "]") { |
| 32 | + return "", "", fmt.Errorf("incorrectly placed ']'") |
| 33 | + } |
| 34 | + line, props, ok := strings.Cut(strings.TrimSuffix(line, "]"), "[") |
| 35 | + if !ok || len(props) < 3 || !strings.Contains(props[1:len(props)-1], "=") { |
| 36 | + return "", "", fmt.Errorf("badly formatted props") |
| 37 | + } |
| 38 | + return line, props, nil |
| 39 | +} |
| 40 | + |
| 41 | +// parseProp turns a prop like `foo=2` into `foo` and `int(5)`. |
| 42 | +// It handles bool, int, quoted strings and (as a fallback) unquoted strings. |
| 43 | +func parseProp(rawProp string) (string, any, error) { |
| 44 | + propName, rawPropVal, hasProp := strings.Cut(rawProp, "=") |
| 45 | + if !hasProp || len(propName) == 0 || len(rawPropVal) == 0 { |
| 46 | + return "", nil, fmt.Errorf("badly formatted prop") |
| 47 | + } |
| 48 | + |
| 49 | + var propVal any |
| 50 | + if rawPropVal == "true" { |
| 51 | + propVal = true |
| 52 | + } else if rawPropVal == "false" { |
| 53 | + propVal = false |
| 54 | + } else if v, err := strconv.Atoi(rawPropVal); err == nil { |
| 55 | + propVal = v |
| 56 | + } else if strings.HasPrefix(rawPropVal, `"`) && strings.HasSuffix(rawPropVal, `"`) { |
| 57 | + propVal = strings.Trim(rawPropVal, `"`) |
| 58 | + } else { |
| 59 | + propVal = rawPropVal |
| 60 | + } |
| 61 | + return propName, propVal, nil |
| 62 | +} |
| 63 | + |
| 64 | +// extractNodes parses a line like "a -- b -- c" to return []string{"a", "b", "c"}. |
| 65 | +func extractNodes(line string) ([]string, error) { |
| 66 | + nodes := strings.Split(line, "--") |
| 67 | + if len(nodes) == 0 || len(nodes) == 1 && nodes[0] == "" { |
| 68 | + return nil, nil |
| 69 | + } |
| 70 | + for i, node := range nodes { |
| 71 | + node = strings.TrimSpace(node) |
| 72 | + nodes[i] = node |
| 73 | + if len(node) == 0 { |
| 74 | + return nil, fmt.Errorf("badly formatted node, line %q", line) |
| 75 | + } |
| 76 | + for _, r := range node { |
| 77 | + if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '1')) { |
| 78 | + return nil, fmt.Errorf("node name should be alnum, got %s", node) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return nodes, nil |
| 83 | +} |
| 84 | + |
| 85 | +// nodesToEdges takes a slice of nodes eg []string{"a", "b", "c"} and returns edges, |
| 86 | +// like []string{"{a b", "{b c}"} |
| 87 | +func nodesToEdges(nodes []string) []string { |
| 88 | + if len(nodes) < 2 { |
| 89 | + return nil |
| 90 | + } |
| 91 | + edges := make([]string, len(nodes)-1) |
| 92 | + for i := range len(nodes) - 1 { |
| 93 | + a, b := nodes[i], nodes[i+1] |
| 94 | + if a > b { |
| 95 | + a, b = b, a |
| 96 | + } |
| 97 | + edges[i] = fmt.Sprintf("{%s %s}", a, b) |
| 98 | + } |
| 99 | + return edges |
| 100 | +} |
| 101 | + |
| 102 | +// addKeys updates a map, setting missing entries to nil entries. |
| 103 | +func addKeys(keys []string, myMap map[string]Properties) { |
| 104 | + for _, key := range keys { |
| 105 | + if _, ok := myMap[key]; !ok { |
| 106 | + myMap[key] = nil |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// setProp updates the Properties in a map for a set of keys. |
| 112 | +func setProp(keys []string, myMap map[string]Properties, prop string, value any) { |
| 113 | + for _, key := range keys { |
| 114 | + if myMap[key] == nil { |
| 115 | + myMap[key] = Properties{} |
| 116 | + } |
| 117 | + myMap[key][prop] = value |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// parseLine updates the Graph with a single line. |
| 122 | +func (g *Graph) parseLine(line string) error { |
| 123 | + // Check the line format. |
| 124 | + line = strings.TrimSpace(line) |
| 125 | + if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") { |
| 126 | + return nil |
| 127 | + } |
| 128 | + if !strings.HasSuffix(line, ";") { |
| 129 | + return fmt.Errorf("line does not end in a semicolon") |
| 130 | + } |
| 131 | + line = strings.TrimSuffix(line, ";") |
| 132 | + |
| 133 | + // Parse the property, if any. |
| 134 | + var propName string |
| 135 | + var propVal any |
| 136 | + var hasProp bool |
| 137 | + |
| 138 | + if lineSection, propSection, err := extractPropSection(line); err != nil { |
| 139 | + return err |
| 140 | + } else if propSection != "" { |
| 141 | + hasProp = true |
| 142 | + line = lineSection |
| 143 | + if propName, propVal, err = parseProp(propSection); err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + nodes, err := extractNodes(line) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + edges := nodesToEdges(nodes) |
| 153 | + addKeys(nodes, g.nodes) |
| 154 | + addKeys(edges, g.edges) |
| 155 | + |
| 156 | + if hasProp { |
| 157 | + switch len(nodes) { |
| 158 | + case 0: |
| 159 | + g.attrs[propName] = propVal |
| 160 | + case 1: |
| 161 | + setProp(nodes, g.nodes, propName, propVal) |
| 162 | + default: |
| 163 | + setProp(edges, g.edges, propName, propVal) |
| 164 | + } |
| 165 | + } |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +// Parse creates a Graph from a text blob. |
| 170 | +func Parse(data string) (*Graph, error) { |
| 171 | + g := &Graph{make(map[string]Properties), make(map[string]Properties), Properties{}} |
| 172 | + lines := strings.Split(data, "\n") |
| 173 | + // Check the graph start/end. |
| 174 | + if len(lines) < 2 || lines[0] != "graph {" || lines[len(lines)-1] != "}" { |
| 175 | + return nil, fmt.Errorf("not a graph") |
| 176 | + } |
| 177 | + for i, line := range lines[1 : len(lines)-1] { |
| 178 | + if err := g.parseLine(line); err != nil { |
| 179 | + return nil, fmt.Errorf("line %d: %q -- parse error %w", i, line, err) |
| 180 | + } |
| 181 | + } |
| 182 | + return g, nil |
| 183 | +} |
0 commit comments