Files
197f3068c2 Add filters support to generic forwarding (#2742)
* Add filtering support to Generic Forwarding

* Update CHANGELOG.md

* Update forwarder_test.go

* Update distributor component documentation

* Revert Makefile change

* Update docs/sources/tempo/configuration/_index.md

Co-authored-by: Kim Nylander <[email protected]>

* Update docs/sources/tempo/operations/generic_forwarding.md

Co-authored-by: Kim Nylander <[email protected]>

* Update CHANGELOG.md

---------

Co-authored-by: Kim Nylander <[email protected]>
2023-08-04 16:54:50 +02:00

60 lines
1.0 KiB
Go

package participle
import (
"fmt"
"strings"
)
// Perform some post-construction validation. This currently does:
//
// Checks for left recursion.
func validate(n node) error {
checked := map[*strct]bool{}
seen := map[node]bool{}
return visit(n, func(n node, next func() error) error {
if n, ok := n.(*strct); ok {
if !checked[n] && isLeftRecursive(n) {
return fmt.Errorf("left recursion detected on\n\n%s", indent(n.String()))
}
checked[n] = true
if seen[n] {
return nil
}
}
seen[n] = true
return next()
})
}
func isLeftRecursive(root *strct) (found bool) {
defer func() { _ = recover() }()
seen := map[node]bool{}
_ = visit(root.expr, func(n node, next func() error) error {
if found {
return nil
}
switch n := n.(type) {
case *strct:
if root.typ == n.typ {
found = true
}
case *sequence:
if !n.head {
panic("done")
}
}
if seen[n] {
return nil
}
seen[n] = true
return next()
})
return
}
func indent(s string) string {
return " " + strings.Join(strings.Split(s, "\n"), "\n ")
}