home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / c++2l.zoo / c++2latex.l < prev    next >
Text File  |  1990-04-30  |  12KB  |  579 lines

  1. ###    This is a flex input file but should be edited in -*-C-*- mode
  2. ###
  3. ###    C++2LaTeX: Produce prettyprinted LaTeX files from  C++ or C sources.
  4. ###    Copyright (C) 1990 Norbert Kiesel
  5. ###
  6. ###    This program is free software; you can redistribute it and/or modify
  7. ###    it under the terms of the GNU General Public License as published by
  8. ###    the Free Software Foundation; either version 1, or (at your option)
  9. ###    any later version.
  10. ###
  11. ###    This program is distributed in the hope that it will be useful,
  12. ###    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ###    GNU General Public License for more details.
  15. ###
  16. ###    You should have received a copy of the GNU General Public License
  17. ###    along with this program; if not, write to the Free Software
  18. ###    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ###
  20. ###    Norbert Kiesel     
  21. ###    RWTH Aachen / Institut f. Informatik III
  22. ###    Ahornstr. 55
  23. ###    D-5100 Aachen
  24. ###    West Germany  
  25. ###
  26. ###    Phone:  +49 241 80-7266
  27. ###    EUNET:  norbert@rwthi3.uucp
  28. ###    USENET: ...!mcvax!unido!rwthi3!norbert
  29. ###    X.400:  norbert@rwthi3.informatik.rwth-aachen.de
  30. ###
  31. ###    Please contact me for any bugs you find in this code or any
  32. ###    improvements! I'd also be very happy to get feedback where and
  33. ###    how frequently this program is used (just drop a little mail :-).
  34.  
  35. %x STRING BCOMMENT INCLUDE
  36. %s CPLUSPLUS
  37.  
  38. %{
  39. #define KEY    printf ("{\\%s %s}", keyword_font, yytext)
  40. #define CPP    printf ("{\\%s \\%s}", cpp_font, yytext)
  41. #define SYM(x)    printf ("$\\%s$", x)
  42. #define OUT(x)    printf ("%s", x)
  43. #define SUB(x)    substitute(x)
  44. #define IND    indent(yytext)
  45.  
  46. #include <stdio.h>
  47.  
  48. #ifdef ANSI_C
  49.   #ifdef C_PLUSPLUS
  50.     #error ANSI_C and C_PLUSPLUS are mutually exclusive
  51.   #else
  52.     int cplusplus_mode = 0;
  53.   #endif
  54. #else /* CPLUSPLUS or default */
  55.   int cplusplus_mode = 1;
  56. #endif
  57.  
  58. int complete_file = 0;
  59. int header = 0;
  60. int tabtotab = 8;
  61. int piped = 0;
  62. char * font_size = "11";
  63. char * indentation = "0.5em";
  64. char * comment_font = "it";
  65. char * keyword_font = "bf";
  66. char * header_font = "sl";
  67. char * cpp_font = "tt";
  68. char * string_font = "tt";
  69. void substitute(const char *);
  70. void indent(const char *);
  71. void newpage(int);
  72. void usage(const char *);
  73. %}
  74.  
  75. %%
  76.  
  77.     if (cplusplus_mode)
  78.       BEGIN (CPLUSPLUS);
  79.  
  80.  
  81. "#"[ \t]*"include"    { CPP; BEGIN (INCLUDE); }
  82. "#"[ \t]*"define"    |
  83. "#"[ \t]*"undef"    |
  84. "#"[ \t]*"if"        |
  85. "#"[ \t]*"ifdef"    |
  86. "#"[ \t]*"ifndef"       |
  87. "#"[ \t]*"elif"        |
  88. "#"[ \t]*"else"        |
  89. "#"[ \t]*"endif"    CPP;
  90.  
  91. <INCLUDE>"<"[^>]*/">"    { OUT ("$<${\\"); OUT (string_font);
  92.               OUT ("{}"); SUB (yytext+1); OUT ("}$>$");
  93.               input(); BEGIN (INITIAL); }
  94. <INCLUDE>\"[^\"]*/\"    { OUT ("\"{\\"); OUT (string_font);
  95.               OUT ("{}"); SUB (yytext+1); OUT ("}\"");
  96.               input(); BEGIN (INITIAL); }
  97. <INCLUDE>[ \t]+        ECHO;
  98. <INCLUDE>[\n]        OUT ("\\mbox{}\\\\\n");
  99. <INCLUDE>.        { yyless (0); BEGIN (INITIAL); }
  100.  
  101. "auto"            |
  102. "double"        |
  103. "int"            |
  104. "struct"        |
  105. "break"            |
  106. "else"            |
  107. "long"            |
  108. "switch"        |
  109. "case"            |
  110. "enum"            |
  111. "register"        |
  112. "typedef"        |
  113. "char"            |
  114. "extern"        |
  115. "return"        |
  116. "union"            |
  117. "const"            |
  118. "float"            |
  119. "short"            |
  120. "unsigned"        |
  121. "continue"        |
  122. "for"            |
  123. "signed"        |
  124. "void"            |
  125. "default"        |
  126. "goto"            |
  127. "sizeof"        |
  128. "volatile"        |
  129. "do"            |
  130. "if"            |
  131. "static"        |
  132. "while"            |
  133. <CPLUSPLUS>"new"    |
  134. <CPLUSPLUS>"delete"    |
  135. <CPLUSPLUS>"this"    |
  136. <CPLUSPLUS>"operator"    |
  137. <CPLUSPLUS>"class"    |
  138. <CPLUSPLUS>"public"    |
  139. <CPLUSPLUS>"protected"    |
  140. <CPLUSPLUS>"private"    |
  141. <CPLUSPLUS>"virtual"    |
  142. <CPLUSPLUS>"friend"    |
  143. <CPLUSPLUS>"inline"    |
  144. <CPLUSPLUS>"overload"    KEY;
  145. "->"            SYM ("rightarrow");
  146. "<<"            SYM ("ll");
  147. ">>"            SYM ("gg");
  148. "<="            SYM ("leq");
  149. ">="            SYM ("geq");
  150. "!="            SYM ("neq");
  151. "||"            SYM ("mid\\mid");
  152. "..."            SYM ("ldots");
  153. "*="            SYM ("ast=");
  154. "<<="            SYM ("ll=");
  155. ">>="            SYM ("gg=");
  156. "^="            SYM ("vee=");
  157. "|="            SYM ("mid=");
  158. "~"            SYM ("sim");
  159. "*"            SYM ("ast");
  160. "^"            SYM ("wedge");
  161. "|"            SYM ("mid");
  162. <CPLUSPLUS>"->*"    SYM ("rightarrow\\ast");
  163. "/"            OUT ("$/$");
  164. "<"            OUT ("$<$");
  165. ">"            OUT ("$>$");
  166. "&&"            OUT ("\\&\\&");
  167. "%="            OUT ("\\%=");
  168. "&="            OUT ("\\&=");
  169. "{"            OUT ("\\{");
  170. "}"            OUT ("\\}");
  171. "&"            OUT ("\\&");
  172. "%"            OUT ("\\%");
  173. "--"            OUT ("-{}-");
  174. <CPLUSPLUS>".*"        OUT (".$\\ast$");
  175. "?"            |
  176. ":"            |
  177. "="            |
  178. ","            |
  179. "."            |
  180. ";"            |
  181. "!"            |
  182. "-"            |
  183. "+"            |
  184. "/="            |
  185. "=="            |
  186. "++"            |
  187. "+="            |
  188. "-="            |
  189. "("            |
  190. ")"            |
  191. "["            |
  192. "]"            |
  193. <CPLUSPLUS>"::"        ECHO;
  194.  
  195. <CPLUSPLUS>[a-zA-Z_$][a-zA-Z_$0-9]*    |
  196. [a-zA-Z_][a-zA-Z_0-9]*            SUB (yytext);
  197.  
  198. "/*"                { BEGIN (BCOMMENT); OUT ("{$/\\ast$\\");
  199.                   OUT (comment_font); OUT ("{}"); }
  200. <BCOMMENT>"*/"            { BEGIN (INITIAL); OUT ("$\\ast/$}"); }
  201. <BCOMMENT>"\n"            OUT ("\\mbox{}\\\\\n");
  202. <BCOMMENT>^[ \t]+        IND;
  203. <BCOMMENT>.            SUB (yytext);
  204.  
  205.  
  206. L?\"                 { BEGIN (STRING); OUT ("{\\");
  207.                   OUT (string_font); OUT ("\""); }
  208. <STRING>\\\\            OUT ("$\\backslash\\backslash$");
  209. <STRING>\\[bfnrtv'"]        { OUT ("$\\backslash$"); SUB (yytext+1); }
  210. <STRING>\"            { BEGIN (INITIAL); OUT ("\"}"); }
  211. <STRING>"\n"            OUT ("\\mbox{}\\\\\n");
  212. <STRING>^[ \t]+            IND;
  213. <STRING>.            SUB (yytext);
  214.  
  215. ([0-9]*\.[0-9]+[fFlL]?)         |
  216. ([0-9]+\.[0-9]*[fFlL]?)         |
  217. ([0-9]*\.?[0-9]+[eE][+-]?[0-9]+) |
  218. ([0-9]+\.?[0-9]*[eE][+-]?[0-9]+) ECHO;
  219.  
  220. [0-9]+[uUlL]?            ECHO;
  221.  
  222. L?'[ -~]'            SUB (yytext);
  223. L?'\\[ntvbrfa\\?'"]'        |
  224. L?'\\[0-7]{1,3}'        |
  225. L?'\\x[0-9a-fA-F]{1,2}'        ECHO;
  226.  
  227. 0[0-7]+[uUlL]?            ECHO;
  228.  
  229. 0x[0-9a-fA-F]+[uUlL]?        ECHO;
  230.  
  231. "\\\n"                OUT ("$\\backslash$\\\\\n");
  232. ^[ \t]+                IND;
  233. [ \t]+                ECHO;
  234. "\f"[\n]?            OUT ("\\newpage\n");
  235. "\n"                OUT ("\\mbox{}\\\\\n");
  236.  
  237. %%
  238.  
  239. void substitute (const char * input)
  240. {
  241.   while (*input)
  242.     {
  243.       switch (*input)
  244.     {
  245.     case '_':
  246.     case '&':
  247.     case '#':
  248.     case '$':
  249.     case '%':
  250.     case '{':
  251.     case '}':
  252.       printf ("\\%c", *input);
  253.       break;
  254.     case '+':
  255.     case '=':
  256.     case '<':
  257.     case '>':
  258.       printf ("$%c$", *input);
  259.       break;
  260.     case '*':
  261.       printf ("$\\ast$");
  262.       break;
  263.     case '|':
  264.       printf ("$\\mid$");
  265.       break;
  266.     case '\\':
  267.       printf ("$\\backslash$");
  268.       break;
  269.     case '^':
  270.       printf ("$\\wedge$");
  271.       break;
  272.     case '~':
  273.       printf ("$\\sim$");
  274.       break;
  275.     default:
  276.       printf ("%c", *input);
  277.       break;
  278.     }
  279.       input++;
  280.     }
  281. }
  282.  
  283. void
  284. indent(const char * blanks)
  285. {
  286.   int i;
  287.   
  288.   i = 0;
  289.   while (*blanks)
  290.     {
  291.       if (*blanks == ' ')
  292.     {
  293.       i++;
  294.     }
  295.       else            /* *blanks == '\t' */
  296.     {
  297.       while (++i % tabtotab) ;
  298.     }
  299.       blanks++;
  300.     }
  301.   printf ("\\hspace*{%d\\indentation}", i);
  302. }
  303.  
  304. #include "getopt.h"
  305. #include <string.h>
  306. #include <fcntl.h>
  307. #include <malloc.h>
  308.  
  309. extern char * version_string;
  310.  
  311. static struct option opts[] =
  312. {
  313.   {"ansi-c", 0, 0, 'a'},
  314.   {"c-plusplus", 0, 0, 'p'},
  315.   {"complete-file", 0, 0, 'c'},
  316.   {"font-size", 1, 0, 's'},
  317.   {"indentation", 1, 0, 'i'},
  318.   {"header", 0, 0, 'h'},
  319.   {"piped", 0, 0, 't'},
  320.   {"output", 1, 0, 'o'},
  321.   {"tabstop", 1, 0, 'T'},
  322.   {"comment-font", 1, 0, 'C'},
  323.   {"string-font", 1, 0, 'S'},
  324.   {"keyword-font", 1, 0, 'K'},
  325.   {"header-font", 1, 0, 'H'},
  326.   {"cpp-font", 1, 0, 'P'},
  327.   {"version", 0, 0, 'V'},
  328.   {0, 0, 0, 0}
  329. };
  330.  
  331.  
  332. main (int argc, char** argv)
  333. {
  334.   int c;
  335.   int index;
  336.   int i;
  337.   int has_filename;
  338.   char * input_name;
  339.   char * output_name;
  340.   char * program_name;
  341.  
  342.   input_name = "Standard Input";
  343.   output_name = 0;
  344.  
  345.   program_name = strrchr (argv[0], '/');
  346.   if (program_name == NULL)    /* no pathname */
  347.     {
  348.       program_name = argv[0];
  349.     }
  350.   else
  351.     {
  352.       program_name++;
  353.     }
  354.   
  355. #ifdef USE_NAME 
  356. #if defined(ANSI_C) || defined(C_PLUSPLUS)
  357. #error USE_NAME, ANSI_C and C_PLUSPLUS are mutually exclusive
  358. #else
  359.   /* simple heuristic: '+' in name means C++ */
  360.   cplusplus_mode = (strchr (program_name, '+') != 0);
  361. #endif
  362. #endif
  363.   
  364.   while ((c = getopt_long (argc, argv,
  365.                "acpo:s:i:thT:C:H:S:K:P:V", opts, &index))
  366.      != EOF)
  367.     {
  368.       if (c == 0)        /* Long option */
  369.     {
  370.       c = opts[index].val;
  371.     }
  372.       switch (c)
  373.     {
  374.     case 'a':
  375.       cplusplus_mode = 0;
  376.       break;
  377.     case 'p':
  378.       cplusplus_mode = 1;
  379.       break;
  380.     case 'c':
  381.       complete_file = 1;
  382.       break;
  383.     case 'o':
  384.       if (piped)
  385.         {
  386.           fprintf (stderr,
  387.                "%s: Can't use {-t,+pipe} and {-o,+output} together\n",
  388.                program_name);
  389.           exit(5);
  390.         }
  391.       output_name = optarg;
  392.       break;
  393.     case 's':
  394.       font_size = optarg;
  395.       break;
  396.     case 'i':
  397.       indentation = optarg;
  398.       break;
  399.     case 'T':
  400.       tabtotab = atoi(optarg);
  401.       break;
  402.         case 't':
  403.           if (output_name != 0)
  404.         {
  405.           fprintf (stderr,
  406.                "%s: Can't use {-t,+pipe} and {-o,+output} together\n",
  407.                program_name);
  408.           exit(5);
  409.         }
  410.       piped = 1;
  411.       break;
  412.     case 'h':
  413.       header = 1;
  414.       complete_file = 1;    /* header implies complete-file */
  415.       break;
  416.     case 'C':
  417.       comment_font = optarg;
  418.       break;
  419.     case 'H':
  420.       header_font = optarg;
  421.       break;
  422.     case 'P':
  423.       cpp_font = optarg;
  424.       break;
  425.     case 'S':
  426.       string_font = optarg;
  427.       break;
  428.     case 'K':
  429.       keyword_font = optarg;
  430.       break;
  431.     case 'V':
  432.       fprintf (stderr, "%s\n", version_string);
  433.       break;
  434.     default:
  435.       usage(program_name);
  436.     }
  437.     }
  438.   has_filename = (argc - optind == 1);
  439.   if (has_filename)        /* last argument is input file name */
  440.     {
  441.       input_name = argv[optind];
  442.       if (freopen (input_name, "r", stdin) == NULL)
  443.     {
  444.       fprintf (stderr, "%s: Can't open `%s' for reading\n",
  445.            program_name, input_name);
  446.       exit (2);
  447.     }
  448.     }
  449.   if ((output_name == 0) && !piped)
  450.     {
  451.       char * tmp;
  452.       if (has_filename)
  453.     {
  454.       char * point;
  455.       
  456.       point = strrchr (input_name, '/');
  457.       if (point == 0)    /* plain filename */
  458.         {
  459.           point = input_name;
  460.         }
  461.       else
  462.         {
  463.           point++;
  464.         }
  465.       tmp = malloc (strlen (point) + 1);
  466.       if (tmp == 0)
  467.         {
  468.           fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  469.           exit (3);
  470.         }
  471.       strcpy (tmp, point);
  472.       point = strrchr (tmp, '.');
  473.       if (point != 0)
  474.         {
  475.           *point = '\0';
  476.         }
  477.     }
  478.       else
  479.     {
  480.       tmp = program_name;
  481.     }
  482.       output_name = malloc (strlen (tmp) + 4);
  483.       if (output_name == 0)
  484.     {
  485.       fprintf (stderr, "%s: Virtual memory exhausted\n", program_name);
  486.       exit (3);
  487.     }
  488.       strcpy (output_name, tmp);
  489.       strcat (output_name, ".tex");
  490.     }
  491.   if (!piped)
  492.     {
  493.       if (freopen (output_name, "w", stdout) == NULL)
  494.     {
  495.       fprintf (stderr, "%s: Can't open `%s' for writing\n",
  496.            program_name, output_name);
  497.       exit (4);
  498.     }
  499.     }
  500.   printf ("\
  501. %%\n\
  502. %% This file was automatically produced at " __DATE__ ", " __TIME__" by\n\
  503. %% %s", program_name);
  504.   for (i = 1; i < argc; i++)
  505.     {
  506.       printf(" %s", argv[i]);
  507.     }
  508.   if (!has_filename)
  509.     {
  510.       printf(" (from Standard Input)");
  511.     }
  512.   printf("\n%%\n");
  513.   if (complete_file)
  514.     {
  515.       if (header)
  516.     {
  517.       if (strcmp (font_size, "10") == 0)
  518.         {
  519.           printf ("\\documentstyle[fancyheadings]{article}\n");
  520.         }
  521.       else
  522.         {
  523.           printf ("\\documentstyle[%spt,fancyheadings]{article}\n",
  524.               font_size);
  525.         }
  526.     }
  527.       else
  528.     {
  529.       if (strcmp (font_size, "10") == 0)
  530.         {
  531.           printf ("\\documentstyle{article}\n");
  532.         }
  533.       else
  534.         {
  535.           printf ("\\documentstyle[%spt]{article}\n", font_size);
  536.         }
  537.     }
  538.       printf ("\\setlength{\\textwidth}{15cm}\n");
  539.       printf ("\\setlength{\\textheight}{22.5cm}\n");
  540.       printf ("\\setlength{\\hoffset}{-2cm}\n");
  541.       printf ("\\setlength{\\voffset}{-2cm}\n");
  542.       if (header)
  543.     {
  544.       printf ("\\chead{\\%s Produced from ", header_font);
  545.       substitute(input_name);
  546.       printf (" at " __DATE__ ", " __TIME__"}\n");
  547.       printf ("\\cfoot{\\rm\\thepage}\n");
  548.       printf ("\\addtolength{\\headheight}{14pt}\n");
  549.       printf ("\\pagestyle{fancy}\n");
  550.     }
  551.       printf ("\\begin{document}\n");
  552.     }
  553.   printf ("\\expandafter\\ifx\\csname indentation\\endcsname\\relax%\n");
  554.   printf ("\\newlength{\\indentation}\\fi\n");
  555.   printf ("\\setlength{\\indentation}{%s}\n", indentation);
  556.   printf ("\\begin{flushleft}\n");
  557.   yylex();
  558.   printf ("\\end{flushleft}\n");
  559.   if (complete_file)
  560.     {
  561.       printf ("\\end{document}\n");
  562.     }
  563. }
  564.  
  565. void
  566. usage(const char * name)
  567. {
  568.   fprintf (stderr, "%s\n", version_string);
  569.   fprintf (stderr, "\
  570. Usage: %s [-a] [-c] [-h] [-i length] [-o path] [-p] [-s size] [-t]\n\
  571.        [-C font][-H font] [-K font] [-P font] [-S font] [-T wide] [-V]\n\
  572.        [+ansi-c] [+complete-file] [+header] [+indentation length]\n\
  573.        [+output path] [+c-plusplus] [+font-size size] [+pipe]\n\
  574.        [+comment-font font] [+keyword-font font] [+cpp-font font]\n\
  575.        [+header-font font] [+string-font font] [+tabstop wide]\n\
  576.        [+version] [path]\n", name);
  577.   exit (1);
  578. }
  579.