token.go 1002 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package token
  2. // Type is a specific type of int which describes the symbols.
  3. type Type int
  4. // Token describes the letter(s) or symbol, is a result of the lexer.
  5. type Token struct {
  6. Type Type
  7. Literal string
  8. Start int // including the first char
  9. End int // including the last char
  10. }
  11. // /about/{fullname:alphabetical}
  12. // /profile/{anySpecialName:string}
  13. // {id:uint64 range(1,5) else 404}
  14. // /admin/{id:int eq(1) else 402}
  15. // /file/{filepath:file else 405}
  16. const (
  17. EOF = iota // 0
  18. ILLEGAL
  19. // Identifiers + literals
  20. LBRACE // {
  21. RBRACE // }
  22. // PARAM_IDENTIFIER // id
  23. COLON // :
  24. LPAREN // (
  25. RPAREN // )
  26. // PARAM_FUNC_ARG // 1
  27. COMMA
  28. IDENT // string or keyword
  29. // Keywords
  30. // keywords_start
  31. ELSE // else
  32. // keywords_end
  33. INT // 42
  34. )
  35. var keywords = map[string]Type{
  36. "else": ELSE,
  37. }
  38. // LookupIdent receives a series of chars
  39. // and tries to resolves the token type.
  40. func LookupIdent(ident string) Type {
  41. if tok, ok := keywords[ident]; ok {
  42. return tok
  43. }
  44. return IDENT
  45. }