This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.h
83 lines (72 loc) · 1.97 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef LEXER_H
#define LEXER_H
typedef enum lex_hint_e {
LEX_NO_HINT,
LEX_HINT_COMPOSING_WORD, // will generate TOKEN_WORD_END when meet a blank
LEX_HINT_CMD_PREFIX, // will detect for io-redir and assign-word
LEX_HINT_CMD_PREFIX_KW, // also detect for keywords
LEX_HINT_EXPECT_IN, // 'in' and only 'in' will be considered as KW
LEX_HINT_CMD_POSTFIX, // will detect for io-redir
} lex_hint_t;
static const bool LNAME_IN_BRACE = true;
static const bool LNAME_NO_BRACE = false;
typedef enum token_type_e {
TOKEN_INVALID,
TOKEN_EOF,
TOKEN_NEWLINE,
TOKEN_PARTIAL_WORD,
TOKEN_WORD_END,
TOKEN_PARTIAL_ASSIGN_WORD,
TOKEN_ASSIGN_WORD_END,
TOKEN_NAME,
TOKEN_IO_NUMBER,
TOKEN_LESS, // <
TOKEN_DLESS, // <<
TOKEN_GREAT, // >
TOKEN_DGREAT, // >>
TOKEN_LESSAND, // <&
TOKEN_GREATAND, // >&
TOKEN_DLESSDASH, // <<-
TOKEN_LESSGREAT, // <>
TOKEN_CLOBBER, // >|
TOKEN_SEMI, // ;
TOKEN_DSEMI, // ;;
TOKEN_LBRACE, // {
TOKEN_RBRACE, // }
TOKEN_DOLLAR, // $
TOKEN_DOLLAR_LBRACE, // ${
TOKEN_DOLLAR_LPAREN, // $(
TOKEN_LPAREN, // (
TOKEN_RPAREN, // )
TOKEN_AMP, // &
TOKEN_DAMP, // &&
TOKEN_BAR, // |
TOKEN_DBAR, // ||
TOKEN_BANG, // !
TOKEN_FOR,
TOKEN_IN,
TOKEN_DO,
TOKEN_DONE,
TOKEN_CASE,
TOKEN_ESAC,
TOKEN_WHILE,
TOKEN_UNTIL,
TOKEN_SELECT,
TOKEN_IF,
TOKEN_ELSE,
TOKEN_ELIF,
TOKEN_FI,
TOKENS_COUNT,
} token_type_t;
typedef struct parser_s parser_t;
typedef struct token_s {
token_type_t type;
char payload[];
} token_t;
const char *token_type_name(token_type_t type);
int peek_char(parser_t *parser);
int get_char(parser_t *parser);
token_t *get_token(parser_t *parser, lex_hint_t hint);
token_t *get_name(parser_t *parser, bool in_brace);
token_t *get_io_number(parser_t *parser);
#endif // LEXER_H