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.

parser.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package chocococon
  2. import (
  3. "bufio"
  4. "github.com/chzyer/readline"
  5. "strconv"
  6. "strings"
  7. )
  8. func makeTokens(inp string) []string {
  9. var (
  10. stringList = []string{}
  11. crString = ""
  12. isInEscape = false
  13. )
  14. for k := range []rune(inp) {
  15. if !isInEscape {
  16. if inp[k] == '\\' {
  17. isInEscape = true
  18. continue
  19. } else {
  20. if inp[k] == ',' {
  21. stringList = append(stringList, crString)
  22. crString = ""
  23. continue
  24. }
  25. crString += string(inp[k])
  26. }
  27. } else {
  28. crString += string(inp[k])
  29. isInEscape = false
  30. }
  31. }
  32. return append(stringList, crString)
  33. }
  34. func readCmdLine(inp *bufio.Reader) (string, error) {
  35. str, err := readline.Line("RLagCON> ")
  36. readline.AddHistory(str)
  37. return str, err
  38. }
  39. func parseParamToFloat64(paramName string, paramTar *float64, value string) error {
  40. value = strings.TrimSpace(value)
  41. if strings.HasPrefix(value, paramName+"=") {
  42. value = strings.TrimPrefix(value, paramName+"=")
  43. fl, err := strconv.ParseFloat(value, 64)
  44. if err != nil {
  45. return err
  46. }
  47. *paramTar = fl
  48. }
  49. return nil
  50. }
  51. func parseParamToInt16(paramName string, paramTar *int16, value string) error {
  52. value = strings.TrimSpace(value)
  53. if strings.HasPrefix(value, paramName+"=") {
  54. value = strings.TrimPrefix(value, paramName+"=")
  55. fl, err := strconv.ParseInt(value, 10, 16)
  56. if err != nil {
  57. return err
  58. }
  59. flt := int16(fl)
  60. *paramTar = flt
  61. }
  62. return nil
  63. }
  64. func parseParamToInt32(paramName string, paramTar *int32, value string) error {
  65. value = strings.TrimSpace(value)
  66. if strings.HasPrefix(value, paramName+"=") {
  67. value = strings.TrimPrefix(value, paramName+"=")
  68. fl, err := strconv.ParseInt(value, 10, 32)
  69. if err != nil {
  70. return err
  71. }
  72. flt := int32(fl)
  73. *paramTar = flt
  74. }
  75. return nil
  76. }