Skip to content

Commit

Permalink
Fixed __Parser_parseCondition consuming ( without actually making…
Browse files Browse the repository at this point in the history
… sure
  • Loading branch information
loumadev committed Nov 14, 2023
1 parent 1b55518 commit 7e06202
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/compiler/parser/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,21 @@ ParserResult __Parser_parseCondition(Parser *parser) {
ExpressionASTNode *expression = NULL;
OptionalBindingConditionASTNode *bindingCondition = NULL;

// consume '('
LexerResult result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

// consume '(' optionally
bool hasOptionalParen = false;
LexerResult peek = Lexer_peekToken(parser->lexer, 1);
if(!peek.success) return LexerToParserError(peek);

if(peek.token->kind == TOKEN_LEFT_PAREN) {
LexerResult result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

hasOptionalParen = true;
}

peek = Lexer_peekToken(parser->lexer, 1);
if(!peek.success) return LexerToParserError(peek);

if(peek.token->kind == TOKEN_LET || peek.token->kind == TOKEN_VAR) {
ParserResult bindingConditionResult = __Parser_parseOptionalBindingCondition(parser);
if(!bindingConditionResult.success) return bindingConditionResult;
Expand All @@ -474,10 +482,16 @@ ParserResult __Parser_parseCondition(Parser *parser) {
expression = (ExpressionASTNode*)expressionResult.node;
}

// consume ')'
result = Lexer_nextToken(parser->lexer);
// consume ')' if we consumed '('
LexerResult result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

if(hasOptionalParen && result.token->kind != TOKEN_RIGHT_PAREN) {
return ParserError(
String_fromFormat("expected ')' in condition"),
Array_fromArgs(1, result.token));
}

ConditionASTNode *condition = new_ConditionASTNode(expression, bindingCondition);

return ParserSuccess(condition);
Expand Down

0 comments on commit 7e06202

Please sign in to comment.