Ejemplo n.º 1
0
 /**
  * Parses a parameter of a routine.
  *
  * @param string $param Parameter's definition.
  *
  * @return array
  */
 public function getReturnType($param)
 {
     $lexer = new Lexer($param);
     // A dummy parser is used for error reporting.
     $type = DataTypeFragment::parse(new Parser(), $lexer->list);
     if ($type === null) {
         return array('', '', '', '', '');
     }
     $options = array();
     foreach ($type->options->options as $opt) {
         $options[] = is_string($opt) ? $opt : $opt['value'];
     }
     return array('', '', $type->name, implode(',', $type->parameters), implode(' ', $options));
 }
Ejemplo n.º 2
0
 /**
  * @param Parser     $parser  The parser that serves as context.
  * @param TokensList $list    The list of tokens that are being parsed.
  * @param array      $options Parameters for parsing.
  *
  * @return ParamDefFragment[]
  */
 public static function parse(Parser $parser, TokensList $list, array $options = array())
 {
     $ret = array();
     $expr = new ParamDefFragment();
     /**
      * The state of the parser.
      *
      * Below are the states of the parser.
      *
      *      0 -----------------------[ ( ]------------------------> 1
      *
      *      1 ----------------[ IN / OUT / INOUT ]----------------> 1
      *      1 ----------------------[ name ]----------------------> 2
      *
      *      2 -------------------[ data type ]--------------------> 3
      *
      *      3 ------------------------[ , ]-----------------------> 1
      *      3 ------------------------[ ) ]-----------------------> -1
      *
      * @var int
      */
     $state = 0;
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          * @var Token
          */
         $token = $list->tokens[$list->idx];
         // End of statement.
         if ($token->type === Token::TYPE_DELIMITER) {
             break;
         }
         // Skipping whitespaces and comments.
         if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) {
             continue;
         }
         if ($state === 0) {
             if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
                 $state = 1;
             }
             continue;
         } elseif ($state === 1) {
             if ($token->value === 'IN' || $token->value === 'OUT' || $token->value === 'INOUT') {
                 $expr->inOut = $token->value;
                 ++$list->idx;
             } elseif ($token->value === ')') {
                 ++$list->idx;
                 break;
             } else {
                 $expr->name = $token->value;
                 $state = 2;
             }
         } elseif ($state === 2) {
             $expr->type = DataTypeFragment::parse($parser, $list);
             $state = 3;
         } elseif ($state === 3) {
             $ret[] = $expr;
             $expr = new ParamDefFragment();
             if ($token->value === ',') {
                 $state = 1;
                 continue;
             } elseif ($token->value === ')') {
                 ++$list->idx;
                 break;
             }
         }
     }
     // Last iteration was not saved.
     if (!empty($expr->name)) {
         $ret[] = $expr;
     }
     --$list->idx;
     return $ret;
 }
