summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNanoTech2016-12-07 06:21:13 +0000
committerNanoTech2017-03-10 23:48:28 -0600
commit7ae2570543db9e97e8f10976365eabcf8541afb8 (patch)
tree887ce9fd457178aa17048301287dcda36aa799b7
parent9bfe1f36502c701d33174293f71647e614e5797e (diff)
Refactor comment end finding
-rw-r--r--src/lexer.rs27
1 files 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<usize> {
+ 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
}