Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ All notable changes to this project will be documented in this file.
* Fix `RFC3987_SYNTAX_TERMS` correctly containing "ipath_rootless" (via [#41])
* Fix `RFC3987_SYNTAX_TERMS` correctly containing "ipath_empty" (via [#41])
* Fix `ucschar` range (via [#45])
* Fix `parse()`'s typehint for arg `term` (via [#50])
Narrowed down to valid values: "iri", "iri_reference", "absolute_iri"
* Fix `is_valid_syntax`'s typehint for arg `term` (via [#50])
Narrowed down to valid values: "iri", "iri_reference", "absolute_iri"
* Added
* Function `is_valid_syntax_scheme` (via [#48])
* Function `is_valid_syntax_non_zero` (via [#48])
Expand All @@ -28,6 +32,7 @@ All notable changes to this project will be documented in this file.
[#45]: https://github.com/jkowalleck/rfc3987-syntax2/pull/45
[#48]: https://github.com/jkowalleck/rfc3987-syntax2/pull/48
[#49]: https://github.com/jkowalleck/rfc3987-syntax2/pull/49
[#50]: https://github.com/jkowalleck/rfc3987-syntax2/pull/50

## v1.2.0 - 2026-07-16

Expand Down
11 changes: 7 additions & 4 deletions src/rfc3987_syntax2/syntax_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pathlib import Path
from threading import Lock
from typing import Any, Callable, Optional, TYPE_CHECKING
from typing import Any, Callable, Optional, TYPE_CHECKING, Literal

from lark import Lark, ParseTree, exceptions

Expand Down Expand Up @@ -110,11 +110,14 @@
"pct_encoded",
]

def parse(term: str, value: str) -> 'ParseTree':
_SYNTAX_PARSER_STARTS = ["iri", "iri_reference", "absolute_iri"]
_SYNTAX_PARSER_TERM = Literal["iri", "iri_reference", "absolute_iri"]

def parse(term: _SYNTAX_PARSER_TERM, value: str) -> 'ParseTree':
return _get_syntax_parser().parse(value, start=term)


def is_valid_syntax(term: str, value: str) -> bool:
def is_valid_syntax(term: _SYNTAX_PARSER_TERM, value: str) -> bool:
try:
parse(term=term, value=value)
return True
Expand Down Expand Up @@ -167,7 +170,7 @@ def _get_syntax_parser() -> Lark:
with _syntax_parser_lock:
if _syntax_parser is None:
_syntax_parser = Lark(grammar,
start=["iri", "iri_reference", "absolute_iri"],
start=_SYNTAX_PARSER_STARTS,
parser=RFC3987_SYNTAX_PARSER_TYPE)
return _syntax_parser

Expand Down