Skip to content

Commit

Permalink
Solve most compiler warnings #52
Browse files Browse the repository at this point in the history
  • Loading branch information
mverleg committed Jun 20, 2018
1 parent f79c75b commit cdda4d4
Show file tree
Hide file tree
Showing 33 changed files with 120 additions and 302 deletions.
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
reorder_imports = true
max_width = 140
6 changes: 1 addition & 5 deletions src/mango/ast_full/node/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ impl AssignmentAST {

impl ToText for AssignmentAST {
fn to_text(&self) -> String {
return format!(
"{0:} = ({1:})",
self.assignee.to_text(),
self.value.to_text()
);
return format!("{0:} = ({1:})", self.assignee.to_text(), self.value.to_text());
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/mango/ast_full/node/unary_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ impl UnaryOperationAST {

impl ToText for UnaryOperationAST {
fn to_text(&self) -> String {
return format!(
"({0:} {1:})",
self.operator.to_text(),
self.subject.to_text()
);
return format!("({0:} {1:})", self.operator.to_text(), self.subject.to_text());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/mango/ast_full/terminal/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub struct StringLiteralAST {

impl FloatLiteralAST {
pub fn new(value: f64) -> Self {
FloatLiteralAST {
value: f64eq::new(value),
}
FloatLiteralAST { value: f64eq::new(value) }
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/mango/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ pub struct RegexCache {
impl RegexCache {
// Not public to prevent having more than one instance.
fn new() -> Self {
RegexCache {
cache: HashMap::new(),
}
RegexCache { cache: HashMap::new() }
}

pub fn make_or_get(&mut self, subpattern: &str) -> &Regex {
Expand Down
58 changes: 28 additions & 30 deletions src/mango/lexing/code_lexer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use mango::io::typ::Reader;
use mango::io::typ::ReaderResult::*;
use mango::lexing::string_lexer::StringLexer;
use mango::lexing::typ::Lexer;
use mango::lexing::typ::MaybeToken;
use mango::lexing::typ::SubLexer;
use mango::lexing::typ::SubLexerResult;
use mango::token::special::UnlexableToken;
Expand All @@ -17,7 +15,6 @@ use mango::token::tokens::ParenthesisCloseToken;
use mango::token::tokens::ParenthesisOpenToken;
use mango::token::tokens::StartBlockToken;
use mango::token::Tokens;
use mango::util::collection::Queue;
use mango::util::strslice::char_ops::CharOps;
use mango::util::strslice::charsliceto;

Expand Down Expand Up @@ -72,8 +69,6 @@ impl SubLexer for CodeLexer {
fn lex_pass(&mut self, reader: &mut Box<Reader>) -> SubLexerResult {
use self::SubLexerResult::*;

// TODO: put all these match results inline

// End of line continuation
if let Match(_) = reader.matches(r"\.\.\.") {
// Line continuation has no token, it just continues on the next line, ignoring indents (for now).
Expand All @@ -83,8 +78,7 @@ impl SubLexer for CodeLexer {
// The rest of this line is unparsable.
if let Match(word) = reader.matches("[^\\n]*\\n\\r?") {
// This is a new line, so there may be indents.
return self
.token_and_indents(reader, Tokens::Unlexable(UnlexableToken::new(word)));
return self.token_and_indents(reader, Tokens::Unlexable(UnlexableToken::new(word)));
} else {
// TODO: I don't know yet how to deal with '...' followed by end-of-file
panic!()
Expand All @@ -95,24 +89,16 @@ impl SubLexer for CodeLexer {
if let Match(_) = reader.matches("\\n\\r?") {
// Newline WITHOUT line continuation.
// This is a new line, so there may be indents.
return self.token_and_indents(
reader,
Tokens::EndStatement(EndStatementToken::new_end_line()),
);
return self.token_and_indents(reader, Tokens::EndStatement(EndStatementToken::new_end_line()));
}
// End of statement
if let Match(_) = reader.matches(";") {
// Semicolon, which ends a statement.
if let Match(_) = reader.matches("\\n\\r?") {
// If semicolon is followed by a newline, it is redundant. Deal with indents (but ignore the newline itself).
return self.token_and_indents(
reader,
Tokens::EndStatement(EndStatementToken::new_semicolon()),
);
return self.token_and_indents(reader, Tokens::EndStatement(EndStatementToken::new_semicolon()));
} else {
return SubLexerResult::single(Tokens::EndStatement(
EndStatementToken::new_semicolon(),
));
return SubLexerResult::single(Tokens::EndStatement(EndStatementToken::new_semicolon()));
}
}
//
Expand All @@ -124,9 +110,7 @@ impl SubLexer for CodeLexer {
if let Ok(keyword) = KeywordToken::from_str(word.clone()) {
return SubLexerResult::single(Tokens::Keyword(keyword));
}
return SubLexerResult::single(Tokens::Identifier(
IdentifierToken::from_str(word).unwrap(),
));
return SubLexerResult::single(Tokens::Identifier(IdentifierToken::from_str(word).unwrap()));
}
// Literal
if let Match(_) = reader.matches("[a-z]?\"") {
Expand All @@ -146,22 +130,16 @@ impl SubLexer for CodeLexer {
debug_assert!(token.chars().last().unwrap() == '=');
if token.char_len() > 1 {
match AssociationToken::from_str(charsliceto(token, -1)) {
Ok(association) => {
return SubLexerResult::single((Tokens::Association(association)))
}
Ok(association) => return SubLexerResult::single(Tokens::Association(association)),
Err(msg) => panic!(format!("Invalid association prefix: {}", msg)),
}
} else {
return SubLexerResult::single(
(Tokens::Association(AssociationToken::from_unprefixed())),
);
return SubLexerResult::single(Tokens::Association(AssociationToken::from_unprefixed()));
}
}
// Operator (after association)
if let Match(token) = reader.matches(OperatorToken::subpattern()) {
return SubLexerResult::single(Tokens::Operator(
OperatorToken::from_str(&token).unwrap(),
));
return SubLexerResult::single(Tokens::Operator(OperatorToken::from_str(&token).unwrap()));
}
// Grouping symbols
if let Match(_) = reader.matches(r"\(") {
Expand Down Expand Up @@ -190,3 +168,23 @@ impl SubLexer for CodeLexer {
};
}
}

#[cfg(test)]
mod tests {
use mango::lexing::util::test_util::assert_text_to_tokens;
use mango::token::tokens::EndStatementToken;
use mango::token::tokens::KeywordToken;
use mango::token::Tokens;

#[test]
fn test_lexing_individual() {
assert_text_to_tokens(
"if",
vec![
Tokens::Keyword(KeywordToken::from_str("if".to_owned()).unwrap()),
Tokens::EndStatement(EndStatementToken::new_end_line()),
],
);
// todo: more
}
}
20 changes: 1 addition & 19 deletions src/mango/lexing/combi_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ mod tests {
use mango::token::tokens::KeywordToken;
use mango::token::tokens::LiteralToken;
use mango::token::tokens::OperatorToken;
use mango::token::tokens::ParenthesisCloseToken;
use mango::token::tokens::ParenthesisOpenToken;
use mango::token::tokens::StartBlockToken;
use mango::token::Tokens;
use mango::util::encdec::to_text::ToText;
use std::cell::RefCell;
use std::rc::Rc;

fn assert_text_to_tokens(text: &str, tokens: Vec<Tokens>) {
let expected = LexList::from_tokens(tokens);
let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(
text.to_owned(),
))));
let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(text.to_owned()))));
assert_eq!(
expected,
actual,
Expand All @@ -99,18 +93,6 @@ mod tests {
);
}

#[test]
fn test_lexing_individual() {
assert_text_to_tokens(
"if",
vec![
Tokens::Keyword(KeywordToken::from_str("if".to_owned()).unwrap()),
Tokens::EndStatement(EndStatementToken::new_end_line()),
],
);
// todo: more
}

#[test]
fn test_lexing_combined() {
assert_text_to_tokens(
Expand Down
10 changes: 4 additions & 6 deletions src/mango/lexing/string_lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use mango::lexing::typ::SubLexerResult;
use mango::token::tokens::LiteralToken;
use mango::token::Tokens;

#[allow(dead_code)] // TODO: TMP
pub enum StringType {
SingleQuotedInline,
DoubleQuotedInline,
Expand All @@ -13,6 +14,7 @@ pub enum StringType {

/// Lexes a string literal token.
// Starts after the opening quote and expected to consume until closing quote.
#[allow(dead_code)] // TODO: TMP
pub struct StringLexer {
typ: StringType,
}
Expand All @@ -32,13 +34,9 @@ impl SubLexer for StringLexer {
// TODO: doesn't handle escaping etc at all now
// TODO: this is going to have a problem if `matches` automatically eats whitespace
match reader.matches("[^\"\\n]*") {
Match(value) => {
return SubLexerResult::single(Tokens::Literal(LiteralToken::string(value)))
}
Match(value) => return SubLexerResult::single(Tokens::Literal(LiteralToken::string(value))),
NoMatch() => panic!("failed to parse string"), // This can't really go wrong since empty pattern matches
EOF() => {
return SubLexerResult::single(Tokens::Literal(LiteralToken::string("".to_owned())))
} // Unclosed string literal, let code parser deal with it
EOF() => return SubLexerResult::single(Tokens::Literal(LiteralToken::string("".to_owned()))), // Unclosed string literal, let code parser deal with it
}
}
}
9 changes: 3 additions & 6 deletions src/mango/lexing/util/lex_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ impl LexList {
LexList { tokens }
}

#[allow(unused)]
pub fn from_reader(lexer: &mut Lexer) -> Self {
lex_all(lexer)
}
}

impl ToText for LexList {
fn to_text(&self) -> String {
self.tokens
.iter()
.map(|token| token.to_text())
.collect::<Vec<_>>()
.join(" ")
self.tokens.iter().map(|token| token.to_text()).collect::<Vec<_>>().join(" ")
}
}

Expand All @@ -35,5 +32,5 @@ pub fn lex_all(lexer: &mut Lexer) -> LexList {
list.push(token)
}
list.shrink_to_fit();
LexList { tokens: list }
LexList::from_tokens(list)
}
2 changes: 2 additions & 0 deletions src/mango/lexing/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod lex_all;

pub mod test_util;
19 changes: 19 additions & 0 deletions src/mango/lexing/util/test_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use mango::io::fortest::stringreader::StringReader;
use mango::lexing::combi_lexer::CombiLexer;
use mango::lexing::util::lex_all::lex_all;
use mango::lexing::util::lex_all::LexList;
use mango::token::Tokens;
use mango::util::encdec::to_text::ToText;

#[allow(dead_code)]
pub fn assert_text_to_tokens(text: &str, tokens: Vec<Tokens>) {
let expected = LexList::from_tokens(tokens);
let actual = lex_all(&mut CombiLexer::new(Box::new(StringReader::new(text.to_owned()))));
assert_eq!(
expected,
actual,
"\nexpected:\n{}\nactual:\n{}",
expected.to_text(),
actual.to_text(),
);
}
4 changes: 1 addition & 3 deletions src/mango/token/tokens/association.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ pub struct AssociationToken {

impl AssociationToken {
pub fn from_unprefixed() -> Self {
AssociationToken {
symbol: Option::None,
}
AssociationToken { symbol: Option::None }
}

pub fn from_str<S: Into<String>>(symbol_txt: S) -> Result<AssociationToken, Msg> {
Expand Down
5 changes: 1 addition & 4 deletions src/mango/token/tokens/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ impl StartBlockToken {
impl EndBlockToken {
pub fn new(is_dedent: bool, is_end_keyword: bool) -> Self {
assert!(is_dedent || is_end_keyword);
EndBlockToken {
is_dedent,
is_end_keyword,
}
EndBlockToken { is_dedent, is_end_keyword }
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/mango/towasm/control/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ impl Block {

impl Wasm for Block {
fn as_wat(&self) -> String {
format!(
"(block {0:}\n{1:}\n) ;; block {0:}",
self.name.as_wat(),
self.group.as_wat()
)
format!("(block {0:}\n{1:}\n) ;; block {0:}", self.name.as_wat(), self.group.as_wat())
}

fn write_wasm(&self, file: &mut File) -> io::Result<()> {
Expand Down
6 changes: 1 addition & 5 deletions src/mango/towasm/control/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ impl Loop {

impl Wasm for Loop {
fn as_wat(&self) -> String {
format!(
"loop {0:}\n{1:}\nend ;; loop {0:}",
self.name.as_wat(),
self.group.as_wat()
)
format!("loop {0:}\n{1:}\nend ;; loop {0:}", self.name.as_wat(), self.group.as_wat())
}

fn write_wasm(&self, file: &mut File) -> io::Result<()> {
Expand Down
Loading

0 comments on commit cdda4d4

Please sign in to comment.