info.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package ast
  2. import (
  3. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser/g4/gen/api"
  4. )
  5. // InfoExpr defines info syntax for api
  6. type InfoExpr struct {
  7. Info Expr
  8. Lp Expr
  9. Rp Expr
  10. Kvs []*KvExpr
  11. }
  12. // VisitInfoSpec implements from api.BaseApiParserVisitor
  13. func (v *ApiVisitor) VisitInfoSpec(ctx *api.InfoSpecContext) interface{} {
  14. var expr InfoExpr
  15. expr.Info = v.newExprWithToken(ctx.GetInfoToken())
  16. expr.Lp = v.newExprWithToken(ctx.GetLp())
  17. expr.Rp = v.newExprWithToken(ctx.GetRp())
  18. list := ctx.AllKvLit()
  19. for _, each := range list {
  20. kvExpr := each.Accept(v).(*KvExpr)
  21. expr.Kvs = append(expr.Kvs, kvExpr)
  22. }
  23. if v.infoFlag {
  24. v.panic(expr.Info, "duplicate declaration 'info'")
  25. }
  26. return &expr
  27. }
  28. // Format provides a formatter for api command, now nothing to do
  29. func (i *InfoExpr) Format() error {
  30. // todo
  31. return nil
  32. }
  33. // Equal compares whether the element literals in two InfoExpr are equal
  34. func (i *InfoExpr) Equal(v interface{}) bool {
  35. if v == nil {
  36. return false
  37. }
  38. info, ok := v.(*InfoExpr)
  39. if !ok {
  40. return false
  41. }
  42. if !i.Info.Equal(info.Info) {
  43. return false
  44. }
  45. var expected, actual []*KvExpr
  46. expected = append(expected, i.Kvs...)
  47. actual = append(actual, info.Kvs...)
  48. if len(expected) != len(actual) {
  49. return false
  50. }
  51. for index, each := range expected {
  52. ac := actual[index]
  53. if !each.Equal(ac) {
  54. return false
  55. }
  56. }
  57. return true
  58. }