I’m stuck on Stage #(Scanning: Division operator & comments). My solution is to ignore everything until I encounter \n whenever I encounter//. I’m solving the challenge using Rust, and I’ve written a test case against it; it works locally.
Here are my logs:
[tester::#ML2] Running tests for Stage #ML2 (Scanning: Division operator & comments)
[tester::#ML2] [test-1] Running test case: 1
[tester::#ML2] [test-1] Writing contents to ./test.lox:
[tester::#ML2] [test-1.lox] //Comment
[tester::#ML2] [test-1] $ ./your_program.sh tokenize test.lox
[your_program] Logs from your program will appear here!
[your_program] EOF null
[tester::#ML2] [test-1] ✓ 1 line(s) match on stdout
[tester::#ML2] [test-1] ✓ Received exit code 0.
[tester::#ML2] [test-2] Running test case: 2
[tester::#ML2] [test-2] Writing contents to ./test.lox:
[tester::#ML2] [test-2.lox] (///Unicode:£§᯽☺♣)
[tester::#ML2] [test-2] $ ./your_program.sh tokenize test.lox
[your_program] Logs from your program will appear here!
[your_program]
[your_program] thread 'main' has overflowed its stack
[your_program] fatal runtime error: stack overflow, aborting
[tester::#ML2] [test-2] expected no error (exit code 0), got exit code -1
[tester::#ML2] [test-2] Test failed
And here’s a snippet of my code:
fn handle_double_slash(&mut self) {
let new_line_regex = Regex::new(r"\r?\n").unwrap();
while !self.matches(&new_line_regex) && self.pointer < self.source_code.len() {
let re = Regex::new("[^\r?\n]*").unwrap();
let sc = &self.source_code[self.pointer..].to_owned();
let caps = re.captures(sc);
if caps.is_some() {
let cap = &caps.unwrap()[0];
let _ = self.eat(cap);
}
}
}
fn matches(&self, re: &Regex) -> bool {
re.is_match(&self.source_code[self.pointer..])
}
fn eat(&mut self, s: &str) -> Result<(), String> {
if self.pointer + s.len() > self.source_code.len() {
return Err("Unexpected EOF".into());
}
if &self.source_code[self.pointer..self.pointer + s.len()] != s {
return Err(format!(
"Expected to find {}, found {}",
s,
self.source_code[self.pointer..self.pointer + s.len()].to_string()
));
};
self.pointer += s.len();
self.start_pos += s.len();
Ok(())
}