-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.c
153 lines (142 loc) · 4.08 KB
/
parser.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
#include "parser.h"
static int max_call_lim = 0;
static inline void watch_compile_error(int size)
{
if (max_call_lim > size)
{
printf("\ncompile error!\n");
exit(0);
};
max_call_lim++;
}
// EP
Words* split(char *line)
{
if (line[0] == '"')
{
printf(" * you may have empty line that end with ` \" `\n");
return NULL; // raise error
}
max_call_lim = 0;
static bool nl = false;
static bool wrapped = false;
int w_size;
/* bool may_empty = false; */
if (nl) nl = false;
Words* line_temp = new_words();
Word* word = new_word_p("", 0);
for (int len = 0; len < strlen(line); len++)
{
watch_compile_error(strlen(line));
if (wrapped) goto STILL_WRAPPED;
switch (line[len])
{
case '\\':
nl = true;
/* return; */
case ' ':
case '\t':
break;
case TEXT_WRAP_SYM:
wrapped = true;
len++; // go inside of symbols
STILL_WRAPPED:
w_size = rdw_ignspc(word, &line[len], &wrapped);
if (w_size)
{
COPY_TO_WORDS(line_temp, word);
word = new_word_p("", 0);
}
len += w_size - 1;
if (!wrapped)
len++; // escape wrapped string
break;
default:
if (is_sym(line[len]))
{
w_size = rd_sym(word, &line[len]);
if (w_size)
{
COPY_TO_WORDS(line_temp, word);
word = new_word_p("", 0);
}
len += w_size - 1;
}
else
{
w_size = rdw(word, &line[len]);
int temp_p = w_size + len;
word->l_spc = 0;
while (line[temp_p] == ' ')
{
temp_p++;
word->l_spc++;
}
if (w_size)
{
if (strcmp(word->string, " "))
{
COPY_TO_WORDS(line_temp, word);
}
word = new_word_p("", 0);
}
len += w_size - 1;
}
}
}
if (wrapped)
line_temp->is_eol = true;
return line_temp;
}
bool parse(Words *words, int line_num)
{
static Words* prev_words;
static bool still_wrapped = false;
#ifdef DEBUG
printf("is wrapped : %d\n", words->is_eol);
#endif
if (still_wrapped)
{
// merge prev_words[-1] cur_words[0]
#ifdef DEBUG
printf("merge -> '%s' : '%s' \n", prev_words->c[prev_words->lp-1]->string, words->c[words->lp-1]->string);
#endif
words = connect_words(prev_words, words);
#ifdef DEBUG
printf("merged. '%s'\n", words->c[words->lp - 1]->string);
#endif
still_wrapped = false;
prev_words = NULL;
}
if (words->is_eol)
{
prev_words = words;
still_wrapped = true;
return false;
}
for (int i = 0; i < words->lp; i++)
{
// search and set each words roll
Type temp_T = words->c[i]->type;
if (temp_T == Uninitialized)
temp_T = Unknown;
words->ewp ^= temp_T << (i * 2);
#ifdef DEBUG
printf(" - word : %s, type : %d, d_type : %d, size : %d, r_size : %lu, spc : %d\n", words->c[i]->string, words->c[i]->type, words->c[i]->d_type, words->c[i]->length, strlen(words->c[i]->string), words->c[i]->l_spc);
print_uint32_t("ewp : ", words->ewp);
#endif
}
#ifdef DEBUG
print_uint32_t("ewp : ", words->ewp);
#endif
preprocessing(words); // '"A" as a' to single word
if (check_is_participant(words) ||
check_is_arrow(words))
return true;
#ifdef PYTHON_BINDING
failed_at = line_num;
#else
printf(KRED"**** PARSE FAILED at line %d ****\n"RESET, line_num);
#endif
return false;
}