forked from IBM/Project_CodeNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_common.c
223 lines (203 loc) · 6.27 KB
/
token_common.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (c) 2021 International Business Machines Corporation
Prepared by: Geert Janssen <[email protected]>
Code functionality shared by all tokenizers.
This obviously avoids code duplication and associated maintenance problems.
*/
#include "token_common.h"
// Program globals:
char *filename = "stdin"; // current file being parsed
unsigned linenr = 1; // physical line number counted from 1
unsigned column = 0; // byte position in physical line, from 0
unsigned char_count = 0; // total byte count
unsigned utf8_count = 0; // total utf-8 encoded unicode codepoints
int buffer[MAX_BUF]; // use buffer as multi-char lookahead.
unsigned buffered = 0; // number of buffered bytes
unsigned saved_col = 0; // one-place buf for last column on prev line
unsigned illegals = 0; // count number of illegal characters
unsigned unexpect_eof = 0; // encountered unexpected EOF
unsigned num_files = 0; // number of files read
// Program option settings:
int debug = 0; // when 1 debug output to stderr
int verbose = 0; // when 1 info output to stderr
int nowarn = 0; // when 1 warnings are suppressed
Language source = C; // language mode
/* Conversion table from filename extension to language code.
To find language code, consider all entries and check each ext
against filename; matched language is langs[i].lang.
Invariant: langs[X].lang == X for every Language value.
String representation of language code is langs[X].name.
*/
static const struct { const char *ext; Language lang; const char *name; }
langs[] = {
{ ".c", C, "C" },
{ ".cc", CPP, "C++" },
{ ".java", JAVA, "Java" },
{ ".js", JAVASCRIPT, "JavaScript" },
{ ".py", PYTHON, "Python" },
// Alternatives:
{ ".C", CPP, 0 },
{ ".cpp", CPP, 0 }
};
/* Generic binary search lookup in some keyword table.
`word' to be searched must be NUL-terminated C string.
`table' is array of const char * of `size' sorted alphabetically.
Returns word found (i.e., pointer value in table) or 0.
*/
const char *is_keyword(const char *word,
const char *table[], unsigned size)
{
int i = 0, j = size;
while (i < j) {
int k = (i + j) >> 1 /* / 2 */;
int cmp = strcmp(word, table[k]);
if (!cmp)
return table[k];
if (cmp < 0) j = k; else i = k + 1;
}
return 0;
}
/* Must be called right after a file is opened as stdin.
Will attempt to remove any UTF-8 unicode signature (byte-order mark, BOM)
at the beginning of the file.
Unicode: U+FEFF
UTF-8: EF BB BF
First bytes Encoding Must remove?
00 00 FE FF UTF-32 big endian Yes
FF FE 00 00 UTF-32 little endian Yes
FE FF UTF-16 big endian Yes
FF FE UTF-16 little endian Yes
00 00 00 xx UTF-32 big endian No
xx 00 00 00 UTF-32 little endian No
00 xx UTF-16 big endian No
xx 00 UTF-16 little endian No
otherwise UTF-8 No
*/
void remove_BOM(void)
{
int c1 = getchar();
if (c1 == 0xEF) {
int c2 = getchar();
if (c2 == 0xBB) {
int c3 = getchar();
if (c3 == 0xBF) {
return;
}
if (c3 != EOF) buffer[buffered++] = c3;
}
if (c2 != EOF) buffer[buffered++] = c2;
}
if (c1 != EOF) buffer[buffered++] = c1;
}
/* Deal with DOS (\r \n) and classic Mac OS (\r) (physical) line endings.
In case of CR LF skip (but count) the CR and return LF.
In case of CR not followed by LF turns the CR into LF and returns that.
All other chars are returned as is.
Note: never returns a CR (\r).
*/
static int normalize_newline(void)
{
/* No need to recognize Unicode code points here. */
int cc = getchar();
if (cc == '\r') {
// Maybe \r \n (CR NL) combination?
int nc = getchar();
if (nc == '\n') {
char_count++; // counts the carriage return
utf8_count++;
// No use incrementing column.
return nc; // effectively skip the \r
}
// Mind nc not \n.
if (nc != EOF) ungetc(nc, stdin);
// cc == '\r'; consider a newline as well, so turn into \n:
cc = '\n';
}
return cc;
}
/* Detects escaped newlines (line continuations) and signals them with the
special '\r' character (that otherwise is not used).
Keeps track of physical coordinates and absolute location for each character.
*/
int get(void)
{
int cc;
// Get the next character:
if (buffered) { // chars available in lookahead buffer
cc = buffer[--buffered]; // never EOF
// cc maybe '\r'; act like '\n':
if (cc == '\n' || cc == '\r') {
linenr++;
saved_col = column;
column = 0;
return cc;
}
column++;
return cc;
}
// Read a fresh char:
cc = normalize_newline(); // cc != '\r'
if (cc == EOF) return EOF;
char_count++;
if (utf8_start(cc)) utf8_count++;
if (cc == '\n') { // a normalized (physical) end-of-line
linenr++;
saved_col = column;
column = 0;
return cc;
}
// Deal with explicit \ line continuations!
if (cc == '\\') {
// Must look ahead (never maintained across get calls!):
int nc = normalize_newline(); // cc != '\r'
if (nc == '\n') {
char_count++; // counts the newline
utf8_count++;
linenr++; // on next physical line
saved_col = column+1; // +1 for backslash
column = 0;
// Signal that this was an escaped newline:
return '\r';
}
// Mind nc not \n.
if (nc != EOF) ungetc(nc, stdin);
// cc == '\\' a regular backslash
}
column++;
return cc;
}
void unget(int cc)
{
if (cc == EOF) return;
if (buffered < MAX_BUF) {
if (cc == '\n' || cc == '\r') {
linenr--;
// column was 0 right after getting the \n
// hopefully there are no multiple ungets of \n
column = saved_col;
}
else
column--;
buffer[buffered++] = cc;
}
else {
fprintf(stderr, "(F): Lookahead buffer overflow (MAX=%u).\n", MAX_BUF);
exit(2);
}
}
/* Determine programming language from filename extension:
.c | C
.cc, .C, .cpp | C++
.java | Java
Uses global filename (maybe stdin).
*/
Language detect_lang(void)
{
char *p;
if (p = strrchr(filename, '.')) {
int i;
for (i = 0; i < sizeof(langs)/sizeof(langs[0]); i++)
if (!strcmp(p, langs[i].ext))
return langs[i].lang;
}
return C;
}