Skip to content

Commit

Permalink
Merge pull request #466 from kamil-tekiela/merge-else-if
Browse files Browse the repository at this point in the history
Merge else if
  • Loading branch information
MauricioFauth committed Jun 7, 2023
2 parents 332054f + 91f820c commit d42b402
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
18 changes: 8 additions & 10 deletions src/Components/CaseExpression.php
Expand Up @@ -156,16 +156,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
break 2;
}
}
} else {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $newResult;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $newResult;
} elseif ($token->type === Token::TYPE_KEYWORD) {
$parser->error('Unexpected keyword.', $token);
break;
}
} elseif ($state === 2) {
if ($type === 0) {
Expand Down
24 changes: 11 additions & 13 deletions src/Parser.php
Expand Up @@ -494,21 +494,19 @@ public function parse(): void
}

$list->idx = $lastIdx;
} else {
} elseif (empty(static::$statementParsers[$token->keyword])) {
// Checking if it is a known statement that can be parsed.
if (empty(static::$statementParsers[$token->keyword])) {
if (! isset(static::$statementParsers[$token->keyword])) {
// A statement is considered recognized if the parser
// is aware that it is a statement, but it does not have
// a parser for it yet.
$this->error('Unrecognized statement type.', $token);
}

// Skipping to the end of this statement.
$list->getNextOfType(Token::TYPE_DELIMITER);
$prevLastIdx = $list->idx;
continue;
if (! isset(static::$statementParsers[$token->keyword])) {
// A statement is considered recognized if the parser
// is aware that it is a statement, but it does not have
// a parser for it yet.
$this->error('Unrecognized statement type.', $token);
}

// Skipping to the end of this statement.
$list->getNextOfType(Token::TYPE_DELIMITER);
$prevLastIdx = $list->idx;
continue;
}

/**
Expand Down
36 changes: 16 additions & 20 deletions src/Utils/Formatter.php
Expand Up @@ -528,28 +528,24 @@ public function formatList($list)

// Finishing the line.
if ($lineEnded) {
$ret .= $this->options['line_ending']
. str_repeat($this->options['indentation'], (int) $indent);

$ret .= $this->options['line_ending'] . str_repeat($this->options['indentation'], (int) $indent);
$lineEnded = false;
} else {
// If the line ended there is no point in adding whitespaces.
} elseif (
$prev->keyword === 'DELIMITER'
|| ! (
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
// No space after . (
|| ($curr->type === Token::TYPE_OPERATOR
&& ($curr->value === '.' || $curr->value === ','
|| $curr->value === '(' || $curr->value === ')'))
// No space before . , ( )
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen((string) $curr->value, 'UTF-8') < 2
)
) {
// If the line ended, there is no point in adding whitespaces.
// Also, some tokens do not have spaces before or after them.
if (
// A space after delimiters that are longer than 2 characters.
$prev->keyword === 'DELIMITER'
|| ! (
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
// No space after . (
|| ($curr->type === Token::TYPE_OPERATOR
&& ($curr->value === '.' || $curr->value === ','
|| $curr->value === '(' || $curr->value === ')'))
// No space before . , ( )
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen((string) $curr->value, 'UTF-8') < 2
)
) {
$ret .= ' ';
}
// A space after delimiters that are longer than 2 characters.
$ret .= ' ';
}
}

Expand Down

0 comments on commit d42b402

Please sign in to comment.