Skip to content

Commit 7311313

Browse files
committed
Haddock + more tests
1 parent 9ce7630 commit 7311313

File tree

7 files changed

+79
-24
lines changed

7 files changed

+79
-24
lines changed

sample-sources/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn main() {
1919
let x: Bound1 + Bound2 + 'static;
2020
let x: impl Bound1 + Bound2 + 'static;
2121
let x: dyn Bound1 + Bound2 + 'static;
22+
let x: dyn for<'a> Debug;
2223
let x: (i32);
2324
let x: typeof(1i32);
2425
let x: _;

src/Language/Rust/Data/Ident.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ instance Eq Ident where
4343
i1 == i2 = hash i1 == hash i2 && name i1 == name i2
4444
i1 /= i2 = hash i1 /= hash i2 || name i1 /= name i2
4545

46+
-- | Uses 'hash' to short-circuit
4647
instance Ord Ident where
4748
compare i1 i2 = case compare i1 i2 of
4849
EQ -> compare (name i1) (name i2)

src/Language/Rust/Data/Position.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ minPos p1@(Position a1 _ _) p2@(Position a2 _ _) = if a1 < a2 then p1 else p2
9494
initPos :: Position
9595
initPos = Position 0 1 0
9696

97-
-- | Advance columnn a certain number of times.
97+
-- | Advance column a certain number of times.
9898
{-# INLINE incPos #-}
9999
incPos :: Position -> Int -> Position
100100
incPos NoPosition _ = NoPosition

src/Language/Rust/Parser/Internal.y

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ import qualified Data.List.NonEmpty as N
281281
%nonassoc IDENT ntIdent default union catch self auto dyn
282282

283283
-- These are all very low precedence unary operators
284-
%nonassoc box return yield break continue IMPLTRAIT LAMBDA
284+
%nonassoc box return yield break continue for IMPLTRAIT LAMBDA
285285

286286
-- 'static' needs to have higher precedenc than 'LAMBDA' so that statements starting in static get
287287
-- considered as static items, and not a static lambda
@@ -479,18 +479,30 @@ lt_ty_qual_path :: { Spanned (Ty Span) }
479479

480480
-- parse_generic_args() but with the '<' '>'
481481
generic_values :: { Spanned ([Lifetime Span], [Ty Span], [(Ident, Ty Span)]) }
482-
: '<' sep_by1(lifetime,',') ',' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>' { Spanned (toList $2, toList $4, toList $6) ($1 # $>) }
483-
| '<' sep_by1(lifetime,',') ',' sep_by1T(ty,',') gt '>' { Spanned (toList $2, toList $4, [] ) ($1 # $>) }
484-
| '<' sep_by1(lifetime,',') ',' sep_by1T(binding,',') gt '>' { Spanned (toList $2, [], toList $4) ($1 # $>) }
485-
| '<' sep_by1T(lifetime,',') gt '>' { Spanned (toList $2, [], [] ) ($1 # $>) }
486-
| '<' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>' { Spanned ([], toList $2, toList $4) ($1 # $>) }
487-
| '<' sep_by1T(ty,',') gt '>' { Spanned ([], toList $2, [] ) ($1 # $>) }
488-
| '<' sep_by1T(binding,',') gt '>' { Spanned ([], [], toList $2) ($1 # $>) }
489-
| '<' gt '>' { Spanned ([], [], [] ) ($1 # $>) }
490-
| lt_ty_qual_path ',' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>' { Spanned ([], unspan $1 : toList $3, toList $5) ($1 # $>) }
491-
| lt_ty_qual_path ',' sep_by1T(ty,',') gt '>' { Spanned ([], unspan $1 : toList $3, [] ) ($1 # $>) }
492-
| lt_ty_qual_path ',' sep_by1T(binding,',') gt '>' { Spanned ([], [unspan $1],toList $3) ($1 # $>) }
493-
| lt_ty_qual_path gt '>' { Spanned ([], [unspan $1],[] ) ($1 # $>) }
482+
: '<' sep_by1(lifetime,',') ',' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>'
483+
{ Spanned (toList $2, toList $4, toList $6) ($1 # $>) }
484+
| '<' sep_by1(lifetime,',') ',' sep_by1T(ty,',') gt '>'
485+
{ Spanned (toList $2, toList $4, [] ) ($1 # $>) }
486+
| '<' sep_by1(lifetime,',') ',' sep_by1T(binding,',') gt '>'
487+
{ Spanned (toList $2, [], toList $4) ($1 # $>) }
488+
| '<' sep_by1T(lifetime,',') gt '>'
489+
{ Spanned (toList $2, [], [] ) ($1 # $>) }
490+
| '<' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>'
491+
{ Spanned ([], toList $2, toList $4) ($1 # $>) }
492+
| '<' sep_by1T(ty,',') gt '>'
493+
{ Spanned ([], toList $2, [] ) ($1 # $>) }
494+
| '<' sep_by1T(binding,',') gt '>'
495+
{ Spanned ([], [], toList $2) ($1 # $>) }
496+
| '<' gt '>'
497+
{ Spanned ([], [], [] ) ($1 # $>) }
498+
| lt_ty_qual_path ',' sep_by1(ty,',') ',' sep_by1T(binding,',') gt '>'
499+
{ Spanned ([], unspan $1 : toList $3, toList $5) ($1 # $>) }
500+
| lt_ty_qual_path ',' sep_by1T(ty,',') gt '>'
501+
{ Spanned ([], unspan $1 : toList $3, [] ) ($1 # $>) }
502+
| lt_ty_qual_path ',' sep_by1T(binding,',') gt '>'
503+
{ Spanned ([], [unspan $1],toList $3) ($1 # $>) }
504+
| lt_ty_qual_path gt '>'
505+
{ Spanned ([], [unspan $1],[] ) ($1 # $>) }
494506

495507
binding :: { (Ident, Ty Span) }
496508
: ident '=' ty { (unspan $1, $3) }

src/Language/Rust/Pretty.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Maintainer : alec.theriault@gmail.com
77
Stability : experimental
88
Portability : portable
99
10-
Using a pretty printer is as easy as calling 'pretty' or 'prettyAnn' on the AST node of interest.
10+
Using a pretty printer is as easy as calling 'pretty' or 'prettyAnnotated' on the AST node of interest.
1111
1212
>>> :set -XOverloadedStrings
1313
>>> import Language.Rust.Syntax

src/Language/Rust/Syntax/AST.hs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,30 @@ instance Read Abi where
198198
-- | An argument in a function header (@syntax::ast::Arg@, except with @SelfKind@ and @ExplicitSelf@
199199
-- inlined).
200200
--
201-
-- Example: @bar: usize@ as in @fn foo(bar: usize)@
201+
-- Example: @x: usize@, @self@, @mut self@, @&self@, @&'lt mut self@, @mut self: Foo@ as in
202+
--
203+
-- @
204+
-- trait Foo {
205+
-- // Regular argument
206+
-- fn new(x: usize) -> Foo;
207+
--
208+
-- // Self argument, by value
209+
-- fn foo(self) -> i32;
210+
-- fn bar(mut self);
211+
--
212+
-- // Self argument, by reference
213+
-- fn baz(&self) -> Bar<'lt>;
214+
-- fn qux(&'lt mut self) -> Bar<'lt>;
215+
--
216+
-- // Explicit self argument
217+
-- fn quux(mut self: Foo);
218+
-- }
219+
-- @
202220
data Arg a
203-
= Arg (Maybe (Pat a)) (Ty a) a -- ^ @x: i32@, @(&x, &y): (&i32, &i32)@, @i32@
204-
| SelfValue Mutability a -- ^ @self@, @mut self@
205-
| SelfRegion (Maybe (Lifetime a)) Mutability a -- ^ @&'lt self@, @&'lt mut self@
206-
| SelfExplicit (Ty a) Mutability a -- ^ @self: i32@, @mut self: i32@
221+
= Arg (Maybe (Pat a)) (Ty a) a -- ^ Regular argument
222+
| SelfValue Mutability a -- ^ Self argument, by value
223+
| SelfRegion (Maybe (Lifetime a)) Mutability a -- ^ Self argument, by reference
224+
| SelfExplicit (Ty a) Mutability a -- ^ Explicit self argument
207225
deriving (Eq, Ord, Show, Functor, Typeable, Data, Generic, Generic1, NFData)
208226

209227
instance Located a => Located (Arg a) where
@@ -215,7 +233,15 @@ instance Located a => Located (Arg a) where
215233
-- | An arm of a 'Match' expression (@syntax::ast::Arm@). An arm has at least one patten, possibly a
216234
-- guard expression, and a body expression.
217235
--
218-
-- Example: @_ if 1 == 1 => { println!("match!") }@ as in @match n { _ if 1 == 1 => { println!("match!") } }@
236+
-- Example: @n if n % 4 == 3 => { println!("{} % 4 = 3", n) }@ as in
237+
--
238+
-- @
239+
-- match n {
240+
-- n if n % 4 == 3 => { println!("{} % 4 = 3", n) }
241+
-- n if n % 4 == 1 => { println!("{} % 4 = 1", n) }
242+
-- _ => println!("{} % 2 = 0", n)
243+
-- }
244+
-- @
219245
data Arm a = Arm [Attribute a] (NonEmpty (Pat a)) (Maybe (Expr a)) (Expr a) a
220246
deriving (Eq, Ord, Show, Functor, Typeable, Data, Generic, Generic1, NFData)
221247

@@ -224,9 +250,14 @@ instance Located a => Located (Arm a) where spanOf (Arm _ _ _ _ s) = spanOf s
224250
-- | 'Attribute's are annotations for other AST nodes (@syntax::ast::Attribute@). Note that
225251
-- doc-comments are promoted to attributes.
226252
--
227-
-- Example: @#[repr(C)]@ in @#[derive(Clone, Copy)] struct Complex { re: f32, im: f32 }@
253+
-- Example: @#[derive(Copy,Clone)]@ as in
254+
--
255+
-- @
256+
-- #[derive(Clone, Copy)]
257+
-- struct Complex { re: f32, im: f32 }
258+
-- @
228259
data Attribute a
229-
-- | Regular attributes starting with '#'.
260+
-- | Regular attributes of the form '#[...]'
230261
= Attribute AttrStyle (Path a) TokenStream a
231262
-- | Doc comment attributes. The 'Bool' argument identifies if the comment is inline or not, and
232263
-- the 'Name' contains the actual doc comment content.
@@ -241,7 +272,7 @@ instance Located a => Located (Attribute a) where
241272
-- describe the node that contains them (@syntax::ast::AttrStyle@). These two cases need to be
242273
-- distinguished only for pretty-printing - they are otherwise fundamentally equivalent.
243274
--
244-
-- Example: @#[repr(C)]@ is an outer attribute while @#![repr(C)]@ is an inner one
275+
-- Example: @#[repr(C)]@ is an outer attribute while @#![feature(slice_patterns)]@ is an inner one
245276
data AttrStyle = Outer | Inner deriving (Eq, Ord, Enum, Bounded, Show, Typeable, Data, Generic, NFData)
246277

247278
-- | Binary operators, used in the 'Binary' and 'AssignOp' constructors of 'Expr'

test/unit-tests/ParserTest.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ parserTypes = testGroup "parsing types"
399399
(PathTy Nothing (Path False [PathSegment "Fn" (Just (Parenthesized [] (Just (Rptr Nothing Immutable (ParenTy (TraitObject [ TraitTyParamBound (PolyTraitRef [] (TraitRef (Path False [PathSegment "Object" Nothing ()] ())) ()) None ()
400400
, TraitTyParamBound (PolyTraitRef [] (TraitRef send) ()) None ()] ()) ()) ())) ())) ()] ()) ())
401401
, testP "foo![ x ]" (MacTy (Mac (Path False [PathSegment "foo" Nothing ()] ()) (Tree (Token (Span (Position 6 1 6) (Position 7 1 7)) (IdentTok "x"))) ()) ())
402+
, testP "impl Debug + Clone"
403+
(ImplTrait
404+
[ TraitTyParamBound (PolyTraitRef [] (TraitRef debug) ()) None ()
405+
, TraitTyParamBound (PolyTraitRef [] (TraitRef clone) ()) None ()
406+
] ())
407+
, testP "dyn Debug + Clone"
408+
(TraitObject
409+
[ TraitTyParamBound (PolyTraitRef [] (TraitRef debug) ()) None ()
410+
, TraitTyParamBound (PolyTraitRef [] (TraitRef clone) ()) None ()
411+
] ())
402412
]
403413

404414

0 commit comments

Comments
 (0)