private String generateMinMaxInt(Integer min_value, Integer max_value, int decimals_left, boolean top_level) {
        var content = new StringBuilder();

        if (min_value != null && max_value != null) {
            if (min_value < 0 && max_value < 0) {
                content.append("\"-\" (")
                        .append(generateMinMaxInt(-max_value, -min_value, decimals_left, true))
                        .append(")");
                return content.toString();
            }

            if (min_value < 0) {
                content.append("\"-\" (")
                        .append(generateMinMaxInt(0, -min_value, decimals_left, true))
                        .append(") | ");
                min_value = 0;
            }

            var min_s = Integer.toString(min_value);
            var max_s = Integer.toString(max_value);
            var minDigits = min_s.length();
            var maxDigits = max_s.length();

            for (var digits = minDigits; digits < maxDigits; digits++) {
                content.append(generateUniformRange(min_s, "9".repeat(digits)));
                min_s = "1" + "0".repeat(digits);
                content.append(" | ");
            }

            content.append(generateUniformRange(min_s, max_s));
            return content.toString();
        }

        if (min_value != null) {
            if (min_value < 0) {
                content.append("\"-\" (")
                        .append(generateMinMaxInt(null, -min_value, decimals_left, false))
                        .append(") | [0] | [1-9] ")
                        .append(generateMoreDigits(0, decimals_left - 1));
            } else if (min_value == 0) {
                if (top_level) {
                    content.append("[0] | [1-9] ")
                            .append(generateMoreDigits(0, decimals_left - 1));
                } else {
                    content.append(generateMoreDigits(1, decimals_left));
                }
            } else if (min_value <= 9) {
                var c = Integer.toString(min_value);
                var rangeStart = top_level ? "1" : "0";
                if (c.compareTo(rangeStart) > 0) {
                    content.append(generateUniformRange(rangeStart, Character.toString((char) (c.charAt(0) - 1))))
                            .append(" ")
                            .append(generateMoreDigits(1, decimals_left - 1))
                            .append(" | ");
                }

                content.append(generateUniformRange(c, "9"))
                        .append(" ")
                        .append(generateMoreDigits(0, decimals_left - 1));
            } else {
                var min_s = Integer.toString(min_value);
                var length = min_s.length();
                var c = min_s.charAt(0);

                if (c > '1') {
                    content.append(generateUniformRange(top_level ? "1" : "0", Character.toString((char) (c - 1))))
                            .append(" ")
                            .append(generateMoreDigits(length, decimals_left - 1))
                            .append(" | ");
                }

                content.append(generateUniformRange(Character.toString(c), Character.toString(c)))
                        .append(" (")
                        .append(generateMinMaxInt(Integer.parseInt(min_s.substring(1)), null, decimals_left - 1, false))
                        .append(")");

                if (c < '9') {
                    content.append(" | ")
                            .append(generateUniformRange(Character.toString((char) (c + 1)), "9"))
                            .append(" ")
                            .append(generateMoreDigits(length - 1, decimals_left - 1));
                }
            }
            return content.toString();
        }

        if (max_value != null) {
            if (max_value >= 0) {
                if (top_level) {
                    content.append("\"-\" [1-9] ")
                            .append(generateMoreDigits(0, decimals_left - 1))
                            .append(" | ");
                }
                content.append(generateMinMaxInt(0, max_value, decimals_left, true));
            } else {
                content.append("\"-\" (")
                        .append(generateMinMaxInt(-max_value, null, decimals_left, false))
                        .append(")");
            }

            return content.toString();
        }

        throw new RuntimeException("At least one of min_value or max_value must be set");
    }
Converts a JSON Schema to a GBNF grammar to be used for structured output with llama.cpp.
It is a port of Python implementation from llama.cpp repository.
Current implementation doesn't support regex patterns, resolving $ref or fetching schemas from URLs.
The plan is to replace this with C++ implementation from llama.cpp though JNI.
model-integration/src/main/java/ai/vespa/llm/clients/JsonSchemaToGrammar.java