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
				
			
		| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
pub enum ErrorKind {
 | 
					pub enum ErrorKind {
 | 
				
			||||||
    UnexpectedChar(char),
 | 
					    UnexpectedChar(char),
 | 
				
			||||||
 | 
					    UnterminatedString,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,7 +27,7 @@ pub enum TokenKind {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Literals.
 | 
					    // Literals.
 | 
				
			||||||
    Identifier,
 | 
					    Identifier,
 | 
				
			||||||
    String,
 | 
					    String(String),
 | 
				
			||||||
    Number,
 | 
					    Number,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Keywords.
 | 
					    // Keywords.
 | 
				
			||||||
| 
						 | 
					@ -117,14 +117,16 @@ impl<'a> Scanner<'a> {
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    self.add_token(TokenKind::Slash);
 | 
					                    self.add_token(TokenKind::Slash);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            },
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // ignore whitespace
 | 
					            // ignore whitespace
 | 
				
			||||||
            ' ' => {},
 | 
					            ' ' => {}
 | 
				
			||||||
            '\r' => {},
 | 
					            '\r' => {}
 | 
				
			||||||
            '\t' => {},
 | 
					            '\t' => {}
 | 
				
			||||||
            '\n' => self.line += 1,
 | 
					            '\n' => self.line += 1,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            '"' => self.scan_string(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            unexpected => self.errors.push(Error {
 | 
					            unexpected => self.errors.push(Error {
 | 
				
			||||||
                line: self.line,
 | 
					                line: self.line,
 | 
				
			||||||
                kind: ErrorKind::UnexpectedChar(unexpected),
 | 
					                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>> {
 | 
					    fn scan_tokens(mut self) -> Vec<Token<'a>> {
 | 
				
			||||||
        while !self.is_at_end() {
 | 
					        while !self.is_at_end() {
 | 
				
			||||||
            self.start = self.current;
 | 
					            self.start = self.current;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue