home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / util / mbq307 / qmapfmt.l < prev    next >
Encoding:
Lex Description  |  1996-07-09  |  1.3 KB  |  67 lines

  1. %{
  2. /*
  3.  
  4. This is a quick-and-dirty indenter/formatter for Quake .map files.
  5. You'll need a C compiler and lex or flex to compile this.
  6.  
  7. Copy, redistribute and alter freely.  No warranty.
  8.  
  9. Jeff Epler
  10. jepler@inetnebr.com
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #define MAX(x,y) (fprintf(stderr,"%d %d\n",(x),(y)),(x)<(y)?(y):(x))
  16. int indent=0;
  17. int ilevel=4;
  18. int chars=0;
  19. char ichar=' ';
  20.  
  21. void indent_in(void) {
  22.     indent += ilevel;
  23. }
  24.  
  25. void indent_out(void) {
  26.     if(indent>0) indent -= ilevel;
  27.     else fprintf(stderr,"Braces don't balance\n");
  28. }
  29.  
  30. void nl(void) {
  31.     int i=indent;
  32.     putchar('\n');
  33.     for(;i>0;i--) putchar(ichar);
  34. }
  35.  
  36. %}
  37.  
  38. %%
  39. \{    { putchar('{'); indent_in(); nl(); }
  40. \}    { indent_out(); if(chars) nl(); putchar('}'); nl(); }
  41. ^[\t ]+    { ; }
  42. [\t ]+  { if(chars) putchar(' '); }
  43. \"([^"](\\\")?)*\"[\t ]*\"([^"](\\\")?)*\" { 
  44.         char *s0, *s1, *s2;
  45.         int i=0;
  46.         s0=s2=yytext+1; /* Point at the beginning of the string */
  47.         while(*s2!='\"') { /* Find the end */
  48.             if(*s2=='\\') s2++;
  49.             s2++;
  50.         }
  51.         *s2=0; /* Zot! */
  52.         while(*s2!='\"') { /* Find beginning of second string */
  53.             if(*s2=='\\') s2++;
  54.             s2++;
  55.         }
  56.         s1=s2=s2+1; /* Point to it */
  57.         while(*s2!='\"') { /* Find the end */
  58.             if(*s2=='\\') s2++;
  59.             s2++;
  60.         }
  61.         *s2=0; /* Zot! */
  62.         printf("\"%s\"%*s\"%s\"",s0,MAX(40-strlen(s0),0),"",s1);
  63.         chars=1;
  64.     }
  65. \n    { if (chars) nl(); chars=0; }
  66. .    { chars=1; putchar(*yytext); }
  67.