sexp automated format & cleanup

floats
Ondřej Hruška 4 years ago
parent 3cb445a31c
commit 7272eecc48
Signed by untrusted user: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 45
      lib/spanned_sexp/src/lib.rs

@ -75,7 +75,7 @@ impl fmt::Debug for Error {
fn get_line_and_column(s: &str, pos: usize) -> (usize, usize) {
let mut line: usize = 1;
let mut col: isize = -1;
for c in s.chars().take(pos+1) {
for c in s.chars().take(pos + 1) {
if c == '\n' {
line += 1;
col = -1;
@ -112,12 +112,12 @@ fn dbg(msg: &str, pos: &usize) {
fn atom_of_string(s: String) -> Atom {
match FromStr::from_str(&s) {
Ok(i) => return Atom::I(i),
Err(_) => {},
Err(_) => {}
};
match FromStr::from_str(&s) {
Ok(f) => return Atom::F(f),
Err(_) => {},
Err(_) => {}
};
Atom::S(s)
@ -126,7 +126,7 @@ fn atom_of_string(s: String) -> Atom {
// returns the char it found, and the new size if you wish to consume that char
fn peek(s: &str, pos: &usize) -> ERes<(char, usize)> {
dbg("peek", pos);
if *pos == s.len() { return err("unexpected eof", s, pos) }
if *pos == s.len() { return err("unexpected eof", s, pos); }
if s.is_char_boundary(*pos) {
let ch = s[*pos..].chars().next().unwrap();
let next = *pos + ch.len_utf8();
@ -146,10 +146,10 @@ fn expect(s: &str, pos: &mut usize, c: char) -> ERes<()> {
fn consume_until_newline(s: &str, pos: &mut usize) -> ERes<()> {
loop {
if *pos == s.len() { return Ok(()) }
if *pos == s.len() { return Ok(()); }
let (ch, next) = peek(s, pos)?;
*pos = next;
if ch == '\n' { return Ok(()) }
if ch == '\n' { return Ok(()); }
}
}
@ -157,12 +157,10 @@ fn consume_until_newline(s: &str, pos: &mut usize) -> ERes<()> {
fn zspace(s: &str, pos: &mut usize) -> ERes<()> {
dbg("zspace", pos);
loop {
if *pos == s.len() { return Ok(()) }
if *pos == s.len() { return Ok(()); }
let (ch, next) = peek(s, pos)?;
if ch == ';' { consume_until_newline(s, pos)? }
else if ch.is_whitespace() { *pos = next; }
else { return Ok(()) }
if ch == ';' { consume_until_newline(s, pos)? } else if ch.is_whitespace() { *pos = next; } else { return Ok(()); }
}
}
@ -201,11 +199,14 @@ fn parse_unquoted_atom(s: &str, pos: &mut usize) -> ERes<Atom> {
let mut cs: String = String::new();
loop {
if *pos == s.len() { break }
if *pos == s.len() { break; }
let (c, next) = peek(s, pos)?;
if c == ';' { consume_until_newline(s, pos)?; break }
if c.is_whitespace() || c == '(' || c == ')' { break }
if c == ';' {
consume_until_newline(s, pos)?;
break;
}
if c.is_whitespace() || c == '(' || c == ')' { break; }
cs.push(c);
*pos = next;
}
@ -217,8 +218,7 @@ fn parse_atom(s: &str, pos: &mut usize) -> ERes<Atom> {
dbg("parse_atom", pos);
let (ch, _) = peek(s, pos)?;
if ch == '"' { parse_quoted_atom (s, pos) }
else { parse_unquoted_atom(s, pos) }
if ch == '"' { parse_quoted_atom(s, pos) } else { parse_unquoted_atom(s, pos) }
}
fn parse_list(s: &str, pos: &mut usize) -> ERes<Vec<Sexp>> {
@ -248,8 +248,7 @@ fn parse_sexp(s: &str, pos: &mut usize) -> ERes<Sexp> {
zspace(s, pos)?;
let (c, _) = peek(s, pos)?;
let r =
if c == '(' { Ok(Sexp::List(parse_list(s, pos)?)) }
else { Ok(Sexp::Atom(parse_atom(s, pos)?)) };
if c == '(' { Ok(Sexp::List(parse_list(s, pos)?)) } else { Ok(Sexp::Atom(parse_atom(s, pos)?)) };
zspace(s, pos)?;
r
}
@ -293,7 +292,7 @@ fn is_num_string(s: &str) -> bool {
fn string_contains_whitespace(s: &str) -> bool {
for c in s.chars() {
if c.is_whitespace() { return true }
if c.is_whitespace() { return true; }
}
false
}
@ -332,7 +331,7 @@ impl fmt::Display for Sexp {
write!(f, "{}{}", s, x)?;
}
write!(f, ")")
},
}
}
}
}
@ -357,14 +356,14 @@ mod test {
fn test_hello_world() {
assert_eq!(
parse("(hello -42\n\t -4.0 \"world\") ; comment").unwrap(),
list(&[ atom_s("hello"), atom_i(-42), atom_f(-4.0), atom_s("world") ]));
list(&[atom_s("hello"), atom_i(-42), atom_f(-4.0), atom_s("world")]));
}
#[test]
fn test_escaping() {
assert_eq!(
parse("(\"\\\"\\q\" \"1234\" 1234)").unwrap(),
list(&[ atom_s("\"\\q"), atom_s("1234"), atom_i(1234) ]));
list(&[atom_s("\"\\q"), atom_s("1234"), atom_i(1234)]));
}
#[test]
@ -388,7 +387,7 @@ mod test {
#[test]
fn test_space_in_atom() {
let sexp = list(&[ atom_s("hello world")]);
let sexp = list(&[atom_s("hello world")]);
let sexp_as_string = sexp.to_string();
assert_eq!("(\"hello world\")", sexp_as_string);
assert_eq!(sexp, parse(&sexp_as_string).unwrap());
@ -418,6 +417,6 @@ mod test {
fn sexp_size() {
// I just want to see when this changes, in the diff.
use std::mem;
assert_eq!(mem::size_of::<Sexp>(), mem::size_of::<isize>()*5);
assert_eq!(mem::size_of::<Sexp>(), mem::size_of::<isize>() * 5);
}
}

Loading…
Cancel
Save