From 7ae2570543db9e97e8f10976365eabcf8541afb8 Mon Sep 17 00:00:00 2001 From: NanoTech Date: Wed, 7 Dec 2016 06:21:13 +0000 Subject: Refactor comment end finding --- src/lexer.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 5081bcb..1f54a52 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -234,6 +234,10 @@ named!(token<&str, Tok>, alt!( value!(Tok::Equals, tag!("=")) )); +fn find_end(input: &str, ending: &str) -> Option { + input.find(ending).map(|i| i + ending.len()) +} + pub struct Lexer<'input> { input: &'input str, offset: usize, @@ -269,24 +273,11 @@ impl<'input> Lexer<'input> { return false; } let skip = match &input[0..2] { - "{-" => { - if let Some(i) = input.find("-}") { - // println!("skipped {} bytes of block comment", i + 2); - i + 2 - } else { - 0 - } - } - "--" => { - if let Some(i) = input.find("\n") { // FIXME Find CRLF too - // println!("skipped {} bytes of line comment", i + 1); - i + 1 - } else { - 0 - } - } - _ => 0, - }; + "{-" => find_end(input, "-}"), + "--" => find_end(input, "\n"), // Also skips past \r\n (CRLF) + _ => None, + }.unwrap_or(0); + // println!("skipped {} bytes of comment", skip); self.offset += skip; skip != 0 } -- cgit v1.2.3