Skip to content

Commit

Permalink
Loop using peek instead of next
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhuichen committed Oct 27, 2023
1 parent baeea04 commit 72b69ed
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions crates/vimfuncs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> FuncLexer<'a> {
pub fn lex(&mut self) -> Vec<FuncToken> {
let mut result = vec![];

while let Some(ch) = self.chars.next() {
while let Some(ch) = self.chars.peek() {
let tok = match ch {
'{' => self.process_left_brace(),
'}' => self.process_right_brace(),
Expand All @@ -70,11 +70,11 @@ impl<'a> FuncLexer<'a> {
',' => self.process_comma(),
'"' => self.process_quotation(),
'#' => self.process_hash(),
ch if ch.is_numeric() => self.process_number(Some(ch)),
ch if ch.is_alphabetic() => self.process_alphabets(Some(ch)),
ch if ch.is_numeric() => self.process_number(),
ch if ch.is_alphabetic() => self.process_alphabets(),
ch if ch.is_whitespace() => self.process_whitespace(),
';' => self.process_semi_colon(),
'/' if *self.chars.peek().unwrap() == '/' => self.process_double_forward_slash(),
'/' => self.process_double_forward_slash(),
ch => todo!("unhandled: {:?}", ch),
};

Expand All @@ -87,50 +87,50 @@ impl<'a> FuncLexer<'a> {
}

fn process_left_brace(&mut self) -> FuncToken {
self.chars.next();
FuncToken::LeftBrace
}

fn process_right_brace(&mut self) -> FuncToken {
self.chars.next();
FuncToken::RightBrace
}

fn process_left_paren(&mut self) -> FuncToken {
self.chars.next();
FuncToken::LeftParen
}

fn process_right_paren(&mut self) -> FuncToken {
self.chars.next();
FuncToken::RightParen
}

fn process_comma(&mut self) -> FuncToken {
self.chars.next();
FuncToken::Comma
}

fn process_quotation(&mut self) -> FuncToken {
self.chars.next();
let is_terminal = |ch: char| ch == '"';
let value = self.read_until_iter_terminal(is_terminal);
let value = String::from_iter(value);

FuncToken::String(value)
}

fn process_number(&mut self, first_ch: Option<char>) -> FuncToken {
fn process_number(&mut self) -> FuncToken {
let is_terminal = |ch: char| !ch.is_numeric();
let mut value = self.read_until_peek_terminal(is_terminal);
if let Some(ch) = first_ch {
value.insert(0, ch);
}
let value = self.read_until_peek_terminal(is_terminal);
let value = String::from_iter(value).parse().unwrap();

FuncToken::Number(value)
}

fn process_alphabets(&mut self, first_ch: Option<char>) -> FuncToken {
fn process_alphabets(&mut self) -> FuncToken {
let is_terminal = |ch: char| !(ch.is_alphanumeric() || ch == '_');
let mut value = self.read_until_peek_terminal(is_terminal);
if let Some(ch) = first_ch {
value.insert(0, ch);
}
let value = self.read_until_peek_terminal(is_terminal);
let value = String::from_iter(value);

FuncToken::Identifier(value)
Expand All @@ -142,26 +142,29 @@ impl<'a> FuncLexer<'a> {
}

fn process_semi_colon(&mut self) -> FuncToken {
self.chars.next();
FuncToken::Skip
}

fn process_double_forward_slash(&mut self) -> FuncToken {
self.chars.next();
self.skip_line();
FuncToken::Skip
}

fn process_hash(&mut self) -> FuncToken {
// read #ifdef ...
self.chars.next();
self.skip_line();
self.skip_whitespace();
let proc_if = self.process_alphabets(None).into();
let proc_if = self.process_alphabets().into();
self.skip_whitespace();

// read #else
self.skip_line();

self.skip_whitespace();
let proc_else = self.process_alphabets(None).into();
let proc_else = self.process_alphabets().into();
self.skip_whitespace();

// read #endif
Expand Down

0 comments on commit 72b69ed

Please sign in to comment.