Ejemplo n.º 3
0
 /**
  * Parsing the `CREATE` statement.
  *
  * @param Parser     $parser The instance that requests parsing.
  * @param TokensList $list   The list of tokens to be parsed.
  * @param Token      $token  The token that is being parsed.
  *
  * @return void
  */
 public function before(Parser $parser, TokensList $list, Token $token)
 {
     ++$list->idx;
     $this->name = CreateDefFragment::parse($parser, $list);
     if ($this->options->has('TABLE')) {
         ++$list->idx;
         $this->fields = FieldDefFragment::parse($parser, $list);
         ++$list->idx;
         $this->entityOptions = OptionsFragment::parse($parser, $list, CreateDefFragment::$TABLE_OPTIONS);
     } elseif ($this->options->has('PROCEDURE') || $this->options->has('FUNCTION')) {
         ++$list->idx;
         $this->parameters = ParamDefFragment::parse($parser, $list);
         if ($this->options->has('FUNCTION')) {
             $token = $list->getNextOfType(Token::TYPE_KEYWORD);
             if ($token->value !== 'RETURNS') {
                 $parser->error('\'RETURNS\' keyword was expected.', $token);
             } else {
                 ++$list->idx;
                 $this->return = DataTypeFragment::parse($parser, $list);
             }
         }
         ++$list->idx;
         $this->entityOptions = OptionsFragment::parse($parser, $list, CreateDefFragment::$FUNC_OPTIONS);
         ++$list->idx;
         $this->body = array();
         for (; $list->idx < $list->count; ++$list->idx) {
             $token = $list->tokens[$list->idx];
             $this->body[] = $token;
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'END') {
                 break;
             }
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * @param Parser     $parser  The parser that serves as context.
  * @param TokensList $list    The list of tokens that are being parsed.
  * @param array      $options Parameters for parsing.
  *
  * @return FieldDefFragment[]
  */
 public static function parse(Parser $parser, TokensList $list, array $options = array())
 {
     $ret = array();
     $expr = new FieldDefFragment();
     /**
      * The state of the parser.
      *
      * Below are the states of the parser.
      *
      *      0 -----------------------[ ( ]------------------------> 1
      *
      *      1 --------------------[ CONSTRAINT ]------------------> 1
      *      1 -----------------------[ key ]----------------------> 2
      *      1 -------------[ constraint / column name ]-----------> 2
      *
      *      2 --------------------[ data type ]-------------------> 3
      *
      *      3 ---------------------[ options ]--------------------> 4
      *
      *      4 --------------------[ REFERENCES ]------------------> 4
      *
      *      5 ------------------------[ , ]-----------------------> 1
      *      5 ------------------------[ ) ]-----------------------> -1
      *
      * @var int
      */
     $state = 0;
     for (; $list->idx < $list->count; ++$list->idx) {
         /**
          * Token parsed at this moment.
          * @var Token
          */
         $token = $list->tokens[$list->idx];
         // End of statement.
         if ($token->type === Token::TYPE_DELIMITER) {
             break;
         }
         // Skipping whitespaces and comments.
         if ($token->type === Token::TYPE_WHITESPACE || $token->type === Token::TYPE_COMMENT) {
             continue;
         }
         if ($state === 0) {
             if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
                 $state = 1;
             }
         } elseif ($state === 1) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'CONSTRAINT') {
                 $expr->isConstraint = true;
             } elseif ($token->type === Token::TYPE_KEYWORD && $token->flags & Token::FLAG_KEYWORD_KEY) {
                 $expr->key = KeyFragment::parse($parser, $list);
                 $state = 4;
             } else {
                 $expr->name = $token->value;
                 if (!$expr->isConstraint) {
                     $state = 2;
                 }
             }
         } elseif ($state === 2) {
             $expr->type = DataTypeFragment::parse($parser, $list);
             $state = 3;
         } elseif ($state === 3) {
             $expr->options = OptionsFragment::parse($parser, $list, static::$FIELD_OPTIONS);
             $state = 4;
         } elseif ($state === 4) {
             if ($token->type === Token::TYPE_KEYWORD && $token->value === 'REFERENCES') {
                 ++$list->idx;
                 // Skipping keyword 'REFERENCES'.
                 $expr->references = ReferencesKeyword::parse($parser, $list);
             } else {
                 --$list->idx;
             }
             $state = 5;
         } else {
             if ($state === 5) {
                 if (!empty($expr->type) || !empty($expr->key)) {
                     $ret[] = $expr;
                 }
                 $expr = new FieldDefFragment();
                 if ($token->value === ',') {
                     $state = 1;
                     continue;
                 } elseif ($token->value === ')') {
                     ++$list->idx;
                     break;
                 }
             }
         }
     }
     // Last iteration was not saved.
     if (!empty($expr->type) || !empty($expr->key)) {
         $ret[] = $expr;
     }
     --$list->idx;
     return $ret;
 }