package goabnf

import (
	
	
)

// Identifies left-recursive rules so the recognizer can handle them by seed-growing
// instead of refusing them. A rule is left-recursive when it can re-derive itself
// without consuming input -- i.e. it appears on a cycle of the "left-corner"
// relation: A left-corners B if B can begin a derivation of A at the same input
// position. Crucially this skips over prefixes that can vanish (zero-min repetitions,
// options, and nullable elements), so it also catches *hidden* left recursion such as
// `a = b a / "x"` where `b` is nullable -- which a purely positional check misses.

// nullableRules computes, by least fixpoint over user and core rules, which rules can
// derive the empty string.
func ( *Grammar) () map[string]bool {
	 := map[string]*Rule{}
	for  := range .Rulemap {
		if  := GetRule(, .Rulemap);  != nil {
			[strings.ToLower(.Name)] = 
		}
	}
	for  := range coreRules {
		if  := GetRule(, .Rulemap);  != nil {
			[strings.ToLower(.Name)] = 
		}
	}
	 := map[string]bool{}
	for  := true; ; {
		 = false
		for ,  := range  {
			if ![] && altNullable(, , .Alternation) {
				[] = true
				 = true
			}
		}
	}
	return 
}

func altNullable( *Grammar,  map[string]bool,  Alternation) bool {
	for ,  := range .Concatenations {
		 := true
		for ,  := range .Repetitions {
			if !repNullable(, , ) {
				 = false
				break
			}
		}
		if  {
			return true
		}
	}
	return false
}

func repNullable( *Grammar,  map[string]bool,  Repetition) bool {
	if .Min == 0 {
		return true
	}
	return elemNullable(, , .Element)
}

func elemNullable( *Grammar,  map[string]bool,  ElemItf) bool {
	switch v := .(type) {
	case ElemOption:
		return true
	case ElemGroup:
		return altNullable(, , .Alternation)
	case ElemRulename:
		return [strings.ToLower(.Name)]
	case ElemCharVal:
		return len(.Values) == 0
	}
	// num-val always consumes; prose-val is treated as unmatchable.
	return false
}

// leftRecursiveSCCs returns, for every left-recursive rule (keyed by lowercase
// name), the set of rule names in its left-corner strongly-connected component
// (mutually left-recursive rules grown together). Non-left-recursive rules are
// absent from the map.
func ( *Grammar) () map[string][]string {
	 := .nullableRules()

	// Build the left-corner dependency graph and reuse Tarjan's SCC finder.
	 := Depgraph{}
	 := func( *Rule) {
		 := strings.ToLower(.Name)
		if ,  := [];  {
			return
		}
		[] = &node{Rulename: .Name, Dependencies: leftCornerDeps(, , .Alternation)}
	}
	for  := range .Rulemap {
		if  := GetRule(, .Rulemap);  != nil {
			()
		}
	}
	for  := range coreRules {
		if  := GetRule(, .Rulemap);  != nil {
			()
		}
	}

	 := &cycle{index: 0, stack: []*node{}, dg: }
	.find()

	 := map[string][]string{}
	for ,  := range .sccs {
		 := make([]string, len())
		for ,  := range  {
			[] = strings.ToLower(.Rulename)
		}
		 := len() > 1
		if ! { // size 1: left-recursive only with a self left-corner
			 = slices.Contains([0].Dependencies, [0])
		}
		if  {
			for ,  := range  {
				[] = 
			}
		}
	}
	return 
}

// leftCornerDeps collects the rules reachable as a left corner of alt: walk each
// concatenation left to right, collecting rulename references, and stop at the
// first repetition that cannot vanish.
func leftCornerDeps( *Grammar,  map[string]bool,  Alternation) []string {
	 := []string{}
	var  func(Alternation)
	 := func( ElemItf) {
		switch v := .(type) {
		case ElemRulename:
			 = appendDeps(, strings.ToLower(.Name))
		case ElemGroup:
			(.Alternation)
		case ElemOption:
			(.Alternation)
		}
	}
	 = func( Alternation) {
		for ,  := range .Concatenations {
			for ,  := range .Repetitions {
				(.Element)
				if .Min == 0 || elemNullable(, , .Element) {
					continue // this position can be empty; the next is also a left corner
				}
				break
			}
		}
	}
	()
	return 
}