c-tokenize.lex 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. %top{
  2. /* Include <config.h> before anything else because Gnulib headers such
  3. as <stdio.h> rely on it.
  4. However, when cross-compiling, don't include <config.h> because it
  5. contains information about the host, not about the build. */
  6. #ifndef CROSS_COMPILING
  7. # include <config.h>
  8. #endif
  9. }
  10. %option noyywrap
  11. %option nounput
  12. %pointer
  13. EOL \n
  14. SPACE [ \t\v\f]
  15. WS [ \t\v\n\f]
  16. DIGIT [0-9]
  17. LETTER [a-zA-Z_]
  18. OCTDIGIT [0-7]
  19. HEXDIGIT [a-fA-F0-9]
  20. EXPONENT [Ee][+-]?{DIGIT}+
  21. FLOQUAL (f|F|l|L)
  22. INTQUAL (l|L|ll|LL|lL|Ll|u|U)
  23. %{
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. /* Prevent compilation of static input() function in generated scanner
  28. code. This function is never actually used, and GCC 4.3 will emit
  29. an error for that. */
  30. #define YY_NO_INPUT
  31. int filter_snarfage = 0;
  32. int print = 1;
  33. enum t_state {
  34. SKIP,
  35. MULTILINE,
  36. MULTILINE_COOKIE,
  37. COOKIE
  38. };
  39. enum t_state state = SKIP;
  40. int cookie_was_last = 0;
  41. #define OUT_RAW(type,text) if (print) printf ("(%s . \"%s\")\n", #type, text)
  42. #define OUT_T(type) OUT_RAW (type, yytext)
  43. #define OUT_S if (print) printf ("%s\n", yytext)
  44. #define OUT(type) if (print) printf ("%s\n", #type)
  45. #define IS_COOKIE cookie_was_last = 1
  46. #define IS_NOT_COOKIE cookie_was_last = 0
  47. %}
  48. %%
  49. \/\*(\n|[^*]|\*[^/])*\*\/ { OUT_T (comment); }
  50. ({SPACE}*(\\\n)*{SPACE}*)+ ;
  51. ({SPACE}*\n*{SPACE}*)+ { OUT(eol); }
  52. #.*\n { OUT(hash); IS_NOT_COOKIE; }
  53. {LETTER}({LETTER}|{DIGIT})* { OUT_T (id); IS_NOT_COOKIE; }
  54. 0[xX]{HEXDIGIT}+{INTQUAL}? { OUT_RAW (int_hex, yytext + 2); IS_NOT_COOKIE; }
  55. 0{OCTDIGIT}+{INTQUAL}? { OUT_RAW (int_oct, yytext + 1); IS_NOT_COOKIE; }
  56. {DIGIT}+{INTQUAL}? { OUT_T (int_dec); IS_NOT_COOKIE; }
  57. L?\'(\\.|[^\\\'])+\' { OUT_T (char); IS_NOT_COOKIE; }
  58. {DIGIT}+{EXPONENT}{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  59. {DIGIT}*"."{DIGIT}+({EXPONENT})?{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  60. {DIGIT}+"."{DIGIT}*({EXPONENT})?{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  61. L?\"(\\.|[^\\\"])*\" { OUT_S; IS_NOT_COOKIE; }
  62. "..." { OUT (ellipsis); IS_NOT_COOKIE; }
  63. ">>=" { OUT (shift_right_assign); IS_NOT_COOKIE; }
  64. "<<=" { OUT (shift_left_assign); IS_NOT_COOKIE; }
  65. "+=" { OUT (add_assign); IS_NOT_COOKIE; }
  66. "-=" { OUT (sub_assign); IS_NOT_COOKIE; }
  67. "*=" { OUT (mul-assign); IS_NOT_COOKIE; }
  68. "/=" { OUT (div_assign); IS_NOT_COOKIE; }
  69. "%=" { OUT (mod_assign); IS_NOT_COOKIE; }
  70. "&=" { OUT (logand_assign); IS_NOT_COOKIE; }
  71. "^=" { OUT (logxor_assign); IS_NOT_COOKIE; }
  72. "|=" { OUT (logior_assign); IS_NOT_COOKIE; }
  73. ">>" { OUT (right_shift); IS_NOT_COOKIE; }
  74. "<<" { OUT (left_shift); IS_NOT_COOKIE; }
  75. "++" { OUT (inc); IS_NOT_COOKIE; }
  76. "--" { OUT (dec); IS_NOT_COOKIE; }
  77. "->" { OUT (ptr); IS_NOT_COOKIE; }
  78. "&&" { OUT (and); IS_NOT_COOKIE; }
  79. "||" { OUT (or); IS_NOT_COOKIE; }
  80. "<=" { OUT (le); IS_NOT_COOKIE; }
  81. ">=" { OUT (ge); IS_NOT_COOKIE; }
  82. "==" { OUT (eq); IS_NOT_COOKIE; }
  83. "!=" { OUT (ne); IS_NOT_COOKIE; }
  84. ";" { OUT (semicolon); IS_NOT_COOKIE; }
  85. ("{"|"<%") {
  86. OUT (brace_open);
  87. if (filter_snarfage && cookie_was_last && state == COOKIE)
  88. state = MULTILINE;
  89. IS_NOT_COOKIE; }
  90. ("}"|"%>") {
  91. OUT (brace_close);
  92. if (filter_snarfage && cookie_was_last && state == MULTILINE_COOKIE) {
  93. state = SKIP;
  94. print = 0;
  95. }
  96. IS_NOT_COOKIE; }
  97. "," { OUT (comma); IS_NOT_COOKIE; }
  98. ":" { OUT (colon); IS_NOT_COOKIE; }
  99. "=" { OUT (assign); IS_NOT_COOKIE; }
  100. "(" { OUT (paren_open); IS_NOT_COOKIE; }
  101. ")" { OUT (paren_close); IS_NOT_COOKIE; }
  102. ("["|"<:") { OUT (bracket_open); IS_NOT_COOKIE; }
  103. ("]"|":>") { OUT (bracket_close); IS_NOT_COOKIE; }
  104. "." { OUT (dot); IS_NOT_COOKIE; }
  105. "&" { OUT (amp); IS_NOT_COOKIE; }
  106. "!" { OUT (bang); IS_NOT_COOKIE; }
  107. "~" { OUT (tilde); IS_NOT_COOKIE; }
  108. "-" { OUT (minus); IS_NOT_COOKIE; }
  109. "+" { OUT (plus); IS_NOT_COOKIE; }
  110. "*" { OUT (star); IS_NOT_COOKIE; }
  111. "/" { OUT (slash); IS_NOT_COOKIE; }
  112. "%" { OUT (percent); IS_NOT_COOKIE; }
  113. "<" { OUT (lt); IS_NOT_COOKIE; }
  114. ">" { OUT (gt); IS_NOT_COOKIE; }
  115. \^{WS}*\^ {
  116. if (filter_snarfage)
  117. switch (state) {
  118. case SKIP:
  119. state = COOKIE;
  120. print = 1;
  121. OUT (snarf_cookie);
  122. break;
  123. case MULTILINE:
  124. case MULTILINE_COOKIE:
  125. state = MULTILINE_COOKIE;
  126. OUT (snarf_cookie);
  127. break;
  128. case COOKIE:
  129. state = SKIP;
  130. OUT (snarf_cookie);
  131. print = 0;
  132. break;
  133. default:
  134. /* whoops */
  135. abort ();
  136. break;
  137. }
  138. else
  139. OUT (snarf_cookie);
  140. IS_COOKIE; }
  141. "^" { OUT (caret); IS_NOT_COOKIE; }
  142. "|" { OUT (pipe); IS_NOT_COOKIE; }
  143. "?" { OUT (question); IS_NOT_COOKIE; }
  144. . { fprintf (stderr, "*%s", yytext); fflush (stderr); IS_NOT_COOKIE; }
  145. %%
  146. int
  147. main (int argc, char *argv[])
  148. {
  149. if (argc > 1 && !strcmp (argv[1], "--filter-snarfage")) {
  150. filter_snarfage = 1;
  151. print = 0;
  152. }
  153. yylex ();
  154. return EXIT_SUCCESS;
  155. }
  156. /*
  157. Local Variables:
  158. c-file-style: "gnu"
  159. End:
  160. */