
    *`i->                     .   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ ddlmZmZ d	e	eeeef         ef         d
efdZde	eee         eeef         f         d
efdZdee         deeef         d
efdZ G d de          ZdS )z3This module provides classes representing grammars.    N)AnyDictListOptionalTupleTypeUnionoverload)	BaseModel)
deprecated   )	XGRObject_core)StructuralTagStructuralTagIteminstancereturnc                     t          | t                    rt          j        |           S t          | t                    r| S t          | t
                    r|                                 S t          d          )a)  Convert a instance to a string representation. It returns the schema in string format because
    it's faster to send to C++.

    This function handles different instance input types and converts them to a JSON string:
    - StructuralTag.
    - String inputs are returned as-is (assumed to be valid JSON)
    - Dictionary inputs are converted to JSON strings

    Parameters
    ----------
    instance : Union[str, StructuralTag, Dict[str, Any]]
        The instance to convert, which can be a StructuralTag,
        a JSON schema string, or a dictionary representing a JSON schema.

    Returns
    -------
    str
        The JSON schema as a string.

    Raises
    ------
    ValueError
        When the instance type is not supported.
    TypeError
        When he dictionary is not serializable.
    zInvalid instance type)
isinstancedictjsondumpsstrr   model_dump_json
ValueError)r   s    d/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/xgrammar/grammar.py_convert_instance_to_strr      sp    6 (D!! 2z(###	Hc	"	" 2	Hm	,	, 2'')))0111    schemac                    t          | t                    rt          | t                    r{t	          | d          r&t          j        |                                           S t	          | d          r&t          j        |                                           S t          d          t          | t                    r| S t          | t                    rt          j        |           S t          d          )aS  Convert a schema to a string representation. It returns the schema in string format because
    it's faster to send to C++.

    This function handles different schema input types and converts them to a JSON string:
    - Pydantic models are converted using their schema methods
    - String inputs are returned as-is (assumed to be valid JSON)
    - Dictionary inputs are converted to JSON strings

    Parameters
    ----------
    schema : Union[str, Type[BaseModel], Dict[str, Any]]
        The schema to convert, which can be a Pydantic model class,
        a JSON schema string, or a dictionary representing a JSON schema.

    Returns
    -------
    str
        The JSON schema as a string.

    Raises
    ------
    ValueError
        When the schema type is not supported.
    TypeError
        When the dictionary is not serializable.
    model_json_schemaschema_jsonzAThe schema should have a model_json_schema or json_schema method.z2The schema should be a string or a Pydantic model.)r   type
issubclassr   hasattrr   r   r!   r"   r   r   r   )r   s    r   _convert_schema_to_strr&   2   s    6 &$ OJvy$A$A O6.// 	::f66889996=)) 	b:f0022333`aaa	FC	 	  O	FD	!	! Oz&!!!MNNNr   argskwargsc                    t          |           dk    rLt          | d         t          t          t          f          rt          | d                   S t          d          t          |           dk    rkt          | d         t                    rPt          | d         t                    r5t	          j        | d         | d                   	                    d          S d|v rt          |d                   S d|v r9d	|v r5t	          j        |d         |d	                   	                    d          S t          d
          )a  Get the structural tag string from the arguments. It returns the structural tag in string
    format because it's faster to send to C++.

    Parameters
    ----------
    args : List[Any]
        The positional arguments.
    kwargs : Dict[str, Any]
        The keyword arguments.

    Returns
    -------
    str
        The structural tag string.

    Raises
    ------
    TypeError
        When the arguments are invalid.
    r   r   z-Invalid argument type for from_structural_tag   N)indentstructural_tagtagstriggersz)Invalid arguments for from_structural_tag)
lenr   r   r   r   r   	TypeErrorlistfrom_legacy_structural_tagr   )r'   r(   s     r   !_get_structural_tag_str_from_argsr3   \   s4   * 4yyA~~d1gT=9:: 	M+DG444KLLL	TaJtAw55*T!Wd:S:S7QaIIYY Z 
 
 	
 
