import.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package ast
  2. import (
  3. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/gen/api"
  4. )
  5. // ImportExpr defines import syntax for api
  6. type ImportExpr struct {
  7. Import Expr
  8. Value Expr
  9. DocExpr []Expr
  10. CommentExpr Expr
  11. }
  12. // VisitImportSpec implements from api.BaseApiParserVisitor
  13. func (v *ApiVisitor) VisitImportSpec(ctx *api.ImportSpecContext) interface{} {
  14. var list []*ImportExpr
  15. if ctx.ImportLit() != nil {
  16. lits := ctx.ImportLit().Accept(v).([]*ImportExpr)
  17. list = append(list, lits...)
  18. }
  19. if ctx.ImportBlock() != nil {
  20. blocks := ctx.ImportBlock().Accept(v).([]*ImportExpr)
  21. list = append(list, blocks...)
  22. }
  23. return list
  24. }
  25. // VisitImportLit implements from api.BaseApiParserVisitor
  26. func (v *ApiVisitor) VisitImportLit(ctx *api.ImportLitContext) interface{} {
  27. importToken := v.newExprWithToken(ctx.GetImportToken())
  28. valueExpr := ctx.ImportValue().Accept(v).(Expr)
  29. return []*ImportExpr{
  30. {
  31. Import: importToken,
  32. Value: valueExpr,
  33. DocExpr: v.getDoc(ctx),
  34. CommentExpr: v.getComment(ctx),
  35. },
  36. }
  37. }
  38. // VisitImportBlock implements from api.BaseApiParserVisitor
  39. func (v *ApiVisitor) VisitImportBlock(ctx *api.ImportBlockContext) interface{} {
  40. importToken := v.newExprWithToken(ctx.GetImportToken())
  41. values := ctx.AllImportBlockValue()
  42. var list []*ImportExpr
  43. for _, value := range values {
  44. importExpr := value.Accept(v).(*ImportExpr)
  45. importExpr.Import = importToken
  46. list = append(list, importExpr)
  47. }
  48. return list
  49. }
  50. // VisitImportBlockValue implements from api.BaseApiParserVisitor
  51. func (v *ApiVisitor) VisitImportBlockValue(ctx *api.ImportBlockValueContext) interface{} {
  52. value := ctx.ImportValue().Accept(v).(Expr)
  53. return &ImportExpr{
  54. Value: value,
  55. DocExpr: v.getDoc(ctx),
  56. CommentExpr: v.getComment(ctx),
  57. }
  58. }
  59. // VisitImportValue implements from api.BaseApiParserVisitor
  60. func (v *ApiVisitor) VisitImportValue(ctx *api.ImportValueContext) interface{} {
  61. return v.newExprWithTerminalNode(ctx.STRING())
  62. }
  63. // Format provides a formatter for api command, now nothing to do
  64. func (i *ImportExpr) Format() error {
  65. // todo
  66. return nil
  67. }
  68. // Equal compares whether the element literals in two ImportExpr are equal
  69. func (i *ImportExpr) Equal(v interface{}) bool {
  70. if v == nil {
  71. return false
  72. }
  73. imp, ok := v.(*ImportExpr)
  74. if !ok {
  75. return false
  76. }
  77. if !EqualDoc(i, imp) {
  78. return false
  79. }
  80. return i.Import.Equal(imp.Import) && i.Value.Equal(imp.Value)
  81. }
  82. // Doc returns the document of ImportExpr, like // some text
  83. func (i *ImportExpr) Doc() []Expr {
  84. return i.DocExpr
  85. }
  86. // Comment returns the comment of ImportExpr, like // some text
  87. func (i *ImportExpr) Comment() Expr {
  88. return i.CommentExpr
  89. }