From 0be80ebeae069df1f95afd34db03e8989f71ef9e Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 31 May 2018 20:56:39 +0200 Subject: [PATCH] With sacrifices to design and brevity, it now works! #52 --- src/mango/lexing/code_lexer.rs | 21 +++++++++++++++------ src/mango/util/format/strings.rs | 3 ++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/mango/lexing/code_lexer.rs b/src/mango/lexing/code_lexer.rs index f410028d..41af68c9 100644 --- a/src/mango/lexing/code_lexer.rs +++ b/src/mango/lexing/code_lexer.rs @@ -98,7 +98,8 @@ impl Lexer for CodeLexer { return Token(token); } // Past this point, we assume that hte buffer is empty. When adding stuff, pop it or re-enter lex() soon. - if let Match(_) = self.reader.borrow_mut().matches("\\.\\.\\.") { + let continue_match_res = self.reader.borrow_mut().matches("\\.\\.\\."); + if let Match(_) = continue_match_res { // Line continuation has no token, it just continues on the next line. if let Match(_) = self.reader.borrow_mut().matches("\\n\\r?") { // There should always be a newline after continuations, so that they can be ignored together. @@ -115,11 +116,14 @@ impl Lexer for CodeLexer { // Newline WITHOUT line continuation. return Token(Tokens::EndStatement(EndStatementToken::new_end_line())); } - if let Match(_) = self.reader.borrow_mut().matches(";") { + let end_statement_match_res = self.reader.borrow_mut().matches(";"); + if let Match(_) = end_statement_match_res { // Semicolon, which ends a statement. // Need to do some extra work with buffer, because there may be a newline followed by indentation, which ; should precede. - self.buffer.push(Tokens::EndStatement(EndStatementToken::new_semicolon())); - if let Match(_) = self.reader.borrow_mut().matches("\\n\\r?") { + self.buffer + .push(Tokens::EndStatement(EndStatementToken::new_semicolon())); + let end_line_match_res = self.reader.borrow_mut().matches("\\n\\r?"); + if let Match(_) = end_line_match_res { // If semicolon is followed by a newline (redundant), then we need to deal with indents (but ignore the newline itself). // This will return the queue of tokens, including the semicolon. return self.lex_indents(); @@ -131,7 +135,11 @@ impl Lexer for CodeLexer { // Indentation done; do the rest of lexing. // // Parse identifers and keywords. This assumes that keywords are a subset of identifiers. - if let Match(word) = self.reader.borrow_mut().matches(IdentifierToken::subpattern()) { + if let Match(word) = self + .reader + .borrow_mut() + .matches(IdentifierToken::subpattern()) + { // later: maybe turn identifier into keyword to avoid a string copy? kind of elaborate... if let Ok(keyword) = KeywordToken::from_str(word.clone()) { return Token(Tokens::Keyword(keyword)); @@ -139,7 +147,8 @@ impl Lexer for CodeLexer { return Token(Tokens::Identifier(IdentifierToken::from_str(word).unwrap())); } // Literal - if let Match(_) = self.reader.borrow_mut().matches("[a-z]?\"") { + let string_match_res = self.reader.borrow_mut().matches("[a-z]?\""); + if let Match(_) = string_match_res { let sublexer: Box = Box::new(StringLexer::new_double_quoted(self.reader.clone())); self.reader_or_delegate = ReaderOrDelegate::Delegate(sublexer); diff --git a/src/mango/util/format/strings.rs b/src/mango/util/format/strings.rs index f4aab63c..7161c5a9 100644 --- a/src/mango/util/format/strings.rs +++ b/src/mango/util/format/strings.rs @@ -2,7 +2,8 @@ /// string when parsed by a typical language. pub fn to_double_quoted_str(txt: &str) -> String { // todo: performance? mostly I'd like to add the quotes as part of the stream, but it seems difficult - let esc: String = txt.chars() + let esc: String = txt + .chars() .map(|c| match c { '\\' => r"\\".to_string(), '\"' => "\\\"".to_string(),