Advertisement
JoshDreamland

this needs to be fast

Mar 17th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. /**
  2.   @section Implementation
  3.  
  4.   For the preprocessor's implementation, we switch gears a bit. The preprocessor needs
  5.   to be the fastest aspect of the entire parser, as directives are everywhere. As such,
  6.   it operates on a hacked-up switch statement using a whole heap of gotos, designed to
  7.   support a specific set of macros. Inserting a new directive into this switch is still
  8.   simple enough, but unlike other aspects of the parser, is not as trivial as a single
  9.   function call. Don't gripe; originally I was going to use a perfect hash.
  10. **/
  11. void lexer_cpp::handle_preprocessor() {
  12.   while (cfile[pos] == ' ' or cfile[pos] == '\t') ++pos;
  13.   switch (cfile[pos++])
  14.   {
  15.     case 'd':
  16.       if (strbw(cfile+pos, "efine")) { pos += 5; goto case_define; }
  17.       goto failout;
  18.       case 'e':
  19.       if (cfile[pos] == 'n') { if (strbw(cfile+pos+1, "dif")) { pos += 4; goto case_endif; } goto failout; }
  20.       if (cfile[pos] == 'l')
  21.       {
  22.         if (cfile[++pos] == 's') { if (cfile[++pos] == 'e') { ++pos; goto case_else; } goto failout; }
  23.         if (cfile[pos] == 'i' and cfile[++pos] == 'f')
  24.         {
  25.           if (strbw(cfile[++pos])) goto case_elif;
  26.           if (cfile[pos] == 'd') { if (strbw(cfile+pos+1, "ef"))  { pos += 3; goto case_elifdef;  } goto failout; }
  27.           if (cfile[pos] == 'n') { if (strbw(cfile+pos+1, "def")) { pos += 4; goto case_elifndef; } goto failout; }
  28.         }
  29.         goto failout;
  30.       }
  31.       if (strbw(cfile+pos, "rror")) { pos += 4; goto case_error; }
  32.       goto failout;
  33.       case 'i':
  34.       if (cfile[pos] == 'f')
  35.       {
  36.         if (strbw(cfile[++pos])) goto case_if;
  37.         if (cfile[pos] == 'd') { if (strbw(cfile+pos+1, "ef"))  { pos += 3; goto case_ifdef;  } goto failout; }
  38.         if (cfile[pos] == 'n') { if (strbw(cfile+pos+1, "def")) { pos += 4; goto case_ifndef; } goto failout; }
  39.         goto failout;
  40.       }
  41.       if (cfile[pos] == 'n') { if (strbw(cfile+pos+1, "clude")) { pos += 6; goto case_include; } goto failout; }
  42.       if (cfile[pos] == 'm') { if (strbw(cfile+pos+1, "port"))  { pos += 5; goto case_import;  } goto failout; }
  43.       goto failout;
  44.       case 'l':
  45.       if (strbw(cfile+pos, "ine")) { pos += 3; goto case_line; }
  46.       goto failout;
  47.       case 'p':
  48.       if (strbw(cfile+pos, "ragma")) { pos += 5; goto case_pragma; }
  49.       goto failout;
  50.       case 'u':
  51.       if (strbw(cfile+pos, "ndef")) { pos += 4; goto case_undef; }
  52.       if (strbw(cfile+pos, "sing")) { pos += 4; goto case_using; }
  53.       goto failout;
  54.     case 'w':
  55.       if (strbw(cfile+pos, "arning")) { pos += 7; goto case_warning; }
  56.       goto failout;
  57.     default: goto failout;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement