Skip to content

Commit

Permalink
Difference tests for 'Pat', 'Ty', 'Expr'
Browse files Browse the repository at this point in the history
Completed difference tests for 'Pat', 'Ty', 'Expr', most of 'Stmt', 'Lit' (and
other more minor trees). Getting very close to #16.
  • Loading branch information
harpocrates committed Apr 19, 2017
1 parent eb7e73d commit f4d4617
Show file tree
Hide file tree
Showing 17 changed files with 611 additions and 74 deletions.
1 change: 1 addition & 0 deletions language-rust.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@ test-suite rustc-tests
, transformers >=0.5 && <0.6
, test-framework
, vector
, text
, unordered-containers
, language-rust
46 changes: 46 additions & 0 deletions sample-sources/expressions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
fn main() {
let x = box "foo";
let x = y <- 1;
let x = [1,2,3];
let x = foo(1,2,x);
let x = x.foo::<Bar, Baz>(a, b, c, d);
let x = (a, b, c ,d);
let x = a + b;
let x = a * b;
let x = !x;
let x = *x;
let x = true as f64;
let x = 1: f64;
if true { } else { };
if true { };
if let y = true { };
while true { }
'l: while true { }
while let y = true { continue; }
'l: while let y = true { continue 'l; }
for i in 1.. { }
'l: for i in 1..10 { }
loop { break; }
'l: loop { break 'l 1; }
match x { _ => () }
// let x = move |a,b,c| { a + b + c };
let x = { 1 };
let x = unsafe { 1 };
a = 1;
a += 1;
let x = obj.foo;
let x = foo.0;
let x = foo[2];
let x = &a;
let x = &mut a;
let x = return 1;
// let x = asm!("NOP");
// let x = println!("hi");
let x = Foo { x: 1, y: 2 };
let x = Foo { x: 1, ..base };
let x = [1; 5];
let x = 1 * (2 + 3);
let x = foo()?;
return 0;
return;
}
39 changes: 39 additions & 0 deletions sample-sources/items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

extern crate foo;
extern crate foo_bar as foo;

// use foo;
// use foo::bar;
// use foo::bar as FooBar;

static FOO: i32 = 42;
static mut FOO: i32 = 42;
static FOO: &'static str = "bar";

const FOO: i32 = 42;

mod foo { }
mod bar {

extern { }
extern "C" { }

type Foo = Bar<u8>;

enum Foo<A, B> { C(A), D(B) }

struct Foo<A> { x: A }
union Foo<A, B> { x: A, y: B }

trait Foo { }
trait Foo<T> { }

impl Trait for .. {}
impl Trait<T> for .. {}
impl<A> Foo<A> { }
impl<A> Trait for Foo<A> { }

macro_rules! foo { }
foo!();

}
33 changes: 33 additions & 0 deletions sample-sources/literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
fn main() {
// Numeric
1;
1isize;
1i8;
1i16;
1i32;
1i64;
1i128;
1usize;
1u8;
1u16;
1u32;
1u64;
1u128;
1f32;
1f64;

// Strings
"hello world";
r"hello world";
r#"hello " world"#;
// b"hello world";
br"hello world";
br#"hello " world"#;

// Booleans, Characters, and Bytes
'a';
'\n';
b'a';
true;
false;
}
26 changes: 26 additions & 0 deletions sample-sources/patterns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main() {
match 0 {
_ => 0,
x => 0,
&x => 0,
mut x => 0,
&mut x => 0,
Person { age, name } => 0,
Point { age: 0, .. } => 0,
Point(x, y, z) => 0,
Point(x, y, .., z) => 0,
std::math::pi => 0,
<i32 as T>::math::e => 0,
(x, y, z) => 0,
(x, y, .., z) => 0,
box x => 0,
&mut (a,b) => 0,
0 => 0,
0...1 => 0,
[a, b, i.., y, z] => 0,
[a, b, .., y, z] => 0,
[a, b, c] => 0,
// LinkedList!(1,2,3) => 0,
}
}

