| 1 |
/* topformflat.l; see License.txt for copyright and terms of use |
| 2 |
* |
| 3 |
* Scott McPeak <[email protected]> |
| 4 |
* |
| 5 |
* flatten all toplevel forms to single lines. |
| 6 |
* very heuristic... */ |
| 7 |
|
| 8 |
%{ |
| 9 |
#include <stdlib.h> // atoi |
| 10 |
|
| 11 |
// emit yytext as-is |
| 12 |
void emit(); |
| 13 |
|
| 14 |
// debugging diagnostic, emitted when enabled |
| 15 |
void diag(char const *str); |
| 16 |
|
| 17 |
// add a newline if nesting <= threshold |
| 18 |
void possibleNewline(); |
| 19 |
|
| 20 |
// keep track of brace nesting (0 means not inside any pair) |
| 21 |
int nesting = 0; |
| 22 |
|
| 23 |
// nesting threshold; when nesting is greater than threshold, |
| 24 |
// newlines are suppressed |
| 25 |
int threshold = 0; |
| 26 |
|
| 27 |
%} |
| 28 |
|
| 29 |
/* don't try to call yywrap() */ |
| 30 |
%option noyywrap |
| 31 |
/* dsw: don't define yyunput() */ |
| 32 |
%option nounput |
| 33 |
|
| 34 |
/* start condition for strings */ |
| 35 |
%x STRING |
| 36 |
%x CHARLIT |
| 37 |
|
| 38 |
|
| 39 |
%% |
| 40 |
|
| 41 |
";" { emit(); possibleNewline(); } |
| 42 |
|
| 43 |
"/\n" { if (nesting <= threshold) { /* end of C comment */ |
| 44 |
emit(); |
| 45 |
} |
| 46 |
else { |
| 47 |
printf("%c", yytext[0]); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
"{" { nesting++; |
| 52 |
emit(); |
| 53 |
possibleNewline(); // so the header is separated from the components |
| 54 |
} |
| 55 |
|
| 56 |
"}"(";"?) { nesting--; |
| 57 |
emit(); |
| 58 |
possibleNewline(); |
| 59 |
} |
| 60 |
|
| 61 |
/* a hash, then some non-newlines. then, possibly, an escaped |
| 62 |
* newline followed by more non-newlines (repeat as needed). |
| 63 |
* finally, a newline */ |
| 64 |
"#".*("\\\n".*)*"\n" { |
| 65 |
printf("\n"); /* make sure starts on own line */ |
| 66 |
emit(); /* preprocessor */ |
| 67 |
} |
| 68 |
|
| 69 |
"\n" { printf(" "); } /* not any above case, eat it*/ |
| 70 |
|
| 71 |
"//".*"\n" { emit(); } /* C++ comment */ |
| 72 |
|
| 73 |
"\"" { diag("<STR>"); emit(); BEGIN(STRING); } /* start quote */ |
| 74 |
|
| 75 |
<STRING>{ |
| 76 |
"\\"(.|\n) { emit(); } /* escaped character */ |
| 77 |
"\"" { emit(); diag("</STR>"); BEGIN(INITIAL); } /* close quote */ |
| 78 |
(.|\n) { emit(); } /* ordinary char */ |
| 79 |
} |
| 80 |
|
| 81 |
"\'" { diag("<CHAR>"); emit(); BEGIN(CHARLIT); } /* start tick */ |
| 82 |
|
| 83 |
<CHARLIT>{ |
| 84 |
"\\"(.|\n) { emit(); } /* escaped character */ |
| 85 |
"\'" { emit(); diag("</CHAR>"); BEGIN(INITIAL); } /* close tick */ |
| 86 |
(.|\n) { emit(); } /* ordinary char */ |
| 87 |
} |
| 88 |
|
| 89 |
. { emit(); } |
| 90 |
|
| 91 |
%% |
| 92 |
|
| 93 |
void emit() |
| 94 |
{ |
| 95 |
printf("%.*s", (int) yyleng, yytext); |
| 96 |
} |
| 97 |
|
| 98 |
void diag(char const *str) |
| 99 |
{ |
| 100 |
//printf("%s", str); |
| 101 |
} |
| 102 |
|
| 103 |
void possibleNewline() |
| 104 |
{ |
| 105 |
if (nesting <= threshold) { |
| 106 |
printf("\n"); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
char *version = "2003.7.14"; |
| 111 |
int main(int argc, char *argv[]) |
| 112 |
{ |
| 113 |
if (isatty(0)) { |
| 114 |
printf("topformflat version %s\n", version); |
| 115 |
printf("usage: %s [threshold] <input.c >output.c\n", argv[0]); |
| 116 |
printf(" The threshold (default: 0) specifies at what nesting level\n" |
| 117 |
" of braces will line breaks be allowed (or inserted). By\n" |
| 118 |
" starting with 0, you get all top-level forms, one per line\n" |
| 119 |
" (roughly). Increasing the threshold leads to finer-grained\n" |
| 120 |
" structure on each line. The intent is to use the delta\n" |
| 121 |
" minimizer on each level of granularity.\n"); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
if (argc >= 2) { |
| 126 |
threshold = atoi(argv[1]); // user-specified threshold |
| 127 |
} |
| 128 |
|
| 129 |
yyin = stdin; |
| 130 |
yylex(); |
| 131 |
return 0; |
| 132 |
} |