Exemple #1
0
 private static function validateTokens($tokenStream)
 {
     // No return value --- throws exception when error encountered.
     $stack = array();
     for ($i = 0; $i < count($tokenStream); $i++) {
         $node = $tokenStream[$i];
         switch ($node->type) {
             case ExpType::$TERNARY:
                 // drop through
             // drop through
             case ExpType::$PAREN:
                 // Check balance
                 if (in_array($node->value, ExpLexer::$ENDS)) {
                     $stackLeft = array_pop($stack);
                     if (ExpLexer::$PAIRS[$node->value] != $stackLeft) {
                         throw new ExpLexerException("Unbalanced expression");
                     }
                 } else {
                     array_push($stack, $node->value);
                 }
                 break;
             case ExpType::$UNARY_OP:
                 // Check unary operator gramma
                 if (!ExpLexer::hasUnaryLeft($tokenStream, $i)) {
                     throw new ExpLexerException("Mal-format unary operator");
                 }
                 break;
             case ExpType::$BINARY_OP:
                 // Check binary operator gramma
                 if (ExpLexer::hasUnaryLeft($tokenStream, $i) || !ExpLexer::hasBinaryRight($tokenStream, $i)) {
                     throw new ExpLexerException("Mal-format binary operator");
                 }
                 break;
             case ExpType::$DOT:
                 // Check dot gramma
                 if ($i == count($tokenStream) - 1 || $tokenStream[$i + 1]->type != ExpType::$IDENTITY) {
                     throw new ExpLexerException("Mal-format dot");
                 }
                 break;
             case ExpType::$FUNCTION:
                 // Check function gramma
                 if ($i == count($tokenStream) - 1 || $tokenStream[$i + 1]->value != '(') {
                     throw new ExpLexerException("Mal-format function");
                 }
                 break;
         }
     }
     if (count($stack) != 0) {
         throw new ExpLexerException("Unbalanced expression");
     }
 }