19 changes: 19 additions & 0 deletions sample-sources/statements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn main() {
// Local
let x = 1;

// Item
fn foo() { return 1 }

// NoSemi
if true {
foo()
}
let b = { let y = 3; y };

// Semi
2 + { 1 };

// Mac
// println!("hi")
}
21 changes: 18 additions & 3 deletions sample-sources/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fn main() {
let x: i32;
let x: [i32];
let x: [i32; 128];
let x: *mut i32;
Expand All @@ -8,6 +7,22 @@ fn main() {
let x: &mut i32;
let x: &'a i32;
let x: &i32;
let x: fn(i32) -> i32;
let x: fn(i32,i32);
let x: fn() -> i32;
// let x: fn(i32) -> i32;
// let x: fn(i32,i32);
let x: !;
let x: (i32,);
let x: (i32,!);
let x: i32;
let x: T;
let x: <Vec<T> as SomeTrait>::SomeType;
let x: Bound1 + Bound2 + 'static;
let x: (i32);
let x: typeof(1i32);
let x: _;
// let x: HList![i32,(),u8];
}

fn foo() -> impl Bound1 + Bound2 + Bound3 { }


7 changes: 2 additions & 5 deletions src/Language/Rust/Parser/Internal.y
Original file line number Diff line number Diff line change
Expand Up @@ -1117,11 +1117,8 @@ struct_expr :: { Expr Span }
| expr_path '{' sep_byT(field,',') '}' { Struct [] $1 $3 Nothing ($1 # $>) }

field :: { Field Span }
: ident ':' expr { Field (unspan $1) $3 ($1 # $3) }
| ident {
let path = (PathExpr [] Nothing (Path False [(unspan $1, NoParameters mempty)] (spanOf $1)) (spanOf $1))
in Field (unspan $1) path (spanOf $1)
}
: ident ':' expr { Field (unspan $1) (Just $3) ($1 # $3) }
| ident { Field (unspan $1) Nothing (spanOf $1) }


----------------
Expand Down
5 changes: 4 additions & 1 deletion src/Language/Rust/Parser/Literals.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import Data.Word (Word8)
translateLit :: LitTok -> Suffix -> a -> Lit a
translateLit (ByteTok s) = let Just (w8,"") = unescapeByte s in Byte w8
translateLit (CharTok s) = let Just (c,"") = unescapeChar s in Char c
translateLit (IntegerTok s) = uncurry Int (unescapeInteger s)
translateLit (FloatTok s) = Float (unescapeFloat s)
translateLit (StrTok s) = Str (unfoldr unescapeChar s) Cooked
translateLit (StrRawTok s n) = Str s (Raw n)
translateLit (ByteStrTok s) = ByteStr (unfoldr unescapeByte s) Cooked
translateLit (ByteStrRawTok s n) = ByteStr (map (fromIntegral . ord) s) (Raw n)
translateLit (IntegerTok s) = \suf -> case (suf, unescapeInteger s) of
(F32, (Dec, n)) -> Float (fromInteger n) F32
(F64, (Dec, n)) -> Float (fromInteger n) F64
(_, (rep, n)) -> Int rep n suf

-- | Given a string of characters read from a Rust source, extract the next underlying char taking
-- into account escapes and unicode.
Expand Down
3 changes: 2 additions & 1 deletion src/Language/Rust/Pretty/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ expressionAttrs (Try as _ _) = as

-- | Print a field
printField :: Field a -> Doc a
printField (Field ident expr x) = annotate x (printIdent ident <> ":" <+> printExpr expr)
printField (Field ident Nothing x) = annotate x (printIdent ident)
printField (Field ident (Just expr) x) = annotate x (printIdent ident <>":" <+> printExpr expr)

-- | Print range limits
printRangeLimits :: RangeLimits -> Doc a
Expand Down
5 changes: 4 additions & 1 deletion src/Language/Rust/Pretty/Resolve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,10 @@ parenE False e = e

-- | A field just requires the identifier and expression to be valid
resolveField :: Monoid a => Field a -> Either String (Field a)
resolveField (Field i e x) = Field <$> resolveIdent i <*> resolveExpr AnyExpr e <*> pure x
resolveField (Field i e x) = do
i' <- resolveIdent i
e' <- sequence (resolveExpr AnyExpr <$> e)
pure (Field i' e' x)

instance Monoid a => Resolve (Field a) where resolve = resolveField

Expand Down
4 changes: 2 additions & 2 deletions src/Language/Rust/Syntax/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ instance Located a => Located (Expr a) where
-- Example: @x: 1@ in @Point{ x: 1, y: 2 }@
data Field a
= Field
{ ident :: Ident -- ^ the field name
, expr :: Expr a -- ^ value assigned to the field
{ ident :: Ident -- ^ the field name
, expr :: Maybe (Expr a) -- ^ value assigned to the field
, nodeInfo :: a
} deriving (Eq, Functor, Show, Typeable, Data, Generic)

Expand Down
Loading

0 comments on commit f4d4617

Please sign in to comment.