github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/dag/parse.go (about) 1 // Copyright 2022 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package dag implements a language for expressing directed acyclic 6 // graphs. 7 // 8 // The general syntax of a rule is: 9 // 10 // a, b < c, d; 11 // 12 // which means c and d come after a and b in the partial order 13 // (that is, there are edges from c and d to a and b), 14 // but doesn't provide a relative order between a vs b or c vs d. 15 // 16 // The rules can chain together, as in: 17 // 18 // e < f, g < h; 19 // 20 // which is equivalent to 21 // 22 // e < f, g; 23 // f, g < h; 24 // 25 // Except for the special bottom element "NONE", each name 26 // must appear exactly once on the right-hand side of any rule. 27 // That rule serves as the definition of the allowed successor 28 // for that name. The definition must appear before any uses 29 // of the name on the left-hand side of a rule. (That is, the 30 // rules themselves must be ordered according to the partial 31 // order, for easier reading by people.) 32 // 33 // Negative assertions double-check the partial order: 34 // 35 // i !< j 36 // 37 // means that it must NOT be the case that i < j. 38 // Negative assertions may appear anywhere in the rules, 39 // even before i and j have been defined. 40 // 41 // Comments begin with #. 42 package dag 43 44 type Graph struct { 45 Nodes []string 46 byLabel map[string]int 47 edges map[string]map[string]bool 48 } 49 50 func (g *Graph) AddEdge(from, to string) 51 52 func (g *Graph) DelEdge(from, to string) 53 54 func (g *Graph) HasEdge(from, to string) bool 55 56 func (g *Graph) Edges(from string) []string 57 58 // Parse parses the DAG language and returns the transitive closure of 59 // the described graph. In the returned graph, there is an edge from "b" 60 // to "a" if b < a (or a > b) in the partial order. 61 func Parse(dag string) (*Graph, error)