You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lex.h 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef L2_PARSE_LEX_H
  2. #define L2_PARSE_LEX_H
  3. #include "../io.h"
  4. enum l2_token_kind {
  5. L2_TOK_OPEN_PAREN_NS,
  6. L2_TOK_OPEN_PAREN,
  7. L2_TOK_CLOSE_PAREN,
  8. L2_TOK_OPEN_BRACE,
  9. L2_TOK_CLOSE_BRACE,
  10. L2_TOK_OPEN_BRACKET,
  11. L2_TOK_CLOSE_BRACKET,
  12. L2_TOK_QUOT,
  13. L2_TOK_COMMA,
  14. L2_TOK_PERIOD,
  15. L2_TOK_DOT_NUMBER,
  16. L2_TOK_COLON,
  17. L2_TOK_COLON_EQ,
  18. L2_TOK_EQUALS,
  19. L2_TOK_EOL,
  20. L2_TOK_EOF,
  21. L2_TOK_NUMBER,
  22. L2_TOK_STRING,
  23. L2_TOK_IDENT,
  24. L2_TOK_ERROR,
  25. };
  26. enum l2_token_flags {
  27. L2_TOK_SMALL = 1 << 7,
  28. };
  29. const char *l2_token_kind_name(enum l2_token_kind kind);
  30. struct l2_token_value {
  31. union {
  32. struct {
  33. unsigned char flags;
  34. union {
  35. char *str;
  36. double num;
  37. int integer;
  38. };
  39. };
  40. struct {
  41. unsigned char padding;
  42. char strbuf[15];
  43. };
  44. };
  45. };
  46. struct l2_token {
  47. int line;
  48. int ch;
  49. struct l2_token_value v;
  50. };
  51. #define l2_token_get_kind(tok) ((enum l2_token_kind)((tok)->v.flags & ~(1 << 7)))
  52. #define l2_token_get_name(tok) (l2_token_kind_name(l2_token_get_kind(tok)))
  53. #define l2_token_is_small(tok) ((tok)->v.flags & L2_TOK_SMALL)
  54. void l2_token_free(struct l2_token *tok);
  55. struct l2_token_value l2_token_extract_val(struct l2_token *tok);
  56. const char *l2_token_get_str(struct l2_token *tok);
  57. void l2_token_print(struct l2_token *tok, struct l2_io_writer *w);
  58. struct l2_lexer {
  59. struct l2_token toks[4];
  60. int tokidx;
  61. int line;
  62. int ch;
  63. int parens;
  64. int do_log_tokens;
  65. struct l2_bufio_reader reader;
  66. };
  67. void l2_lexer_init(struct l2_lexer *lexer, struct l2_io_reader *r);
  68. struct l2_token *l2_lexer_peek(struct l2_lexer *lexer, int count);
  69. void l2_lexer_consume(struct l2_lexer *lexer);
  70. void l2_lexer_skip_opt(struct l2_lexer *lexer, enum l2_token_kind kind);
  71. #endif