type.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. const _name, _nameOrInt = `([A-Za-z]\w*)`, "(" + _name + `|\d+)`
  9. const _index = "(" + _nameOrInt + "|:|" + _nameOrInt + ":|:" + _nameOrInt + "|" + _nameOrInt + ":" + _nameOrInt + ")"
  10. var nameExp = regexp.MustCompile(`^"` + _name + "$")
  11. var nameWithIndexExp = regexp.MustCompile(`^"` + _name + `((\[` + _index + `])*\[` + _index + `\\])?$`)
  12. func isName(s string) bool {
  13. return nameExp.MatchString(s)
  14. }
  15. func toName(s string) string {
  16. if !isName(s) {
  17. panic(fmt.Errorf("name: %s (%s)", errInvalidName, s))
  18. }
  19. return s[1:]
  20. }
  21. func isNameWithIndex(s string) bool {
  22. return nameWithIndexExp.MatchString(s)
  23. }
  24. func toNameWithIndex(s string) (string, []string) {
  25. if !isNameWithIndex(s) {
  26. panic(fmt.Errorf("name: %s (%s)", errInvalidName, s))
  27. }
  28. index := strings.Split(s, "[")
  29. name, cnt := index[0][1:], len(index)-1
  30. for i := 1; i < cnt; i++ {
  31. index[i] = index[i][:len(index[i])-1]
  32. }
  33. index[cnt] = index[cnt][:len(index[cnt])-2]
  34. return name, index[1:]
  35. }
  36. func isNumber(s string) bool {
  37. if s != "" && s[0] == '"' {
  38. s = s[1:]
  39. }
  40. _, err := strconv.ParseFloat(s, 64)
  41. return err == nil
  42. }
  43. func toNumber(s string) float64 {
  44. if s != "" && s[0] == '"' {
  45. s = s[1:]
  46. }
  47. val, err := strconv.ParseFloat(s, 64)
  48. if err != nil {
  49. panic(fmt.Errorf("number: %s (%s)", errInvalidNumber, s))
  50. }
  51. return val
  52. }
  53. func toInt(s string) int {
  54. if isName(`"` + s) {
  55. val, err := getValue(`"` + s)
  56. if err == nil {
  57. s = val.val
  58. }
  59. }
  60. if s != "" && s[0] == '"' {
  61. s = s[1:]
  62. }
  63. val, err := strconv.ParseInt(s, 10, 64)
  64. if err != nil {
  65. panic(fmt.Errorf("int: %s (%s)", errInvalidInteger, s))
  66. }
  67. return int(val)
  68. }
  69. func isWord(s string) bool {
  70. return s != "" && s[0] == '"'
  71. }
  72. func isList(s string) bool {
  73. return s != "" && s[0] == '['
  74. }
  75. func isBool(s string) bool {
  76. if s != "" && s[0] == '"' {
  77. s = s[1:]
  78. }
  79. _, err := strconv.ParseBool(s)
  80. return err == nil
  81. }
  82. func toBool(s string) bool {
  83. if s != "" && s[0] == '"' {
  84. s = s[1:]
  85. }
  86. val, err := strconv.ParseBool(s)
  87. if err != nil {
  88. panic(fmt.Errorf("bool: %s (%s)", errInvalidBool, s))
  89. }
  90. return val
  91. }
  92. func isEmpty(s string) bool {
  93. return s == `"` || s == "[ ]"
  94. }
  95. func parseFunc(s string) value {
  96. if !isList(s) {
  97. return value{val: s}
  98. }
  99. list := parseList(s)
  100. if len(list) != 2 || !isList(list[0]) || !isList(list[1]) {
  101. return value{val: s, list: list}
  102. }
  103. param := parseList(list[0])
  104. for _, name := range param {
  105. if !isName(`"` + name) {
  106. return value{val: s, list: list}
  107. }
  108. }
  109. local := make(environ)
  110. for name, val := range env[len(env)-1] {
  111. local[name] = val
  112. }
  113. return value{val: s, list: list, param: param, env: local}
  114. }
  115. func isValue(s string) bool {
  116. return isNumber(s) || isWord(s) || isList(s) || isBool(s)
  117. }
  118. func toValue(val value) value {
  119. if !isValue(val.val) {
  120. panic(fmt.Errorf("mua: %s (%s)", errValueExpected, val.val))
  121. }
  122. if isWord(val.val) {
  123. if val.val[1] == '[' {
  124. val.val = val.val[:1] + "\\" + val.val[1:]
  125. }
  126. if val.val[len(val.val)-1] == ']' {
  127. val.val = val.val[:len(val.val)-1]
  128. escape := len(val.val) - len(strings.TrimRight(val.val, "\\"))
  129. if (escape & 1) == 0 {
  130. val.val += "\\"
  131. }
  132. val.val += "]"
  133. }
  134. return val
  135. }
  136. if isList(val.val) {
  137. if val.list == nil {
  138. return parseFunc(val.val)
  139. }
  140. return val
  141. }
  142. return val
  143. }
  144. func escapeValue(s string) string {
  145. for i := 0; i < len(s); i++ {
  146. if s[i] == '\\' {
  147. s = s[:i] + s[i+1:]
  148. }
  149. }
  150. return s
  151. }
  152. func opIsName(val1 value) value {
  153. return value{val: strconv.FormatBool(isName(val1.val))}
  154. }
  155. func opIsNumber(val1 value) value {
  156. return value{val: strconv.FormatBool(isNumber(val1.val))}
  157. }
  158. func opIsWord(val1 value) value {
  159. return value{val: strconv.FormatBool(isWord(val1.val))}
  160. }
  161. func opIsList(val1 value) value {
  162. return value{val: strconv.FormatBool(isList(val1.val))}
  163. }
  164. func opIsBool(val1 value) value {
  165. return value{val: strconv.FormatBool(isBool(val1.val))}
  166. }
  167. func opIsEmpty(val1 value) value {
  168. return value{val: strconv.FormatBool(isEmpty(val1.val))}
  169. }