~sircmpwn/mrsh

mrsh/include/parser.h -rw-r--r-- 2.4 KiB View raw
6770e0deDrew DeVault Add configure script and Makefile 8 months ago
                                                                                
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef PARSER_H
#define PARSER_H

#include <stdio.h>
#include <mrsh/buffer.h>
#include <mrsh/parser.h>

enum symbol_name {
	EOF_TOKEN,
	TOKEN,

	NEWLINE,

	// The following are the operators (see XBD Operator) containing more than
	// one character.

	AND_IF,
	OR_IF,
	DSEMI,

	DLESS,
	DGREAT,
	LESSAND,
	GREATAND,
	LESSGREAT,
	DLESSDASH,

	CLOBBER,
};

struct symbol {
	enum symbol_name name;
	char *str;
};

extern const struct symbol operators[];
extern const size_t operators_len;
extern const size_t operators_max_str_len;

extern const char *keywords[];
extern const size_t keywords_len;

struct mrsh_parser {
	int fd; // can be -1
	struct mrsh_buffer *in_buf; // can be NULL
	bool eof;

	struct mrsh_buffer buf; // internal read buffer
	struct mrsh_position pos;

	struct {
		char *msg;
		struct mrsh_position pos;
	} error;

	bool has_sym;
	enum symbol_name sym;

	struct mrsh_array here_documents;
	bool continuation_line;

	mrsh_parser_alias_func alias;
	void *alias_user_data;
};

typedef struct mrsh_word *(*word_func)(struct mrsh_parser *state, char end);

size_t parser_peek(struct mrsh_parser *state, char *buf, size_t size);
char parser_peek_char(struct mrsh_parser *state);
size_t parser_read(struct mrsh_parser *state, char *buf, size_t size);
char parser_read_char(struct mrsh_parser *state);
bool token(struct mrsh_parser *state, const char *str,
	struct mrsh_range *range);
bool expect_token(struct mrsh_parser *state, const char *str,
	struct mrsh_range *range);
char *read_token(struct mrsh_parser *state, size_t len,
	struct mrsh_range *range);
void read_continuation_line(struct mrsh_parser *state);
void parser_set_error(struct mrsh_parser *state, const char *msg);
void parser_begin(struct mrsh_parser *state);
bool is_operator_start(char c);
enum symbol_name get_symbol(struct mrsh_parser *state);
void consume_symbol(struct mrsh_parser *state);
bool symbol(struct mrsh_parser *state, enum symbol_name sym);
bool eof(struct mrsh_parser *state);
bool newline(struct mrsh_parser *state);
void linebreak(struct mrsh_parser *state);
bool newline_list(struct mrsh_parser *state);

size_t peek_name(struct mrsh_parser *state, bool in_braces);
size_t peek_word(struct mrsh_parser *state, char end);
struct mrsh_word *expect_dollar(struct mrsh_parser *state);
struct mrsh_word *back_quotes(struct mrsh_parser *state);
struct mrsh_word *word(struct mrsh_parser *state, char end);
struct mrsh_word *arithmetic_word(struct mrsh_parser *state, char end);

#endif