Skip to content

Commit

Permalink
[glsl-in] Extraxt lexer next_line method (gfx-rs#232)
Browse files Browse the repository at this point in the history
Have single place where advancing to next line
  • Loading branch information
pjoe committed Oct 8, 2020
1 parent aa35110 commit 2b268c9
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions src/front/glsl/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,25 +283,36 @@ impl<'a> Lexer<'a> {
}

pub fn new(input: &'a str) -> Self {
let mut lines = input.lines().enumerate();
let (line, input) = lines.next().unwrap_or((0, ""));
let mut input = String::from(input);
let mut lexer = Lexer {
lines: input.lines().enumerate(),
input: "".to_string(),
line: 0,
offset: 0,
inside_comment: false,
};
lexer.next_line();
lexer
}

while input.ends_with('\\') {
if let Some((_, next)) = lines.next() {
input.pop();
input.push_str(next);
} else {
break;
fn next_line(&mut self) -> bool {
if let Some((line, input)) = self.lines.next() {
let mut input = String::from(input);

while input.ends_with('\\') {
if let Some((_, next)) = self.lines.next() {
input.pop();
input.push_str(next);
} else {
break;
}
}
}

Lexer {
lines,
input,
line,
offset: 0,
inside_comment: false,
self.input = input;
self.line = line;
self.offset = 0;
true
} else {
false
}
}

Expand Down Expand Up @@ -331,22 +342,9 @@ impl<'a> Lexer<'a> {
self.next()
}
} else {
let (line, input) = self.lines.next()?;

let mut input = String::from(input);

while input.ends_with('\\') {
if let Some((_, next)) = self.lines.next() {
input.pop();
input.push_str(next);
} else {
break;
}
if !self.next_line() {
return None;
}

self.input = input;
self.line = line;
self.offset = 0;
self.next()
}
}
Expand Down

0 comments on commit 2b268c9

Please sign in to comment.