Skip to content

Commit 1d6827c

Browse files
committed
break up timedata regex into multiple statements so it compiles for micropython
1 parent 579331e commit 1d6827c

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

src/tomli/_parser.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from collections import namedtuple
66

77
from ._re import (
8-
RE_DATETIME,
8+
RE_DATETIME_TIME,
9+
RE_DATETIME_YMD,
10+
RE_DATETIME_ZONE,
911
RE_LOCALTIME,
1012
RE_NUMBER,
1113
match_to_datetime,
@@ -610,13 +612,22 @@ def parse_value( # noqa: C901
610612
return parse_inline_table(src, pos, parse_float)
611613

612614
# Dates and times
613-
datetime_match = RE_DATETIME.match(src, pos)
614-
if datetime_match:
615+
datetime_ymd_match = RE_DATETIME_YMD.match(src, pos)
616+
if datetime_ymd_match:
617+
end = datetime_ymd_match.end()
618+
datetime_time_match = RE_DATETIME_TIME.match(src, datetime_ymd_match.end())
619+
if datetime_time_match:
620+
end = datetime_time_match.end()
621+
datetime_zone_match = RE_DATETIME_ZONE.match(src, datetime_time_match.end())
622+
if datetime_zone_match:
623+
end = datetime_zone_match.end()
624+
else:
625+
datetime_zone_match = None
615626
try:
616-
datetime_obj = match_to_datetime(datetime_match)
627+
datetime_obj = match_to_datetime(datetime_ymd_match, datetime_time_match, datetime_zone_match)
617628
except ValueError as e:
618629
raise suffixed_err(src, pos, "Invalid date or datetime") from e
619-
return datetime_match.end(), datetime_obj
630+
return end, datetime_obj
620631
localtime_match = RE_LOCALTIME.match(src, pos)
621632
if localtime_match:
622633
return localtime_match.end(), match_to_localtime(localtime_match)

src/tomli/_re.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,55 @@
1111

1212
)
1313
RE_LOCALTIME = re.compile(r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?")
14-
RE_DATETIME = re.compile(
15-
# Currently the last group is too long for micropython (>127 bytes)
16-
r"""([\d][\d][\d][\d])-(0[1-9]|1[0-2])-(0[1-9]|[12][\d]|3[01])([Tt ]([01][\d]|2[0-3]):([0-5][\d]):([0-5][\d])(\.([\d][\d]?[\d]?[\d]?[\d]?[\d]?)[\d]*)?(([Zz])|([+-])([01][\d]|2[0-3]):([0-5][\d]))?)?"""
14+
15+
RE_DATETIME_YMD = re.compile(r"""([\d][\d][\d][\d])-(0[1-9]|1[0-2])-(0[1-9]|[12][\d]|3[01])""")
16+
RE_DATETIME_TIME = re.compile(
17+
r"""([Tt ]([01][\d]|2[0-3]):([0-5][\d]):([0-5][\d])(\.([\d][\d]?[\d]?[\d]?[\d]?[\d]?)[\d]*)?)?"""
1718
)
19+
RE_DATETIME_ZONE = re.compile(r"(([Zz])|([+-])([01][\d]|2[0-3]):([0-5][\d]))?")
1820

1921

20-
def match_to_datetime(match):
22+
def match_to_datetime(ymd_match, time_match, zone_match):
2123
"""Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
2224
2325
Raises ValueError if the match does not correspond to a valid date
2426
or datetime.
2527
"""
2628
(
27-
year_str, # 1
28-
month_str, # 2
29-
day_str, # 3
30-
_,
31-
hour_str, # 4
32-
minute_str, # 5
33-
sec_str, # 6
34-
_,
35-
micros_str, # 7
36-
_,
37-
zulu_time, # 8
38-
offset_sign_str, # 9
39-
offset_hour_str, # 10
40-
offset_minute_str, # 11
41-
) = match.groups()
29+
year_str,
30+
month_str,
31+
day_str,
32+
) = ymd_match.groups()
33+
34+
if time_match:
35+
(
36+
_,
37+
hour_str,
38+
minute_str,
39+
sec_str,
40+
_,
41+
micros_str,
42+
) = time_match.groups()
43+
else:
44+
hour_str = None
45+
minute_str = None
46+
sec_str = None
47+
micros_str = None
48+
49+
if zone_match:
50+
(
51+
_,
52+
zulu_time,
53+
offset_sign_str,
54+
offset_hour_str,
55+
offset_minute_str,
56+
) = zone_match.groups()
57+
else:
58+
zulu_time = None
59+
offset_sign_str = None
60+
offset_hour_str = None
61+
offset_minute_str = None
62+
4263
year, month, day = int(year_str), int(month_str), int(day_str)
4364
if hour_str is None:
4465
return date(year, month, day)

0 commit comments

Comments
 (0)