Exemple #1
0
 public function testMap()
 {
     $lex = new RegexLexer(true);
     $lex->operator = '[-+]';
     $lex->integer = '[0-9]+';
     $lex->map('integer', function ($value, $matches, $offset) {
         return intval($value) + 2;
     });
     $tokens = $lex('5-7');
     $this->assertCount(3, $tokens);
     $this->assertEquals('integer', $tokens[0][0]);
     $this->assertEquals('operator', $tokens[1][0]);
     $this->assertEquals('integer', $tokens[2][0]);
     $this->assertEquals(7, $tokens[0][1]);
     $this->assertEquals('-', $tokens[1][1]);
     $this->assertEquals(9, $tokens[2][1]);
     $this->assertEquals(0, $tokens[0][3]);
     $this->assertEquals(1, $tokens[1][3]);
     $this->assertEquals(2, $tokens[2][3]);
 }
Exemple #2
0
 /**
  * @param string $expression
  * @return ParseInput
  */
 public static function lex($expression, $vars)
 {
     $lexer = new RegexLexer(true, 'i');
     $lexer->is = 'is';
     $lexer->not = 'not';
     $lexer->bool = 'true|false';
     $lexer->null = 'null';
     $lexer->operator = 'like|in|!=|<>|>=|<=|!<|!>|=|<|>|and|or';
     $lexer->dot = '\\.';
     $lexer->name = '[a-z][a-z0-9]*';
     $lexer->model = '\\{(.+?)\\}';
     $lexer->modelPlaceholder = '%(model|m)';
     $lexer->field = '\\[(.+?)\\]';
     $lexer->fieldPlaceholder = '%(column|field|c)';
     $lexer->number = '-?(0|[1-9]\\d*)(\\.\\d+)?([eE][+-]?\\d+)?';
     $lexer->string = '"((?:[^"\\\\]|\\\\.)*)"';
     $lexer->placeholder = '((\\?)|%([a-z_\\\\]+))(\\(\\))?';
     $lexer->map('model', function ($value, $matches) {
         return $matches[1];
     });
     $lexer->map('field', function ($value, $matches) {
         return $matches[1];
     });
     $lexer->map('number', function ($value) {
         if (strpos($value, '.') !== false or stripos($value, 'e') !== false) {
             return new Literal(DataType::float(), floatval($value));
         } else {
             return new Literal(DataType::integer(), intval($value));
         }
     });
     $lexer->mapType('number', 'literal');
     $lexer->map('string', function ($value, $matches) {
         return new Literal(DataType::text(), stripslashes($matches[1]));
     });
     $lexer->mapType('string', 'literal');
     $lexer->map('bool', function ($value) {
         return new Literal(DataType::boolean(), strtolower($value) == 'true');
     });
     $lexer->mapType('bool', 'literal');
     $lexer->map('model', function ($value, $matches) {
         return $matches[1];
     });
     $lexer->map('field', function ($value, $matches) {
         return $matches[1];
     });
     $i = 0;
     $lexer->map('modelPlaceholder', function ($value, $matches) use(&$i, $vars) {
         $value = $vars[$i];
         $i++;
         if (!is_string($value)) {
             assume($value instanceof Model);
             $value = $value->getName();
         }
         return $value;
     });
     $lexer->mapType('modelPlaceholder', 'model');
     $lexer->map('fieldPlaceholder', function ($value, $matches) use(&$i, $vars) {
         $value = $vars[$i];
         $i++;
         assume(is_string($value));
         return $value;
     });
     $lexer->mapType('fieldPlaceholder', 'field');
     $lexer->map('placeholder', function ($value, $matches) use(&$i, $vars) {
         $value = $vars[$i];
         $i++;
         $type = null;
         if (isset($matches[3])) {
             if ($matches[3] == '_') {
                 if (!is_string($value)) {
                     assume($value instanceof DataType);
                     $value = $value->placeholder;
                 }
                 $matches[3] = ltrim($value, '%');
                 $value = $vars[$i];
                 $i++;
             }
             if ($matches[3] == 'e' or $matches[3] == 'expr' or $matches[3] == 'expression') {
                 assume($value instanceof Expression);
                 return $value;
             }
             if ($matches[3] != '()') {
                 $type = DataType::fromPlaceholder($matches[3]);
             }
         }
         if (!isset($type)) {
             $type = DataType::detectType($value);
         }
         if (isset($matches[4]) or isset($matches[3]) and $matches[3] == '()') {
             assume(is_array($value));
             foreach ($value as $key => $v) {
                 $value[$key] = $v;
             }
             return new ArrayLiteral($type, $value);
         }
         return new Literal($type, $value);
     });
     $lexer->mapType('placeholder', 'literal');
     return new ParseInput($lexer($expression));
 }