refactor(tazjin/rlox): Add helper method for parsing identifiers
Change-Id: I9a45f823f16919319d6135225d5bd53ed54c2530 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2388 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									fcd9801b01
								
							
						
					
					
						commit
						8bcbb04160
					
				
					 2 changed files with 19 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -6,7 +6,6 @@ pub enum ErrorKind {
 | 
			
		|||
    ExpectedExpression(String),
 | 
			
		||||
    ExpectedSemicolon,
 | 
			
		||||
    ExpectedClosingBrace,
 | 
			
		||||
    ExpectedVariableName,
 | 
			
		||||
    ExpectedToken(&'static str),
 | 
			
		||||
    TypeError(String),
 | 
			
		||||
    UndefinedVariable(String),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -169,24 +169,17 @@ impl<'a> Parser<'a> {
 | 
			
		|||
    fn var_declaration(&mut self) -> StmtResult<'a> {
 | 
			
		||||
        // Since `TokenKind::Identifier` carries data, we can't use
 | 
			
		||||
        // `consume`.
 | 
			
		||||
        if let TokenKind::Identifier(_) = self.peek().kind {
 | 
			
		||||
            let mut var = Var {
 | 
			
		||||
                name: self.advance(),
 | 
			
		||||
                initialiser: None,
 | 
			
		||||
            };
 | 
			
		||||
        let mut var = Var {
 | 
			
		||||
            name: self.identifier("Expected variable name.")?,
 | 
			
		||||
            initialiser: None,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
            if self.match_token(&TokenKind::Equal) {
 | 
			
		||||
                var.initialiser = Some(self.expression()?);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;
 | 
			
		||||
            return Ok(Statement::Var(var));
 | 
			
		||||
        if self.match_token(&TokenKind::Equal) {
 | 
			
		||||
            var.initialiser = Some(self.expression()?);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return Err(Error {
 | 
			
		||||
            line: self.peek().line,
 | 
			
		||||
            kind: ErrorKind::ExpectedVariableName,
 | 
			
		||||
        });
 | 
			
		||||
        self.consume(&TokenKind::Semicolon, ErrorKind::ExpectedSemicolon)?;
 | 
			
		||||
        Ok(Statement::Var(var))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn statement(&mut self) -> StmtResult<'a> {
 | 
			
		||||
| 
						 | 
				
			
			@ -492,6 +485,17 @@ impl<'a> Parser<'a> {
 | 
			
		|||
 | 
			
		||||
    // internal helpers
 | 
			
		||||
 | 
			
		||||
    fn identifier(&mut self, err: &'static str) -> Result<Token<'a>, Error> {
 | 
			
		||||
        if let TokenKind::Identifier(_) = self.peek().kind {
 | 
			
		||||
            Ok(self.advance())
 | 
			
		||||
        } else {
 | 
			
		||||
            Err(Error {
 | 
			
		||||
                line: self.peek().line,
 | 
			
		||||
                kind: ErrorKind::ExpectedToken(err),
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Check if the next token is in `oneof`, and advance if it is.
 | 
			
		||||
    fn match_token(&mut self, token: &TokenKind) -> bool {
 | 
			
		||||
        if self.check_token(token) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue