package goabnf
import (
"fmt"
"slices"
"strings"
)
type node struct {
Rulename string
index, lowlink int
onStack bool
Dependencies []string
}
type Depgraph map [string ]*node
func (g *Grammar ) DependencyGraph () Depgraph {
graph := Depgraph {}
for _ , corerule := range coreRules {
graph [strings .ToLower (corerule .Name )] = &node {
Rulename : corerule .Name ,
Dependencies : getDependencies (corerule .Alternation ),
}
}
for _ , rule := range g .Rulemap {
graph [strings .ToLower (rule .Name )] = &node {
Rulename : rule .Name ,
Dependencies : getDependencies (rule .Alternation ),
}
}
return graph
}
func getDependencies(alt Alternation ) []string {
deps := []string {}
for _ , conc := range alt .Concatenations {
for _ , rep := range conc .Repetitions {
switch v := rep .Element .(type ) {
case ElemGroup :
deps = appendDeps (deps , getDependencies (v .Alternation )...)
case ElemOption :
deps = appendDeps (deps , getDependencies (v .Alternation )...)
case ElemRulename :
deps = appendDeps (deps , strings .ToLower (v .Name ))
}
}
}
return deps
}
func appendDeps(deps []string , ndeps ...string ) []string {
for _ , ndep := range ndeps {
if !slices .Contains (deps , ndep ) {
deps = append (deps , ndep )
}
}
return deps
}
func (dg Depgraph ) Mermaid () string {
var out strings .Builder
out .WriteString ("flowchart TD\n" )
for _ , node := range dg {
for _ , dep := range node .Dependencies {
fmt .Fprintf (&out , "\t%s --> %s\n" , node .Rulename , dep )
}
out .WriteString ("\n" )
}
return out .String ()
}
func (g *Grammar ) IsDAG () bool {
scc := &cycle {
index : 0 ,
stack : []*node {},
dg : g .DependencyGraph (),
}
scc .find ()
for _ , sccs := range scc .sccs {
if len (sccs ) > 1 {
return false
}
}
return true
}
func (g *Grammar ) RuleContainsCycle (rulename string ) (bool , error ) {
rule := GetRule (rulename , g .Rulemap )
if rule == nil {
return false , &ErrRuleNotFound {
Rulename : rulename ,
}
}
scc := &cycle {
index : 0 ,
stack : []*node {},
dg : g .DependencyGraph (),
}
scc .find ()
return ruleContainsCycle (scc .sccs , rulename ), nil
}
func ruleContainsCycle(sccs [][]*node , rulename string ) bool {
nodeOf := make (map [string ]*node , len (sccs ))
inCycle := make (map [string ]bool , len (sccs ))
for _ , s := range sccs {
nontrivial := len (s ) > 1
for _ , nd := range s {
key := strings .ToLower (nd .Rulename )
nodeOf [key ] = nd
inCycle [key ] = nontrivial
}
}
return ruleCycleVisit (strings .ToLower (rulename ), nodeOf , inCycle , map [string ]bool {})
}
func ruleCycleVisit(key string , nodeOf map [string ]*node , inCycle , visited map [string ]bool ) bool {
if visited [key ] {
return false
}
visited [key ] = true
nd := nodeOf [key ]
if nd == nil {
return false
}
for _ , dep := range nd .Dependencies {
if strings .ToLower (dep ) == key {
return true
}
}
if inCycle [key ] {
return true
}
for _ , dep := range nd .Dependencies {
dk := strings .ToLower (dep )
if dk == key {
continue
}
if ruleCycleVisit (dk , nodeOf , inCycle , visited ) {
return true
}
}
return false
}
type cycle struct {
index int
stack []*node
sccs [][]*node
dg Depgraph
}
func (c *cycle ) find () {
for _ , v := range c .dg {
if v .index == 0 {
c .strongconnect (v )
}
}
}
func (c *cycle ) strongconnect (v *node ) {
v .index = c .index
v .lowlink = c .index
c .index ++
c .stack = append (c .stack , v )
v .onStack = true
for _ , dep := range v .Dependencies {
w , ok := c .dg [dep ]
if !ok {
continue
}
if w .index == 0 {
c .strongconnect (w )
v .lowlink = min (v .lowlink , w .lowlink )
} else {
if w .onStack {
v .lowlink = min (v .lowlink , w .index )
}
}
}
if v .lowlink == v .index {
scc := []*node {}
w := (*node )(nil )
for w == nil || !strings .EqualFold (v .Rulename , w .Rulename ) {
w = c .stack [len (c .stack )-1 ]
c .stack = c .stack [:len (c .stack )-1 ]
w .onStack = false
scc = append (scc , w )
}
c .sccs = append (c .sccs , scc )
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .