示例#1
0
 /**
  * Try to match all acceptable strings to the corresponding tokens
  */
 public function testTokenMatching()
 {
     $validStringToTokenArray = ['(' => 'T_BRACKET_OPEN', ')' => 'T_BRACKET_CLOSE', "!=" => 'T_OP_NEQ', "not" => 'T_OP_NEQ', "is not" => 'T_OP_NEQ', "<=" => 'T_OP_LTE', ">=" => 'T_OP_GTE', "<" => 'T_OP_LT', "less than" => 'T_OP_LT', "before" => 'T_OP_LT', "=" => 'T_OP_EQ', "equals" => 'T_OP_EQ', "on" => 'T_OP_EQ', "is" => 'T_OP_EQ', ">" => 'T_OP_GT', "more than" => 'T_OP_GT', "after" => 'T_OP_GT', "in" => 'T_OP_IN', "and" => 'T_LOGIC_AND', "or" => 'T_LOGIC_OR', "xor" => 'T_LOGIC_XOR', "'hello there 123 {}'" => 'T_LITERAL', '"this is a random . = string"' => 'T_LITERAL', "someIndentifier" => 'T_IDENTIFIER', "aRandomThingWith_underscores" => 'T_IDENTIFIER', "isNotAnEqualsOperator" => 'T_IDENTIFIER', "falseIsNotActuallyFalse" => 'T_IDENTIFIER', " " => 'T_WHITESPACE', "      " => 'T_WHITESPACE', "\n" => 'T_WHITESPACE', "\t" => 'T_WHITESPACE'];
     $invalidStringToTokenArray = ['\'this literal has mismatching quotes"'];
     // Try matching the valid ones
     foreach ($validStringToTokenArray as $string => $token) {
         $tokenResult = Lexer::matchToken($string, 0);
         $this->assertEquals($token, $tokenResult['token'], "Failed to match '{$string}' to token {$token}");
     }
     // Try matching them all again in uppercase
     foreach ($validStringToTokenArray as $string => $token) {
         $string = strtoupper($string);
         $tokenResult = \Netdudes\DataSourceryBundle\UQL\Lexer::matchToken($string, 0);
         $this->assertEquals($token, $tokenResult['token'], "Failed to match '{$string}' to token {$token}");
     }
     // Try failing to math the invalid ones
     foreach ($invalidStringToTokenArray as $string) {
         $this->assertFalse(Lexer::matchToken($string, 0), "Lexer should return false for string [{$string}]");
     }
 }
示例#2
0
 /**
  * Lex, initialise and return the AST.
  *
  * @param $string
  *
  * @return bool|ASTAssertion|ASTGroup
  */
 public function parse($string)
 {
     $this->tokenStream = Lexer::lex($string);
     $this->tokenIndex = -1;
     return $this->getAST();
 }
示例#3
0
 /**
  * Tests the functionality of the array matcher
  */
 public function testMatchArray()
 {
     $testArrays = ["[1, 2, 3, 4, 5]" => [1, 2, 3, 4, 5], "[1, \"abc\", 2, \"def\"]" => [1, "\"abc\"", 2, "\"def\""]];
     $parser = new Parser();
     foreach ($testArrays as $testArray => $expectedResult) {
         $tokenStream = Lexer::lex($testArray);
         $parser->setTokenStream($tokenStream);
         $parser->setTokenIndex(-1);
         $array = $parser->matchArray();
         $this->assertNotEquals($array, false, 'Array should not be false (meaning it did interpret an array)');
         $this->assertTrue($array instanceof ASTArray, 'Arrays should Parse into ASTArrays');
         $this->assertCount(count($expectedResult), $array->getElements(), 'Array doesn\'t match the expected number of items');
         foreach ($array->getElements() as $index => $element) {
             $this->assertEquals($expectedResult[$index], $element, "Element '{$element}' on index {$index} doesn't match the expected {$expectedResult[$index]}");
         }
     }
 }