home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / pars_htm.cpp < prev    next >
C/C++ Source or Header  |  2000-12-13  |  7KB  |  294 lines

  1. /*
  2. ** Module   :PARS_HTM.CPP
  3. ** Abstract :
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Thu  09/04/1998 Created
  8. **
  9. */
  10.  
  11. #include <parser.h>
  12. #include <version.h>
  13.  
  14. //----------------------------------------------------------------------
  15. //
  16. // Class Parser_HTML
  17. //
  18. //----------------------------------------------------------------------
  19.  
  20. int Parser_HTML::next_token()
  21. {
  22.     old_tok = tok;
  23.     tok_len = 0;
  24.     color = CL_DEFAULT;
  25.     char *tmp = tok;
  26.  
  27.     if(state == ST_COMMENT) //HTML style comment
  28.     {
  29.         color = CL_COMMENT;
  30.  
  31.         for(;*tmp; tmp++)
  32.         {
  33.             if(tmp[0] == '-' && tmp[1] == '-' && tmp[2] == '>')
  34.             {
  35.                 tmp += 3;
  36.                 state = ST_INITIAL;
  37.                 break;
  38.             }
  39.         }
  40.  
  41.         return (tok_len = (tmp - tok));
  42.     }
  43.  
  44.     while(state == ST_QUOTE1)  /* HTML code */
  45.     {
  46.         switch(*tmp)
  47.         {
  48.             case '\t':
  49.             case ' ':
  50.                 for(++tmp; __issp(*tmp);)
  51.                     tmp++;
  52.  
  53.                 return (tok_len = (tmp - tok));
  54.  
  55.             case '"':
  56.                 for(++tmp; *tmp != '"' && *tmp;)
  57.                 {
  58.                     if(*tmp == '\\' && tmp[1])
  59.                     {
  60.                         tmp += 2;
  61.                         continue;
  62.                     }
  63.                     tmp++;
  64.                 }
  65.  
  66.                 if(*tmp == '"')
  67.                     tmp++;
  68.  
  69.                 color = CL_CONST;
  70.  
  71.                 return (tok_len = (tmp - tok));
  72.  
  73.             case '>':
  74.                 state = ST_INITIAL;
  75.                 break;
  76.  
  77.             default:
  78.                 if(__isic(*tmp))
  79.                 {
  80.                     for(++tmp;__isic(*tmp);)
  81.                         tmp++;
  82.  
  83.                     return (tok_len = (tmp - tok));
  84.                 }
  85.         }
  86.         return (tok_len = 1);
  87.     }
  88.  
  89.     while(state >= ST_PHP_START && state <= ST_PHP_END)   /* PHP code */
  90.     {
  91.         int q_chr = (state == ST_STR_PHP1) ? '"': ((state == ST_STR_PHP2) ? '\'':0);
  92.  
  93.         if(state == ST_COMMENT_PHP)
  94.         {
  95.             while(*tmp)
  96.             {
  97.                 if(*tmp == '*' && *(tmp+1) == '/')
  98.                 {
  99.                     tmp += 2;
  100.                     state = ST_PHP_START;
  101.                     break;
  102.                 }
  103.                 tmp++;
  104.             }
  105.             color = CL_COMMENT;
  106.  
  107.             return (tok_len = (tmp - tok));
  108.         }
  109.  
  110.         if(state == ST_STR_PHP1 || state == ST_STR_PHP2)
  111.         {
  112.             while(*tmp)
  113.             {
  114. //                if(*tmp == '\\' && tmp[1] == q_chr)
  115.                 if(*tmp == '\\' && tmp[1])
  116.                 {
  117.                     tmp += 2;
  118.                     continue;
  119.                 }
  120.                 if(*tmp == q_chr)
  121.                 {
  122.                     tmp++;
  123.                     state = ST_PHP_START;
  124.                     break;
  125.                 }
  126.                 tmp++;
  127.             }
  128.             color = CL_CONST;
  129.             return (tok_len = (tmp - tok));
  130.         }
  131.  
  132.         switch(*tmp)
  133.         {
  134.             case ' ':
  135.             case '\t':
  136.                 for(++tmp; __issp(*tmp);)
  137.                     tmp++;
  138.                 return (tok_len = (tmp - tok));
  139.  
  140.             case '"':
  141.             case '\'':
  142.  
  143.                 q_chr = *tmp;
  144.  
  145.                 state = (q_chr == '"') ? ST_STR_PHP1:ST_STR_PHP2;
  146.                 tmp++;
  147.  
  148.                 color = CL_CONST;
  149.                 return (tok_len = (tmp - tok));
  150.  
  151.             case '/':
  152.  
  153.                 if(tok[1] == '*')
  154.                 {
  155.                     tmp += 2;
  156.  
  157.                     state = ST_COMMENT_PHP;
  158.                     color = CL_COMMENT;
  159.  
  160.                     return (tok_len = (tmp - tok));
  161.                 }
  162.                 if(tok[1] == '/')
  163.                 {
  164.                     tmp += 2;
  165.                     while(*tmp)
  166.                         tmp++;
  167.  
  168.                     color = CL_COMMENT;
  169.                     return (tok_len = (tmp - tok));
  170.                 }
  171.                 break;
  172.  
  173.             case '?':
  174.                 if(tmp[1] != '>')
  175.                     break;
  176.  
  177.                 tmp += 2;
  178.  
  179.                 state = ST_INITIAL;
  180.                 color = CL_PREPROC;
  181.  
  182.                 return (tok_len = (tmp - tok));
  183.  
  184.             case '0':
  185.             case '1':
  186.             case '2':
  187.             case '3':
  188.             case '4':
  189.             case '5':
  190.             case '6':
  191.             case '7':
  192.             case '8':
  193.             case '9':
  194.  
  195.                 while(__isdd(*tmp))
  196.                     tmp++;
  197.                 color = CL_NUMBER;
  198.                 return (tok_len = (tmp - tok));
  199.  
  200.             case ';':
  201.                 color = CL_SEMICOL;
  202.                 break;
  203.  
  204.             case '\\': /* Skip next character*/
  205.                 return (tok_len = (tok[1]) ? 2:1);
  206.  
  207.             case '%':
  208.             case '$':
  209.             case '@':
  210.                 tmp++;
  211.  
  212.             default:
  213.                 if(__isic(*tmp))
  214.                 {
  215.                     for(++tmp;__isic(*tmp);)
  216.                     {
  217.                         tmp++;
  218.  
  219.                         if(tmp[0] == ':' && tmp[1] == ':')
  220.                             tmp+= 2;
  221.                     }
  222.  
  223.                     tok_len = (tmp - tok);
  224.  
  225.                     color = CL_IDENT;
  226.  
  227.                     {
  228.                         while(*tmp)
  229.                         {
  230.                             if(__issp(*tmp))
  231.                             {
  232.                                 tmp++;
  233.                                 continue;
  234.                             }
  235.                             if(*tmp == '(')
  236.                                 color = CL_FUNCTION;
  237.                             break;
  238.                         }
  239.                     }
  240.  
  241.                     return tok_len;
  242.                 }
  243.         }
  244.         return (tok_len = 1);
  245.     }
  246.  
  247.     if(*tmp == '<')
  248.     {
  249.         if(tmp[1] == '!' && tmp[2] == '-' && tmp[3] == '-')
  250.         {
  251.             if(tmp[4] != '#')
  252.             {
  253.                 tmp += 4;
  254.                 state = ST_COMMENT;
  255.                 color = CL_COMMENT;
  256.  
  257.                 return (tok_len = (tmp - tok));
  258.             }
  259.         }
  260.  
  261.         if(tmp[1] == '?')
  262.         {
  263.             tmp += 2;
  264.  
  265.             state = ST_PHP_START;  //PHP
  266.             color = CL_PREPROC;
  267.  
  268.             return (tok_len = (tmp - tok));
  269.         }
  270.  
  271.         state = ST_QUOTE1;
  272.  
  273.         //NOTE: caller will be ought to issue another call
  274.         //for the same position. But state already changed
  275.         //and next time token will be processed by other
  276.         //part of code
  277.  
  278.         return 0;
  279.     }
  280.  
  281.     color = CL_IDENT;
  282.  
  283.     while(*tmp)
  284.     {
  285.         if(*tmp == '<')
  286.             break;
  287.  
  288.         tmp++;
  289.     }
  290.  
  291.     return (tok_len = (tmp - tok));
  292. }
  293.  
  294.