forked from toon-format/toon-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_literal_utils.py
More file actions
78 lines (62 loc) · 2.3 KB
/
_literal_utils.py
File metadata and controls
78 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (c) 2025 TOON Format Organization
# SPDX-License-Identifier: MIT
"""Utilities for detecting literal token types.
This module provides functions to identify different types of literal
values in TOON syntax, such as booleans, null, and numeric literals.
Used during decoding to distinguish between literal values and strings.
"""
from .constants import FALSE_LITERAL, NULL_LITERAL, TRUE_LITERAL
def is_boolean_or_null_literal(token: str) -> bool:
"""Check if a token is a boolean or null literal (`true`, `false`, `null`).
Args:
token: The token to check
Returns:
True if the token is a boolean or null literal
Examples:
>>> is_boolean_or_null_literal("true")
True
>>> is_boolean_or_null_literal("null")
True
>>> is_boolean_or_null_literal("hello")
False
"""
return token == TRUE_LITERAL or token == FALSE_LITERAL or token == NULL_LITERAL
def is_numeric_literal(token: str) -> bool:
"""Check if a token represents a valid numeric literal.
Rejects numbers with leading zeros (except `"0"` itself or decimals like `"0.5"`).
Per Section 7.3 of the TOON specification.
Args:
token: The token to check
Returns:
True if the token is a valid numeric literal
Examples:
>>> is_numeric_literal("42")
True
>>> is_numeric_literal("3.14")
True
>>> is_numeric_literal("0.5")
True
>>> is_numeric_literal("0123") # Leading zero - not valid
False
>>> is_numeric_literal("-01") # Negative with leading zero - not valid
False
>>> is_numeric_literal("hello")
False
"""
if not token:
return False
# Handle negative numbers
start_idx = 1 if token.startswith("-") else 0
if start_idx >= len(token):
return False
# Must not have leading zeros (except for `"0"` itself or decimals like `"0.5"`)
# Check the first digit after optional minus sign
if len(token) > start_idx + 1 and token[start_idx] == "0" and token[start_idx + 1] != ".":
return False
# Check if it's a valid number
try:
num = float(token)
# Reject NaN and infinity
return not (num != num or not (-float("inf") < num < float("inf")))
except ValueError:
return False