parsemessages.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "path"
  8. filepath "path/filepath"
  9. "regexp"
  10. "strings"
  11. )
  12. func main() {
  13. if len(os.Args) < 2 {
  14. os.Exit(1)
  15. }
  16. multi := false
  17. folder := false
  18. for i := 2; i < len(os.Args); i++ {
  19. if os.Args[i] == "MULTI" {
  20. multi = true
  21. folder = true //For multi-file generation folder-based approach is enabled by default and could not be switched off
  22. }
  23. if os.Args[i] == "FOLDER" {
  24. folder = true
  25. }
  26. }
  27. file, err := os.Open(os.Args[1])
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. defer file.Close()
  32. messageFinder, err := regexp.Compile("^message\\s+([a-zA-Z0-9_]+)")
  33. if err != nil {
  34. log.Fatalf("Invalid regexp %s\n", err)
  35. }
  36. serviceFinder, err := regexp.Compile("^service\\s+([a-zA-Z0-9_]+)")
  37. if err != nil {
  38. log.Fatalf("Invalid regexp %s\n", err)
  39. }
  40. packageFinder, err := regexp.Compile("^package\\s+(.+);")
  41. if err != nil {
  42. log.Fatalf("Invalid regexp %s\n", err)
  43. }
  44. scanner := bufio.NewScanner(file)
  45. fullpath := ""
  46. for scanner.Scan() {
  47. capture := packageFinder.FindStringSubmatch(scanner.Text())
  48. if len(capture) == 2 {
  49. packages := strings.Split(capture[1], ".")
  50. fullpath = strings.Join(packages, "/")
  51. fullpath += "/"
  52. break
  53. }
  54. }
  55. if multi {
  56. for scanner.Scan() {
  57. if fullpath == "" {
  58. log.Fatalf("Package is not specified correctly in %s file\n", os.Args[1])
  59. }
  60. capture := messageFinder.FindStringSubmatch(scanner.Text())
  61. if len(capture) == 2 {
  62. fmt.Printf("%s%s.h;", fullpath, strings.ToLower(capture[1]))
  63. }
  64. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  65. if len(capture) == 2 {
  66. fmt.Printf("%s%sclient.h;", fullpath, strings.ToLower(capture[1]))
  67. }
  68. }
  69. } else {
  70. if folder && fullpath == "" {
  71. log.Fatalf("Package is not specified correctly in %s file\n", os.Args[1])
  72. }
  73. //Singlefile version
  74. enumFinder, err := regexp.Compile("^enum\\s+([a-zA-Z0-9_]+)")
  75. if err != nil {
  76. log.Fatalf("Invalid regexp %s\n", err)
  77. }
  78. basename := path.Base(os.Args[1])
  79. basename = strings.TrimSuffix(basename, filepath.Ext(basename))
  80. messageFound := false
  81. serviceFound := false
  82. for scanner.Scan() {
  83. capture := messageFinder.FindStringSubmatch(scanner.Text())
  84. if len(capture) == 2 {
  85. messageFound = true
  86. }
  87. capture = enumFinder.FindStringSubmatch(scanner.Text())
  88. if len(capture) == 2 {
  89. messageFound = true
  90. }
  91. capture = serviceFinder.FindStringSubmatch(scanner.Text())
  92. if len(capture) == 2 {
  93. serviceFound = true
  94. }
  95. }
  96. if messageFound {
  97. if folder {
  98. fmt.Printf("%s%s.qpb.h;", fullpath, basename)
  99. } else {
  100. fmt.Printf("%s.qpb.h;", basename)
  101. }
  102. }
  103. if serviceFound {
  104. if folder {
  105. fmt.Printf("%s%s_grpc.qpb.h;", fullpath, basename)
  106. } else {
  107. fmt.Printf("%s_grpc.qpb.h;", basename)
  108. }
  109. }
  110. }
  111. if err := scanner.Err(); err != nil {
  112. log.Fatal(err)
  113. }
  114. }