Пример #1
0
    function highlight(&$code)
    {
      $ch = '';
      $next_ch = '';
      $l=strlen($code);
      $out='';
      $i=0;
      while ($i < $l)
      {
        if ($this->state==S_NONE)
        {
          $ch = $code{$i};
          if ($i+1 < $l)
            $next_ch = $code{$i+1};
          else
            $next_ch = '';
          if ($ch=="'")
          {
            $this->state=S_COMMENT1;
            $out=$ch.$next_ch;
            $i++;
            $i++;
          }
          else if (is_identifier_open($ch) or ($ch=='#'))
          {
            $this->state=S_KEYWORD;
            $out=$ch;
            $i++;
          }
          else if ($ch=='"')
          {
            $this->state=S_STRING;
            $out=$ch;
            $i++;
          }
          else
          {
            $out=$ch;
          }
          $this->open_state=$this->state;
          $this->close_state=S_NONE;
        }

          if ($this->state!=S_NONE)
          {
            switch ($this->state)
            {
              case S_COMMENT1:
                $j=strpos($code,"\n",$i);
                if ($j===false)
                  $j=$l-1;
                else
                  $this->close_state=$this->state;
                $out.=substr($code, $i, $j - $i + 1);
                $i=$j;
                break;
              case S_KEYWORD:
              {
                $this->process_std_identifier($i, $l, $code, $this->keywords, $out, false);
                break;
              }
              case S_STRING:
              {
                $j=$i;
                while ($j < $l)
                {
                  if ($code{$j}=='"')
                    break;
                  $j++;
                }
                $this->close_state=$this->state;
                $out.=substr($code, $i, $j - $i + 1);
                $i=$j;
                break;
              }
            }
          }

          $this->text_out($out);
          $i++;
        }
    }
Пример #2
0
    function highlight(&$code)
    {
      $ch = '';
      $next_ch = '';
      $l=strlen($code);
      $out='';
      $i=0;
      while ($i < $l)
      {
        if ($this->state==S_NONE)
        {
          $ch = $code{$i};
          if ($i+1 < $l)
            $next_ch = $code{$i+1};
          else
            $next_ch = '';
          if ($ch=='-' and $next_ch=='-')
          {
            $this->state=S_COMMENT1;
            $out = $ch.$next_ch;
            $i++;
            $i++;
          }
          else if ($ch=='/' and $next_ch=='*')
          {
            $this->state=S_COMMENT2;
            $out=$ch.$next_ch;
            $i++;
            $i++;
          }
          else if (is_identifier_open($ch))
          {
            $this->state=S_KEYWORD;
            $out=$ch;
            $i++;
          }
          else if ($ch=='\'')
          {
            $this->state=S_STRING;
      $out=$ch;
      $i++;
          }
          else
          {
            $out=$ch;
          }
    $this->open_state=$this->state;
    $this->close_state=S_NONE;
        }

                          if ($this->state!=S_NONE)
  {
          switch ($this->state)
          {
            case S_COMMENT1:
              $j=strpos($code,"\n",$i);
              if ($j===false)
                $j=$l-1;
        else
              $this->close_state=$this->state;
              $out.=substr($code, $i, $j - $i + 1);
              $i=$j;
              break;
            case S_COMMENT2:
              $j=strpos($code,'*/',$i);
              if ($j===false)
                $j = $l - 1;
        else
                $this->close_state=$this->state;
              $out.=substr($code, $i, $j + 1 - $i + 1);
              $i=$j + 1;
              break;
            case S_KEYWORD:
            {
              $j= $i;
              while ($j < $l)
              {
                if (!is_identifier($code{$j}))
                  break;
                $j++;
              }
              $this->close_state=$this->state;//close if string breaked
              $out.=substr($code, $i, $j - $i);
              $i=$j - 1;
              if (!$this->keywords->found($out))
                    {
                      $this->state=S_NONE;
                      $this->open_state=S_NONE;
                      $this->close_state=S_NONE;
                    }
              break;
            }
            case S_STRING:
            {
              $j = $i;
              while ($j < $l)
              {
                if ($code{$j}=='\'')
                {
                 $this->close_state=$this->state;
                 break;
                }
                $j++;
              }
              $out.=substr($code, $i, $j - $i + 1);
              $i = $j;
              break;
            }
          }
        }

        $this->text_out($out);
        $i++;
      }
    }
Пример #3
0
    function highlight(&$code)
    {
      $ch = '';
      $next_ch = '';
      $l=strlen($code);
      $out='';
      $i=0;
      while ($i < $l)
      {
        if ($this->state==S_NONE)
        {
          $ch = $code{$i};
          if ($i+1 < $l)
            $next_ch = $code{$i+1};
          else
            $next_ch = '';

          if ($ch=='/' and $next_ch=='/')
          {
            $this->state=S_COMMENT1;
            $out=$ch.$next_ch;
            $i++;
            $i++;
          }
          else if ($ch=='/' and $next_ch=='*')
          {
            $this->state=S_COMMENT2;
            $out=$ch.$next_ch;
            $i++;
            $i++;
          }
          else if (is_identifier_open($ch))
          {
            $this->state = S_KEYWORD;
            $out = $ch;
            $i++;
          }
          else if ($ch=='#')
          {
            $this->state = S_DIRECTIVE;
            $out = $ch;
            $i++;
          }
          else if ($ch=='\'')
          {
            $this->state=S_STRING;
            $out=$ch;
            $i++;
          }
          else if ($ch=='"')
          {
            $this->state=S_STRING2;
            $out=$ch;
            $i++;
          }
          else
          {
            $out=$ch;
          }
          $this->open_state=$this->state;
          $this->close_state = S_NONE;
        }

        if ($this->state != S_NONE)
        {
          switch ($this->state)
          {
            case S_COMMENT1:
              $j=strpos($code,"\n",$i);
              if ($j===false)
                $j=$l-1;
              else
                $this->close_state=$this->state;
              $out.=substr($code, $i, $j - $i + 1);
              $i=$j;
              break;
            case S_COMMENT2:
              $j=strpos($code,'*/',$i);
              if ($j===false)
                $j = $l - 1;
              else
                $this->close_state=$this->state;
              $out.=substr($code, $i, $j + 1 - $i + 1);
              $i=$j + 1;
              break;

            case S_KEYWORD:
            {
              $this->process_std_identifier($i, $l, $code, $this->keywords, $out);
              break;
            }

            case S_DIRECTIVE:
            {
              $this->process_std_identifier($i, $l, $code, $this->directives, $out);
              break;
            }

            case S_STRING:
            {
              $j=$i;
              while ($j < $l)
              {
                if ($code{$j}=="\\")
                  $j++;
                else if ($code{$j} == "\n") //break if eol
                  break;
                else if ($code{$j} == '\'')
                  break;
                $j++;
              }
              $this->close_state = $this->state;
              $out.=substr($code, $i, $j - $i + 1);
              $i=$j;
              break;
            }
            case S_STRING2:
            {
              $j = $i;
              while ($j < $l)
              {
                if ($code{$j}=="\\" and !(($j + 1 < $l) and $code{$j} == "\n"))//escape but not multiline
                  $j++;
                else if ($code{$j} == "\n") //break if eol
                  break;
                else if ($code{$j}=='"')
                  break;
                $j++;
              }
              $this->close_state=$this->state;
              $out.=substr($code, $i, $j - $i + 1);
              $i=$j;
              break;
            }
          }
        }
        $this->text_out($out);
        $i++;
      }
    }