ApiParser.g4 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. grammar ApiParser;
  2. import ApiLexer;
  3. @lexer::members{
  4. const COMEMNTS = 88
  5. }
  6. api: spec*;
  7. spec: syntaxLit
  8. |importSpec
  9. |infoSpec
  10. |typeSpec
  11. |serviceSpec
  12. ;
  13. // syntax
  14. syntaxLit: {match(p,"syntax")}syntaxToken=ID assign='=' {checkVersion(p)}version=STRING;
  15. // import
  16. importSpec: importLit|importBlock;
  17. importLit: {match(p,"import")}importToken=ID importValue ;
  18. importBlock: {match(p,"import")}importToken=ID '(' importBlockValue+ ')';
  19. importBlockValue: importValue;
  20. importValue: {checkImportValue(p)}STRING;
  21. // info
  22. infoSpec: {match(p,"info")}infoToken=ID lp='(' kvLit+ rp=')';
  23. // type
  24. typeSpec: typeLit
  25. |typeBlock;
  26. // eg: type Foo int
  27. typeLit: {match(p,"type")}typeToken=ID typeLitBody;
  28. // eg: type (...)
  29. typeBlock: {match(p,"type")}typeToken=ID lp='(' typeBlockBody* rp=')';
  30. typeLitBody: typeStruct|typeAlias;
  31. typeBlockBody: typeBlockStruct|typeBlockAlias;
  32. typeStruct: {checkKeyword(p)}structName=ID structToken=ID? lbrace='{' field* rbrace='}';
  33. typeAlias: {checkKeyword(p)}alias=ID assign='='? dataType;
  34. typeBlockStruct: {checkKeyword(p)}structName=ID structToken=ID? lbrace='{' field* rbrace='}';
  35. typeBlockAlias: {checkKeyword(p)}alias=ID assign='='? dataType;
  36. field: {isNormal(p)}? normalField|anonymousFiled ;
  37. normalField: {checkKeyword(p)}fieldName=ID dataType tag=RAW_STRING?;
  38. anonymousFiled: star='*'? ID;
  39. dataType: {isInterface(p)}ID
  40. |mapType
  41. |arrayType
  42. |inter='interface{}'
  43. |time='time.Time'
  44. |pointerType
  45. |typeStruct
  46. ;
  47. pointerType: star='*' {checkKeyword(p)}ID;
  48. mapType: {match(p,"map")}mapToken=ID lbrack='[' {checkKey(p)}key=ID rbrack=']' value=dataType;
  49. arrayType: lbrack='[' rbrack=']' dataType;
  50. // service
  51. serviceSpec: atServer? serviceApi;
  52. atServer: ATSERVER lp='(' kvLit+ rp=')';
  53. serviceApi: {match(p,"service")}serviceToken=ID serviceName lbrace='{' serviceRoute* rbrace='}';
  54. serviceRoute: atDoc? (atServer|atHandler) route;
  55. atDoc: ATDOC lp='('? ((kvLit+)|STRING) rp=')'?;
  56. atHandler: ATHANDLER ID;
  57. route: {checkHttpMethod(p)}httpMethod=ID path request=body? returnToken=ID? response=replybody?;
  58. body: lp='(' (ID)? rp=')';
  59. replybody: lp='(' dataType? rp=')';
  60. // kv
  61. kvLit: key=ID {checkKeyValue(p)}value=LINE_VALUE;
  62. serviceName: (ID '-'?)+;
  63. path: (('/' (ID ('-' ID)*))|('/:' (ID ('-' ID)?)))+;