aboutsummaryrefslogtreecommitdiff
path: root/5.19/token.c
blob: 7f4d555d88c55078fea454cfc000fedf8e76bc8f (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
#include <string.h>
#include <ctype.h>
#include "getch.h"
#include "token.h"

enum { NO, YES };

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

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

	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;

	type = gettoken();
	prevtoken = YES;
	return type;
}