44
55from pathlib import Path
66from threading import Lock
7- from typing import Any , Callable , Optional , TYPE_CHECKING , Literal
7+ from typing import TYPE_CHECKING , Any , Callable , Literal , Optional
88from warnings import warn
99
1010from lark import Lark , ParseTree , exceptions
1111
1212from .utils import load_grammar
1313
14-
1514__all__ = [
1615 "RFC3987_SYNTAX_PARSER_TYPE" ,
1716 "RFC3987_SYNTAX_GRAMMAR_PATH" ,
5958 "is_valid_syntax_ipv4address" ,
6059 "is_valid_syntax_dec_octet" ,
6160 "is_valid_syntax_digit" ,
62- "is_valid_syntax_non_zero" ,
6361 "is_valid_syntax_unreserved" ,
6462 "is_valid_syntax_alpha" ,
6563 "is_valid_syntax_hexdig" ,
127125"""
128126_SYNTAX_PARSER_STARTS : list [T_SYNTAX_PARSER_TERM ] = ["iri" , "iri_reference" , "absolute_iri" ]
129127
128+
130129def parse (term : T_SYNTAX_PARSER_TERM , value : str ) -> ParseTree :
131130 """Parse text as one of the top-level RFC 3987 syntax terms.
132131
133- :param term: Start rule used for parsing. Must be one of ``"iri"``,
134- ``"iri_reference"``, or ``"absolute_iri"``.
132+ :param term: Start rule used for parsing.
133+ Must be one of ``"iri"``, ``"iri_reference"``, or ``"absolute_iri"``.
135134 :param value: Input text to parse.
136135 :return: The Lark parse tree for ``value`` under the selected start rule.
137- :raises lark.exceptions.LarkError: If parser initialization fails or parsing
138- cannot be completed.
136+ :raises lark.exceptions.LarkError: If parser initialization fails or parsing cannot be completed.
139137 """
140138
141139 return _get_syntax_parser ().parse (value , start = term )
@@ -146,10 +144,11 @@ def is_valid_syntax(term: T_SYNTAX_PARSER_TERM, value: str) -> bool:
146144
147145 This is a boolean convenience wrapper around :func:`parse`.
148146
149- .. warning:: Emits a ``RuntimeWarning`` (via :func:`warnings.warn`) when a non-``UnexpectedInput`` ``lark.exceptions.LarkError`` occurs.
147+ .. warning:: Emits a ``RuntimeWarning`` (via :func:`warnings.warn`) when a non-``UnexpectedInput``
148+ ``lark.exceptions.LarkError`` occurs.
150149
151- :param term: Start rule used for validation. Must be one of ``"iri"``,
152- ``"iri_reference"``, or ``"absolute_iri"``.
150+ :param term: Start rule used for validation.
151+ Must be one of ``"iri"``, ``"iri_reference"``, or ``"absolute_iri"``.
153152 :param value: Input text to validate.
154153 :return: ``True`` if parsing succeeds; otherwise ``False``.
155154 """
@@ -163,11 +162,12 @@ def is_valid_syntax(term: T_SYNTAX_PARSER_TERM, value: str) -> bool:
163162 # from Lark internals / initialization (non-UnexpectedInput)
164163 warn ("Unexpected LarkError (non-UnexpectedInput) "
165164 f"for term={ term !r} : { type (err ).__name__ } : { err } " ,
166- RuntimeWarning ,
167- stacklevel = 2 )
165+ RuntimeWarning ,
166+ stacklevel = 2 )
168167 return False
169168 return True
170169
170+
171171T_SYNTAX_VALIDATOR = Callable [[str ], bool ]
172172"""Callable validator for one RFC 3987 grammar rule.
173173
@@ -212,6 +212,7 @@ def syntax_validator(text: str) -> bool:
212212_grammar : Optional [str ] = None
213213_grammar_lock = Lock ()
214214
215+
215216def _get_grammar () -> str :
216217 """this is private API"""
217218
@@ -225,6 +226,7 @@ def _get_grammar() -> str:
225226_syntax_parser : Optional [Lark ] = None
226227_syntax_parser_lock = Lock ()
227228
229+
228230def _get_syntax_parser () -> Lark :
229231 """this is private API"""
230232
@@ -239,6 +241,7 @@ def _get_syntax_parser() -> Lark:
239241 parser = RFC3987_SYNTAX_PARSER_TYPE )
240242 return _syntax_parser
241243
244+
242245if TYPE_CHECKING :
243246 def is_valid_syntax_iri (text : str ) -> bool :
244247 """Validate that input text conforms to the RFC 3987 ``IRI`` rule."""
@@ -486,19 +489,6 @@ def is_valid_syntax_digit(text: str) -> bool:
486489else :
487490 is_valid_syntax_digit = make_syntax_validator ("digit" )
488491
489- if TYPE_CHECKING :
490- def is_valid_syntax_non_zero (text : str ) -> bool :
491- """
492- Local helper token used in ``dec_octet``.
493-
494- .. warning::
495- Deprecated: this is not a stable/public entry point and may be removed
496- in a future release.
497- """
498- ...
499- else :
500- is_valid_syntax_non_zero = make_syntax_validator ("non_zero" )
501-
502492if TYPE_CHECKING :
503493 def is_valid_syntax_unreserved (text : str ) -> bool :
504494 """Validate that input text conforms to the RFC 3987 ``unreserved`` rule."""
@@ -571,7 +561,8 @@ def is_valid_syntax_pct_encoded(text: str) -> bool:
571561 "ipv4address" : is_valid_syntax_ipv4address ,
572562 "dec_octet" : is_valid_syntax_dec_octet ,
573563 "digit" : is_valid_syntax_digit ,
574- "non_zero" : is_valid_syntax_non_zero ,
564+ # non_zero - a local helper token used in `dec_octet`
565+ "non_zero" : make_syntax_validator ("non_zero" ),
575566 "unreserved" : is_valid_syntax_unreserved ,
576567 "alpha" : is_valid_syntax_alpha ,
577568 "hexdig" : is_valid_syntax_hexdig ,
@@ -605,6 +596,7 @@ def __getattr__(name: str) -> Any:
605596 return syntax_parser
606597 raise AttributeError (f"module { __name__ !r} has no attribute { name !r} " )
607598
599+
608600def __dir__ () -> list [str ]:
609601 return sorted (set (globals ().keys ()) | set (__all__ ))
610602
0 commit comments