ast.go 6.4 KB

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