toml_char.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import string
  2. class TOMLChar(str):
  3. def __init__(self, c):
  4. super().__init__()
  5. if len(self) > 1:
  6. raise ValueError("A TOML character must be of length 1")
  7. BARE = string.ascii_letters + string.digits + "-_"
  8. KV = "= \t"
  9. NUMBER = string.digits + "+-_.e"
  10. SPACES = " \t"
  11. NL = "\n\r"
  12. WS = SPACES + NL
  13. def is_bare_key_char(self) -> bool:
  14. """
  15. Whether the character is a valid bare key name or not.
  16. """
  17. return self in self.BARE
  18. def is_kv_sep(self) -> bool:
  19. """
  20. Whether the character is a valid key/value separator or not.
  21. """
  22. return self in self.KV
  23. def is_int_float_char(self) -> bool:
  24. """
  25. Whether the character if a valid integer or float value character or not.
  26. """
  27. return self in self.NUMBER
  28. def is_ws(self) -> bool:
  29. """
  30. Whether the character is a whitespace character or not.
  31. """
  32. return self in self.WS
  33. def is_nl(self) -> bool:
  34. """
  35. Whether the character is a new line character or not.
  36. """
  37. return self in self.NL
  38. def is_spaces(self) -> bool:
  39. """
  40. Whether the character is a space or not
  41. """
  42. return self in self.SPACES