1 %{ /* make it look better in emacs: -*- mode: Makefile; -*- */
2 /* We need the Makefile mode in emacs to type \t (tab) characters */
3 
4 /*
5   Copyright (C) 1999, 2000 Florian Schintke
6   Copyright (C) 1999       Martin Kammerhofer for the CGI feature
7   Copyright (C) 2000       Rob Ewan           for the indexing feature
8 
9   This is free software; you can redistribute it and/or modify it under
10   the terms of the GNU General Public License as published by the Free
11   Software Foundation; either version 2, or (at your option) any later
12   version.
13 
14   This is distributed in the hope that it will be useful, but WITHOUT
15   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17   for more details.
18 
19   You should have received a copy of the GNU General Public License with
20   the java2html source package as the
21   file COPYING. If not, write to the Free Software Foundation, Inc.,
22   59 Temple Place - Suite 330, Boston, MA
23   02111-1307, USA.
24 */
25 
26 /*
27  * We build a lexical analyzer that converts a Java or C++ source
28  * file to a beautifully highlighted HTML file now.
29  */
30 
31 #include "colors.h"
32 #include "mymain.h"
33 
34 unsigned int oldstate = 0;
35 
36 %}
37 %s COMMENT
38 %s ONELINECOMMENT
39 %s STRING
40  /* We define handling for #define etc to support C++ with this
41     program too */
42 %s DEFINELINE
43 %s PREPROLINE
44 
45 id [a-zA-Z_][a-zA-Z01-9_]*
46 wsnl [\t \n]*
47 ws   [\t ]*
48 accessmode ("public"|"private"|"protected")
49  /* when to break parsing comments, strings, etc to handle special
50     characters: */
51 commentnospecial         [^&<>�-��-��-��-�\\\n*/]
52 onelinecommentnospecial  [^&<>�-��-��-��-�\n]
53 stringnospecial          [^&<>�-��-��-��-�\\\n"]
54 definelinenospecial      [^&<>�-��-��-��-�\\\n"*/]
55 preprolinenospecial      [^&<>�-��-��-��-�\n"*/]
56 
57 %%
58 	/* This characters have to be recoded everywhere
59 	 * because the WWW-browsers interpret them in their
60 	 * own (here not wanted) way.
61 	 */
62 "&"	{ MyStringOutput (yyout, "&amp;"); }
63 "<"	{ MyStringOutput (yyout, "&lt;"); }
64 ">"	{ MyStringOutput (yyout, "&gt;"); }
65 	/* All character as latin1 greater than dec192 or hexBF */
66 ""	{ MyStringOutput (yyout, "&Agrave;"); }
67 ""	{ MyStringOutput (yyout, "&Aacute;"); }
68 ""	{ MyStringOutput (yyout, "&Acirc;"); }
69 ""	{ MyStringOutput (yyout, "&Atilde;"); }
70 ""	{ MyStringOutput (yyout, "&Auml;"); }
71 ""	{ MyStringOutput (yyout, "&Aring;"); }
72 ""	{ MyStringOutput (yyout, "&AElig;"); }
73 ""	{ MyStringOutput (yyout, "&Ccedil;"); }
74 ""	{ MyStringOutput (yyout, "&Egrave;"); }
75 ""	{ MyStringOutput (yyout, "&Eacute;"); }
76 ""	{ MyStringOutput (yyout, "&Ecirc;"); }
77 ""	{ MyStringOutput (yyout, "&Euml;"); }
78 ""	{ MyStringOutput (yyout, "&Igrave;"); }
79 ""	{ MyStringOutput (yyout, "&Iacute;"); }
80 ""	{ MyStringOutput (yyout, "&Icirc;"); }
81 ""	{ MyStringOutput (yyout, "&Iuml;"); }
82 	/* ""	{ MyStringOutput (yyout, "", yytext); } */
83 ""	{ MyStringOutput (yyout, "&Ntilde;"); }
84 ""	{ MyStringOutput (yyout, "&Ograve;"); }
85 ""	{ MyStringOutput (yyout, "&Oacute;"); }
86 ""	{ MyStringOutput (yyout, "&Ocirc;"); }
87 ""	{ MyStringOutput (yyout, "&Otilde;"); }
88 ""	{ MyStringOutput (yyout, "&Ouml;"); }
89 	/* ""	{ MyStringOutput (yyout, "", yytext); } */
90 ""	{ MyStringOutput (yyout, "&Oslash;"); }
91 ""	{ MyStringOutput (yyout, "&Ugrave;"); }
92 ""	{ MyStringOutput (yyout, "&Uacute;"); }
93 ""	{ MyStringOutput (yyout, "&Ucirc;"); }
94 ""	{ MyStringOutput (yyout, "&Uuml;"); }
95 ""	{ MyStringOutput (yyout, "&Yacute;"); }
96 ""	{ MyStringOutput (yyout, "&THORN;"); }
97 ""	{ MyStringOutput (yyout, "&szlig;"); }
98 ""	{ MyStringOutput (yyout, "&agrave;"); }
99 ""	{ MyStringOutput (yyout, "&aacute;"); }
100 ""	{ MyStringOutput (yyout, "&acirc;"); }
101 ""	{ MyStringOutput (yyout, "&atilde;"); }
102 ""	{ MyStringOutput (yyout, "&auml;"); }
103 ""	{ MyStringOutput (yyout, "&aring;"); }
104 ""	{ MyStringOutput (yyout, "&aelig;"); }
105 ""	{ MyStringOutput (yyout, "&ccedil;"); }
106 ""	{ MyStringOutput (yyout, "&egrave;"); }
107 ""	{ MyStringOutput (yyout, "&eacute;"); }
108 ""	{ MyStringOutput (yyout, "&ecirc;"); }
109 ""	{ MyStringOutput (yyout, "&euml;"); }
110 ""	{ MyStringOutput (yyout, "&igrave;"); }
111 ""	{ MyStringOutput (yyout, "&iacute;"); }
112 ""	{ MyStringOutput (yyout, "&icirc;"); }
113 ""	{ MyStringOutput (yyout, "&iuml;"); }
114 ""	{ MyStringOutput (yyout, "&eth;"); }
115 ""	{ MyStringOutput (yyout, "&ntilde;"); }
116 ""	{ MyStringOutput (yyout, "&ograve;"); }
117 ""	{ MyStringOutput (yyout, "&oacute;"); }
118 ""	{ MyStringOutput (yyout, "&ocirc;"); }
119 ""	{ MyStringOutput (yyout, "&otilde;"); }
120 ""	{ MyStringOutput (yyout, "&ouml;"); }
121 	/* ""	{ MyStringOutput (yyout, "", yytext); } */
122 ""	{ MyStringOutput (yyout, "&oslash;"); }
123 ""	{ MyStringOutput (yyout, "&ugrave;"); }
124 ""	{ MyStringOutput (yyout, "&uacute;"); }
125 ""	{ MyStringOutput (yyout, "&ucirc;"); }
126 ""	{ MyStringOutput (yyout, "&uuml;"); }
127 ""	{ MyStringOutput (yyout, "&yacute;"); }
128 ""	{ MyStringOutput (yyout, "&thorn;"); }
129 ""	{ MyStringOutput (yyout, "&yuml;"); }
130 
131 \\\"		{  MyStringOutput(yyout, yytext); }
132 <STRING>\\\\	{ MyStringOutput(yyout, yytext); }
133 <STRING>\"	{
134 		  MyStringOutput(yyout, yytext);
135 		  switch (oldstate)
136 		  {
137                   case 0:
138 		    /* Close the string font */
139 		    BEGIN 0;
140 		    ChangeFontTo(yyout, NULL, NORMAL);
141 		    break;
142 		  case DEFINELINE:
143 		    /* close a string in a defineline */
144 		    BEGIN DEFINELINE;
145 		    oldstate = 0;
146 		    ChangeFontTo(yyout, definelinecolor, NO_CHANGE);
147 		    break;
148 		  case PREPROLINE:
149 		    /* close a string in a preprocessor line */
150 		    BEGIN PREPROLINE;
151 		    oldstate = 0;
152 		    ChangeFontTo(yyout, preprolinecolor, NO_CHANGE);
153 		    break;
154 		  default:
155 		    fprintf (stderr, "internal error: bad oldstate\n");
156 		  }
157 		}
158 <STRING>\'	{
159 		  MyStringOutput(yyout, yytext);
160 		}
161 <STRING>\\\n	{
162 		  MyStringOutput(yyout, yytext);
163 		  if (oldstate == PREPROLINE) oldstate = 0;
164 		}
165 <STRING>\n	{
166 		  switch (oldstate)
167 		  {
168 		  case 0:
169 		    BEGIN STRING;
170 		    MyStringOutput(yyout, yytext);
171 		    break;
172 		  case DEFINELINE:
173 		  case PREPROLINE:
174 		    /* This was a string in a define or a */
175 		    /* preprocessor line */
176 		    /* With this newline we close this line */
177 		    /* and continue scanning the string until */
178 		    /* it is closed */
179 		    BEGIN STRING;
180 		    oldstate = 0;
181 		    ChangeFontTo(yyout, NULL, NORMAL);
182 		    MyStringOutput(yyout, yytext);
183 		    /* Now it is only a string */
184 		    ChangeFontTo(yyout, stringcolor, NORMAL);
185 		    break;
186 		  default:
187 		    fprintf (stderr, "internal error: bad oldstate\n");
188 		  }
189 		}
190 <STRING>{stringnospecial}+	{ MyStringOutput(yyout, yytext); }
191 
192 <ONELINECOMMENT>\n	{
193 		  ChangeFontTo(yyout, NULL, NORMAL);
194 		  MyStringOutput(yyout, yytext);
195 		  BEGIN 0;
196 		}
197 <ONELINECOMMENT>{onelinecommentnospecial}+	{
198 		  MyStringOutput(yyout, yytext);
199 		}
200 <COMMENT>"/*"	{ MyStringOutput(yyout,yytext); }
201 <COMMENT>\\\n	{
202 		  MyStringOutput(yyout, yytext);
203 		  if (oldstate == PREPROLINE) oldstate = 0;
204 		}
205 <COMMENT>\n	{
206 		  switch (oldstate)
207 		  {
208 		  case 0:
209 		    BEGIN COMMENT;
210 		    MyStringOutput(yyout, yytext);
211 		    break;
212 		  case DEFINELINE:
213 		  case PREPROLINE:
214 		    /* this was a comment in a define or preprocessor */
215 		    /* line. So we close this line and start a normal */
216 		    /* comment scanning */
217 		    BEGIN COMMENT;
218 		    oldstate = 0;
219 		    ChangeFontTo(yyout, NULL, NORMAL);
220 		    MyStringOutput(yyout, yytext);
221 		    ChangeFontTo(yyout, commentcolor, NORMAL);
222 		    break;
223 		  default:
224 		    fprintf (stderr, "internal error: bad oldstate\n");
225 		  }
226 		}
227 <COMMENT>{commentnospecial}+	{ MyStringOutput(yyout, yytext); }
228 <COMMENT>{commentnospecial}/[^/]	{ MyStringOutput(yyout, yytext); }
229 <COMMENT>"*/"	{
230 		  MyStringOutput(yyout, yytext);
231 		  switch (oldstate)
232 		  {
233                   case 0:
234 		    BEGIN 0;
235 		    ChangeFontTo(yyout, NULL, NORMAL);
236 		    break;
237 		  case DEFINELINE:
238 		    BEGIN DEFINELINE;
239 		    oldstate = 0;
240 		    ChangeFontTo(yyout, definelinecolor, NO_CHANGE);
241 		    break;
242 		  case PREPROLINE:
243 		    BEGIN PREPROLINE;
244 		    oldstate = 0;
245 		    ChangeFontTo(yyout, preprolinecolor, NO_CHANGE);
246 		    break;
247 		  default:
248 		    fprintf (stderr, "internal error: bad oldstate\n");
249 		  }
250 		}
251 <COMMENT>{commentnospecial}\/	{ MyStringOutput(yyout, yytext); }
252 <COMMENT>"//"	{ MyStringOutput(yyout,yytext); }
253 
254 <DEFINELINE>\"		{
255 		  /* Start a string in a defineline */
256 		  BEGIN STRING;
257 		  oldstate = DEFINELINE;
258 		  ChangeFontTo(yyout, stringcolor, NO_CHANGE);
259 		  MyStringOutput(yyout, yytext);
260 		}
261 <DEFINELINE>"/*"	{
262 		  /* Start a comment in a defineline */
263 		  BEGIN COMMENT;
264 		  oldstate = DEFINELINE;
265 		  ChangeFontTo(yyout, commentcolor, NO_CHANGE);
266 		  MyStringOutput(yyout, yytext);
267 		}
268 <DEFINELINE>\\\n	{ MyStringOutput(yyout, yytext); }
269 <DEFINELINE>\n	{
270 		  /* close a defineline */
271 		  BEGIN 0;
272 		  oldstate = 0;
273 		  ChangeFontTo(yyout, NULL, NORMAL);
274 		  MyStringOutput(yyout, yytext);
275 		}
276 <DEFINELINE>{definelinenospecial}+	{ MyStringOutput(yyout, yytext); }
277 ^{ws}("#"|"??=")("define") |
278 ^{ws}("#"|"??=")("undef")	{
279 		  /* Start a define line */
280 		  BEGIN DEFINELINE;
281 		  ChangeFontTo(yyout, definelinecolor, BOLD);
282 		  MyStringOutput(yyout, yytext);
283 		}
284 <PREPROLINE>\"	{
285 		  /* Start a string in a preproline */
286 		  BEGIN STRING;
287 		  oldstate = PREPROLINE;
288 		  ChangeFontTo(yyout, stringcolor, NO_CHANGE);
289 		  MyStringOutput(yyout, yytext);
290 		}
291 <PREPROLINE>"/*"	{
292 		  /* Start a comment in a preproline */
293 		  BEGIN COMMENT;
294 		  oldstate = PREPROLINE;
295 		  ChangeFontTo(yyout, commentcolor, NO_CHANGE);
296 		  MyStringOutput(yyout, yytext);
297 		}
298 <PREPROLINE>\n	{
299 		  /* Close a preproline */
300 		  BEGIN 0;
301 		  oldstate = 0;
302 		  ChangeFontTo(yyout, NULL, NORMAL);
303 		  MyStringOutput(yyout, yytext);
304 		}
305 <PREPROLINE>{preprolinenospecial}+	{ MyStringOutput(yyout, yytext); }
306 ^[\t ]*("#"|"??=")("") |
307 ^[\t ]*("#"|"??=")("elif") |
308 ^[\t ]*("#"|"??=")("else") |
309 ^[\t ]*("#"|"??=")("endif") |
310 ^[\t ]*("#"|"??=")("error") |
311 ^[\t ]*("#"|"??=")("if ! defined") |
312 ^[\t ]*("#"|"??=")("if defined") |
313 ^[\t ]*("#"|"??=")("if") |
314 ^[\t ]*("#"|"??=")("ifdef") |
315 ^[\t ]*("#"|"??=")("ifndef") |
316 ^[\t ]*("#"|"??=")("include") |
317 ^[\t ]*("#"|"??=")("line") |
318 ^[\t ]*("#"|"??=")("pragma")	{
319 		  /* Start a preproline */
320 		  BEGIN PREPROLINE;
321 		  ChangeFontTo(yyout, preprolinecolor, NORMAL);
322 		  MyStringOutput(yyout, yytext);
323 		  /* highlight preprozessor statements */
324 		}
325 ^"import"{ws}.*;	{
326 			  ChangeFontTo(yyout, definelinecolor, NORMAL);
327 			  MyStringOutput(yyout, yytext);
328 			  ChangeFontTo(yyout, NULL, NORMAL);
329 	}
330 
331 \"	{
332 	  /* Start a string */
333 	  BEGIN STRING;
334 	  oldstate = 0;
335 	  ChangeFontTo(yyout, stringcolor, NORMAL);
336 	  MyStringOutput(yyout, yytext);
337 	}
338 \'([^\n']+|"\\\'")\'	{
339 	  MyStringOutput(yyout, yytext);
340 	}
341 "/*"	{
342 	  /* Start a comment */
343 	  BEGIN COMMENT;
344 	  oldstate = 0;
345 	  ChangeFontTo(yyout, commentcolor, NORMAL);
346 	  MyStringOutput(yyout, yytext);
347 	}
348 "//"	{
349 	  BEGIN ONELINECOMMENT;
350 	  ChangeFontTo(yyout, commentcolor, NORMAL);
351 	  MyStringOutput(yyout, yytext);
352 	}
353 [{}]	{ MyStringOutput(yyout, yytext); }
354 
355 
356 ^{ws}{accessmode}{ws}({id}{wsnl}|"*")*"("({id}|{wsnl}|"\.\.\."|[][*(),])*")"	{
357 		  /* Write a function definition */
358 		  ChangeFontTo(yyout, keywordcolor, BOLD);
359 		  /* The labeling isn't stable for C++ */
360 		  /* AddLabelForFunction(yyout, yytext); */
361 		  MyStringOutput(yyout, yytext);
362 		  ChangeFontTo(yyout, NULL, NORMAL);
363 		}
364 
365 ^({accessmode}{ws}|{ws})"class"{ws}{id}({id}{ws})*	{
366 		  ChangeFontTo(yyout, keywordcolor, BOLD);
367 		  AddLabelForClass(yyout, yytext);
368 		  MyStringOutput(yyout, yytext);
369 		  EndLabelTag(yyout, yytext);
370 		  ChangeFontTo(yyout, NULL, NORMAL);
371 		}
372 
373 ^"typedef"({id}|{ws}|[*{}])*";"	{
374 		  ChangeFontTo(yyout, keywordcolor, NORMAL);
375 		  MyStringOutput(yyout, yytext);
376 		  ChangeFontTo(yyout, NULL, NORMAL);
377 		}
378 "struct "{id}	{
379 		  ChangeFontTo(yyout, keywordcolor, NORMAL);
380 		  MyStringOutput(yyout, yytext);
381 		  ChangeFontTo(yyout, NULL, NORMAL);
382 		}
383 ^{ws}{id}":"    {
384 		  ChangeFontTo(yyout, labelcolor, BOLD);
385 		  MyStringOutput(yyout, yytext);
386 		  ChangeFontTo(yyout, NULL, NORMAL);
387 		}
388 
389 (char|double|float|int|long|short|signed|void|unsigned)/[^a-zA-Z_0123456789]	{
390 		  MyStringOutput(yyout, yytext);
391 		  /* highlight basic types */
392 		}
393 
394 (char|double|float|int|long|short|signed|void|unsigned)\*/[^a-zA-Z_0123456789]	{
395 		  MyStringOutput(yyout, yytext);
396 		  /* highlight basic pointer types */
397 		}
398 (char|double|float|int|long|short|signed|void|unsigned)" *"	{
399 		  MyStringOutput(yyout, yytext);
400 		  /* highlight basic pointer types in the common style*/
401 		}
402 
403 ("auto"|"const"|"extern"|"register"|"static"|"volatile")/[^a-zA-Z_0123456789] 	{
404 		  MyStringOutput(yyout, yytext);
405 		  /* highlight attributes for types */
406 		}
407 
408 ("break"|"case"|"continue"|"default"|"do"|"else"|"enum"|"for"|"goto"|"if"|"return"|"sizeof"|"struct"|"switch"|"typedef"|"union"|"while"|"try"|"catch"|"throw"|"throws")/[^a-zA-Z_0123456789] 	{
409 		  ChangeFontTo(yyout, keywordcolor, NORMAL);
410 		  MyStringOutput(yyout, yytext);
411 		  ChangeFontTo(yyout, NULL, NORMAL);
412 		  /* highlight the rest of the reserved words */
413 		}
414 
415 ("abort"|"abs"|"acos"|"asctime"|"asin"|"atan"|"atan2"|"atexit"|"atof"|"atoi"|"atol"|"bsearch"|"calloc"|"ceil"|"clearerr"|"clock"|"cos"|"cosh"|"ctime"|"difftime"|"div"|"exit"|"exp"|"fabs"|"fclose"|"feof"|"ferror"|"fflush"|"fgetc"|"fgetpos"|"fgets"|"floor"|"fmod"|"fopen"|"fprintf"|"fputc"|"fputs"|"fread"|"free"|"freopen"|"frexp"|"fscanf"|"fseek"|"fsetpos"|"ftell"|"fwrite"|"getc"|"getchar"|"getenv"|"gets"|"gmtime"|"isalnum"|"isalpha"|"iscntrl"|"isdigit"|"isgraph"|"islower"|"isprint"|"ispunct"|"isspace"|"isupper"|"isxdigit"|"labs"|"ldexp"|"ldiv"|"localtime"|"log"|"log10"|"longjmp"|"malloc"|"memchr"|"memcmp"|"memcpy"|"memmove"|"memset"|"mktime"|"modf"|"perror"|"pow"|"printf"|"putc"|"putchar"|"puts"|"qsort"|"raise"|"rand"|"realloc"|"remove"|"rename"|"rewind"|"scanf"|"setbuf"|"setvbuf"|"setjmp"|"signal"|"sin"|"sinh"|"sprintf"|"sqrt"|"srand"|"sscanf"|"strcat"|"strchr"|"strcmp"|"strcpy"|"strcspn"|"strerror"|"strftime"|"strlen"|"strncat"|"strncmp"|"strncpy"|"strpbrk"|"strrchr"|"strspn"|"strstr"|"strtod"|"strtok"|"strtol"|"strtul"|"system"|"tan"|"tanh"|"time"|"tmpfile"|"tmpnam"|"tolower"|"toupper"|"ungetc"|"vfprintf"|"vprintf"|"vsprintf")/[^a-zA-Z_0123456789]	{
416 		  MyStringOutput(yyout, yytext);
417 		  /* highlight lybrary functions */
418 		}
419 
420 ("close"|"create"|"fstat"|"lseek"|"open"|"read"|"abrk"|"stat"|"unlink"|"write")/[^a-zA-Z_0123456789]	{
421 		  MyStringOutput(yyout, yytext);
422 		  /* highlight basic system calls */
423 		}
424 
425 [\t ]+		{
426 		  MyStringOutput(yyout, yytext);
427 		  /* let whitespaces like they are */
428 		}
429 
430 [a-zA-Z_][a-zA-Z_0123456789]*	{
431 		  MyStringOutput(yyout, yytext);
432 		}
433 
434 .		{
435 		  MyStringOutput(yyout, yytext);
436 		}
437 \n		{
438 		  MyStringOutput(yyout, yytext);
439 		}
440 
441 %%
442 
443 void
444 StartNewYylex(FILE * in, FILE * out)
445 {
446   BEGIN 0;
447   oldstate              = 0;
448   yyin                  = in;
449   yyout                 = out;
450   config.lineNumber     = 1;
451   config.needLabel      = 1;
452   config.currentColor   = NULL;
453   config.currentWeight  = NORMAL;
454   config.suppressOutput = config.indexOnly;
455 
456   yylex();
457 }
458 
459 int
460 main(int argc, char *argv[])
461 {
462   return MyMain(argc, argv);
463   /* unreachable, but suppresses compiler warning. */
464   yyunput(0, NULL);
465 }
466 
467