-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
287 lines (226 loc) · 7.02 KB
/
main.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* main.c
*
* Main C Code
*
* Part III Project at University of Southampton, UK
* Alex Walker - javascript@soton.net
*
* Created 980129
*/
#include "header.h"
#include "y.tab.h"
#include <stdio.h>
/* Prototypes of functions defined in this file */
void insert(SYMB *);
SYMB *lookup(char *);
void *safe_malloc(size_t );
void do_start();
void do_commandline(int, char **);
void do_predefined();
void add_predefined(char *, int p_type);
void show_usage();
/* Some declarations */
FILE *file, *yyin;
SYMB *symboltable;
ERRMSG *errorlist;
int current_line;
int numerrors = 0;
int numwarnings = 0;
char linebuf[MAX_LINE_LENGTH];
char *lineptr;
char *filename;
/* Flags */
int warnings_flag = 1;
int errors_flag = 1;
int undeclared_flag = 0;
int portability_flag = 0;
void main(int argc, char *argv[]) {
/* Initialise some variables */
symboltable = NULL;
current_line = 1;
lineptr = linebuf;
/* Process any command line arguments */
do_commandline(argc, argv);
/* Display the welcome screen */
do_start();
/* Open any requested file */
if (filename != NULL) {
file = fopen(filename, "r");
if (!file) {
fprintf(stderr,"jsparse: Could not open file requested file \"%s\"\n", filename);
exit(1);
}
/* yyin is the file handle for yacc */
yyin = file;
}
/* Build in predefined symbols */
do_predefined();
/* Start parsing */
do {
yyparse();
}
while (!feof(yyin));
current_line--;
/* Report any unreported errors if we received at least one line of code */
if (current_line > 0) {
report_errors();
}
/* Display summary statistics */
printf("\n%d line", current_line);
if (current_line != 1) printf("s");
printf(" processed. %d Error", numerrors);
if (numerrors != 1) printf("s");
printf(", ");
/* if (warnings_flag || !(warnings_flag || errors_flag)) {*/
if (warnings_flag) {
printf("%d Warning", numwarnings);
if (numwarnings != 1) printf("s");
printf(".\n\n");
} else {
printf("Warnings disabled.\n\n");
}
}
void * safe_malloc(size_t n) {
void *t = (void *) malloc(n);
if (t == NULL) {
printf("\n\nmalloc() failed!\n\n");
exit(0);
}
return t;
}
SYMB *get_symb() {
return (SYMB *)safe_malloc(sizeof(SYMB));
}
void insert(SYMB *s) {
/* Inserts an entry into the symbol table */
if (symboltable != NULL) {
s->link = symboltable;
}
symboltable = s;
}
SYMB *lookup(char *s) {
/* Looks for an entry of name s in the symbol table.
If it exists, it returns a pointer to it */
SYMB *temp = symboltable;
while (temp != NULL) {
if (strcmp(s,temp->name) == 0) return temp;
temp = temp->link;
}
return NULL;
}
void do_predefined() {
/* Adds navigator predefined objects functions and variables into the symbol table */
/* Navigator (non-ECMAScript) entries */
add_predefined("navigator", T_OBJECT);
add_predefined("document", T_OBJECT);
add_predefined("self", T_OBJECT);
add_predefined("parent", T_OBJECT);
add_predefined("window", T_OBJECT);
add_predefined("alert", T_FUNCTION);
add_predefined("prompt", T_FUNCTION);
add_predefined("confirm", T_FUNCTION);
add_predefined("write", T_FUNCTION);
add_predefined("writeln", T_FUNCTION);
add_predefined("setTimeout", T_FUNCTION);
add_predefined("clearTimeout", T_FUNCTION);
/* ECMAScript functions and constructors */
add_predefined("eval", T_FUNCTION);
add_predefined("parseInt", T_FUNCTION);
add_predefined("parseFloat", T_FUNCTION);
add_predefined("escape", T_FUNCTION);
add_predefined("unescape", T_FUNCTION);
add_predefined("isNaN", T_FUNCTION);
add_predefined("isFinite", T_FUNCTION);
add_predefined("Object", T_FUNCTION);
add_predefined("Function", T_FUNCTION);
add_predefined("Array", T_FUNCTION);
add_predefined("String", T_FUNCTION);
add_predefined("Boolean", T_FUNCTION);
add_predefined("Number", T_FUNCTION);
add_predefined("Date", T_FUNCTION);
/* ECMAScript objects */
add_predefined("Math", T_OBJECT);
}
void add_predefined(char *name, int p_type) {
/* Declare temporary variables */
SYMB *t;
char *s;
/* Copy the name of the variable */
s = (char *)safe_malloc(strlen(name)+1);
strcpy(s, name);
s[strlen(name)] = 0;
/* Make a new entry, and initialise it */
t = get_symb();
t->type = p_type;
t->name = s;
t->predefined = YES;
/* Insert it into the symbol table */
insert(t);
}
void do_commandline(int argc, char *argv[]) {
/* Processes command line arguments */
int currentarg = 1;
filename = NULL;
while (currentarg < argc) {
if (argv[currentarg][0] != '-') {
if (filename == NULL) {
filename = (char *)malloc(strlen(argv[currentarg])+1);
strcpy(filename, argv[currentarg]);
}
else {
printf("Error: Cannot specify more than one file at a time\n");
exit(1);
}
}
else {
if ((strcmp(argv[currentarg],"-nowarn")== 0) || (strcmp(argv[currentarg],"-n")==0)) {
warnings_flag = 0;
} else if ((strcmp(argv[currentarg],"-statsonly")==0) || (strcmp(argv[currentarg],"-s")==0)) {
errors_flag = 0;
} else if (strcmp(argv[currentarg],"--help")==0) {
show_usage();
exit(0);
} else if ((strcmp(argv[currentarg], "-undeclared") == 0) || (strcmp(argv[currentarg],"-u") == 0)) {
undeclared_flag = 1;
} else if ((strcmp(argv[currentarg], "-portability") == 0) || (strcmp(argv[currentarg],"-p") == 0)) {
portability_flag = 1;
} else {
printf("Unknown option: %s\n\n", argv[currentarg]);
show_usage();
exit(1);
}
}
currentarg++;
}
}
void do_start() {
/* Displays the welcome screen */
printf("\nLast Compiled: %s\n\n", LAST_COMPILED_DATE);
printf("********************************************************\n");
printf("* *\n");
printf("* JavaScript Syntax Checker *\n");
printf("* *\n");
printf("* Part III Computer Science Project *\n");
printf("* Department of Electronics & Computer Science *\n");
printf("* *\n");
printf("* By Alex Walker *\n");
printf("* *\n");
printf("* Email: javascript@soton.net *\n");
printf("* WWW: http://www.soton.net/jssyntaxchecker *\n");
printf("* *\n");
printf("********************************************************\n\n");
if (filename != NULL) {
printf("Processing file \"%s\"\n\n", filename);
}
}
void show_usage() {
/* Shows usage information - called by do_commandline() */
printf("JavaScript (ECMAScript) Syntax Checker\n\n");
printf("Usage: jsparse [options] [filename] [options]\n\n");
printf(" -n, -nowarn\t\tDisable warning messages\n");
printf(" -s, -statsonly\tOnly show file statistics\n");
printf(" -u, -undefined\tShow warnings of possible undefined variables\n");
printf(" -p, -portability\tShow portability warnings of different browsers\n\n");
printf("If no filename is specified, input is taken from standard input.\n\n");
printf("Report bugs to javascript@soton.net\n");
}