-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer2.c
115 lines (107 loc) · 2.15 KB
/
lexer2.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
#include <stdio.h>
#include "parse.h"
int lex_is_special_char(char ch)
{
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == ';'
|| ch == '|' || ch == '>' || ch == '<' || ch == '"' || ch == '\'')
return (1);
return (0);
}
int lex_read_word(t_parse_buffer *buf, t_token *result)
{
int pos;
int ch;
if (lex_escaped(buf, result))
return (1);
pos = 0;
while (1)
{
ch = lex_getc(buf);
if (ch == EOF)
break ;
if (ch == '\\' || lex_is_special_char(ch) || (ch == '$' && pos > 0))
{
lex_ungetc(buf);
break ;
}
result->text[pos++] = ch;
if (pos == result->max_length)
lex_expand_text_buf(result);
}
result->length = pos;
return (1);
}
int lex_read_double_quoted(t_parse_buffer *buf, t_token *result)
{
int pos;
int ch;
if (lex_escaped(buf, result))
return (1);
pos = 0;
while (1)
{
ch = lex_getc(buf);
if (ch == '"' || ch == '\n' || ch == EOF)
buf->lex_stat = LEXSTAT_NORMAL;
if (ch == '\n' || ch == EOF)
result->type = TOKTYPE_PARSE_ERROR;
if (ch == '\\' || ch == '\n' || ch == EOF || (ch == '$' && pos > 0))
lex_ungetc(buf);
if (ch == '\\' || ch == '"' || ch == '\n' || ch == EOF
|| (ch == '$' && pos > 0))
break ;
result->text[pos++] = ch;
if (pos == result->max_length)
lex_expand_text_buf(result);
}
result->length = pos;
return (1);
}
int lex_read_single_quoted(t_parse_buffer *buf, t_token *result)
{
int pos;
int ch;
pos = 0;
while (1)
{
ch = lex_getc(buf);
if (ch == '\'' || ch == '\n' || ch == EOF)
buf->lex_stat = LEXSTAT_NORMAL;
if (ch == '\n' || ch == EOF)
{
result->type = TOKTYPE_PARSE_ERROR;
lex_ungetc(buf);
}
if (ch == '\'' || ch == '\n' || ch == EOF)
break ;
result->text[pos++] = ch;
if (pos == result->max_length)
lex_expand_text_buf(result);
}
result->length = pos;
return (1);
}
int lex_get_spaces(t_parse_buffer *buf, t_token *result, int ch)
{
if (ch == ' ' || ch == '\t')
{
while (1)
{
ch = lex_getc(buf);
if (ch != ' ' && ch != '\t')
{
if (ch != EOF)
lex_ungetc(buf);
result->type = TOKTYPE_SPACE;
break ;
}
}
return (1);
}
else if (ch == '\n')
{
result->type = TOKTYPE_NEWLINE;
return (1);
}
return (0);
}