parser: move parenthesized expression handling to a dedicated function

This commit is contained in:
Antoine Viallon 2023-05-24 00:48:43 +02:00
parent 275728a635
commit 9463af0014
Signed by: aviallon
GPG key ID: D126B13AB555E16F

View file

@ -107,10 +107,8 @@ class Parser:
@tracer.trace_method
def factor(self, mandatory: bool = False) -> Value:
if self.accept(Tokens.Parens_Left):
v = self.expression(mandatory=True)
self.expect(Tokens.Parens_Right)
return v
if par_expression := self.parenthesized_expression(mandatory=False):
return par_expression
elif num := self.number(mandatory=False):
return num
elif call := self.call(mandatory=False):
@ -172,17 +170,31 @@ class Parser:
elif mandatory:
raise UnexpectedTokenError(self.token, "definition")
@tracer.trace_method
def parenthesized_expression(self, mandatory: bool = False):
if lparens := self.accept(Tokens.Parens_Left):
expression = self.expression(mandatory=True)
rparens = self.expect(Tokens.Parens_Right)
expression.pseudo_nodes = [PseudoNode(lparens), PseudoNode(rparens)]
return expression
elif mandatory:
raise UnexpectedTokenError(self.token, "parenthesized expression")
@tracer.trace_method
def expression(self, mandatory: bool = False) -> Value:
node: Node | None = None
if self.peek(Tokens.KwLet) and (definition := self.definition(mandatory)):
return Expression(definition)
node = definition
elif self.peek_several(Tokens.Identifier, Tokens.Equal) and (assignment := self.assignment(mandatory)):
return Expression(assignment)
node = assignment
elif summation := self.summation(mandatory):
return Expression(summation)
node = summation
elif mandatory:
raise UnexpectedTokenError(self.token, "expression")
if node is not None:
return Expression(node)
@tracer.trace_method
def statement(self, mandatory: bool = False) -> Statement:
if lbrace := self.accept(Tokens.Brace_Left):