import.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package ast
  2. import (
  3. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  4. )
  5. type ImportExpr struct {
  6. Import Expr
  7. Value Expr
  8. DocExpr []Expr
  9. CommentExpr Expr
  10. }
  11. func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
  12. var list []*ImportExpr
  13. if ctx.ImportLit() != nil {
  14. lits := ctx.ImportLit().Accept(v).([]*ImportExpr)
  15. list = append(list, lits...)
  16. }
  17. if ctx.ImportBlock() != nil {
  18. blocks := ctx.ImportBlock().Accept(v).([]*ImportExpr)
  19. list = append(list, blocks...)
  20. }
  21. return list
  22. }
  23. func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
  24. importToken := v.newExprWithToken(ctx.GetImportToken())
  25. valueExpr := ctx.ImportValue().Accept(v).(Expr)
  26. return []*ImportExpr{
  27. {
  28. Import: importToken,
  29. Value: valueExpr,
  30. DocExpr: v.getDoc(ctx),
  31. CommentExpr: v.getComment(ctx),
  32. },
  33. }
  34. }
  35. func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
  36. importToken := v.newExprWithToken(ctx.GetImportToken())
  37. values := ctx.AllImportBlockValue()
  38. var list []*ImportExpr
  39. for _, value := range values {
  40. importExpr := value.Accept(v).(*ImportExpr)
  41. importExpr.Import = importToken
  42. list = append(list, importExpr)
  43. }
  44. return list
  45. }
  46. func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{} {
  47. value := ctx.ImportValue().Accept(v).(Expr)
  48. return &ImportExpr{
  49. Value: value,
  50. DocExpr: v.getDoc(ctx),
  51. CommentExpr: v.getComment(ctx),
  52. }
  53. }
  54. func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{} {
  55. return v.newExprWithTerminalNode(ctx.STRING())
  56. }
  57. func (i *ImportExpr) Format() error {
  58. // todo
  59. return nil
  60. }
  61. func (i *ImportExpr) Equal(v interface{}) bool {
  62. if v == nil {
  63. return false
  64. }
  65. imp, ok := v.(*ImportExpr)
  66. if !ok {
  67. return false
  68. }
  69. if !EqualDoc(i, imp) {
  70. return false
  71. }
  72. return i.Import.Equal(imp.Import) && i.Value.Equal(imp.Value)
  73. }
  74. func (i *ImportExpr) Doc() []Expr {
  75. return i.DocExpr
  76. }
  77. func (i *ImportExpr) Comment() Expr {
  78. return i.CommentExpr
  79. }