Skip to content

Commit

Permalink
fix: Surround operators with parens when pretty-printing
Browse files Browse the repository at this point in the history
Fixes #60
  • Loading branch information
Marwes committed Sep 9, 2016
1 parent b112c45 commit 7ccc6f2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions base/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,7 @@ fn get_return_type(env: &TypeEnv, alias_type: &ArcType, arg_count: usize) -> Arc
}
}
}

pub fn is_operator_char(c: char) -> bool {
"+-*/&|=<>".chars().any(|x| x == c)
}
12 changes: 10 additions & 2 deletions base/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::marker::PhantomData;

use pretty::{DocAllocator, Arena, DocBuilder};

use ast::DisplayEnv;
use ast::{DisplayEnv, is_operator_char};
use symbol::{Symbol, SymbolRef};

/// Trait for values which contains kinded values which can be refered by name
Expand Down Expand Up @@ -800,6 +800,14 @@ impl<'a, I, T, E> DisplayType<'a, I, T, E>
fn pretty(&self, arena: &'a Arena<'a>) -> DocBuilder<'a, Arena<'a>>
where I: AsRef<str>,
{
fn ident<'b>(arena: &'b Arena<'b>, name: &'b str) -> DocBuilder<'b, Arena<'b>> {
if name.starts_with(is_operator_char) {
chain![arena; "(", name, ")"]
} else {
arena.text(name)
}
}

let p = self.prec;
match *self.typ {
Type::Hole => arena.text("_"),
Expand Down Expand Up @@ -913,7 +921,7 @@ impl<'a, I, T, E> DisplayType<'a, I, T, E>
_ => rhs = rhs.nest(4),
}
let f = chain![arena;
self.env.string(&field.name),
ident(arena, self.env.string(&field.name)),
" : ",
rhs.group(),
if i + 1 != fields.len() {
Expand Down
9 changes: 7 additions & 2 deletions base/tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ fn some_record() -> ArcType<&'static str> {
Field {
name: "test",
typ: test.clone(),
},
Field {
name: "+",
typ: Type::function(vec![Type::int(), Type::int()], Type::int()),
}])
}

Expand Down Expand Up @@ -97,7 +101,7 @@ fn show_record() {
}]);
assert_eq_display!(format!("{}", typ), "{ Test a = a -> String, x : Int }");
assert_eq_display!(format!("{}", some_record()),
"{ Test a = a -> String, x : Int, test : Test a }");
"{ Test a = a -> String, x : Int, test : Test a, (+) : Int -> Int -> Int }");
let typ = Type::record(vec![Field {
name: "Test",
typ: Alias::new("Test",
Expand Down Expand Up @@ -159,7 +163,8 @@ fn show_record_multi_line() {
record_looooooooooooooooooooooooooooooooooong : {
Test a = a -> String,
x : Int,
test : Test a
test : Test a,
(+) : Int -> Int -> Int
},
looooooooooooooooooooooooooooooooooong_field : Test a
}"#;
Expand Down
5 changes: 1 addition & 4 deletions parser/src/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cmp::Ordering;
use std::fmt;

use base::ast::is_operator_char;
use base::pos::{BytePos, CharPos, Location, Span, Spanned};

use combine::primitives::{Consumed, Error as CombineError, RangeStream};
Expand Down Expand Up @@ -303,10 +304,6 @@ impl Contexts {
}
}

fn is_operator_char(c: char) -> bool {
"+-*/&|=<>".chars().any(|x| x == c)
}

pub struct Lexer<'input, I>
where I: RangeStream<Item = char, Range = &'input str>,
{
Expand Down

0 comments on commit 7ccc6f2

Please sign in to comment.