token.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package token
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/placeholder"
  5. "github.com/zeromicro/go-zero/tools/goctl/util"
  6. )
  7. const (
  8. Syntax = "syntax"
  9. Info = "info"
  10. Service = "service"
  11. Returns = "returns"
  12. Any = "any"
  13. TypeKeyword = "type"
  14. MapKeyword = "map"
  15. ImportKeyword = "import"
  16. )
  17. // Type is the type of token.
  18. type Type int
  19. // EofToken is the end of file token.
  20. var EofToken = Token{Type: EOF}
  21. // ErrorToken is the error token.
  22. var ErrorToken = Token{Type: error}
  23. // Token is the token of a rune.
  24. type Token struct {
  25. Type Type
  26. Text string
  27. Position Position
  28. }
  29. // Fork forks token for a given Type.
  30. func (t Token) Fork(tp Type) Token {
  31. return Token{
  32. Type: tp,
  33. Text: t.Text,
  34. Position: t.Position,
  35. }
  36. }
  37. // IsEmptyString returns true if the token is empty string.
  38. func (t Token) IsEmptyString() bool {
  39. if t.Type != STRING && t.Type != RAW_STRING {
  40. return false
  41. }
  42. text := util.TrimWhiteSpace(t.Text)
  43. return text == `""` || text == "``"
  44. }
  45. // IsComment returns true if the token is comment.
  46. func (t Token) IsComment() bool {
  47. return t.IsType(COMMENT)
  48. }
  49. // IsDocument returns true if the token is document.
  50. func (t Token) IsDocument() bool {
  51. return t.IsType(DOCUMENT)
  52. }
  53. // IsType returns true if the token is the given type.
  54. func (t Token) IsType(tp Type) bool {
  55. return t.Type == tp
  56. }
  57. // Line returns the line number of the token.
  58. func (t Token) Line() int {
  59. return t.Position.Line
  60. }
  61. // String returns the string of the token.
  62. func (t Token) String() string {
  63. if t == ErrorToken {
  64. return t.Type.String()
  65. }
  66. return fmt.Sprintf("%s %s %s", t.Position.String(), t.Type.String(), t.Text)
  67. }
  68. // Valid returns true if the token is valid.
  69. func (t Token) Valid() bool {
  70. return t.Type != token_bg
  71. }
  72. // IsKeyword returns true if the token is keyword.
  73. func (t Token) IsKeyword() bool {
  74. return golang_keyword_beg < t.Type && t.Type < golang_keyword_end
  75. }
  76. // IsBaseType returns true if the token is base type.
  77. func (t Token) IsBaseType() bool {
  78. _, ok := baseDataType[t.Text]
  79. return ok
  80. }
  81. // IsHttpMethod returns true if the token is http method.
  82. func (t Token) IsHttpMethod() bool {
  83. _, ok := httpMethod[t.Text]
  84. return ok
  85. }
  86. // Is returns true if the token text is one of the given list.
  87. func (t Token) Is(text ...string) bool {
  88. for _, v := range text {
  89. if t.Text == v {
  90. return true
  91. }
  92. }
  93. return false
  94. }
  95. const (
  96. token_bg Type = iota
  97. error
  98. ILLEGAL
  99. EOF
  100. COMMENT
  101. DOCUMENT
  102. literal_beg
  103. IDENT // main
  104. INT // 123
  105. DURATION // 3s,3ms
  106. STRING // "abc"
  107. RAW_STRING // `abc`
  108. PATH // `abc`
  109. KEY // `abc:`
  110. literal_end
  111. operator_beg
  112. SUB // -
  113. MUL // *
  114. QUO // /
  115. ASSIGN // =
  116. LPAREN // (
  117. LBRACK // [
  118. LBRACE // {
  119. COMMA // ,
  120. DOT // .
  121. RPAREN // )
  122. RBRACE // }
  123. RBRACK // ]
  124. SEMICOLON // ;
  125. COLON // :
  126. ELLIPSIS
  127. operator_end
  128. golang_keyword_beg
  129. BREAK
  130. CASE
  131. CHAN
  132. CONST
  133. CONTINUE
  134. DEFAULT
  135. DEFER
  136. ELSE
  137. FALLTHROUGH
  138. FOR
  139. FUNC
  140. GO
  141. GOTO
  142. IF
  143. IMPORT
  144. INTERFACE
  145. MAP
  146. PACKAGE
  147. RANGE
  148. RETURN
  149. SELECT
  150. STRUCT
  151. SWITCH
  152. TYPE
  153. VAR
  154. golang_keyword_end
  155. api_keyword_bg
  156. AT_DOC
  157. AT_HANDLER
  158. AT_SERVER
  159. ANY
  160. api_keyword_end
  161. token_end
  162. )
  163. // String returns the string of the token type.
  164. func (t Type) String() string {
  165. if t >= token_bg && t < token_end {
  166. return tokens[t]
  167. }
  168. return ""
  169. }
  170. var tokens = [...]string{
  171. ILLEGAL: "ILLEGAL",
  172. EOF: "EOF",
  173. COMMENT: "COMMENT",
  174. DOCUMENT: "DOCUMENT",
  175. IDENT: "IDENT",
  176. INT: "INT",
  177. DURATION: "DURATION",
  178. STRING: "STRING",
  179. RAW_STRING: "RAW_STRING",
  180. PATH: "PATH",
  181. KEY: "KEY",
  182. SUB: "-",
  183. MUL: "*",
  184. QUO: "/",
  185. ASSIGN: "=",
  186. LPAREN: "(",
  187. LBRACK: "[",
  188. LBRACE: "{",
  189. COMMA: ",",
  190. DOT: ".",
  191. RPAREN: ")",
  192. RBRACK: "]",
  193. RBRACE: "}",
  194. SEMICOLON: ";",
  195. COLON: ":",
  196. ELLIPSIS: "...",
  197. BREAK: "break",
  198. CASE: "case",
  199. CHAN: "chan",
  200. CONST: "const",
  201. CONTINUE: "continue",
  202. DEFAULT: "default",
  203. DEFER: "defer",
  204. ELSE: "else",
  205. FALLTHROUGH: "fallthrough",
  206. FOR: "for",
  207. FUNC: "func",
  208. GO: "go",
  209. GOTO: "goto",
  210. IF: "if",
  211. IMPORT: "import",
  212. INTERFACE: "interface",
  213. MAP: "map",
  214. PACKAGE: "package",
  215. RANGE: "range",
  216. RETURN: "return",
  217. SELECT: "select",
  218. STRUCT: "struct",
  219. SWITCH: "switch",
  220. TYPE: "type",
  221. VAR: "var",
  222. AT_DOC: "@doc",
  223. AT_HANDLER: "@handler",
  224. AT_SERVER: "@server",
  225. ANY: "interface{}",
  226. }
  227. // HttpMethods returns the http methods.
  228. var HttpMethods = []interface{}{"get", "head", "post", "put", "patch", "delete", "connect", "options", "trace"}
  229. var httpMethod = map[string]placeholder.Type{
  230. "get": placeholder.PlaceHolder,
  231. "head": placeholder.PlaceHolder,
  232. "post": placeholder.PlaceHolder,
  233. "put": placeholder.PlaceHolder,
  234. "patch": placeholder.PlaceHolder,
  235. "delete": placeholder.PlaceHolder,
  236. "connect": placeholder.PlaceHolder,
  237. "options": placeholder.PlaceHolder,
  238. "trace": placeholder.PlaceHolder,
  239. }
  240. var keywords = map[string]Type{
  241. // golang_keyword_bg
  242. "break": BREAK,
  243. "case": CASE,
  244. "chan": CHAN,
  245. "const": CONST,
  246. "continue": CONTINUE,
  247. "default": DEFAULT,
  248. "defer": DEFER,
  249. "else": ELSE,
  250. "fallthrough": FALLTHROUGH,
  251. "for": FOR,
  252. "func": FUNC,
  253. "go": GO,
  254. "goto": GOTO,
  255. "if": IF,
  256. "import": IMPORT,
  257. "interface": INTERFACE,
  258. "map": MAP,
  259. "package": PACKAGE,
  260. "range": RANGE,
  261. "return": RETURN,
  262. "select": SELECT,
  263. "struct": STRUCT,
  264. "switch": SWITCH,
  265. "type": TYPE,
  266. "var": VAR,
  267. // golang_keyword_end
  268. }
  269. var baseDataType = map[string]placeholder.Type{
  270. "bool": placeholder.PlaceHolder,
  271. "uint8": placeholder.PlaceHolder,
  272. "uint16": placeholder.PlaceHolder,
  273. "uint32": placeholder.PlaceHolder,
  274. "uint64": placeholder.PlaceHolder,
  275. "int8": placeholder.PlaceHolder,
  276. "int16": placeholder.PlaceHolder,
  277. "int32": placeholder.PlaceHolder,
  278. "int64": placeholder.PlaceHolder,
  279. "float32": placeholder.PlaceHolder,
  280. "float64": placeholder.PlaceHolder,
  281. "complex64": placeholder.PlaceHolder,
  282. "complex128": placeholder.PlaceHolder,
  283. "string": placeholder.PlaceHolder,
  284. "int": placeholder.PlaceHolder,
  285. "uint": placeholder.PlaceHolder,
  286. "uintptr": placeholder.PlaceHolder,
  287. "byte": placeholder.PlaceHolder,
  288. "rune": placeholder.PlaceHolder,
  289. "any": placeholder.PlaceHolder,
  290. }
  291. // LookupKeyword returns the keyword type if the given ident is keyword.
  292. func LookupKeyword(ident string) (Type, bool) {
  293. tp, ok := keywords[ident]
  294. return tp, ok
  295. }
  296. // NewIllegalToken returns a new illegal token.
  297. func NewIllegalToken(b rune, pos Position) Token {
  298. return Token{
  299. Type: ILLEGAL,
  300. Text: string(b),
  301. Position: pos,
  302. }
  303. }