Debian Codegraph @

Lib/tomllib/_parser.py

raw
1 # SPDX-License-Identifier: MIT
2 # SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3 # Licensed to PSF under a Contributor Agreement.
4
5 from __future__ import annotations
6
7 import sys
8 from types import MappingProxyType
9
10 from ._re import (
11 RE_DATETIME,
12 RE_LOCALTIME,
13 RE_NUMBER,
14 match_to_datetime,
15 match_to_localtime,
16 match_to_number,
17 )
18
19 TYPE_CHECKING = False
20 if TYPE_CHECKING:
21 from collections.abc import Iterable
22 from typing import IO, Any, Final
23
24 from ._types import Key, ParseFloat, Pos
25
26 # Pathologically excessive number of parts in a key runs into quadratic
27 # behavior (e.g. in Flags.is_).
28 # Even if keys aren't currently parsed using recursion, they name a
29 # recursive structure, so it makes sense to limit it using getrecursionlimit()
30 # and RecursionError.
31 MAX_KEY_PARTS: Final = sys.getrecursionlimit()
32
33 ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
34
35 # Neither of these sets include quotation mark or backslash. They are
36 # currently handled as separate cases in the parser functions.
37 ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t")
38 ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n")
39
40 ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
41 ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS
42
43 ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
44
45 TOML_WS = frozenset(" \t")
46 TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n")
47 BARE_KEY_CHARS = frozenset(
48 "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789" "-_"
49 )
50 KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
51 HEXDIGIT_CHARS = frozenset("abcdef" "ABCDEF" "0123456789")
52
53 BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
54 {
55 "\\b": "\u0008", # backspace
56 "\\t": "\u0009", # tab
57 "\\n": "\u000A", # linefeed
58 "\\f": "\u000C", # form feed
59 "\\r": "\u000D", # carriage return
60 '\\"': "\u0022", # quote
61 "\\\\": "\u005C", # backslash
62 }
63 )
64
65
66 class DEPRECATED_DEFAULT:
67 """Sentinel to be used as default arg during deprecation
68 period of TOMLDecodeError's free-form arguments."""
69
70
71 class TOMLDecodeError(ValueError):
72 """An error raised if a document is not valid TOML.
73
74 Adds the following attributes to ValueError:
75 msg: The unformatted error message
76 doc: The TOML document being parsed
77 pos: The index of doc where parsing failed
78 lineno: The line corresponding to pos
79 colno: The column corresponding to pos
80 """
81
82 def __init__(
83 self,
84 msg: str = DEPRECATED_DEFAULT, # type: ignore[assignment]
85 doc: str = DEPRECATED_DEFAULT, # type: ignore[assignment]
86 pos: Pos = DEPRECATED_DEFAULT, # type: ignore[assignment]
87 *args: Any,
88 ):
89 if (
90 args
91 or not isinstance(msg, str)
92 or not isinstance(doc, str)
93 or not isinstance(pos, int)
94 ):
95 import warnings
96
97 warnings.warn(
98 "Free-form arguments for TOMLDecodeError are deprecated. "
99 "Please set 'msg' (str), 'doc' (str) and 'pos' (int) arguments only.",
100 DeprecationWarning,
101 stacklevel=2,
102 )
103 if pos is not DEPRECATED_DEFAULT: # type: ignore[comparison-overlap]
104 args = pos, *args
105 if doc is not DEPRECATED_DEFAULT: # type: ignore[comparison-overlap]
106 args = doc, *args
107 if msg is not DEPRECATED_DEFAULT: # type: ignore[comparison-overlap]
108 args = msg, *args
109 ValueError.__init__(self, *args)
110 return
111
112 lineno = doc.count("\n", 0, pos) + 1
113 if lineno == 1:
114 colno = pos + 1
115 else:
116 colno = pos - doc.rindex("\n", 0, pos)
117
118 if pos >= len(doc):
119 coord_repr = "end of document"
120 else:
121 coord_repr = f"line {lineno}, column {colno}"
122 errmsg = f"{msg} (at {coord_repr})"
123 ValueError.__init__(self, errmsg)
124
125 self.msg = msg
126 self.doc = doc
127 self.pos = pos
128 self.lineno = lineno
129 self.colno = colno
130
131
132 def load(fp: IO[bytes], /, *, parse_float: ParseFloat = float) -> dict[str, Any]:
133 """Parse TOML from a binary file object."""
134 b = fp.read()
135 try:
136 s = b.decode()
137 except AttributeError:
138 raise TypeError(
139 "File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`"
140 ) from None
141 return loads(s, parse_float=parse_float)
142
143
144 def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # noqa: C901
145 """Parse TOML from a string."""
146
147 # The spec allows converting "\r\n" to "\n", even in string
148 # literals. Let's do so to simplify parsing.
149 try:
150 src = s.replace("\r\n", "\n")
151 except (AttributeError, TypeError):
152 raise TypeError(
153 f"Expected str object, not '{type(s).__qualname__}'"
154 ) from None
155 pos = 0
156 out = Output()
157 header: Key = ()
158 parse_float = make_safe_parse_float(parse_float)
159
160 # Parse one statement at a time
161 # (typically means one line in TOML source)
162 while True:
163 # 1. Skip line leading whitespace
164 pos = skip_chars(src, pos, TOML_WS)
165
166 # 2. Parse rules. Expect one of the following:
167 # - end of file
168 # - end of line
169 # - comment
170 # - key/value pair
171 # - append dict to list (and move to its namespace)
172 # - create dict (and move to its namespace)
173 # Skip trailing whitespace when applicable.
174 try:
175 char = src[pos]
176 except IndexError:
177 break
178 if char == "\n":
179 pos += 1
180 continue
181 if char in KEY_INITIAL_CHARS:
182 pos = key_value_rule(src, pos, out, header, parse_float)
183 pos = skip_chars(src, pos, TOML_WS)
184 elif char == "[":
185 try:
186 second_char: str | None = src[pos + 1]
187 except IndexError:
188 second_char = None
189 out.flags.finalize_pending()
190 if second_char == "[":
191 pos, header = create_list_rule(src, pos, out)
192 else:
193 pos, header = create_dict_rule(src, pos, out)
194 pos = skip_chars(src, pos, TOML_WS)
195 elif char != "#":
196 raise TOMLDecodeError("Invalid statement", src, pos)
197
198 # 3. Skip comment
199 pos = skip_comment(src, pos)
200
201 # 4. Expect end of line or end of file
202 try:
203 char = src[pos]
204 except IndexError:
205 break
206 if char != "\n":
207 raise TOMLDecodeError(
208 "Expected newline or end of document after a statement", src, pos
209 )
210 pos += 1
211
212 return out.data.dict
213
214
215 class Flags:
216 """Flags that map to parsed keys/namespaces."""
217
218 # Marks an immutable namespace (inline array or inline table).
219 FROZEN = 0
220 # Marks a nest that has been explicitly created and can no longer
221 # be opened using the "[table]" syntax.
222 EXPLICIT_NEST = 1
223
224 def __init__(self) -> None:
225 self._flags: dict[str, dict[Any, Any]] = {}
226 self._pending_flags: set[tuple[Key, int]] = set()
227
228 def add_pending(self, key: Key, flag: int) -> None:
229 self._pending_flags.add((key, flag))
230
231 def finalize_pending(self) -> None:
232 for key, flag in self._pending_flags:
233 self.set(key, flag, recursive=False)
234 self._pending_flags.clear()
235
236 def unset_all(self, key: Key) -> None:
237 cont = self._flags
238 for k in key[:-1]:
239 if k not in cont:
240 return
241 cont = cont[k]["nested"]
242 cont.pop(key[-1], None)
243
244 def set(self, key: Key, flag: int, *, recursive: bool) -> None: # noqa: A003
245 cont = self._flags
246 key_parent, key_stem = key[:-1], key[-1]
247 for k in key_parent:
248 if k not in cont:
249 cont[k] = {"flags": set(), "recursive_flags": set(), "nested": {}}
250 cont = cont[k]["nested"]
251 if key_stem not in cont:
252 cont[key_stem] = {"flags": set(), "recursive_flags": set(), "nested": {}}
253 cont[key_stem]["recursive_flags" if recursive else "flags"].add(flag)
254
255 def is_(self, key: Key, flag: int) -> bool:
256 if not key:
257 return False # document root has no flags
258 cont = self._flags
259 for k in key[:-1]:
260 if k not in cont:
261 return False
262 inner_cont = cont[k]
263 if flag in inner_cont["recursive_flags"]:
264 return True
265 cont = inner_cont["nested"]
266 key_stem = key[-1]
267 if key_stem in cont:
268 cont = cont[key_stem]
269 return flag in cont["flags"] or flag in cont["recursive_flags"]
270 return False
271
272
273 class NestedDict:
274 def __init__(self) -> None:
275 # The parsed content of the TOML document
276 self.dict: dict[str, Any] = {}
277
278 def get_or_create_nest(
279 self,
280 key: Key,
281 *,
282 access_lists: bool = True,
283 ) -> dict[str, Any]:
284 cont: Any = self.dict
285 for k in key:
286 if k not in cont:
287 cont[k] = {}
288 cont = cont[k]
289 if access_lists and isinstance(cont, list):
290 cont = cont[-1]
291 if not isinstance(cont, dict):
292 raise KeyError("There is no nest behind this key")
293 return cont # type: ignore[no-any-return]
294
295 def append_nest_to_list(self, key: Key) -> None:
296 cont = self.get_or_create_nest(key[:-1])
297 last_key = key[-1]
298 if last_key in cont:
299 list_ = cont[last_key]
300 if not isinstance(list_, list):
301 raise KeyError("An object other than list found behind this key")
302 list_.append({})
303 else:
304 cont[last_key] = [{}]
305
306
307 class Output:
308 def __init__(self) -> None:
309 self.data = NestedDict()
310 self.flags = Flags()
311
312
313 def skip_chars(src: str, pos: Pos, chars: Iterable[str]) -> Pos:
314 try:
315 while src[pos] in chars:
316 pos += 1
317 except IndexError:
318 pass
319 return pos
320
321
322 def skip_until(
323 src: str,
324 pos: Pos,
325 expect: str,
326 *,
327 error_on: frozenset[str],
328 error_on_eof: bool,
329 ) -> Pos:
330 try:
331 new_pos = src.index(expect, pos)
332 except ValueError:
333 new_pos = len(src)
334 if error_on_eof:
335 raise TOMLDecodeError(f"Expected {expect!r}", src, new_pos) from None
336
337 if not error_on.isdisjoint(src[pos:new_pos]):
338 while src[pos] not in error_on:
339 pos += 1
340 raise TOMLDecodeError(f"Found invalid character {src[pos]!r}", src, pos)
341 return new_pos
342
343
344 def skip_comment(src: str, pos: Pos) -> Pos:
345 try:
346 char: str | None = src[pos]
347 except IndexError:
348 char = None
349 if char == "#":
350 return skip_until(
351 src, pos + 1, "\n", error_on=ILLEGAL_COMMENT_CHARS, error_on_eof=False
352 )
353 return pos
354
355
356 def skip_comments_and_array_ws(src: str, pos: Pos) -> Pos:
357 while True:
358 pos_before_skip = pos
359 pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
360 pos = skip_comment(src, pos)
361 if pos == pos_before_skip:
362 return pos
363
364
365 def create_dict_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
366 pos += 1 # Skip "["
367 pos = skip_chars(src, pos, TOML_WS)
368 pos, key = parse_key(src, pos)
369
370 if out.flags.is_(key, Flags.EXPLICIT_NEST) or out.flags.is_(key, Flags.FROZEN):
371 raise TOMLDecodeError(f"Cannot declare {key} twice", src, pos)
372 out.flags.set(key, Flags.EXPLICIT_NEST, recursive=False)
373 try:
374 out.data.get_or_create_nest(key)
375 except KeyError:
376 raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
377
378 if not src.startswith("]", pos):
379 raise TOMLDecodeError(
380 "Expected ']' at the end of a table declaration", src, pos
381 )
382 return pos + 1, key
383
384
385 def create_list_rule(src: str, pos: Pos, out: Output) -> tuple[Pos, Key]:
386 pos += 2 # Skip "[["
387 pos = skip_chars(src, pos, TOML_WS)
388 pos, key = parse_key(src, pos)
389
390 if out.flags.is_(key, Flags.FROZEN):
391 raise TOMLDecodeError(f"Cannot mutate immutable namespace {key}", src, pos)
392 # Free the namespace now that it points to another empty list item...
393 out.flags.unset_all(key)
394 # ...but this key precisely is still prohibited from table declaration
395 out.flags.set(key, Flags.EXPLICIT_NEST, recursive=False)
396 try:
397 out.data.append_nest_to_list(key)
398 except KeyError:
399 raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
400
401 if not src.startswith("]]", pos):
402 raise TOMLDecodeError(
403 "Expected ']]' at the end of an array declaration", src, pos
404 )
405 return pos + 2, key
406
407
408 def key_value_rule(
409 src: str, pos: Pos, out: Output, header: Key, parse_float: ParseFloat
410 ) -> Pos:
411 pos, key, value = parse_key_value_pair(src, pos, parse_float)
412 key_parent, key_stem = key[:-1], key[-1]
413 abs_key_parent = header + key_parent
414
415 relative_path_cont_keys = (header + key[:i] for i in range(1, len(key)))
416 for cont_key in relative_path_cont_keys:
417 # Check that dotted key syntax does not redefine an existing table
418 if out.flags.is_(cont_key, Flags.EXPLICIT_NEST):
419 raise TOMLDecodeError(f"Cannot redefine namespace {cont_key}", src, pos)
420 # Containers in the relative path can't be opened with the table syntax or
421 # dotted key/value syntax in following table sections.
422 out.flags.add_pending(cont_key, Flags.EXPLICIT_NEST)
423
424 if out.flags.is_(abs_key_parent, Flags.FROZEN):
425 raise TOMLDecodeError(
426 f"Cannot mutate immutable namespace {abs_key_parent}", src, pos
427 )
428
429 try:
430 nest = out.data.get_or_create_nest(abs_key_parent)
431 except KeyError:
432 raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
433 if key_stem in nest:
434 raise TOMLDecodeError("Cannot overwrite a value", src, pos)
435 # Mark inline table and array namespaces recursively immutable
436 if isinstance(value, (dict, list)):
437 out.flags.set(header + key, Flags.FROZEN, recursive=True)
438 nest[key_stem] = value
439 return pos
440
441
442 def parse_key_value_pair(
443 src: str, pos: Pos, parse_float: ParseFloat
444 ) -> tuple[Pos, Key, Any]:
445 pos, key = parse_key(src, pos)
446 try:
447 char: str | None = src[pos]
448 except IndexError:
449 char = None
450 if char != "=":
451 raise TOMLDecodeError("Expected '=' after a key in a key/value pair", src, pos)
452 pos += 1
453 pos = skip_chars(src, pos, TOML_WS)
454 pos, value = parse_value(src, pos, parse_float)
455 return pos, key, value
456
457
458 def parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
459 pos, key_part = parse_key_part(src, pos)
460 key: Key = (key_part,)
461 pos = skip_chars(src, pos, TOML_WS)
462 while True:
463 try:
464 char: str | None = src[pos]
465 except IndexError:
466 char = None
467 if char != ".":
468 return pos, key
469 pos += 1
470 pos = skip_chars(src, pos, TOML_WS)
471 pos, key_part = parse_key_part(src, pos)
472 key += (key_part,)
473 if len(key) > MAX_KEY_PARTS:
474 raise RecursionError(
475 f"TOML key has more than the allowed {MAX_KEY_PARTS} parts"
476 )
477 pos = skip_chars(src, pos, TOML_WS)
478
479
480 def parse_key_part(src: str, pos: Pos) -> tuple[Pos, str]:
481 try:
482 char: str | None = src[pos]
483 except IndexError:
484 char = None
485 if char in BARE_KEY_CHARS:
486 start_pos = pos
487 pos = skip_chars(src, pos, BARE_KEY_CHARS)
488 return pos, src[start_pos:pos]
489 if char == "'":
490 return parse_literal_str(src, pos)
491 if char == '"':
492 return parse_one_line_basic_str(src, pos)
493 raise TOMLDecodeError("Invalid initial character for a key part", src, pos)
494
495
496 def parse_one_line_basic_str(src: str, pos: Pos) -> tuple[Pos, str]:
497 pos += 1
498 return parse_basic_str(src, pos, multiline=False)
499
500
501 def parse_array(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, list[Any]]:
502 pos += 1
503 array: list[Any] = []
504
505 pos = skip_comments_and_array_ws(src, pos)
506 if src.startswith("]", pos):
507 return pos + 1, array
508 while True:
509 pos, val = parse_value(src, pos, parse_float)
510 array.append(val)
511 pos = skip_comments_and_array_ws(src, pos)
512
513 c = src[pos : pos + 1]
514 if c == "]":
515 return pos + 1, array
516 if c != ",":
517 raise TOMLDecodeError("Unclosed array", src, pos)
518 pos += 1
519
520 pos = skip_comments_and_array_ws(src, pos)
521 if src.startswith("]", pos):
522 return pos + 1, array
523
524
525 def parse_inline_table(src: str, pos: Pos, parse_float: ParseFloat) -> tuple[Pos, dict[str, Any]]:
526 pos += 1
527 nested_dict = NestedDict()
528 flags = Flags()
529
530 pos = skip_chars(src, pos, TOML_WS)
531 if src.startswith("}", pos):
532 return pos + 1, nested_dict.dict
533 while True:
534 pos, key, value = parse_key_value_pair(src, pos, parse_float)
535 key_parent, key_stem = key[:-1], key[-1]
536 if flags.is_(key, Flags.FROZEN):
537 raise TOMLDecodeError(f"Cannot mutate immutable namespace {key}", src, pos)
538 try:
539 nest = nested_dict.get_or_create_nest(key_parent, access_lists=False)
540 except KeyError:
541 raise TOMLDecodeError("Cannot overwrite a value", src, pos) from None
542 if key_stem in nest:
543 raise TOMLDecodeError(f"Duplicate inline table key {key_stem!r}", src, pos)
544 nest[key_stem] = value
545 pos = skip_chars(src, pos, TOML_WS)
546 c = src[pos : pos + 1]
547 if c == "}":
548 return pos + 1, nested_dict.dict
549 if c != ",":
550 raise TOMLDecodeError("Unclosed inline table", src, pos)
551 if isinstance(value, (dict, list)):
552 flags.set(key, Flags.FROZEN, recursive=True)
553 pos += 1
554 pos = skip_chars(src, pos, TOML_WS)
555
556
557 def parse_basic_str_escape(
558 src: str, pos: Pos, *, multiline: bool = False
559 ) -> tuple[Pos, str]:
560 escape_id = src[pos : pos + 2]
561 pos += 2
562 if multiline and escape_id in {"\\ ", "\\\t", "\\\n"}:
563 # Skip whitespace until next non-whitespace character or end of
564 # the doc. Error if non-whitespace is found before newline.
565 if escape_id != "\\\n":
566 pos = skip_chars(src, pos, TOML_WS)
567 try:
568 char = src[pos]
569 except IndexError:
570 return pos, ""
571 if char != "\n":
572 raise TOMLDecodeError("Unescaped '\\' in a string", src, pos)
573 pos += 1
574 pos = skip_chars(src, pos, TOML_WS_AND_NEWLINE)
575 return pos, ""
576 if escape_id == "\\u":
577 return parse_hex_char(src, pos, 4)
578 if escape_id == "\\U":
579 return parse_hex_char(src, pos, 8)
580 try:
581 return pos, BASIC_STR_ESCAPE_REPLACEMENTS[escape_id]
582 except KeyError:
583 raise TOMLDecodeError("Unescaped '\\' in a string", src, pos) from None
584
585
586 def parse_basic_str_escape_multiline(src: str, pos: Pos) -> tuple[Pos, str]:
587 return parse_basic_str_escape(src, pos, multiline=True)
588
589
590 def parse_hex_char(src: str, pos: Pos, hex_len: int) -> tuple[Pos, str]:
591 hex_str = src[pos : pos + hex_len]
592 if len(hex_str) != hex_len or not HEXDIGIT_CHARS.issuperset(hex_str):
593 raise TOMLDecodeError("Invalid hex value", src, pos)
594 pos += hex_len
595 hex_int = int(hex_str, 16)
596 if not is_unicode_scalar_value(hex_int):
597 raise TOMLDecodeError(
598 "Escaped character is not a Unicode scalar value", src, pos
599 )
600 return pos, chr(hex_int)
601
602
603 def parse_literal_str(src: str, pos: Pos) -> tuple[Pos, str]:
604 pos += 1 # Skip starting apostrophe
605 start_pos = pos
606 pos = skip_until(
607 src, pos, "'", error_on=ILLEGAL_LITERAL_STR_CHARS, error_on_eof=True
608 )
609 return pos + 1, src[start_pos:pos] # Skip ending apostrophe
610
611
612 def parse_multiline_str(src: str, pos: Pos, *, literal: bool) -> tuple[Pos, str]:
613 pos += 3
614 if src.startswith("\n", pos):
615 pos += 1
616
617 if literal:
618 delim = "'"
619 end_pos = skip_until(
620 src,
621 pos,
622 "'''",
623 error_on=ILLEGAL_MULTILINE_LITERAL_STR_CHARS,
624 error_on_eof=True,
625 )
626 result = src[pos:end_pos]
627 pos = end_pos + 3
628 else:
629 delim = '"'
630 pos, result = parse_basic_str(src, pos, multiline=True)
631
632 # Add at maximum two extra apostrophes/quotes if the end sequence
633 # is 4 or 5 chars long instead of just 3.
634 if not src.startswith(delim, pos):
635 return pos, result
636 pos += 1
637 if not src.startswith(delim, pos):
638 return pos, result + delim
639 pos += 1
640 return pos, result + (delim * 2)
641
642
643 def parse_basic_str(src: str, pos: Pos, *, multiline: bool) -> tuple[Pos, str]:
644 if multiline:
645 error_on = ILLEGAL_MULTILINE_BASIC_STR_CHARS
646 parse_escapes = parse_basic_str_escape_multiline
647 else:
648 error_on = ILLEGAL_BASIC_STR_CHARS
649 parse_escapes = parse_basic_str_escape
650 result = ""
651 start_pos = pos
652 while True:
653 try:
654 char = src[pos]
655 except IndexError:
656 raise TOMLDecodeError("Unterminated string", src, pos) from None
657 if char == '"':
658 if not multiline:
659 return pos + 1, result + src[start_pos:pos]
660 if src.startswith('"""', pos):
661 return pos + 3, result + src[start_pos:pos]
662 pos += 1
663 continue
664 if char == "\\":
665 result += src[start_pos:pos]
666 pos, parsed_escape = parse_escapes(src, pos)
667 result += parsed_escape
668 start_pos = pos
669 continue
670 if char in error_on:
671 raise TOMLDecodeError(f"Illegal character {char!r}", src, pos)
672 pos += 1
673
674
675 def parse_value( # noqa: C901
676 src: str, pos: Pos, parse_float: ParseFloat
677 ) -> tuple[Pos, Any]:
678 try:
679 char: str | None = src[pos]
680 except IndexError:
681 char = None
682
683 # IMPORTANT: order conditions based on speed of checking and likelihood
684
685 # Basic strings
686 if char == '"':
687 if src.startswith('"""', pos):
688 return parse_multiline_str(src, pos, literal=False)
689 return parse_one_line_basic_str(src, pos)
690
691 # Literal strings
692 if char == "'":
693 if src.startswith("'''", pos):
694 return parse_multiline_str(src, pos, literal=True)
695 return parse_literal_str(src, pos)
696
697 # Booleans
698 if char == "t":
699 if src.startswith("true", pos):
700 return pos + 4, True
701 if char == "f":
702 if src.startswith("false", pos):
703 return pos + 5, False
704
705 # Arrays
706 if char == "[":
707 return parse_array(src, pos, parse_float)
708
709 # Inline tables
710 if char == "{":
711 return parse_inline_table(src, pos, parse_float)
712
713 # Dates and times
714 datetime_match = RE_DATETIME.match(src, pos)
715 if datetime_match:
716 try:
717 datetime_obj = match_to_datetime(datetime_match)
718 except ValueError as e:
719 raise TOMLDecodeError("Invalid date or datetime", src, pos) from e
720 return datetime_match.end(), datetime_obj
721 localtime_match = RE_LOCALTIME.match(src, pos)
722 if localtime_match:
723 return localtime_match.end(), match_to_localtime(localtime_match)
724
725 # Integers and "normal" floats.
726 # The regex will greedily match any type starting with a decimal
727 # char, so needs to be located after handling of dates and times.
728 number_match = RE_NUMBER.match(src, pos)
729 if number_match:
730 return number_match.end(), match_to_number(number_match, parse_float)
731
732 # Special floats
733 first_three = src[pos : pos + 3]
734 if first_three in {"inf", "nan"}:
735 return pos + 3, parse_float(first_three)
736 first_four = src[pos : pos + 4]
737 if first_four in {"-inf", "+inf", "-nan", "+nan"}:
738 return pos + 4, parse_float(first_four)
739
740 raise TOMLDecodeError("Invalid value", src, pos)
741
742
743 def is_unicode_scalar_value(codepoint: int) -> bool:
744 return (0 <= codepoint <= 55295) or (57344 <= codepoint <= 1114111)
745
746
747 def make_safe_parse_float(parse_float: ParseFloat) -> ParseFloat:
748 """A decorator to make `parse_float` safe.
749
750 `parse_float` must not return dicts or lists, because these types
751 would be mixed with parsed TOML tables and arrays, thus confusing
752 the parser. The returned decorated callable raises `ValueError`
753 instead of returning illegal types.
754 """
755 # The default `float` callable never returns illegal types. Optimize it.
756 if parse_float is float:
757 return float
758
759 def safe_parse_float(float_str: str) -> Any:
760 float_value = parse_float(float_str)
761 if isinstance(float_value, (dict, list)):
762 raise ValueError("parse_float must not return dicts or lists")
763 return float_value
764
765 return safe_parse_float