feat(tazjin/rlox): Implement string scanning
Note that Lox does not support escapes, and I don't care about that. Change-Id: Ie848cbc1164c4b005b15e29aad8fe723aaa68d1b Reviewed-on: https://cl.tvl.fyi/c/depot/+/2190 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									800d2ccde8
								
							
						
					
					
						commit
						47aa92b87d
					
				
					 2 changed files with 36 additions and 6 deletions
				
			
		| 
						 | 
				
			
			@ -27,7 +27,7 @@ pub enum TokenKind {
 | 
			
		|||
 | 
			
		||||
    // Literals.
 | 
			
		||||
    Identifier,
 | 
			
		||||
    String,
 | 
			
		||||
    String(String),
 | 
			
		||||
    Number,
 | 
			
		||||
 | 
			
		||||
    // Keywords.
 | 
			
		||||
| 
						 | 
				
			
			@ -76,7 +76,7 @@ impl<'a> Scanner<'a> {
 | 
			
		|||
 | 
			
		||||
    fn advance(&mut self) -> char {
 | 
			
		||||
        self.current += 1;
 | 
			
		||||
        self.source[self.current-1]
 | 
			
		||||
        self.source[self.current - 1]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn add_token(&mut self, kind: TokenKind) {
 | 
			
		||||
| 
						 | 
				
			
			@ -117,14 +117,16 @@ impl<'a> Scanner<'a> {
 | 
			
		|||
                } else {
 | 
			
		||||
                    self.add_token(TokenKind::Slash);
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // ignore whitespace
 | 
			
		||||
            ' ' => {},
 | 
			
		||||
            '\r' => {},
 | 
			
		||||
            '\t' => {},
 | 
			
		||||
            ' ' => {}
 | 
			
		||||
            '\r' => {}
 | 
			
		||||
            '\t' => {}
 | 
			
		||||
            '\n' => self.line += 1,
 | 
			
		||||
 | 
			
		||||
            '"' => self.scan_string(),
 | 
			
		||||
 | 
			
		||||
            unexpected => self.errors.push(Error {
 | 
			
		||||
                line: self.line,
 | 
			
		||||
                kind: ErrorKind::UnexpectedChar(unexpected),
 | 
			
		||||
| 
						 | 
				
			
			@ -157,6 +159,33 @@ impl<'a> Scanner<'a> {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn scan_string(&mut self) {
 | 
			
		||||
        while (self.peek() != '"' && !self.is_at_end()) {
 | 
			
		||||
            if self.peek() == '\n' {
 | 
			
		||||
                self.line += 1;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            self.advance();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if self.is_at_end() {
 | 
			
		||||
            self.errors.push(Error {
 | 
			
		||||
                line: self.line,
 | 
			
		||||
                kind: ErrorKind::UnterminatedString,
 | 
			
		||||
            });
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // closing '"'
 | 
			
		||||
        self.advance();
 | 
			
		||||
 | 
			
		||||
        // add token without surrounding quotes
 | 
			
		||||
        let string: String = self.source[(self.start + 1)..(self.current - 1)]
 | 
			
		||||
            .iter()
 | 
			
		||||
            .collect();
 | 
			
		||||
        self.add_token(TokenKind::String(string));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn scan_tokens(mut self) -> Vec<Token<'a>> {
 | 
			
		||||
        while !self.is_at_end() {
 | 
			
		||||
            self.start = self.current;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue