-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
160 lines (129 loc) · 2.93 KB
/
reader.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "error.h"
#ifndef READLINE
#define READLINE 0
#endif
#if READLINE
#define _FUNCTION_DEF
#include <readline/readline.h>
#include <readline/history.h>
#define IF_READLINE(b) if (input == stdin) { b
#define ELSE_READLINE } else {
#define ENDIF_READLINE }
#else
#define IF_READLINE(b)
#define ELSE_READLINE
#define ENDIF_READLINE
#endif
static char *readed_line = NULL;
#define READING_DONE -2
static int position = 0;
extern int prompt;
static FILE *input = NULL;
void set_input(FILE *inp)
{
position = READING_DONE;
input = inp;
IF_READLINE({
rl_bind_key ('\t', rl_insert);
if (readed_line != NULL) {
free(readed_line); readed_line = NULL; }})
ELSE_READLINE
if (readed_line == NULL)
readed_line = malloc(sizeof(char) * MAX_NAME_LENGTH);
ENDIF_READLINE;
}
char read_char()
{
if (position == EOF) return EOF;
char c;
if (position == READING_DONE) {
IF_READLINE(
if ((readed_line = readline(prompt ? PROMPT : NULL)) == NULL)
return EOF; )
ELSE_READLINE
if (prompt) printf(PROMPT);
if (fgets(readed_line, MAX_NAME_LENGTH, input) == NULL)
return EOF;
ENDIF_READLINE
position = 0;
}
c = readed_line[position++];
if (c == '\0' || c == EOF) {
position = c == EOF ? EOF : READING_DONE;
IF_READLINE(
add_history(readed_line);
free(readed_line);
readed_line = NULL;
if (c == '\0') return '\n'; )
ELSE_READLINE
if (c == '\0') return read_char();
ENDIF_READLINE
}
if (c == '-' && readed_line[position] == '-') {
position = READING_DONE;
return read_char();
}
return c;
}
int read_word(char *chars, int if_remain)
{
char *c = chars;
while (is_whitespace(*c = read_char()));
while (*c != OPEN_TAG && *c != CLOSE_TAG
&& !is_whitespace(*(++c) = read_char())
&& (c - chars) < MAX_NAME_LENGTH);
if (*c == OPEN_TAG || (c - chars) >= MAX_NAME_LENGTH)
ERROR(SYNTAX_ERROR);
if (*c == CLOSE_TAG) {
*c = '\0';
return 0;
}
*c = '\0';
if (if_remain && chars[0] == REMAIN_PARAMS_TAG) {
while ((*c = read_char()) != CLOSE_TAG)
if (!is_whitespace(*c)) ERROR(SYNTAX_ERROR);
return 0;
}
return 1;
}
static char parse_just_char(int quoted)
{
char c = read_char();
if (quoted && c == '\'') {
ERROR(SYNTAX_ERROR);
} else if (c == '\\') {
switch (read_char()) {
case 't': c = '\t'; break;
case 'n': c = '\n'; break;
case '\n': c = '\n'; break;
case '\\': c = '\\'; break;
case '\'': c = '\''; break;
case '"': c = '"'; break;
case '-': c = '-'; break;
default: ERROR(SYNTAX_ERROR);
}
}
if (quoted && read_char() != '\'') ERROR(SYNTAX_ERROR);
return c;
}
inline t_point parse_char()
{
return make_Num(parse_just_char(1));
}
t_point parse_string()
{
Cons ret = {.b = NIL};
Cons *l = &ret;
char c;
c = parse_just_char(0);
while (c != '"' && c != '\0') {
l->b = pnew_Cons(make_Num(c), NIL);
l = next(l);
c = parse_just_char(0);
}
if (c != '"') ERROR(SYNTAX_ERROR);
return ret.b;
}