type.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. func isName(s string) bool {
  9. reg := regexp.MustCompile(`^"[A-Za-z]\w*$`)
  10. return reg.MatchString(s)
  11. }
  12. func toName(s string) string {
  13. if !isName(s) {
  14. panic(fmt.Errorf("name: %s (%s)", errInvalidName, s))
  15. }
  16. return s[1:]
  17. }
  18. func isNameWithIndex(s string) bool {
  19. reg := regexp.MustCompile(`^"[A-Za-z]\w*(\[(\d+|:|\d+:|:\d+|\d+:\d+)\])*$`)
  20. return reg.MatchString(s)
  21. }
  22. func toNameWithIndex(s string) (string, []string) {
  23. if !isNameWithIndex(s) {
  24. panic(fmt.Errorf("name: %s (%s)", errInvalidName, s))
  25. }
  26. index := strings.Split(s, "[")
  27. name := index[0][1:]
  28. for i := 1; i < len(index); i++ {
  29. index[i] = index[i][:len(index[i])-1]
  30. }
  31. return name, index[1:]
  32. }
  33. func isNumber(s string) bool {
  34. if s != "" && s[0] == '"' {
  35. s = s[1:]
  36. }
  37. _, err := strconv.ParseFloat(s, 64)
  38. return err == nil
  39. }
  40. func toNumber(s string) float64 {
  41. if s != "" && s[0] == '"' {
  42. s = s[1:]
  43. }
  44. val, err := strconv.ParseFloat(s, 64)
  45. if err != nil {
  46. panic(fmt.Errorf("number: %s (%s)", errInvalidNumber, s))
  47. }
  48. return val
  49. }
  50. func isInt(s string) bool {
  51. if s != "" && s[0] == '"' {
  52. s = s[1:]
  53. }
  54. _, err := strconv.ParseInt(s, 10, 64)
  55. return err == nil
  56. }
  57. func toInt(s string) int {
  58. if s != "" && s[0] == '"' {
  59. s = s[1:]
  60. }
  61. val, err := strconv.ParseInt(s, 10, 64)
  62. if err != nil {
  63. panic(fmt.Errorf("int: %s (%s)", errInvalidInteger, s))
  64. }
  65. return int(val)
  66. }
  67. func isWord(s string) bool {
  68. return s != "" && s[0] == '"'
  69. }
  70. func isList(s string) bool {
  71. return s != "" && s[0] == '['
  72. }
  73. func isBool(s string) bool {
  74. if s != "" && s[0] == '"' {
  75. s = s[1:]
  76. }
  77. _, err := strconv.ParseBool(s)
  78. return err == nil
  79. }
  80. func toBool(s string) bool {
  81. if s != "" && s[0] == '"' {
  82. s = s[1:]
  83. }
  84. val, err := strconv.ParseBool(s)
  85. if err != nil {
  86. panic(fmt.Errorf("bool: %s (%s)", errInvalidBool, s))
  87. }
  88. return val
  89. }
  90. func isEmpty(s string) bool {
  91. return s == `"` || s == "[ ]"
  92. }
  93. func isValue(s string) bool {
  94. return isNumber(s) || isWord(s) || isList(s) || isBool(s)
  95. }
  96. func opIsName(val1 value) value {
  97. return value{val: strconv.FormatBool(isName(val1.val))}
  98. }
  99. func opIsNumber(val1 value) value {
  100. return value{val: strconv.FormatBool(isNumber(val1.val))}
  101. }
  102. func opIsWord(val1 value) value {
  103. return value{val: strconv.FormatBool(isWord(val1.val))}
  104. }
  105. func opIsList(val1 value) value {
  106. return value{val: strconv.FormatBool(isList(val1.val))}
  107. }
  108. func opIsBool(val1 value) value {
  109. return value{val: strconv.FormatBool(isBool(val1.val))}
  110. }
  111. func opIsEmpty(val1 value) value {
  112. return value{val: strconv.FormatBool(isEmpty(val1.val))}
  113. }