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

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