mess822x
1.23
mess822x
Loading...
Searching...
No Matches
mess822x-1.23
src
tokenize.c
Go to the documentation of this file.
1
#include "buffer.h"
2
#include "getln.h"
3
#include "
mess822.h
"
4
#include "logmsg.h"
5
#include "exit.h"
6
7
#define WHO "tokenize"
8
9
static
void
nomem
()
10
{
11
logmsg(
WHO
,111,FATAL,
"out of memory"
);
12
}
13
14
void
put
(
char
*buf,
int
len)
15
{
16
char
ch;
17
18
while
(len) {
19
ch = *buf;
20
if
(ch ==
'\n'
) ch = 0;
21
buffer_put(buffer_1,&ch,1);
22
++buf;
23
--len;
24
}
25
}
26
27
stralloc
line
= {0};
28
int
match
;
29
30
stralloc
tokens
= {0};
31
32
int
main
()
33
{
34
int
i;
35
int
j;
36
37
for
(;;) {
38
if
(getln(buffer_0,&
line
,&
match
,
'\n'
) == -1)
39
logmsg(
WHO
,111,FATAL,
"unable to read input: "
);
40
if
(!
line
.len)
break
;
41
if
(
match
) --
line
.len;
42
43
for
(i = 0; i <
line
.len; ++i)
44
if
(
line
.s[i] == 0)
45
line
.s[i] =
'\n'
;
46
if
(!stralloc_0(&
line
))
nomem
();
47
48
if
(!
mess822_token
(&
tokens
,
line
.s))
nomem
();
49
50
for
(j = i = 0; j <
tokens
.len; ++j)
51
if
(!
tokens
.s[j]) {
52
if
(
tokens
.s[i] ==
' '
)
53
buffer_puts(buffer_1,
"space\n"
);
54
else
if
(
tokens
.s[i] ==
'\t'
)
55
buffer_puts(buffer_1,
"tab\n"
);
56
else
if
(
tokens
.s[i] ==
'|'
) {
57
buffer_puts(buffer_1,
"string {"
);
58
put
(
tokens
.s + i + 1,j - i - 1);
59
buffer_puts(buffer_1,
"}\n"
);
60
}
else
if
(
tokens
.s[i] ==
'('
) {
61
buffer_puts(buffer_1,
"comment {"
);
62
put
(
tokens
.s + i + 1,j - i - 1);
63
buffer_puts(buffer_1,
"}\n"
);
64
}
else
{
65
buffer_puts(buffer_1,
"special "
);
66
put
(
tokens
.s + i,j - i);
67
buffer_puts(buffer_1,
"\n"
);
68
}
69
i = j + 1;
70
}
71
72
buffer_puts(buffer_1,
"\n"
);
73
if
(!
match
)
break
;
74
}
75
76
buffer_flush(buffer_1);
77
_exit(0);
78
}
nomem
void nomem()
Definition:
quote.c:8
tokens
stralloc tokens
Definition:
tokenize.c:30
put
void put(char *buf, int len)
Definition:
tokenize.c:14
line
stralloc line
Definition:
tokenize.c:27
match
int match
Definition:
tokenize.c:28
WHO
#define WHO
Definition:
tokenize.c:7
main
int main()
Definition:
tokenize.c:32
mess822.h
mess822_token
int mess822_token(stralloc *, char *)
Definition:
mess822_token.c:5
Generated on Mon Mar 3 2025 20:48:09 for mess822x by
1.9.6