aboutsummaryrefslogtreecommitdiff
path: root/5.20/token.c
blob: b04acef94889605ff1d24a5ae108449000d2496e (plain) (blame)
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 <string.h>
#include <ctype.h>
#include "getch.h"
#include "token.h"
#include "err.h"

#define ARR_SIZE(X) sizeof(X)/sizeof(X[0])

char tokentype;
char token[MAXTOKEN];
char prevtoken;

char *typqual[] = {
	"const",
	"volatile"
};

char *storspec[] = {
	"auto",
	"extern",
	"register",
	"static"
};

char *typespec[] = {
	"char",
	"int",
	"long",
	"short",
	"signed",
	"unsigned",
	"void"
};


int gettoken(void)
{
	char c, *p = token;

	if (prevtoken == YES) {
		prevtoken = NO;
		return tokentype;
	}

	while(isblank(c = getch()))
		;

	if (c == '/') { /* ignore comments */
		if ((c = getch()) == '/') {
			while ((c = getch()) != '\n')
				;
		} else if (c == '*') {
			while (getch() != '*' || (c = getch()) != '/')
				if (c == '*')
					ungetch('*');
			return gettoken();
		} else {
			ungetch(c);
			c = '/';
		}
	}

	if (c == '(') {
		if ((c = getch()) == ')') {
			strcpy(token, "()");
			return tokentype = PARENS;
		} else {
			ungetch(c);
			return tokentype = '(';
		}
	} else if (c == '[') {
		for (*p++ = '['; (*p++ = getch()) != ']';)
			;
		*p = '\0';
		return tokentype = BRACKETS;
	} else if (isalpha(c)) {
		for (*p++ = c; isalnum(c = getch());)
			*p++ = c;
		*p = '\0';
		ungetch(c);
		return tokentype = NAME;
	} else {
		return tokentype = c;
	}
}

int peaktoken()
{
	char type;

	if (prevtoken == YES)
		err("prevtoken overflow");
	type = gettoken();
	prevtoken = YES;
	return type;
}

int isdtspec(void)
{
	int i, len;

	for (i = 0, len = ARR_SIZE(typqual); i < len; ++i)
		if(!strcmp(token, typqual[i]))
			return 1;

	for (i = 0, len = ARR_SIZE(storspec); i < len; ++i)
		if(!strcmp(token, storspec[i]))
			return 1;

	for (i = 0, len = ARR_SIZE(typespec); i < len; ++i)
		if(!strcmp(token, typespec[i]))
			return 1;

	return 0;
}