package goabnf

import (
	
	
	
)

// int32ToNumval converts an integer value into its numeric-string representation
// in the given base. It is the inverse of numvalToInt32 and produces the form
// stored in an ElemNumVal's Elems (e.g. 97 in base "x" -> "61").
func int32ToNumval( int32,  string) string {
	switch  {
	case "B", "b":
		return strconv.FormatInt(int64(), 2)
	case "D", "d":
		return strconv.FormatInt(int64(), 10)
	case "X", "x":
		return strings.ToUpper(strconv.FormatInt(int64(), 16))
	}
	return strconv.FormatInt(int64(), 10)
}

// baseRadix maps an ABNF num-val base marker (bin/dec/hex) to its integer radix.
func baseRadix( string) int {
	switch  {
	case "B", "b":
		return 2
	case "D", "d":
		return 10
	case "X", "x":
		return 16
	}
	return 10
}

// numvalToUint64 parses a num-val literal in the given base into its numeric
// value. ok is false only when the literal does not fit in 64 bits (or is
// malformed), which lets callers degrade gracefully instead of overflowing
// silently. This is the single overflow-safe entry point every other conversion
// builds on.
//
// A num-val is a plain non-negative integer, not intrinsically a Unicode scalar:
// values beyond U+10FFFF are legitimate (e.g. for non-textual / on-wire
// grammars). Unicode is handled as the common case at match time; this layer is
// no longer Unicode-bounded.
func numvalToUint64(,  string) (uint64, bool) {
	,  := strconv.ParseUint(, baseRadix(), 64)
	if  != nil {
		return 0, false
	}
	return , true
}

// numvalToInt32 converts a num-val into an int32 codepoint. It never panics: a
// value that does not fit a positive int32 is saturated to math.MaxInt32, which
// is above the maximum Unicode scalar (U+10FFFF) and therefore matches no decoded
// rune. A range bound that merely straddles the Unicode ceiling still covers its
// representable (Unicode) subset, because the saturated upper bound stays above
// every rune.
func numvalToInt32(,  string) int32 {
	,  := numvalToUint64(, )
	if ! ||  > math.MaxInt32 {
		return math.MaxInt32
	}
	return int32()
}

// numvalToRune converts a num-val into the corresponding rune. It never panics;
// a value beyond the Unicode range yields a rune that no UTF-8 input can produce,
// so callers matching against decoded runes simply find no match. Callers that
// require text validity (e.g. a num-val series, which must denote real
// characters) should additionally check utf8.ValidRune.
func numvalToRune(,  string) rune {
	return rune(numvalToInt32(, ))
}

// checkBounds enforces the Unicode-range invariant (value <= U+10FFFF). It is
// the strict-validation path used by SemvalABNF: with validation enabled (the
// default, and the common textual case) a num-val outside the Unicode range is
// rejected. The conversion helpers above are deliberately broader -- a num-val is
// a plain integer and larger values are legitimate for non-textual grammars -- so
// without validation such values are accepted and handled (matched as no rune).
func checkBounds(,  string) error {
	 = strings.TrimLeft(, "0")
	switch  {
	// Whatever the base, the higher value we arbitrary decide to support is
	// the maximal Unicode character i.e. U+10FFFF.
	//
	// Every value (represented as integer) is then in interval [0;1_114_111].
	// For each base, we can compute the number of characters of an acceptable
	// num-val as ceil(log(base,1_114_111)).
	//
	// We also have to ensure that it does not go over the boundaries even if
	// of an acceptable length, e.g., 0xFFFFFF is not.

	case "B", "b":
		// 21 = ceil(log(2, max_unicode))
		// U+10FFFF = Ob100001111111111111111
		if len() > 21 ||
			(len() == 21 && ([1] != '0' || [2] != '0' || [3] != '0' || [4] != '0')) {
			return &ErrTooLargeNumeral{
				Base:  ,
				Value: ,
			}
		}

	case "D", "d":
		// 7 = ceil(log(10, max_unicode))
		// U+10FFFF = 0d1114111
		if len() > 7 ||
			(len() == 7 && ([0] > '1' || ([0] == '1' && ([1] > '1' || ([1] == '1' && ([2] > '1' || ([2] == '1' && ([3] > '4' || ([3] == '4' && ([4] > '1' || ([4] == '1' && ([5] > '1' || ([5] == '1' && [6] > '1'))))))))))))) {
			return &ErrTooLargeNumeral{
				Base:  ,
				Value: ,
			}
		}

	case "X", "x":
		// 6 = ceil(log(16, max_unicode))
		// U+10FFFF = 0x10FFFF
		if len() > 6 ||
			(len() == 6 && ([0] > '1' || ([0] == '1' && ([1] > '0')))) {
			return &ErrTooLargeNumeral{
				Base:  ,
				Value: ,
			}
		}
	}

	return nil
}

// GetRule returns the rule by the given rulename, whether
// it is a core rule or present in the grammar, or nil if not found.
// It validates the RFC 5234 Section 2.1 "rule names are case insensitive".
func ( string,  map[string]*Rule) *Rule {
	// First look in the newly defined rules, as we could override definitions
	if  := getRuleIn(, );  != nil {
		return 
	}
	return getRuleIn(, coreRules)
}

func getRuleIn( string,  map[string]*Rule) *Rule {
	for ,  := range  {
		if strings.EqualFold(, .Name) {
			return 
		}
	}
	return nil
}