ast.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package ast
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/antlr/antlr4/runtime/Go/antlr"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/util"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  10. )
  11. type (
  12. TokenStream interface {
  13. GetStart() antlr.Token
  14. GetStop() antlr.Token
  15. GetParser() antlr.Parser
  16. }
  17. ApiVisitor struct {
  18. api.BaseApiParserVisitor
  19. debug bool
  20. log console.Console
  21. prefix string
  22. infoFlag bool
  23. }
  24. VisitorOption func(v *ApiVisitor)
  25. Spec interface {
  26. Doc() []Expr
  27. Comment() Expr
  28. Format() error
  29. Equal(v interface{}) bool
  30. }
  31. Expr interface {
  32. Prefix() string
  33. Line() int
  34. Column() int
  35. Text() string
  36. SetText(text string)
  37. Start() int
  38. Stop() int
  39. Equal(expr Expr) bool
  40. IsNotNil() bool
  41. }
  42. )
  43. func NewApiVisitor(options ...VisitorOption) *ApiVisitor {
  44. v := &ApiVisitor{
  45. log: console.NewColorConsole(),
  46. }
  47. for _, opt := range options {
  48. opt(v)
  49. }
  50. return v
  51. }
  52. func (v *ApiVisitor) panic(expr Expr, msg string) {
  53. errString := fmt.Sprintf("%s line %d:%d %s", v.prefix, expr.Line(), expr.Column(), msg)
  54. if v.debug {
  55. fmt.Println(errString)
  56. }
  57. panic(errString)
  58. }
  59. func WithVisitorPrefix(prefix string) VisitorOption {
  60. return func(v *ApiVisitor) {
  61. v.prefix = prefix
  62. }
  63. }
  64. func WithVisitorDebug() VisitorOption {
  65. return func(v *ApiVisitor) {
  66. v.debug = true
  67. }
  68. }
  69. type defaultExpr struct {
  70. prefix, v string
  71. line, column int
  72. start, stop int
  73. }
  74. func NewTextExpr(v string) *defaultExpr {
  75. return &defaultExpr{
  76. v: v,
  77. }
  78. }
  79. func (v *ApiVisitor) newExprWithTerminalNode(node antlr.TerminalNode) *defaultExpr {
  80. if node == nil {
  81. return nil
  82. }
  83. token := node.GetSymbol()
  84. return v.newExprWithToken(token)
  85. }
  86. func (v *ApiVisitor) newExprWithToken(token antlr.Token) *defaultExpr {
  87. if token == nil {
  88. return nil
  89. }
  90. instance := &defaultExpr{}
  91. instance.prefix = v.prefix
  92. instance.v = token.GetText()
  93. instance.line = token.GetLine()
  94. instance.column = token.GetColumn()
  95. instance.start = token.GetStart()
  96. instance.stop = token.GetStop()
  97. return instance
  98. }
  99. func (v *ApiVisitor) newExprWithText(text string, line, column, start, stop int) *defaultExpr {
  100. instance := &defaultExpr{}
  101. instance.prefix = v.prefix
  102. instance.v = text
  103. instance.line = line
  104. instance.column = column
  105. instance.start = start
  106. instance.stop = stop
  107. return instance
  108. }
  109. func (e *defaultExpr) Prefix() string {
  110. if e == nil {
  111. return ""
  112. }
  113. return e.prefix
  114. }
  115. func (e *defaultExpr) Line() int {
  116. if e == nil {
  117. return 0
  118. }
  119. return e.line
  120. }
  121. func (e *defaultExpr) Column() int {
  122. if e == nil {
  123. return 0
  124. }
  125. return e.column
  126. }
  127. func (e *defaultExpr) Text() string {
  128. if e == nil {
  129. return ""
  130. }
  131. return e.v
  132. }
  133. func (e *defaultExpr) SetText(text string) {
  134. if e == nil {
  135. return
  136. }
  137. e.v = text
  138. }
  139. func (e *defaultExpr) Start() int {
  140. if e == nil {
  141. return 0
  142. }
  143. return e.start
  144. }
  145. func (e *defaultExpr) Stop() int {
  146. if e == nil {
  147. return 0
  148. }
  149. return e.stop
  150. }
  151. func (e *defaultExpr) Equal(expr Expr) bool {
  152. if e == nil {
  153. if expr != nil {
  154. return false
  155. }
  156. return true
  157. }
  158. if expr == nil {
  159. return false
  160. }
  161. return e.v == expr.Text()
  162. }
  163. func (e *defaultExpr) IsNotNil() bool {
  164. return e != nil
  165. }
  166. func EqualDoc(spec1, spec2 Spec) bool {
  167. if spec1 == nil {
  168. return spec2 == nil
  169. }
  170. if spec2 == nil {
  171. return false
  172. }
  173. var expectDoc, actualDoc []Expr
  174. expectDoc = append(expectDoc, spec2.Doc()...)
  175. actualDoc = append(actualDoc, spec1.Doc()...)
  176. sort.Slice(expectDoc, func(i, j int) bool {
  177. return expectDoc[i].Line() < expectDoc[j].Line()
  178. })
  179. for index, each := range actualDoc {
  180. if !each.Equal(actualDoc[index]) {
  181. return false
  182. }
  183. }
  184. if spec1.Comment() != nil {
  185. if spec2.Comment() == nil {
  186. return false
  187. }
  188. if !spec1.Comment().Equal(spec2.Comment()) {
  189. return false
  190. }
  191. } else {
  192. if spec2.Comment() != nil {
  193. return false
  194. }
  195. }
  196. return true
  197. }
  198. func (v *ApiVisitor) getDoc(t TokenStream) []Expr {
  199. list := v.getHiddenTokensToLeft(t, api.COMEMNTS, false)
  200. return list
  201. }
  202. func (v *ApiVisitor) getComment(t TokenStream) Expr {
  203. list := v.getHiddenTokensToRight(t, api.COMEMNTS)
  204. if len(list) == 0 {
  205. return nil
  206. }
  207. commentExpr := list[0]
  208. stop := t.GetStop()
  209. text := stop.GetText()
  210. nlCount := strings.Count(text, "\n")
  211. if commentExpr.Line() != stop.GetLine()+nlCount {
  212. return nil
  213. }
  214. return commentExpr
  215. }
  216. func (v *ApiVisitor) getHiddenTokensToLeft(t TokenStream, channel int, containsCommentOfDefaultChannel bool) []Expr {
  217. ct := t.GetParser().GetTokenStream().(*antlr.CommonTokenStream)
  218. tokens := ct.GetHiddenTokensToLeft(t.GetStart().GetTokenIndex(), channel)
  219. var tmp []antlr.Token
  220. for _, each := range tokens {
  221. tmp = append(tmp, each)
  222. }
  223. var list []Expr
  224. for _, each := range tmp {
  225. if !containsCommentOfDefaultChannel {
  226. index := each.GetTokenIndex() - 1
  227. if index > 0 {
  228. allTokens := ct.GetAllTokens()
  229. var flag = false
  230. for i := index; i >= 0; i-- {
  231. tk := allTokens[i]
  232. if tk.GetChannel() == antlr.LexerDefaultTokenChannel {
  233. if tk.GetLine() == each.GetLine() {
  234. flag = true
  235. break
  236. }
  237. }
  238. }
  239. if flag {
  240. continue
  241. }
  242. }
  243. }
  244. list = append(list, v.newExprWithToken(each))
  245. }
  246. return list
  247. }
  248. func (v *ApiVisitor) getHiddenTokensToRight(t TokenStream, channel int) []Expr {
  249. ct := t.GetParser().GetTokenStream().(*antlr.CommonTokenStream)
  250. tokens := ct.GetHiddenTokensToRight(t.GetStop().GetTokenIndex(), channel)
  251. var list []Expr
  252. for _, each := range tokens {
  253. list = append(list, v.newExprWithToken(each))
  254. }
  255. return list
  256. }
  257. func (v *ApiVisitor) exportCheck(expr Expr) {
  258. if expr == nil || !expr.IsNotNil() {
  259. return
  260. }
  261. if api.IsBasicType(expr.Text()) {
  262. return
  263. }
  264. if util.UnExport(expr.Text()) {
  265. v.log.Warning("%s line %d:%d unexported declaration '%s', use %s instead", expr.Prefix(), expr.Line(),
  266. expr.Column(), expr.Text(), strings.Title(expr.Text()))
  267. }
  268. }