-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoken.c
120 lines (98 loc) · 2.73 KB
/
token.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
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: token.c,v 2.11 2012-09-30 11:55:19 Gebruiker Exp $
*/
/*
Token interface, implementation part.
*/
#include <stdio.h>
#include "token.h"
static int
Token_in_range(const Token tk, int low, int high) {
int tki = Token2int(tk);
if (tki < low) return 0;
if (tki > high) return 0;
return 1;
}
static int
check_and_print(
FILE *ofile, const char *name, char ch, char low, char high, char offset
) {
int ch1 = ch + offset;
if (low <= ch1 && ch1 <= high) {
fprintf(ofile, "%s(%c)", name,ch1);
return 1;
}
return 0;
}
#define is_simple_token(tk) (Token_in_range(tk, 0x0001, 0x00FF))
#define is_CTRL_token(tk) (Token_in_range(tk, 0x0101, 0x011E))
#define is_NORM_token(tk) (Token_in_range(tk, 0x0121, 0x017E))
#define is_MTCT_token(tk) (Token_in_range(tk, 0x0181, 0x019E))
#define is_META_token(tk) (Token_in_range(tk, 0x01A1, 0x01FE))
#define is_hashed_token(tk) (Token_in_range(tk, 0x0200, 0xFFFE))
void
fprint_token(FILE *ofile, const Token tk) {
/* Prints a regular token in two characters:
normal char meta (bit 9 set)
^A cntl $A meta-cntl
A printable #A meta
and hashed tokens in hexadecimal.
*/
int tki = Token2int(tk);
int ch = tki & 0x7F;
int bit8 = tki & 0x80;
if (Token_EQ(tk, No_Token)) {fprintf(ofile, "--"); return;}
if (Token_EQ(tk, IDF)) {fprintf(ofile, "IDF"); return;}
if (Token_EQ(tk, End_Of_Line)) {fprintf(ofile, "EOL"); return;}
if (is_simple_token(tk)) {
if ('!' <= ch && ch <= '~') {
fprintf(ofile, "%s%c", (bit8 ? "8" : ""), ch);
return;
}
if (0 < ch && ch <= ' ') {
fprintf(ofile, "%s%c", (bit8 ? "$" : "^"), ch + '@');
return;
}
if (ch == 0x7F) {
fprintf(ofile, "%s%c", (bit8 ? "$" : "^"), '?');
return;
}
}
if (is_CTRL_token(tk)) {
if (check_and_print(ofile, "CTRL", (char) ch, 'A', '~', '@')) return;
}
if (is_NORM_token(tk)) {
if (check_and_print(ofile, "NORM", (char) ch, '!', '~', '\0')) return;
}
if (is_MTCT_token(tk)) {
if (check_and_print(ofile, "MTCT", (char) ch, 'A', '~', '@')) return;
}
if (is_META_token(tk)) {
if (check_and_print(ofile, "META", (char) ch, '!', '~', '\0')) return;
}
if (is_hashed_token(tk)) {
fprintf(ofile, "0x%04x", tki);
return;
}
/* gap token! */
fprintf(ofile, "!0x%04x!", tki);
}
#ifdef XXXX
int ch = tki & 0177;
int meta = tki & 0200;
if (' ' <= ch && ch <= '~') {
fprintf(ofile, "%c%c", (meta ? '#' : ' '), ch);
}
else {
fprintf(ofile, "%c%c",
(meta ? '$' : '^'),
(ch == 0177 ? '?' : ch + '@')
);
}
#endif
int
Token_EQ(const Token t1, const Token t2) {
/* to make sure Token_EQ is indeed called with two Token parameters */
return Token2int(t1) == Token2int(t2);
}