[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Webservices/ -> VTQL_Lexer.php (source)

   1  <?php
   2  global $where_col,$orderby,$in_started,$count;
   3  $where_col = false;
   4  $orderby = false;
   5  $in_started = false;
   6  $count = false;
   7  function incrementN($lexer, $count){
   8  $i = 0;
   9  for(;$i<$count;$i++){
  10  incState($lexer);
  11  }
  12  }
  13  function incState($lexer){
  14  $lexer->current_state++;
  15  if($lexer->current_state === sizeof($lexer->mandatory_states)){
  16  $lexer->mandatory = false;
  17  }
  18  }
  19  function handleselect($lexer, $val){
  20  if($lexer->mandatory){
  21  if(strcasecmp($val, $lexer->mandatory_states[$lexer->current_state])===0){
  22  incState($lexer);
  23  return VTQL_Parser::SELECT;
  24  }
  25  }
  26  }
  27  function handlecolumn_list($lexer, $val){
  28  global $count;
  29  if($lexer->mandatory){
  30  if(!(strcasecmp($val, $lexer->mandatory_states[2])===0)){
  31  if(strcmp($val, "*")===0){
  32  if(!$count){
  33  incrementN($lexer, 1);
  34  }
  35  return VTQL_Parser::ASTERISK;
  36  }else if((strcmp($val, "(")===0)){
  37  return VTQL_Parser::PARENOPEN;
  38  }else if(strcmp($val, ")")===0){
  39  return VTQL_Parser::PARENCLOSE;
  40  }else if((strcasecmp($val, "count")===0)){
  41  $count = true;
  42  return VTQL_Parser::COUNT;
  43  }else if(strcmp($val, ",")===0){
  44  return VTQL_Parser::COMMA;
  45  }else{
  46  return VTQL_Parser::COLUMNNAME;
  47  }
  48  }else{
  49  incrementN($lexer, 2);
  50  return VTQL_Parser::FRM;
  51  }
  52  }
  53  }
  54  function handlefrom($lexer, $val){
  55  if((strcasecmp($val, $lexer->mandatory_states[$lexer->current_state])===0)){
  56  incState($lexer);
  57  return VTQL_Parser::FRM;
  58  }
  59  }
  60  function handletable($lexer, $val){
  61  if($lexer->mandatory){
  62  $lexer->current_state =0;
  63  $lexer->mandatory = false;
  64  if(!(strcasecmp($val, $lexer->optional_states[$lexer->current_state])===0)){
  65  return VTQL_Parser::TABLENAME;
  66  }
  67  }
  68  }
  69  function handlewhere($lexer, $val){
  70  global $where_col,$in_started;
  71  $val = trim($val);
  72  if((strcmp($val, "=")===0)){
  73  return VTQL_Parser::EQ;
  74  }else if((strcasecmp($val, $lexer->optional_states[$lexer->current_state])===0)){
  75  return VTQL_Parser::WHERE;
  76  }else if((strcmp($val, "<")===0)){
  77  return VTQL_Parser::LT;
  78  }else if((strcmp($val, "<=")===0)){
  79  return VTQL_Parser::LTE;
  80  }else if((strcmp($val, ">=")===0)){
  81  return VTQL_Parser::GTE;
  82  }else if((strcmp($val, "!=")===0)){
  83  return VTQL_Parser::NE;
  84  }else if((strcmp($val, ">")===0)){
  85  return VTQL_Parser::GT;
  86  }else if((strcmp($val, "(")===0)){
  87  return VTQL_Parser::PARENOPEN;
  88  }else if((strcmp($val, ")")===0)){
  89  if($in_started){
  90  $in_started = false;
  91  $where_col = false;
  92  }
  93  return VTQL_Parser::PARENCLOSE;
  94  }else if((strcasecmp($val, "and")===0)){
  95  return VTQL_Parser::LOGICAL_AND;
  96  }else if((strcasecmp($val, "or")===0)){
  97  return VTQL_Parser::LOGICAL_OR;
  98  }else if(!$where_col){
  99  $where_col = true;
 100  return VTQL_Parser::COLUMNNAME;
 101  }else if((strcasecmp($val, "in")===0)){
 102  $in_started = true;
 103  return VTQL_Parser::IN;
 104  }else if(strcmp($val, ",")===0){
 105  return VTQL_Parser::COMMA;
 106  }else if(strcasecmp($val, "like")===0){
 107  return VTQL_Parser::LIKE;
 108  }else if($where_col){
 109  if(!$in_started){
 110  $where_col = false;
 111  }
 112  return VTQL_Parser::VALUE;
 113  }
 114  }
 115  function handleorderby($lexer, $val){
 116  global $orderby;
 117  if(!$orderby){
 118      $orderby = true;
 119      return VTQL_Parser::ORDERBY;
 120  }
 121  if(strcmp($val, ",")===0){
 122  return VTQL_Parser::COMMA;
 123  }else if(strcasecmp($val, "asc")===0){
 124  return VTQL_Parser::ASC;
 125  }else if(strcasecmp($val, "desc")===0){
 126  return VTQL_Parser::DESC;
 127  }else{
 128  return VTQL_Parser::COLUMNNAME;
 129  }
 130  }
 131  function handlelimit($lexer, $val){
 132  if((strcasecmp($val, "limit")===0)){
 133  return VTQL_Parser::LIMIT;
 134  }else if((strcmp($val, "(")===0)){
 135  return VTQL_Parser::PARENOPEN;
 136  }else if((strcmp($val, ")")===0)){
 137  return VTQL_Parser::PARENCLOSE;
 138  }else if(strcmp($val, ",")===0){
 139  return VTQL_Parser::COMMA;
 140  }else{
 141  return VTQL_Parser::VALUE;
 142  }
 143  }
 144  function handleend($lexer, $val){
 145  return VTQL_Parser::SEMICOLON;
 146  }
 147  class VTQL_Lexer{
 148  private $index;
 149  public $token;
 150  public $value;
 151  public $linenum;
 152  public $state = 1;
 153  private $data;
 154  public $mandatory_states = array('select','column_list','from','table');
 155  public $optional_states = array('where', 'orderby', 'limit');
 156  public $mandatory ;
 157  public $current_state ;
 158  function __construct($data)
 159  {
 160  $this->index = 0;
 161  $this->data = $data;
 162  $this->linenum = 1;
 163  $this->mandatory = true;
 164  $this->current_state = 0;
 165  }
 166  function __toString(){
 167  return $this->token."";
 168  }
 169  
 170      private $_yy_state = 1;
 171      private $_yy_stack = array();
 172  
 173      function yylex()
 174      {
 175          return $this->{'yylex' . $this->_yy_state}();
 176      }
 177  
 178      function yypushstate($state)
 179      {
 180          array_push($this->_yy_stack, $this->_yy_state);
 181          $this->_yy_state = $state;
 182      }
 183  
 184      function yypopstate()
 185      {
 186          $this->_yy_state = array_pop($this->_yy_stack);
 187      }
 188  
 189      function yybegin($state)
 190      {
 191          $this->_yy_state = $state;
 192      }
 193  
 194  
 195  
 196      function yylex1()
 197      {
 198          $tokenMap = array (
 199                1 => 2,
 200                4 => 0,
 201              );
 202          if ($this->index >= strlen($this->data)) {
 203              return false; // end of input
 204          }
 205          $yy_global_pattern = "/^((\\w+|'(?:[^']|'')+'|\\(|\\)|(\\+|-)?\\d+|,|\\*|(?!<|>)=|<(?!=)|>(?!=)|<=|>=|!=|;))|^([ \t\r\n]+)/";
 206  
 207          do {
 208              if (preg_match($yy_global_pattern, substr($this->data, $this->index), $yymatches)) {
 209                  $yysubmatches = $yymatches;
 210                  $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
 211                  if (!count($yymatches)) {
 212                      throw new Exception('Error: lexing failed because a rule matched' .
 213                          'an empty string.  Input "' . substr($this->data,
 214                          $this->index, 5) . '... state INITR');
 215                  }
 216                  next($yymatches); // skip global match
 217                  $this->token = key($yymatches); // token number
 218                  if ($tokenMap[$this->token]) {
 219                      // extract sub-patterns for passing to lex function
 220                      $yysubmatches = array_slice($yysubmatches, $this->token + 1,
 221                          $tokenMap[$this->token]);
 222                  } else {
 223                      $yysubmatches = array();
 224                  }
 225                  $this->value = current($yymatches); // token value
 226                  $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
 227                  if ($r === null) {
 228                      $this->index += strlen($this->value);
 229                      $this->linenum += substr_count("\n", $this->value);
 230                      // accept this token
 231                      return true;
 232                  } elseif ($r === true) {
 233                      // we have changed state
 234                      // process this token in the new state
 235                      return $this->yylex();
 236                  } elseif ($r === false) {
 237                      $this->index += strlen($this->value);
 238                      $this->linenum += substr_count("\n", $this->value);
 239                      if ($this->index >= strlen($this->data)) {
 240                          return false; // end of input
 241                      }
 242                      // skip this token
 243                      continue;
 244                  } else {                    $yy_yymore_patterns = array(
 245          1 => "^([ \t\r\n]+)",
 246          4 => "",
 247      );
 248  
 249                      // yymore is needed
 250                      do {
 251                          if (!strlen($yy_yymore_patterns[$this->token])) {
 252                              throw new Exception('cannot do yymore for the last token');
 253                          }
 254                          if (preg_match($yy_yymore_patterns[$this->token],
 255                                substr($this->data, $this->index), $yymatches)) {
 256                              $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
 257                              next($yymatches); // skip global match
 258                              $this->token = key($yymatches); // token number
 259                              $this->value = current($yymatches); // token value
 260                              $this->linenum = substr_count("\n", $this->value);
 261                          }
 262                      } while ($this->{'yy_r1_' . $this->token}() !== null);
 263                      // accept
 264                      $this->index += strlen($this->value);
 265                      $this->linenum += substr_count("\n", $this->value);
 266                      return true;
 267                  }
 268              } else {
 269                  throw new Exception('Unexpected input at line' . $this->linenum .
 270                      ': ' . $this->data[$this->index]);
 271              }
 272              break;
 273          } while (true);
 274      } // end function
 275  
 276  
 277      const INITR = 1;
 278      function yy_r1_1($yy_subpatterns)
 279      {
 280  
 281  global $orderby;
 282  //echo "<br> ql state: ",$this->current_state," ",$this->value,"<br>";
 283  if($this->mandatory){
 284  //echo "<br> ql state: ",$this->current_state," ",$this->value,"<br>";
 285  $handler = 'handle'.$this->mandatory_states[$this->current_state];
 286  $this->token = $handler($this, $this->value);
 287  }else{
 288  $str = $this->value;
 289  if(strcmp($this->value, ";")===0){
 290  $this->token = handleend($this, $this->value);
 291  return;
 292  }
 293  if(strcasecmp($this->value, "order")===0){
 294  $orderby = true;
 295  return false;
 296  }else if(strcasecmp($this->value, "by") ===0 && $orderby ===true){
 297  $orderby = false;
 298  $this->current_state = 1;
 299  }
 300  $index = array_search(strtolower($str), $this->optional_states, true);
 301  if($index !== false){
 302  $this->current_state = $index;
 303  }
 304  $handler = 'handle'.$this->optional_states[$this->current_state];
 305  $this->token = $handler($this, $this->value);
 306  }//$this->yypushstate($this->value);
 307      }
 308      function yy_r1_4($yy_subpatterns)
 309      {
 310  
 311  return false;
 312      }
 313  
 314  }
 315  
 316  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1