scanner.h 3.2 KiB raw
1
#ifndef SCANNER_H
2
#define SCANNER_H
3
4
#include "types.h"
5
6
/* Token kinds. */
7
typedef enum {
8
    /* End of file token generated by the scanner
9
     * when the input is exhausted. */
10
    T_EOF,
11
12
    /* Special "error" token. */
13
    T_INVALID,
14
15
    /* Single-char tokens. */
16
    T_LPAREN,     /* ( */
17
    T_RPAREN,     /* ) */
18
    T_LBRACE,     /* { */
19
    T_RBRACE,     /* } */
20
    T_LBRACKET,   /* [ */
21
    T_RBRACKET,   /* ] */
22
    T_COMMA,      /* , */
23
    T_DOT,        /* . */
24
    T_DOT_DOT,    /* .. */
25
    T_MINUS,      /* - */
26
    T_PLUS,       /* + */
27
    T_SEMICOLON,  /* ; */
28
    T_SLASH,      /* / */
29
    T_STAR,       /* * */
30
    T_PERCENT,    /* % */
31
    T_AMP,        /* & */
32
    T_PIPE,       /* | */
33
    T_CARET,      /* ^ */
34
    T_TILDE,      /* ~ */
35
    T_UNDERSCORE, /* _ */
36
37
    /* One or two char tokens. */
38
    T_QUESTION, /* ? */
39
    T_BANG,     /* ! */
40
    T_BANG_EQ,  /* != */
41
    T_EQ,       /* = */
42
    T_EQ_EQ,    /* == */
43
    T_GT,       /* > */
44
    T_GT_EQ,    /* >= */
45
    T_LT,       /* < */
46
    T_LT_EQ,    /* <= */
47
    T_LSHIFT,   /* << */
48
    T_RSHIFT,   /* >> */
49
50
    /* Literals. */
51
    T_IDENT,    /* fnord */
52
    T_AT_IDENT, /* @sizeOf */
53
    T_STRING,   /* "fnord" */
54
    T_CHAR,     /* 'f' */
55
    T_NUMBER,   /* 42 */
56
    T_TRUE,     /* true */
57
    T_FALSE,    /* false */
58
    T_NIL,      /* nil */
59
    T_UNDEF,    /* undefined */
60
61
    /* Keywords. */
62
    T_IF,
63
    T_ELSE,
64
    T_RETURN,
65
    T_BREAK,
66
    T_CONTINUE,
67
    T_THROW,
68
    T_PANIC,
69
    T_WHILE,
70
    T_FOR,
71
    T_LOOP,
72
    T_TRY,
73
    T_CATCH,
74
    T_IN,
75
    T_FN,
76
    T_UNION,
77
    T_RECORD,
78
    T_DEFAULT,
79
    T_PUB,
80
    T_MUT,
81
    T_CONST,
82
    T_STATIC,
83
    T_LET,
84
    T_AND,
85
    T_OR,
86
    T_NOT,
87
    T_MATCH,
88
    T_CASE,
89
    T_USE,
90
    T_SUPER,  /* super */
91
    T_EXTERN, /* extern */
92
    T_MOD,    /* mod */
93
    T_AS,     /* as */
94
    T_ALIGN,  /* align */
95
    T_THROWS, /* throws */
96
97
    /* Type-related tokens. */
98
    T_COLON,       /* : */
99
    T_COLON_COLON, /* :: */
100
    T_ARROW,       /* -> */
101
    T_FAT_ARROW,   /* => */
102
103
    /* Builtin type names. */
104
    T_I8,
105
    T_I16,
106
    T_I32,
107
    T_I64,
108
    T_U8,
109
    T_U16,
110
    T_U32,
111
    T_U64,
112
    T_F32,
113
    T_BOOL,
114
    T_VOID,
115
    T_OPAQUE
116
} tokenclass_t;
117
118
/* Code location. */
119
typedef struct {
120
    const char *src;  /* Pointer to source code location. */
121
    const char *file; /* File path. */
122
    u32         line; /* line number. */
123
    u32         col;  /* Column number. */
124
} location_t;
125
126
/* Token structure. */
127
typedef struct {
128
    tokenclass_t cls;
129
    const char  *start;    /* Start of the token in the source code. */
130
    u32          length;   /* Byte length of token in source code. */
131
    u32          position; /* Byte offset in source. */
132
} token_t;
133
134
/* Scanner state. */
135
typedef struct {
136
    const char *file;   /* File path. */
137
    const char *source; /* Start of source buffer. */
138
    const char *token;  /* Start of current token. */
139
    const char *cursor; /* Current position. */
140
} scanner_t;
141
142
/* Initialize scanner with source text. */
143
void scanner_init(scanner_t *s, const char *file, const char *source);
144
145
/* Get next token from scanner. */
146
token_t scanner_next(scanner_t *s);
147
148
/* Get line and column information for a token. */
149
location_t scanner_get_location(scanner_t *s, u32 position);
150
151
#endif