V	#	#'/?(@AAA	6		jF2276NF:.
 

//
&
&	' CDDDr   c                   h   e Zd ZdZdefdZedddededd fd            Zed	d
d
d	d
dddeee	e
         eeef         f         dedee         deeeef                  dedee         dedd fd            Zedddededd fd            Zeedeeeeeef         f         dd fd                        Zee ed          dee         dee         dd fd                                    Zed&d            Zed&d            Zed'd!            Zed'd"            Zdefd#Zed$edd fd%            Zd
S )(Grammara  This class represents a grammar object in XGrammar, and can be used later in the
    grammar-guided generation.

    The Grammar object supports context-free grammar (CFG). EBNF (extended Backus-Naur Form) is
    used as the format of the grammar. There are many specifications for EBNF in the literature,
    and we follow the specification of GBNF (GGML BNF) in
    https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md.

    When printed, the grammar will be converted to GBNF format.
    r   c                 4    | j                                         S )zPrint the BNF grammar to a string, in EBNF format.

        Returns
        -------
        grammar_string : str
            The BNF grammar string.
        )_handle	to_stringselfs    r   __str__zGrammar.__str__   s     |%%'''r   root)root_rule_nameebnf_stringr=   c                r    t                               t          j                             | |                    S )a  Construct a grammar from EBNF string. The EBNF string should follow the format
        in https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md.

        Parameters
        ----------
        ebnf_string : str
            The grammar string in EBNF format.

        root_rule_name : str, default: "root"
            The name of the root rule in the grammar.

        Raises
        ------
        RuntimeError
            When converting the regex pattern fails, with details about the parsing error.
        )r5   _create_from_handler   	from_ebnf)r>   r=   s     r   rA   zGrammar.from_ebnf   s+    $ **5=+B+B;P^+_+_```r   TNF)any_whitespacer+   
separatorsstrict_modemax_whitespace_cntprint_converted_ebnfr   rB   r+   rC   rD   rE   rF   c                    t          |           }t                              t          j                            |||||||                    S )a  Construct a grammar from JSON schema. Pydantic model or JSON schema string can be
        used to specify the schema.

        It allows any whitespace by default. If user want to specify the format of the JSON,
        set `any_whitespace` to False and use the `indent` and `separators` parameters. The
        meaning and the default values of the parameters follows the convention in json.dumps().

        It internally converts the JSON schema to a EBNF grammar.

        Parameters
        ----------
        schema : Union[str, Type[BaseModel], Dict[str, Any]]
            The schema string or Pydantic model or JSON schema dict.

        any_whitespace : bool, default: True
            Whether to use any whitespace. If True, the generated grammar will ignore the
            indent and separators parameters, and allow any whitespace.

        indent : Optional[int], default: None
            The number of spaces for indentation. If None, the output will be in one line.

            Note that specifying the indentation means forcing the LLM to generate JSON strings
            strictly formatted. However, some models may tend to generate JSON strings that
            are not strictly formatted. In this case, forcing the LLM to generate strictly
            formatted JSON strings may degrade the generation quality. See
            <https://github.com/sgl-project/sglang/issues/2216#issuecomment-2516192009> for more
            details.

        separators : Optional[Tuple[str, str]], default: None
            Two separators used in the schema: comma and colon. Examples: (",", ":"), (", ", ": ").
            If None, the default separators will be used: (",", ": ") when the indent is not None,
            and (", ", ": ") otherwise.

        strict_mode : bool, default: True
            Whether to use strict mode. In strict mode, the generated grammar will not allow
            properties and items that is not specified in the schema. This is equivalent to
            setting unevaluatedProperties and unevaluatedItems to false.

            This helps LLM to generate accurate output in the grammar-guided generation with JSON
            schema.

        max_whitespace_cnt : Optional[int], default: None
            The maximum number of whitespace characters allowed between elements, such like keys, values, separators and so on.
            If None, there is no limit on the number of whitespace characters.
            If specified, it will limit the number of whitespace characters to at most max_whitespace_cnt.
            It should be a positive integer.

        print_converted_ebnf : bool, default: False
            If True, the converted EBNF string will be printed. For debugging purposes.

        Returns
        -------
        grammar : Grammar
            The constructed grammar.

        Raises
        ------
        RuntimeError
            When converting the json schema fails, with details about the parsing error.
        )r&   r5   r@   r   from_json_schema)r   rB   r+   rC   rD   rE   rF   
schema_strs           r   rH   zGrammar.from_json_schema   sT    N ,F33
**M**"$ 

 

 
	
r   )rF   regex_stringc                r    t                               t          j                             | |                    S )a  Create a grammar from a regular expression string.

        Parameters
        ----------
        regex_string : str
            The regular expression pattern to create the grammar from.

        print_converted_ebnf : bool, default: False
            This method will convert the regex pattern to EBNF first. If this is true, the converted
            EBNF string will be printed. For debugging purposes. Default: False.

        Returns
        -------
        grammar : Grammar
            The constructed grammar from the regex pattern.

        Raises
        ------
        RuntimeError
            When parsing the regex pattern fails, with details about the parsing error.
        )r5   r@   r   
from_regex)rJ   rF   s     r   rL   zGrammar.from_regex  s2    . **M$$\3GHH
 
 	
r   r,   c                     d S N )r,   s    r   from_structural_tagzGrammar.from_structural_tag  s	     Cr   zqfrom_structural_tag(tags, triggers) is deprecated. Construct structural tag with the StructuralTag class instead.r-   r.   c                     d S rN   rO   )r-   r.   s     r   rP   zGrammar.from_structural_tag#  s     _b^ar   c                      t          | |          }t                              t          j                            |                    S )ag  Create a grammar from a structural tag. See the Structural Tag Usage in XGrammar
        documentation for its usage.

        This method supports two calling patterns:

        1. Single structural tag parameter:
           from_structural_tag(structural_tag)

        2. Legacy pattern (deprecated):
           from_structural_tag(tags, triggers)

        Parameters
        ----------
        structural_tag : Union[StructuralTag, str, Dict[str, Any]]
            The structural tag either as a StructuralTag object, or a JSON string or a dictionary.

        tags : List[StructuralTagItem]
            (Deprecated) The structural tags. Use StructuralTag class instead.

        triggers : List[str]
            (Deprecated) The triggers. Use StructuralTag class instead.

        Returns
        -------
        grammar : Grammar
            The constructed grammar from the structural tag.

        Raises
        ------
        InvalidJSONError
            When the structural tag is not a valid JSON string.
        InvalidStructuralTagError
            When the structural tag is not valid.
        TypeError
            When the arguments are invalid.

        Notes
        -----
        The legacy pattern from_structural_tag(tags, triggers) is deprecated. Use the StructuralTag
        class to construct structural tags instead.

        For the deprecated pattern: The structural tag handles the dispatching of different grammars
        based on the tags and triggers: it initially allows any output, until a trigger is
        encountered, then dispatch to the corresponding tag; when the end tag is encountered, the
        grammar will allow any following output, until the next trigger is encountered. See the
        Advanced Topics of the Structural Tag in XGrammar documentation for its semantic.
        Structural Tag in XGrammar documentation for its semantic.
        )r3   r5   r@   r   rP   )r'   r(   structural_tag_strs      r   rP   zGrammar.from_structural_tag+  s:    d ?tVLL**5=+L+LM_+`+`aaar   c                  n    t                               t          j                                                   S )zGet the grammar of standard JSON. This is compatible with the official JSON grammar
        specification in https://www.json.org/json-en.html.

        Returns
        -------
        grammar : Grammar
            The JSON grammar.
        )r5   r@   r   builtin_json_grammarrO   r   r   rU   zGrammar.builtin_json_grammar`  s&     **5=+M+M+O+OPPPr   grammarsc                      d | D             }t                               t          j                             |                    S )a  Create a grammar that matches the concatenation of the grammars in the list. That is
        equivalent to using the `+` operator to concatenate the grammars in the list.

        Parameters
        ----------
        grammars : List[Grammar]
            The grammars to create the concatenation of.

        Returns
        -------
        grammar : Grammar
            The concatenation of the grammars.
        c                     g | ]	}|j         
S rO   r7   .0grammars     r   
<listcomp>z"Grammar.concat.<locals>.<listcomp>{      CCCw7?CCCr   )r5   r@   r   concatrV   grammar_handless     r   r_   zGrammar.concatl  s<     DC(CCC**5=+?+?+P+PQQQr   c                      d | D             }t                               t          j                             |                    S )a  Create a grammar that matches any of the grammars in the list. That is equivalent to
        using the `|` operator to concatenate the grammars in the list.

        Parameters
        ----------
        grammars : List[Grammar]
            The grammars to create the union of.

        Returns
        -------
        grammar : Grammar
            The union of the grammars.
        c                     g | ]	}|j         
S rO   rY   rZ   s     r   r]   z!Grammar.union.<locals>.<listcomp>  r^   r   )r5   r@   r   unionr`   s     r   rd   zGrammar.union~  s<     DC(CCC**5=+>+>+O+OPPPr   c                 4    | j                                         S )zSerialize the grammar to a JSON string.

        Returns
        -------
        json_string : str
            The JSON string.
        )r7   serialize_jsonr9   s    r   rf   zGrammar.serialize_json  s     |**,,,r   json_stringc                 p    t                               t          j                             |                     S )aQ  Deserialize a grammar from a JSON string.

        Parameters
        ----------
        json_string : str
            The JSON string.

        Returns
        -------
        grammar : Grammar
            The deserialized grammar.

        Raises
        ------
        InvalidJSONError
            When the JSON string is invalid.
        DeserializeFormatError
            When the JSON string does not follow the serialization format of the grammar.
        DeserializeVersionError
            When the __VERSION__ field in the JSON string is not the same as the current version.
        )r5   r@   r   deserialize_json)rg   s    r   ri   zGrammar.deserialize_json  s(    . **5=+I+I++V+VWWWr   )r   r5   )rV   r5   r   r5   )__name__
__module____qualname____doc__r   r;   staticmethodrA   r	   r   r   r   r   boolr   intr   rH   rL   r
   r   rP   r   r   r   rU   r_   rd   rf   ri   rO   r   r   r5   r5      s       	 	( ( ( ( ( =C a a as as a	 a a a \a&   $ $04 ,0%*Q
 Q
 Q
c4	?DcN:;Q
 Q
 	Q

 U38_-Q
 Q
 %SMQ
 #Q
 
Q
 Q
 Q
 \Q
f FK 
 
 
 
t 
PY 
 
 
 \
4 mS$sCx.@A	   \ X Z	'  b$'8"9aT#YaS\aaa	  \ X b2b 2b 2b \2bh 	Q 	Q 	Q \	Q R R R \R" Q Q Q \Q"- - - - - Xc Xi X X X \X X Xr   r5   )rm   r   typingr   r   r   r   r   r   r	   r
   pydanticr   typing_extensionsr   baser   r   r,   r   r   r   r   r&   r3   r5   rO   r   r   <module>ru      s   9 9  J J J J J J J J J J J J J J J J J J J J       ( ( ( ( ( ( " " " " " " " " < < < < < < < <"2uS$sCx.--O'P "2UX "2 "2 "2 "2J'O5d9otCH~)M#N 'OSV 'O 'O 'O 'OT%EDI %EtCH~ %ERU %E %E %E %EPmX mX mX mX mXi mX mX mX mX mXr   