You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB

  1. package toml
  2. // tomlType represents any Go type that corresponds to a TOML type.
  3. // While the first draft of the TOML spec has a simplistic type system that
  4. // probably doesn't need this level of sophistication, we seem to be militating
  5. // toward adding real composite types.
  6. type tomlType interface {
  7. typeString() string
  8. }
  9. // typeEqual accepts any two types and returns true if they are equal.
  10. func typeEqual(t1, t2 tomlType) bool {
  11. if t1 == nil || t2 == nil {
  12. return false
  13. }
  14. return t1.typeString() == t2.typeString()
  15. }
  16. func typeIsTable(t tomlType) bool {
  17. return typeEqual(t, tomlHash) || typeEqual(t, tomlArrayHash)
  18. }
  19. type tomlBaseType string
  20. func (btype tomlBaseType) typeString() string {
  21. return string(btype)
  22. }
  23. func (btype tomlBaseType) String() string {
  24. return btype.typeString()
  25. }
  26. var (
  27. tomlInteger tomlBaseType = "Integer"
  28. tomlFloat tomlBaseType = "Float"
  29. tomlDatetime tomlBaseType = "Datetime"
  30. tomlString tomlBaseType = "String"
  31. tomlBool tomlBaseType = "Bool"
  32. tomlArray tomlBaseType = "Array"
  33. tomlHash tomlBaseType = "Hash"
  34. tomlArrayHash tomlBaseType = "ArrayHash"
  35. )
  36. // typeOfPrimitive returns a tomlType of any primitive value in TOML.
  37. // Primitive values are: Integer, Float, Datetime, String and Bool.
  38. //
  39. // Passing a lexer item other than the following will cause a BUG message
  40. // to occur: itemString, itemBool, itemInteger, itemFloat, itemDatetime.
  41. func (p *parser) typeOfPrimitive(lexItem item) tomlType {
  42. switch lexItem.typ {
  43. case itemInteger:
  44. return tomlInteger
  45. case itemFloat:
  46. return tomlFloat
  47. case itemDatetime:
  48. return tomlDatetime
  49. case itemString:
  50. return tomlString
  51. case itemMultilineString:
  52. return tomlString
  53. case itemRawString:
  54. return tomlString
  55. case itemRawMultilineString:
  56. return tomlString
  57. case itemBool:
  58. return tomlBool
  59. }
  60. p.bug("Cannot infer primitive type of lex item '%s'.", lexItem)
  61. panic("unreachable")
  62. }