Skip to content

Commit

Permalink
Merge pull request #559 from phel-lang/feat/simplify-readExpression-m…
Browse files Browse the repository at this point in the history
…ethod

Replace switch by match in Parser->readExpression()
  • Loading branch information
JesusValera committed Jan 29, 2023
2 parents eda6988 + 86fbb83 commit a7738ab
Showing 1 changed file with 40 additions and 49 deletions.
89 changes: 40 additions & 49 deletions src/php/Compiler/Domain/Parser/Parser.php
Expand Up @@ -22,8 +22,18 @@
use Phel\Compiler\Domain\Parser\ParserNode\StringNode;
use Phel\Compiler\Domain\Parser\ParserNode\WhitespaceNode;

use function in_array;

final class Parser implements ParserInterface
{
private const TOKENS_THAT_SHOULD_STREAM_NEXT = [
Token::T_WHITESPACE,
Token::T_NEWLINE,
Token::T_COMMENT,
Token::T_ATOM,
Token::T_STRING,
];

private ExpressionParserFactoryInterface $parserFactory;
private GlobalEnvironmentInterface $globalEnvironment;

Expand Down Expand Up @@ -78,57 +88,33 @@ public function readExpression(TokenStream $tokenStream): NodeInterface
if ($tokenStream->valid()) {
$token = $tokenStream->current();

switch ($token->getType()) {
case Token::T_WHITESPACE:
$tokenStream->next();
return WhitespaceNode::createWithToken($token);

case Token::T_NEWLINE:
$tokenStream->next();
return NewlineNode::createWithToken($token);

case Token::T_COMMENT:
$tokenStream->next();
return CommentNode::createWithToken($token);

case Token::T_ATOM:
$tokenStream->next();
return $this->parseAtomNode($token, $tokenStream);

case Token::T_STRING:
$tokenStream->next();
return $this->parseStringNode($token, $tokenStream);

case Token::T_FN:
case Token::T_OPEN_PARENTHESIS:
return $this->parseFnListNode($token, $tokenStream);

case Token::T_OPEN_BRACKET:
return $this->parseArrayListNode($token, $tokenStream);

case Token::T_OPEN_BRACE:
return $this->parseMapListNode($token, $tokenStream);
$tokenType = $token->getType();

case Token::T_CLOSE_PARENTHESIS:
case Token::T_CLOSE_BRACKET:
case Token::T_CLOSE_BRACE:
throw $this->createUnexceptedParserException($tokenStream, $token, 'Unterminated list (BRACKETS)');

case Token::T_UNQUOTE_SPLICING:
case Token::T_UNQUOTE:
case Token::T_QUASIQUOTE:
case Token::T_QUOTE:
return $this->parseQuoteNode($token, $tokenStream);

case Token::T_CARET:
return $this->parseMetaNode($tokenStream);

case Token::T_EOF:
throw $this->createUnfinishedParserException($tokenStream, $token, 'Unterminated list (EOF)');

default:
throw $this->createUnexceptedParserException($tokenStream, $token, 'Unhandled syntax token: ' . $token->getCode());
if ($this->shouldTokenStreamGoNext($tokenType)) {
$tokenStream->next();
}

return match ($tokenType) {
Token::T_WHITESPACE => WhitespaceNode::createWithToken($token),
Token::T_NEWLINE => NewlineNode::createWithToken($token),
Token::T_COMMENT => CommentNode::createWithToken($token),
Token::T_ATOM => $this->parseAtomNode($token, $tokenStream),
Token::T_STRING => $this->parseStringNode($token, $tokenStream),
Token::T_FN,
Token::T_OPEN_PARENTHESIS => $this->parseFnListNode($token, $tokenStream),
Token::T_OPEN_BRACKET => $this->parseArrayListNode($token, $tokenStream),
Token::T_OPEN_BRACE => $this->parseMapListNode($token, $tokenStream),
Token::T_CLOSE_PARENTHESIS,
Token::T_CLOSE_BRACKET,
Token::T_CLOSE_BRACE => throw $this->createUnexceptedParserException($tokenStream, $token, 'Unterminated list (BRACKETS)'),
Token::T_UNQUOTE_SPLICING,
Token::T_UNQUOTE,
Token::T_QUASIQUOTE,
Token::T_QUOTE => $this->parseQuoteNode($token, $tokenStream),
Token::T_CARET => $this->parseMetaNode($tokenStream),
Token::T_EOF => throw $this->createUnfinishedParserException($tokenStream, $token, 'Unterminated list (EOF)'),
default => throw $this->createUnexceptedParserException($tokenStream, $token, 'Unhandled syntax token: ' . $token->getCode()),
};
}

// Throw exception differently because we may have not $token
Expand All @@ -141,6 +127,11 @@ public function readExpression(TokenStream $tokenStream): NodeInterface
);
}

private function shouldTokenStreamGoNext(int $tokenType): bool
{
return in_array($tokenType, self::TOKENS_THAT_SHOULD_STREAM_NEXT, true);
}

private function canParseToken(TokenStream $tokenStream): bool
{
return $tokenStream->valid()
Expand Down

0 comments on commit a7738ab

Please sign in to comment.