Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Yachting\Controller\Api\v2;
  4.  
  5. use stdClass;
  6.  
  7. use Yachting\Controller\Api\v2\TokenType as TokenType;
  8.  
  9. class Compiler
  10. {
  11.     private $error;
  12.     private $str;
  13.  
  14.     public function __construct($tokens = [], $length = 0)
  15.     {
  16.         $this->error = new stdClass;
  17.         $this->error->flag = false;
  18.         $this->error->number = 0;
  19.         $this->error->message = '';
  20.  
  21.         if($length < 5) {
  22.             $this->setCompileError(1, 'Unorganized syntax'); return $this->error;
  23.         }
  24.  
  25.         unset($tokens[0]);
  26.         unset($tokens[$length - 1]);
  27.  
  28.         $length -= 2;
  29.  
  30.         $this->checkSyntax($tokens, $length);
  31.         $this->compile($tokens, $length);
  32.     }
  33.  
  34.     // [something>=20-02-10^level=200|exp>100]
  35.     // query: alphanumeric -> (=,>,>=,<,<=) -> alphanumeric
  36.     // queries: query (^,|) query (^,|) query ...
  37.     //
  38.     // 1, 2, 3 |m| 4, 5, 6
  39.     //
  40.     // 0  1  2  3  4  5  6  7  8  9  10
  41.     // 1, 2, 3 |m| 4, 5, 6 |m| 7, 8, 9
  42.     //
  43.     private function checkSyntax($tokens, $length)
  44.     {
  45.         if($length % 2 == 0) {
  46.             $this->setCompileError(1, 'Unorganized syntax'); return $this->error;
  47.         }
  48.  
  49.         $i = 1;
  50.         while($length)
  51.         {
  52.             if($i == $length) break;
  53.  
  54.             if($tokens[$i]->cat != $tokens[$length]->cat)
  55.             {
  56.                 $this->setCompileError(2, "Syntax error in " . $tokens[$i]->type . " token " . $tokens[$i]->literal);
  57.                 return $this->error;
  58.             }
  59.  
  60.             $i++; $length--;
  61.         }
  62.     }
  63.  
  64.     private function compile($tokens, $length)
  65.     {
  66.         $this->str = '';
  67.  
  68.         for($i = 1; $i <= $length; $i++)
  69.         {
  70.            $this->str .= ' ' . $tokens[$i]->literal . ' ';
  71.         }
  72.  
  73.         return $this->str;
  74.     }
  75.  
  76.     public function getCompileError()
  77.     {
  78.         return $this->error;
  79.     }
  80.  
  81.     public function getCompiledStr()
  82.     {
  83.         return $this->str;
  84.     }
  85.  
  86.     private function setCompileError($code = 0, $message = '')
  87.     {
  88.         $this->error->flag = true;
  89.         $this->error->code = $code;
  90.         $this->error->message = $message;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement