-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml_lexer.h
70 lines (58 loc) · 1.63 KB
/
xml_lexer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef __XML_LEXER_H__
#define __XML_LEXER_H__
#include <stdbool.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
/* position struct */
typedef struct src_pos {
const char *file;
int line;
int column;
}src_pos_t;
typedef enum {
TOKEN_NONE = 0,
TOKEN_EOF, /* EOF */
TOKEN_OPEN_TAG, /* < */
TOKEN_CLOSE_TAG, /* > */
TOKEN_OPENSLASH_TAG, /* </ */
TOKEN_CLOSESLASH_TAG, /* /> */
TOKEN_PI, /* processing instruction: <? ?> */
//TOKEN_OPEN_HEADER, /* <? */
//TOKEN_CLOSE_HEADER, /* ?> */
TOKEN_NAME, /* tag name */
TOKEN_COMMENT, /* <!-- --> */
TOKEN_CDATA, /* <![CDATA[ .... ]]> */
TOKEN_DOCTYPE, /* <!DOCTYPE ... ]> */
TOKEN_ASSIGN, /* = */
TOKEN_STRING, /* "" */
TOKEN_TEXT /* tag text */
}token_type_t;
/* token struct */
typedef struct token {
token_type_t type;
const char *literal;
int len;
src_pos_t pos;
}token_t;
/* lex struct */
typedef struct lexer {
const char *input;
int input_len;
const char *file;
char ch;
int line, column;
int position, next_position;
token_t cur_token;
token_t peek_token;
bool inTag;
}lexer_t;
#ifdef DEBUG
void token_dump(token_t tok);
#endif
bool lexer_init(lexer_t *lex, const char *input, const char *filename);
bool lexer_cur_token_is(lexer_t *lex, token_type_t type);
token_type_t lexer_cur_token(lexer_t *lex);
bool lexer_peek_token_is(lexer_t *lex, token_type_t type);
void lexer_next_token(lexer_t *lex);
bool lexer_expect_peek(lexer_t *lex, token_type_t type);
const char *token_type_to_string(token_type_t type);
#endif