78 lines
1.4 KiB
C
78 lines
1.4 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stddef.h>
|
|
|
|
enum token_type {
|
|
tok_ident,
|
|
tok_param_ident,
|
|
tok_int,
|
|
tok_string,
|
|
|
|
/* Symbols */
|
|
tok_arrow,
|
|
tok_backslash,
|
|
tok_equal,
|
|
tok_left_paren,
|
|
tok_right_paren,
|
|
tok_left_square,
|
|
tok_right_square,
|
|
tok_left_brace,
|
|
tok_right_brace,
|
|
tok_left_angle_bracket,
|
|
tok_right_angle_bracket,
|
|
tok_comma,
|
|
tok_pipe,
|
|
tok_colon,
|
|
|
|
/* Keywords */
|
|
tok_true,
|
|
tok_false,
|
|
tok_case,
|
|
tok_let,
|
|
tok_in,
|
|
tok_match,
|
|
tok_of,
|
|
tok_def,
|
|
tok_datatype,
|
|
tok_alias,
|
|
tok_typecheck,
|
|
|
|
tok_eof,
|
|
};
|
|
|
|
struct token {
|
|
enum token_type type;
|
|
char *lexeme;
|
|
};
|
|
|
|
struct location {
|
|
size_t line;
|
|
size_t col;
|
|
};
|
|
|
|
#define LEXEME_MAX_LEN 1024
|
|
|
|
struct lexer {
|
|
/* Input stream to be lexed */
|
|
FILE *in;
|
|
/* Lookahead character */
|
|
char cur;
|
|
/* Internal buffer used to incrementally store lexemes */
|
|
char buf[LEXEME_MAX_LEN];
|
|
/* Number of character currently stored in buf */
|
|
size_t buf_len;
|
|
/* Source Location information */
|
|
struct location loc;
|
|
};
|
|
|
|
/* Initialize lexer.
|
|
* If there is an error, aborts the program.
|
|
*/
|
|
void init_lexer(struct lexer *lex, FILE *in);
|
|
|
|
/* Read next token, and store it in out.
|
|
* Store the token location in loc.
|
|
* If there is an error, aborts the program.
|
|
*/
|
|
void lex_next(struct lexer *lex, struct token *out, struct location *loc);
|