home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcoral16.zip / GEN.L < prev    next >
Text File  |  1993-01-15  |  22KB  |  598 lines

  1. %{
  2.  
  3. #include "global_parse.h"
  4. #include "gen.tab.h"
  5.  
  6. extern YYSTYPE yylval;
  7.  
  8. #include "browser_util.h"
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12.  
  13. /*------------------------------------------------------------------------------
  14. */
  15. int line_count;
  16.  
  17. static int bracket_level;
  18.  
  19. static int parent_level;
  20.  
  21. static int tmp_line_count;
  22.  
  23. static int virtual_flag;
  24.  
  25. static int const_flag;
  26.  
  27.  
  28. /*------------------------------------------------------------------------------
  29. */
  30.  
  31. #define ARG_BUFF_SIZE  4096
  32.  
  33. static char arg_buff[ARG_BUFF_SIZE];
  34.  
  35. static int  arg_index = 0;
  36.  
  37.  
  38. /*------------------------------------------------------------------------------
  39. */
  40.  
  41. static int  state_stack[20];
  42.  
  43. static int  next_free;
  44.  
  45. static int  current_state;
  46.  
  47.  
  48. /*------------------------------------------------------------------------------
  49. */
  50.  
  51. #define LINE_COUNT()         {                                 \
  52.                                char* current;                  \
  53.                                                                \
  54.                                current = yytext + yyleng;      \
  55.                                while ((--current) >= yytext) { \
  56.                                  if ((*current) == '\n')       \
  57.                                    line_count++;               \
  58.                                }                               \
  59.                              }
  60.  
  61.  
  62. /*------------------------------------------------------------------------------
  63. */
  64.  
  65. #define BEGIN_STATE(state)   BEGIN(current_state = state)
  66.  
  67. #define STACK_STATE(state)   { state_stack[next_free++] = current_state; \
  68.                                BEGIN_STATE(state); }
  69.  
  70. #define UNSTACK_STATE()      { BEGIN_STATE(state_stack[--next_free]); }
  71.  
  72.  
  73. #define YY_USER_INIT         BEGIN_STATE(MAIN_STATE)
  74.  
  75. %}
  76.  
  77.  
  78. WS           [ \v\t\n\r\f]
  79.  
  80. IDENT        [_a-zA-Z][_a-zA-Z0-9]*
  81.  
  82.  
  83. OP1    ("+"|"-"|"*"|"/"|"%"|"^"|"&"|"|"|"~"|"!"|"="|"<"|">")
  84.  
  85. OP2    ("+="|"-="|"*="|"/="|"%="|"^="|"&="|"|=")
  86.  
  87. OP3    ("<<"|">>"|">>="|"<<="|"=="|"!="|"<="|">="|"&&"|"||"|"++"|"--")
  88.  
  89. OP4    ("new"|"delete"|","|"->*"|"->"|"()"|"[]")
  90.  
  91. OPERS  ({OP1}|{OP2}|{OP3}|{OP4}|{IDENT})
  92.  
  93.  
  94. QUALIF       {WS}*:: 
  95.  
  96.  
  97. STR_IGNORE   "\\\""|("\""([^\\\"\n]|("\\"(.)))*"\"")
  98.  
  99.  
  100. %s MAIN_STATE
  101.  
  102. %s OPERATOR_STATE
  103. %s PROC_STATE1
  104. %s PROC_STATE2
  105.  
  106. %s CLASS_STATE1
  107. %s CLASS_STATE2
  108.  
  109. %s PARENT_STATE
  110. %s BODY_STATE
  111.  
  112. %s ARG_STATE
  113. %s INIT_STATE
  114. %s TAIL_STATE
  115.  
  116. %s CODE_STATE
  117.  
  118.  
  119. %%
  120.  
  121.  /************************************************************************************/
  122.  /*                    la supression des commentaires et macros                      */
  123.  /************************************************************************************/
  124.  
  125. "/*"                                    { 
  126.                                           int current;
  127.  
  128.                                           current = '\0';
  129.                                           while (current != '/') {
  130.                                             while (current != '*') {
  131.                               current = input();
  132.                          if (current == '\n')
  133.                         line_count++;
  134.                          else if (current == EOF)
  135.                         yyterminate ();
  136.                                             }
  137.                          current = input();
  138.                          if (current == '\n')
  139.                         line_count++;
  140.                          else if (current == EOF)
  141.                         yyterminate ();
  142.                                           }
  143.                                         }
  144.  
  145.  
  146. "//"(.)*                                {}
  147.  
  148.  
  149. ^#[ \v\t\r\f]*{IDENT}                               {
  150.                                           int current;
  151.                                           int previous;
  152.  
  153.                                           previous = '\\';
  154.                                           while (previous == '\\') {
  155.                                             current  = '\0';
  156.                                             while (current != '\n') {
  157.                                               previous = current;
  158.                                               current  = input();
  159.                   if (current == EOF )
  160.             yyterminate ();
  161.                                             }
  162.                                             line_count++;
  163.                                           }
  164.                                         }
  165.  
  166.  
  167. "\n"                                    { line_count++; }
  168.  
  169.  
  170. [ \v\t]+                                {}
  171.  
  172.  
  173.  /************************************************************************************/
  174.  /*     le parcours de la liste des arguments d'une procedure ou d'une methode       */
  175.  /************************************************************************************/
  176.  
  177. <OPERATOR_STATE>{OPERS}                 {
  178.                                           strcat(yylval.text, yytext);
  179.                                           strcat(yylval.text, " ");
  180.                                           BEGIN_STATE(PROC_STATE1);
  181.                                         }
  182.  
  183.  
  184.  /************************************************************************************/
  185.  /*     le parcours de la liste des arguments d'une procedure ou d'une methode       */
  186.  /************************************************************************************/
  187.  
  188. <PROC_STATE1>"("                        {
  189.                                          int current;
  190.                                          int  parent_level;
  191.  
  192.                                          arg_index             = 0;
  193.                                          parent_level          = 1;
  194.                                          tmp_line_count        = 0;
  195.                                          arg_buff[arg_index++] = '(';
  196.                                          while(parent_level != 0) {
  197.                 current = input();
  198.                                            switch(current) {
  199.                            case EOF: yyterminate ();
  200.                             break;
  201.                                              case '(' : parent_level++;
  202.                                                         break;
  203.                                              case ')' : parent_level--;
  204.                                                         break;
  205.                                            }
  206.                                            if (current == '\n')
  207.                                              tmp_line_count++;
  208.                                            if (arg_index < ARG_BUFF_SIZE)
  209.                                              arg_buff[arg_index++] = current;
  210.                                            else {
  211.                                              yylval.position = line_count;
  212.                                              return(LEX_ERROR_TOK);
  213.                                            }
  214.                                          }
  215.                                          const_flag = False;
  216.                                          BEGIN_STATE(PROC_STATE2);
  217.                                        }
  218.  
  219. <PROC_STATE1>.                         {
  220.                                          unput(yytext[0]);
  221.                                          UNSTACK_STATE();
  222.                                          if (current_state != BODY_STATE)
  223.                                            *yy_class_name = '\0';
  224.                                        }
  225.  
  226.  
  227.  /************************************************************************************/
  228.  /*              la recherche du token qui suit la liste des arguments               */
  229.  /************************************************************************************/
  230.  
  231. <PROC_STATE2>":"                       {
  232.                                          while (--arg_index >= 0)
  233.                                            unput(arg_buff[arg_index]);
  234.                                          parent_level = 0;
  235.                                          yylval.is_virtual = virtual_flag;
  236.                                          virtual_flag      = False;
  237.                                          BEGIN_STATE(ARG_STATE);
  238.                                          return(CPLUS_TOK);
  239.                                        }
  240.  
  241.  
  242. <PROC_STATE2>"{"                       {
  243.                                          unput('{');
  244.                                          while (--arg_index >= 0)
  245.                                            unput(arg_buff[arg_index]);
  246.                                          parent_level = 0;
  247.                                          yylval.is_virtual = virtual_flag;
  248.                                          virtual_flag      = False;
  249.                                          BEGIN_STATE(ARG_STATE);
  250.                                          return(CPLUS_TOK);
  251.                                        }
  252.  
  253.  
  254. <PROC_STATE2>"const"                   { const_flag = True; }
  255.  
  256.                                   
  257. <PROC_STATE2>{IDENT}                   {
  258.                                          line_count  += tmp_line_count;
  259.                                          virtual_flag = False;
  260.                                          UNSTACK_STATE();
  261.                                          return(C_TOK);
  262.                                        }
  263.  
  264.                                   
  265. <PROC_STATE2>";"                       {
  266.                                          unput(';');
  267.                                          UNSTACK_STATE();
  268.                                          if (current_state == BODY_STATE) {
  269.                                            while (--arg_index >= 0)
  270.                                              unput(arg_buff[arg_index]);
  271.                                            parent_level = 0;
  272.                                            yylval.is_virtual = virtual_flag;
  273.                                            virtual_flag      = False;
  274.                                            STACK_STATE(ARG_STATE);
  275.                                            return(DECL_TOK);
  276.                                          }
  277.                                          else
  278.                                            line_count += tmp_line_count;
  279.                                          virtual_flag = False;
  280.                                        }
  281.  
  282.                                   
  283. <PROC_STATE2>"="                       {
  284.                                          unput('=');
  285.                                          UNSTACK_STATE();
  286.                                          if (current_state == BODY_STATE) {
  287.                                            while (--arg_index >= 0)
  288.                                              unput(arg_buff[arg_index]);
  289.                                            parent_level = 0;
  290.                                            yylval.is_virtual = virtual_flag;
  291.                                            virtual_flag      = False;
  292.                                            STACK_STATE(ARG_STATE);
  293.                                            return(DECL_TOK);
  294.                                          }
  295.                                          else
  296.                                            line_count += tmp_line_count;
  297.                                          virtual_flag = False;
  298.                                        }
  299.  
  300.                                   
  301. <PROC_STATE2>.                         { 
  302.                                          unput(yytext[0]);
  303.                                          line_count += tmp_line_count;
  304.                                          virtual_flag = False;
  305.                                          UNSTACK_STATE();
  306.                                        }
  307.  
  308.                                   
  309.  /************************************************************************************/
  310.  /*                         La lecture du nom de la classe                           */
  311.  /************************************************************************************/
  312.  
  313. <CLASS_STATE1>{IDENT}                  {
  314.                                          strcpy(yylval.text, yytext);
  315.                                          yylval.position = line_count;
  316.                                          BEGIN_STATE(CLASS_STATE2);
  317.                                        }
  318.  
  319.  
  320. <CLASS_STATE1>.                        { unput(yytext[0]); BEGIN_STATE(MAIN_STATE); }
  321.  
  322.  
  323. <CLASS_STATE2>":"                      {
  324.                                          BEGIN_STATE(PARENT_STATE);
  325.                                          return(IDENT_TOK);
  326.                                        }
  327.  
  328. <CLASS_STATE2>"{"                      {
  329.                                          unput('{');
  330.                                          BEGIN_STATE(PARENT_STATE);
  331.                                          return(IDENT_TOK);
  332.                                        }
  333.  
  334. <CLASS_STATE2>.                        { unput(yytext[0]); BEGIN_STATE(MAIN_STATE); }
  335.  
  336.  
  337.  /************************************************************************************/
  338.  /*                               Quelques mots clefs                                */
  339.  /************************************************************************************/
  340.  
  341. <PARENT_STATE,BODY_STATE>"public"      { return(PUBLIC_TOK);    }
  342.  
  343. <PARENT_STATE,BODY_STATE>"protected"   { return(PROTECTED_TOK); }
  344.  
  345. <PARENT_STATE,BODY_STATE>"private"     { return(PRIVATE_TOK);   }
  346.  
  347.  
  348.  /************************************************************************************/
  349.  /*                       La lecture de la liste des parents                         */
  350.  /************************************************************************************/
  351.  
  352. <PARENT_STATE>"virtual"                {}
  353.  
  354. <PARENT_STATE>{IDENT}                  {
  355.                                          strcpy(yylval.text, yytext);
  356.                                          yylval.position = line_count;
  357.                                          return(IDENT_TOK);
  358.                                        }
  359.  
  360. <PARENT_STATE>"{"                      { BEGIN_STATE(BODY_STATE); return '{'; }
  361.  
  362.  
  363.  /************************************************************************************/
  364.  /*                  La lecture des "members" declares dans une classe               */
  365.  /************************************************************************************/
  366.  
  367. <BODY_STATE>"virtual"                  { virtual_flag = True; }
  368.  
  369. <BODY_STATE>"friend"[^;{]*             { LINE_COUNT(); }
  370.  
  371. <BODY_STATE>"}"                        { 
  372.                                          *yy_class_name = '\0';
  373.                                          BEGIN_STATE(MAIN_STATE);
  374.                                          return('}');
  375.                                        }
  376.  
  377.  
  378.  /************************************************************************************/
  379.  /*      La lecture de la liste des arguments d'une methode ou d'une procedure       */
  380.  /************************************************************************************/
  381.  
  382. <ARG_STATE>"unsigned"                  { return(UNSIGNED_TOK); }
  383.  
  384. <ARG_STATE>"signed"                    { return(SIGNED_TOK);   }
  385.  
  386. <ARG_STATE>"char"                      { return(CHAR_TOK);     }
  387.  
  388. <ARG_STATE>"short"                     { return(SHORT_TOK);    }
  389.  
  390. <ARG_STATE>"int"                       { return(INT_TOK);      }
  391.  
  392. <ARG_STATE>"long"                      { return(LONG_TOK);     }
  393.  
  394. <ARG_STATE>"struct"                    { return(STRUCT_TOK);   }
  395.  
  396. <ARG_STATE>"union"                     { return(UNION_TOK);    }
  397.  
  398. <ARG_STATE>"class"                     { return(CLASS_TOK);    }
  399.  
  400. <ARG_STATE>"const"                     { return(CONST_TOK);    }
  401.  
  402. <ARG_STATE>"*"                         { return('*');          }
  403.  
  404. <ARG_STATE>"&"                         { return('&');          }
  405.  
  406. <ARG_STATE>"["[^\]]*"]"                { LINE_COUNT(); return(ARRAY_TOK);    }
  407.  
  408. <ARG_STATE>","                         { return(',');          }
  409.  
  410. <ARG_STATE>"register"                  {}
  411.  
  412. <ARG_STATE>"auto"                      {}
  413.  
  414. <ARG_STATE>"volatile"                  {}
  415.  
  416.  
  417. <ARG_STATE>"..."                       {
  418.                                          strcpy(yylval.text, yytext);
  419.                                          return(IDENT_TOK);
  420.                                        }
  421.  
  422.  
  423. <ARG_STATE>{IDENT}                     {
  424.                                          strcpy(yylval.text, yytext);
  425.                                          return(IDENT_TOK);
  426.                                        }
  427.  
  428.  
  429. <ARG_STATE>"="                         { BEGIN_STATE(INIT_STATE); }
  430.  
  431.  
  432. <ARG_STATE>"("                         {
  433.                                          parent_level++;
  434.                                          return('(');
  435.                                        }
  436.  
  437.  
  438. <ARG_STATE>")"                         {
  439.                                          if ((--parent_level) == 0)
  440.                                            BEGIN_STATE(TAIL_STATE);
  441.                                          return(')');
  442.                                        }
  443.  
  444.  
  445. <INIT_STATE>{STR_IGNORE}               {}
  446.  
  447.  
  448. <INIT_STATE>"','"                      {}
  449.  
  450.  
  451. <INIT_STATE>"("                        {
  452.                                          int  parent_level;
  453.                                          char current;
  454.  
  455.                                          parent_level = 1;
  456.                                          while (parent_level != 0) {
  457.                                            current = input();
  458.                                           switch(current) {
  459.                            case EOF: yyterminate ();
  460.                             break;
  461.                                              case '(' : parent_level++;
  462.                                                         break;
  463.                                              case ')' : parent_level--;
  464.                                                         break;
  465.                                            }
  466.                                       }
  467.                                     }
  468.  
  469.  
  470. <INIT_STATE>","                        { unput(','); BEGIN_STATE(ARG_STATE); }
  471.  
  472.  
  473. <INIT_STATE>")"                        { unput(')'); BEGIN_STATE(ARG_STATE); }
  474.  
  475.  
  476. <INIT_STATE>.                          {}
  477.  
  478.  
  479.  /************************************************************************************/
  480.  /*                                  Les Divers                                      */
  481.  /************************************************************************************/
  482.  
  483. <TAIL_STATE>[^;{]*/(;|"{")             { 
  484.                                          LINE_COUNT(); 
  485.                                          UNSTACK_STATE();
  486.                                          if (const_flag == True) {
  487.                                            const_flag = False;
  488.                                            return(CONST_TOK);
  489.                                          }
  490.                                        }
  491.  
  492.  
  493.  /************************************************************************************/
  494.  /*                                  Les Divers                                      */
  495.  /************************************************************************************/
  496.  
  497. <CODE_STATE>"{"                        { bracket_level++; }
  498.  
  499. <CODE_STATE>"}"                        {
  500.                                          if ((--bracket_level) == 0) {
  501.                                            UNSTACK_STATE();
  502.                                            return(SYNC_TOK);
  503.                                          }
  504.                                        }
  505.  
  506. <CODE_STATE>"'{'"                      {}
  507.  
  508. <CODE_STATE>"'}'"                      {}
  509.  
  510. <CODE_STATE>"'"                        {}
  511.  
  512. <CODE_STATE>"/"                        {}
  513.  
  514. <CODE_STATE>{STR_IGNORE}               {}
  515.  
  516. <CODE_STATE>[^{}'\"/\n]+               {}
  517.  
  518. "{"                                    {
  519.                                          bracket_level = 1;
  520.                                          STACK_STATE(CODE_STATE);
  521.                                        }
  522.  
  523.  
  524. ";"                                    { return(SYNC_TOK); }
  525.  
  526.  
  527.  /************************************************************************************/
  528.  
  529. <MAIN_STATE>"class"                    {
  530.                                          BEGIN_STATE(CLASS_STATE1);
  531.                                        }
  532.  
  533.  
  534. <MAIN_STATE>{IDENT}{QUALIF}            {
  535.                                          char* current;
  536.  
  537.                                          current = yytext + yyleng - 2;
  538.                                          while ((--current) >= yytext) {
  539.                                            if (isspace(*current)) {
  540.                                              if ((*current) == '\n')
  541.                                                line_count++;
  542.                                            }
  543.                                            else
  544.                                              break;
  545.                                          }
  546.                                          *(current + 1) = '\0';
  547.                                          strcpy(yy_class_name, yytext);
  548.                                        }
  549.  
  550.  
  551. <MAIN_STATE,BODY_STATE>"operator"      {
  552.                                          strcpy(yylval.text, "operator ");
  553.                                          yylval.position = line_count;
  554.                                          STACK_STATE(OPERATOR_STATE);
  555.                                        }
  556.  
  557.  
  558. <MAIN_STATE,BODY_STATE>(~{WS}*)?{IDENT}       {
  559.                  if ( yytext[0] != '~' )
  560.                                              strcpy(yylval.text, yytext);
  561.                  else {
  562.             register char *p = yytext + 1;
  563.             yylval.text [0]= '~';
  564.             while (  isspace( *p ) ) 
  565.                 p++;
  566.             strcpy(&(yylval.text[1]), p );
  567.             LINE_COUNT();
  568.                  }
  569.                                          yylval.position = line_count;
  570.                                          STACK_STATE(PROC_STATE1);
  571.                                        }
  572.  
  573.  
  574. .                                      {}
  575.  
  576.  
  577.  /************************************************************************************/
  578.  
  579. %%
  580.  
  581. void flex_init(file)
  582.    FILE* file;
  583. {
  584.   static int first_call = 1;
  585.   line_count   = 1;
  586.   next_free    = 0;
  587.   virtual_flag = False;
  588.   yyin = file;
  589.  
  590.   yy_init = 1;
  591.   yy_start = 0;
  592.  
  593.   if (first_call == 1)
  594.     first_call = 0;
  595.   else
  596.         yyrestart (file);
  597. }
  598.