apiparser.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package ast
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "path/filepath"
  6. "strings"
  7. "github.com/antlr/antlr4/runtime/Go/antlr"
  8. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  9. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  10. )
  11. type (
  12. // Parser provides api parsing capabilities
  13. Parser struct {
  14. linePrefix string
  15. debug bool
  16. log console.Console
  17. antlr.DefaultErrorListener
  18. }
  19. // ParserOption defines an function with argument Parser
  20. ParserOption func(p *Parser)
  21. )
  22. // NewParser creates an instance for Parser
  23. func NewParser(options ...ParserOption) *Parser {
  24. p := &Parser{
  25. log: console.NewColorConsole(),
  26. }
  27. for _, opt := range options {
  28. opt(p)
  29. }
  30. return p
  31. }
  32. // Accept can parse any terminalNode of api tree by fn.
  33. // -- for debug
  34. func (p *Parser) Accept(fn func(p *api.ApiParserParser, visitor *ApiVisitor) interface{}, content string) (v interface{}, err error) {
  35. defer func() {
  36. p := recover()
  37. if p != nil {
  38. switch e := p.(type) {
  39. case error:
  40. err = e
  41. default:
  42. err = fmt.Errorf("%+v", p)
  43. }
  44. }
  45. }()
  46. inputStream := antlr.NewInputStream(content)
  47. lexer := api.NewApiParserLexer(inputStream)
  48. lexer.RemoveErrorListeners()
  49. tokens := antlr.NewCommonTokenStream(lexer, antlr.LexerDefaultTokenChannel)
  50. apiParser := api.NewApiParserParser(tokens)
  51. apiParser.RemoveErrorListeners()
  52. apiParser.AddErrorListener(p)
  53. var visitorOptions []VisitorOption
  54. visitorOptions = append(visitorOptions, WithVisitorPrefix(p.linePrefix))
  55. if p.debug {
  56. visitorOptions = append(visitorOptions, WithVisitorDebug())
  57. }
  58. visitor := NewApiVisitor(visitorOptions...)
  59. v = fn(apiParser, visitor)
  60. return
  61. }
  62. // Parse is used to parse the api from the specified file name
  63. func (p *Parser) Parse(filename string) (*Api, error) {
  64. data, err := p.readContent(filename)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return p.parse(filename, data)
  69. }
  70. // ParseContent is used to parse the api from the specified content
  71. func (p *Parser) ParseContent(content string) (*Api, error) {
  72. return p.parse("", content)
  73. }
  74. // parse is used to parse api from the content
  75. // filename is only used to mark the file where the error is located
  76. func (p *Parser) parse(filename, content string) (*Api, error) {
  77. root, err := p.invoke(filename, content)
  78. if err != nil {
  79. return nil, err
  80. }
  81. var apiAstList []*Api
  82. apiAstList = append(apiAstList, root)
  83. for _, imp := range root.Import {
  84. path := imp.Value.Text()
  85. data, err := p.readContent(path)
  86. if err != nil {
  87. return nil, err
  88. }
  89. nestedApi, err := p.invoke(path, data)
  90. if err != nil {
  91. return nil, err
  92. }
  93. err = p.valid(root, nestedApi)
  94. if err != nil {
  95. return nil, err
  96. }
  97. apiAstList = append(apiAstList, nestedApi)
  98. }
  99. err = p.checkTypeDeclaration(apiAstList)
  100. if err != nil {
  101. return nil, err
  102. }
  103. allApi := p.memberFill(apiAstList)
  104. return allApi, nil
  105. }
  106. func (p *Parser) invoke(linePrefix, content string) (v *Api, err error) {
  107. defer func() {
  108. p := recover()
  109. if p != nil {
  110. switch e := p.(type) {
  111. case error:
  112. err = e
  113. default:
  114. err = fmt.Errorf("%+v", p)
  115. }
  116. }
  117. }()
  118. if linePrefix != "" {
  119. p.linePrefix = linePrefix
  120. }
  121. inputStream := antlr.NewInputStream(content)
  122. lexer := api.NewApiParserLexer(inputStream)
  123. lexer.RemoveErrorListeners()
  124. tokens := antlr.NewCommonTokenStream(lexer, antlr.LexerDefaultTokenChannel)
  125. apiParser := api.NewApiParserParser(tokens)
  126. apiParser.RemoveErrorListeners()
  127. apiParser.AddErrorListener(p)
  128. var visitorOptions []VisitorOption
  129. visitorOptions = append(visitorOptions, WithVisitorPrefix(p.linePrefix))
  130. if p.debug {
  131. visitorOptions = append(visitorOptions, WithVisitorDebug())
  132. }
  133. visitor := NewApiVisitor(visitorOptions...)
  134. v = apiParser.Api().Accept(visitor).(*Api)
  135. v.LinePrefix = p.linePrefix
  136. return
  137. }
  138. func (p *Parser) valid(mainApi *Api, nestedApi *Api) error {
  139. err := p.nestedApiCheck(mainApi, nestedApi)
  140. if err != nil {
  141. return err
  142. }
  143. mainHandlerMap := make(map[string]PlaceHolder)
  144. mainRouteMap := make(map[string]PlaceHolder)
  145. mainTypeMap := make(map[string]PlaceHolder)
  146. routeMap := func(list []*ServiceRoute) (map[string]PlaceHolder, map[string]PlaceHolder) {
  147. handlerMap := make(map[string]PlaceHolder)
  148. routeMap := make(map[string]PlaceHolder)
  149. for _, g := range list {
  150. handler := g.GetHandler()
  151. if handler.IsNotNil() {
  152. handlerName := handler.Text()
  153. handlerMap[handlerName] = Holder
  154. path := fmt.Sprintf("%s://%s", g.Route.Method.Text(), g.Route.Path.Text())
  155. routeMap[path] = Holder
  156. }
  157. }
  158. return handlerMap, routeMap
  159. }
  160. for _, each := range mainApi.Service {
  161. h, r := routeMap(each.ServiceApi.ServiceRoute)
  162. for k, v := range h {
  163. mainHandlerMap[k] = v
  164. }
  165. for k, v := range r {
  166. mainRouteMap[k] = v
  167. }
  168. }
  169. for _, each := range mainApi.Type {
  170. mainTypeMap[each.NameExpr().Text()] = Holder
  171. }
  172. // duplicate route check
  173. err = p.duplicateRouteCheck(nestedApi, mainHandlerMap, mainRouteMap)
  174. if err != nil {
  175. return err
  176. }
  177. // duplicate type check
  178. for _, each := range nestedApi.Type {
  179. if _, ok := mainTypeMap[each.NameExpr().Text()]; ok {
  180. return fmt.Errorf("%s line %d:%d duplicate type declaration '%s'",
  181. nestedApi.LinePrefix, each.NameExpr().Line(), each.NameExpr().Column(), each.NameExpr().Text())
  182. }
  183. }
  184. return nil
  185. }
  186. func (p *Parser) duplicateRouteCheck(nestedApi *Api, mainHandlerMap map[string]PlaceHolder, mainRouteMap map[string]PlaceHolder) error {
  187. for _, each := range nestedApi.Service {
  188. for _, r := range each.ServiceApi.ServiceRoute {
  189. handler := r.GetHandler()
  190. if !handler.IsNotNil() {
  191. return fmt.Errorf("%s handler not exist near line %d", nestedApi.LinePrefix, r.Route.Method.Line())
  192. }
  193. if _, ok := mainHandlerMap[handler.Text()]; ok {
  194. return fmt.Errorf("%s line %d:%d duplicate handler '%s'",
  195. nestedApi.LinePrefix, handler.Line(), handler.Column(), handler.Text())
  196. }
  197. path := fmt.Sprintf("%s://%s", r.Route.Method.Text(), r.Route.Path.Text())
  198. if _, ok := mainRouteMap[path]; ok {
  199. return fmt.Errorf("%s line %d:%d duplicate route '%s'",
  200. nestedApi.LinePrefix, r.Route.Method.Line(), r.Route.Method.Column(), r.Route.Method.Text()+" "+r.Route.Path.Text())
  201. }
  202. }
  203. }
  204. return nil
  205. }
  206. func (p *Parser) nestedApiCheck(mainApi *Api, nestedApi *Api) error {
  207. if len(nestedApi.Import) > 0 {
  208. importToken := nestedApi.Import[0].Import
  209. return fmt.Errorf("%s line %d:%d the nested api does not support import",
  210. nestedApi.LinePrefix, importToken.Line(), importToken.Column())
  211. }
  212. if mainApi.Syntax != nil && nestedApi.Syntax != nil {
  213. if mainApi.Syntax.Version.Text() != nestedApi.Syntax.Version.Text() {
  214. syntaxToken := nestedApi.Syntax.Syntax
  215. return fmt.Errorf("%s line %d:%d multiple syntax declaration, expecting syntax '%s', but found '%s'",
  216. nestedApi.LinePrefix, syntaxToken.Line(), syntaxToken.Column(), mainApi.Syntax.Version.Text(), nestedApi.Syntax.Version.Text())
  217. }
  218. }
  219. if len(mainApi.Service) > 0 {
  220. mainService := mainApi.Service[0]
  221. for _, service := range nestedApi.Service {
  222. if mainService.ServiceApi.Name.Text() != service.ServiceApi.Name.Text() {
  223. return fmt.Errorf("%s multiple service name declaration, expecting service name '%s', but found '%s'",
  224. nestedApi.LinePrefix, mainService.ServiceApi.Name.Text(), service.ServiceApi.Name.Text())
  225. }
  226. }
  227. }
  228. return nil
  229. }
  230. func (p *Parser) memberFill(apiList []*Api) *Api {
  231. var root Api
  232. for index, each := range apiList {
  233. if index == 0 {
  234. root.Syntax = each.Syntax
  235. root.Info = each.Info
  236. root.Import = each.Import
  237. }
  238. root.Type = append(root.Type, each.Type...)
  239. root.Service = append(root.Service, each.Service...)
  240. }
  241. return &root
  242. }
  243. // checkTypeDeclaration checks whether a struct type has been declared in context
  244. func (p *Parser) checkTypeDeclaration(apiList []*Api) error {
  245. types := make(map[string]TypeExpr)
  246. for _, root := range apiList {
  247. for _, each := range root.Type {
  248. types[each.NameExpr().Text()] = each
  249. }
  250. }
  251. for _, apiItem := range apiList {
  252. linePrefix := apiItem.LinePrefix
  253. err := p.checkTypes(apiItem, linePrefix, types)
  254. if err != nil {
  255. return err
  256. }
  257. err = p.checkServices(apiItem, types, linePrefix)
  258. if err != nil {
  259. return err
  260. }
  261. }
  262. return nil
  263. }
  264. func (p *Parser) checkServices(apiItem *Api, types map[string]TypeExpr, linePrefix string) error {
  265. for _, service := range apiItem.Service {
  266. for _, each := range service.ServiceApi.ServiceRoute {
  267. route := each.Route
  268. err := p.checkRequestBody(route, types, linePrefix)
  269. if err != nil {
  270. return err
  271. }
  272. if route.Reply != nil && route.Reply.Name.IsNotNil() && route.Reply.Name.Expr().IsNotNil() {
  273. reply := route.Reply.Name
  274. var structName string
  275. switch tp := reply.(type) {
  276. case *Literal:
  277. structName = tp.Literal.Text()
  278. case *Array:
  279. switch innerTp := tp.Literal.(type) {
  280. case *Literal:
  281. structName = innerTp.Literal.Text()
  282. case *Pointer:
  283. structName = innerTp.Name.Text()
  284. }
  285. }
  286. if api.IsBasicType(structName) {
  287. continue
  288. }
  289. _, ok := types[structName]
  290. if !ok {
  291. return fmt.Errorf("%s line %d:%d can not found declaration '%s' in context",
  292. linePrefix, route.Reply.Name.Expr().Line(), route.Reply.Name.Expr().Column(), structName)
  293. }
  294. }
  295. }
  296. }
  297. return nil
  298. }
  299. func (p *Parser) checkRequestBody(route *Route, types map[string]TypeExpr, linePrefix string) error {
  300. if route.Req != nil && route.Req.Name.IsNotNil() && route.Req.Name.Expr().IsNotNil() {
  301. _, ok := types[route.Req.Name.Expr().Text()]
  302. if !ok {
  303. return fmt.Errorf("%s line %d:%d can not found declaration '%s' in context",
  304. linePrefix, route.Req.Name.Expr().Line(), route.Req.Name.Expr().Column(), route.Req.Name.Expr().Text())
  305. }
  306. }
  307. return nil
  308. }
  309. func (p *Parser) checkTypes(apiItem *Api, linePrefix string, types map[string]TypeExpr) error {
  310. for _, each := range apiItem.Type {
  311. tp, ok := each.(*TypeStruct)
  312. if !ok {
  313. continue
  314. }
  315. for _, member := range tp.Fields {
  316. err := p.checkType(linePrefix, types, member.DataType)
  317. if err != nil {
  318. return err
  319. }
  320. }
  321. }
  322. return nil
  323. }
  324. func (p *Parser) checkType(linePrefix string, types map[string]TypeExpr, expr DataType) error {
  325. if expr == nil {
  326. return nil
  327. }
  328. switch v := expr.(type) {
  329. case *Literal:
  330. name := v.Literal.Text()
  331. if api.IsBasicType(name) {
  332. return nil
  333. }
  334. _, ok := types[name]
  335. if !ok {
  336. return fmt.Errorf("%s line %d:%d can not found declaration '%s' in context",
  337. linePrefix, v.Literal.Line(), v.Literal.Column(), name)
  338. }
  339. case *Pointer:
  340. name := v.Name.Text()
  341. if api.IsBasicType(name) {
  342. return nil
  343. }
  344. _, ok := types[name]
  345. if !ok {
  346. return fmt.Errorf("%s line %d:%d can not found declaration '%s' in context",
  347. linePrefix, v.Name.Line(), v.Name.Column(), name)
  348. }
  349. case *Map:
  350. return p.checkType(linePrefix, types, v.Value)
  351. case *Array:
  352. return p.checkType(linePrefix, types, v.Literal)
  353. default:
  354. return nil
  355. }
  356. return nil
  357. }
  358. func (p *Parser) readContent(filename string) (string, error) {
  359. filename = strings.ReplaceAll(filename, `"`, "")
  360. abs, err := filepath.Abs(filename)
  361. if err != nil {
  362. return "", err
  363. }
  364. data, err := ioutil.ReadFile(abs)
  365. if err != nil {
  366. return "", err
  367. }
  368. return string(data), nil
  369. }
  370. // SyntaxError accepts errors and panic it
  371. func (p *Parser) SyntaxError(_ antlr.Recognizer, _ interface{}, line, column int, msg string, _ antlr.RecognitionException) {
  372. str := fmt.Sprintf(`%s line %d:%d %s`, p.linePrefix, line, column, msg)
  373. if p.debug {
  374. p.log.Error(str)
  375. }
  376. panic(str)
  377. }
  378. // WithParserDebug returns a debug ParserOption
  379. func WithParserDebug() ParserOption {
  380. return func(p *Parser) {
  381. p.debug = true
  382. }
  383. }
  384. // WithParserPrefix returns a prefix ParserOption
  385. func WithParserPrefix(prefix string) ParserOption {
  386. return func(p *Parser) {
  387. p.linePrefix = prefix
  388. }
  389. }