Example #1
0
    def parse_if_expr(self):
        expr = ast.IfExpression(self.cur_tok, None, None, None)

        if not self.expect(token.LPAREN):
            return None

        self.next()
        expr.condition = self.parse_expr(LOWEST)

        if not self.expect(token.RPAREN):
            return None

        if not self.expect(token.LBRACE):
            return None

        expr.consequence = self.parse_block_statement()

        if self.peek_is(token.ELSE):
            self.next()

            if not self.expect(token.LBRACE):
                return None

            expr.alternative = self.parse_block_statement()
        elif self.peek_is(token.ELIF):
            self.next()

            expr.alternative = ast.BlockStatement(
                self.cur_tok,
                [ast.ExpressionStatement(self.cur_tok, self.parse_if_expr())])

        return expr
Example #2
0
	def parseIfExpression(self):
		expression = ast.IfExpression()
		expression.Token = self.curToken
		
		if not self.expectPeek(token.LPAREN):
			return None
		
		self.nextToken()
		expression.Condition = self.parseExpression(LOWEST)
		
		if not self.expectPeek(token.RPAREN):
			return None
		
		if not self.expectPeek(token.LBRACE):
			return None
		
		expression.Consequence = self.parseBlockStatement()
		
		if self.peekTokenIs(token.ELSE):
			self.nextToken()
			if not self.expectPeek(token.LBRACE):
				return None
			
			expression.Alternative = self.parseBlockStatement()
		
		return expression
Example #3
0
    def parseIFExpression(self):
        ifExp = ast.IfExpression(self.currToken)

        if not self.expectedPeekToken(tkn.LPAREN):
            return None

        self.nextToken()

        ifExp.Condition = self.parseExpression(Precedence.LOWEST)

        if not self.expectedPeekToken(tkn.RPAREN):
            return None

        if not self.expectedPeekToken(tkn.LBRACE):
            return None

        ifExp.Consequence = self.parseBlockStatement()

        if self.peekTokenIs(tkn.ELSE):
            self.nextToken()

            if not self.expectedPeekToken(tkn.LBRACE):
                return None

            ifExp.Alternative = self.parseBlockStatement()

        return ifExp
Example #4
0
 def _parse_if_expression(self) -> Optional[ast.IfExpression]:
     token = self._current_token
     if not self._expect_peek(TokenType.LPAREN):
         return None
     self._next_token()
     condition = self._parse_expression(PrecedenceLevel.LOWEST)
     if condition is None:
         return None
     if not self._expect_peek(TokenType.RPAREN):
         return None
     if not self._expect_peek(TokenType.LBRACE):
         return None
     consequence = self._parse_block_statement()
     alternative = None
     if self._peek_token_is(TokenType.ELSE):
         self._next_token()
         if not self._expect_peek(TokenType.LBRACE):
             return None
         alternative = self._parse_block_statement()
     return ast.IfExpression(token, condition, consequence, alternative)
Example #5
0
 def ifExpr(self):
     l = self._nextTag(IF)
     self._nextTag(LPAREN)
     condExpr = self.expr()
     self._nextTag(RPAREN)
     trueExpr = self.expr()
     if self._peekHang() is ELSE:
         self._next()  # newline
         self._next()  # else
         falseExpr = self.expr()
     elif self._peekTag() is ELSE:
         self._next()
         falseExpr = self.expr()
     else:
         falseExpr = None
     e = ast.IfExpression(condExpr, trueExpr, falseExpr, None,
                          self._location(l.location))
     self._liftComments(e, None,
                        falseExpr if falseExpr is not None else trueExpr)
     return e
Example #6
0
 def expr_if(s):
     return ast.IfExpression(s[2], s[5])