Skip to content

Commit 862f7c6

Browse files
committed
Clean up warnings/imports/haddock
Just some cleanup post-merge.
1 parent aeacbe1 commit 862f7c6

File tree

11 files changed

+66
-95
lines changed

11 files changed

+66
-95
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# NOTE: `language-rust` is complete, but still undergoing testing! [![Build Status][4]][5]
1+
# Parser and pretty printer for the Rust language [![Build Status][4]][5]
22

33
`language-rust` aspires to efficiently and accurately parse and pretty-print the [Rust language][0].
44
The underlying AST structures are also intended to be as similar as possible to the AST `rustc` uses

Setup.hs

Lines changed: 0 additions & 2 deletions
This file was deleted.

language-rust.cabal

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: language-rust
22
version: 0.1.0.0
3-
synopsis: Haskell library for parsing and pretty printing Rust source code.
4-
-- description:
3+
4+
synopsis: Parsing and pretty printing of Rust code
5+
description: Language Rust is a well tested library for converting Rust source code into
6+
and out of abstract syntax trees.
7+
58
homepage: https://github.com/harpocrates/language-rust
69
license: BSD3
710
license-file: LICENSE
@@ -20,7 +23,7 @@ source-repository head
2023
location: https://github.com/harpocrates/language-rust.git
2124

2225
flag useByteStrings
23-
description: Use ByteString instead of String as InputStream datatype
26+
description: Use 'ByteString' instead of 'String' as 'InputStream' datatype
2427
default: True
2528

2629
flag enableQuasiquotes
@@ -38,7 +41,7 @@ library
3841
-Wincomplete-uni-patterns
3942
-Wmissing-signatures
4043

41-
build-tools: alex, happy >= 1.19.8
44+
build-tools: alex >=3.1, happy >=1.19.8
4245
default-language: Haskell2010
4346

4447
exposed-modules: Language.Rust.Syntax
@@ -85,10 +88,9 @@ library
8588
build-depends: semigroups >= 0.18
8689

8790
if flag(useByteStrings)
91+
cpp-options: -DUSE_BYTESTRING
8892
build-depends: utf8-string >=1.0
8993
, bytestring >=0.10
90-
else
91-
cpp-options: -DNO_BYTESTRING
9294

9395
if flag(enableQuasiquotes)
9496
build-depends: template-haskell >=2.10
@@ -144,11 +146,11 @@ test-suite rustc-tests
144146
, test-framework >=0.8.0
145147
, vector >=0.10.0
146148
, text >=1.2.0
147-
, unordered-containers >= 0.2.7
149+
, unordered-containers >=0.2.7
148150
, language-rust
149151
, time >=1.2.0.0
150152
if impl(ghc < 8)
151-
build-depends: semigroups >= 0.18
153+
build-depends: semigroups >=0.18
152154

153155
benchmark timing-benchmarks
154156
hs-source-dirs: bench/timing-benchmarks

src/Language/Rust/Data/InputStream.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Data.Coerce (coerce)
3434
import Data.String (IsString(..))
3535
import System.IO
3636

37-
#ifndef NO_BYTESTRING
37+
#ifdef USE_BYTESTRING
3838
import qualified Data.ByteString as BS
3939
import qualified Data.ByteString.UTF8 as BE
4040
#else
@@ -79,7 +79,7 @@ takeChars :: Int -> InputStream -> String
7979
-- | Returns the number of text lines in the given 'InputStream'
8080
countLines :: InputStream -> Int
8181

82-
#ifndef NO_BYTESTRING
82+
#ifdef USE_BYTESTRING
8383

8484
-- | Opaque input type.
8585
newtype InputStream = IS BS.ByteString

src/Language/Rust/Parser.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ parse' is = case execParser parser is initPos of
5656
Left (pos, msg) -> throw (ParseFail pos msg)
5757
Right x -> x
5858

59+
-- | Same as 'execParser', but working from a list of tokens instead of an 'InputStream'.
5960
execParserTokens :: P a -> [Spanned Token] -> Position -> Either (Position,String) a
6061
execParserTokens p toks = execParser (pushTokens toks *> p) (inputStreamFromString "")
6162
where pushTokens = traverse_ pushToken . reverse

src/Language/Rust/Parser/Internal.y

Lines changed: 42 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,26 @@ To get information about transition states and such, run
2727
{-# LANGUAGE OverloadedStrings, OverloadedLists, PartialTypeSignatures #-}
2828

2929
module Language.Rust.Parser.Internal (
30-
-- * Complete parsers
30+
-- * Parsers
3131
parseLit, parseAttr, parseTy, parsePat, parseStmt, parseExpr, parseItem, parseSourceFile,
3232
parseBlock, parseImplItem, parseTraitItem, parseTt, parseTokenStream, parseTyParam,
3333
parseGenerics, parseWhereClause, parseLifetimeDef,
34-
35-
-- * Partial parsers
36-
parseLitP, parseAttrP, parseTyP, parsePatP, parseStmtP, parseExprP, parseItemP, parseSourceFileP,
37-
parseBlockP, parseImplItemP, parseTraitItemP, parseTtP, parseTokenStreamP, parseTyParamP,
38-
parseGenericsP, parseWhereClauseP, parseLifetimeDefP
3934
) where
4035

4136
import Language.Rust.Syntax
4237
import Language.Rust.Data.Position
43-
import Language.Rust.Parser.Lexer (lexNonSpace, lexShebangLine)
44-
import Language.Rust.Parser.ParseMonad (pushToken, getPosition, P, parseError)
45-
import Language.Rust.Parser.Literals (translateLit)
38+
import Language.Rust.Parser.Lexer ( lexNonSpace, lexShebangLine )
39+
import Language.Rust.Parser.ParseMonad ( pushToken, getPosition, P, parseError )
40+
import Language.Rust.Parser.Literals ( translateLit )
4641
import Language.Rust.Parser.Reversed
4742

48-
import Data.Foldable (toList)
49-
import Data.List ((\\), isSubsequenceOf)
50-
import Data.List.NonEmpty (NonEmpty(..), (<|))
43+
import Data.Foldable ( toList )
44+
import Data.List ( (\\), isSubsequenceOf )
45+
import Text.Read ( readMaybe )
46+
import Data.Semigroup ( (<>) )
47+
48+
import Data.List.NonEmpty ( NonEmpty(..), (<|) )
5149
import qualified Data.List.NonEmpty as N
52-
import Data.Semigroup ((<>))
53-
import Text.Read (readMaybe)
5450
}
5551

5652
-- in order to document the parsers, we have to alias them
@@ -63,7 +59,7 @@ import Text.Read (readMaybe)
6359
%name parseItem mod_item
6460
%name parseSourceFileContents source_file
6561
%name parseBlock export_block
66-
%name parseImplItem impl_item
62+
%name parseImplItem impl_item
6763
%name parseTraitItem trait_item
6864
%name parseTt token_tree
6965
%name parseTokenStream token_stream
@@ -72,26 +68,6 @@ import Text.Read (readMaybe)
7268
%name parseWhereClause where_clause
7369
%name parseGenerics generics
7470

75-
-- we also document the partial parsers
76-
%partial parseLitP lit
77-
%partial parseAttrP export_attribute
78-
%partial parseTyP export_ty
79-
%partial parsePatP pat
80-
%partial parseStmtP stmt
81-
%partial parseExprP expr
82-
%partial parseItemP mod_item
83-
%partial parseSourceFileContentsP source_file
84-
%partial parseBlockP export_block
85-
%partial parseImplItemP impl_item
86-
%partial parseTraitItemP trait_item
87-
%partial parseTtP token_tree
88-
%partial parseTokenStreamP token_stream
89-
%partial parseTyParamP ty_param
90-
%partial parseLifetimeDefP lifetime_def
91-
%partial parseWhereClauseP where_clause
92-
%partial parseGenericsP generics
93-
94-
9571
%tokentype { Spanned Token }
9672
%lexer { lexNonSpace >>= } { Spanned Eof _ }
9773
%monad { P } { >>= } { return }
@@ -340,22 +316,22 @@ ident :: { Spanned Ident }
340316
-- lexers to discard what would have been the troublesome '>>', '>=', or '>>=' token.
341317
gt :: { () }
342318
: {- empty -} {%% \(Spanned tok s) ->
343-
let s' = nudge 1 0 s; s'' = nudge 0 (-1) s
344-
in case tok of
345-
GreaterGreater -> pushToken (Spanned Greater s') *> pushToken (Spanned Greater s'')
346-
GreaterEqual -> pushToken (Spanned Equal s') *> pushToken (Spanned Greater s'')
347-
GreaterGreaterEqual -> pushToken (Spanned GreaterEqual s') *> pushToken (Spanned Greater s'')
348-
_ -> pushToken (Spanned tok s)
319+
let s' = nudge 1 0 s; s'' = nudge 0 (-1) s in
320+
case tok of
321+
GreaterGreater -> pushToken (Spanned Greater s') *> pushToken (Spanned Greater s'')
322+
GreaterEqual -> pushToken (Spanned Equal s') *> pushToken (Spanned Greater s'')
323+
GreaterGreaterEqual -> pushToken (Spanned GreaterEqual s') *> pushToken (Spanned Greater s'')
324+
_ -> pushToken (Spanned tok s)
349325
}
350326

351327
-- This should precede any '|' token which could be absorbed in a '||' token. This works in the same
352328
-- way as 'gt'.
353329
pipe :: { () }
354330
: {- empty -} {%% \(Spanned tok s) ->
355-
let s' = nudge 1 0 s; s'' = nudge 0 (-1) s
356-
in case tok of
357-
PipePipe -> pushToken (Spanned Pipe s') *> pushToken (Spanned Pipe s'')
358-
_ -> pushToken (Spanned tok s)
331+
let s' = nudge 1 0 s; s'' = nudge 0 (-1) s in
332+
case tok of
333+
PipePipe -> pushToken (Spanned Pipe s') *> pushToken (Spanned Pipe s'')
334+
_ -> pushToken (Spanned tok s)
359335
}
360336

361337
-------------
@@ -364,13 +340,13 @@ pipe :: { () }
364340

365341
-- | One or more occurences of 'p'
366342
some(p) :: { Reversed NonEmpty _ }
367-
: some(p) p { let Reversed xs = $1 in Reversed ($2 <| xs) }
368-
| p { [$1] }
343+
: some(p) p { let Reversed xs = $1 in Reversed ($2 <| xs) }
344+
| p { [$1] }
369345

370346
-- | Zero or more occurences of 'p'
371-
many(p) :: { [_] }
372-
: some(p) { toList $1 }
373-
| {- empty -} { [] }
347+
many(p) :: { [ _ ] }
348+
: some(p) { toList $1 }
349+
| {- empty -} { [] }
374350

375351
-- | One or more occurences of 'p', seperated by 'sep'
376352
sep_by1(p,sep) :: { Reversed NonEmpty _ }
@@ -379,19 +355,19 @@ sep_by1(p,sep) :: { Reversed NonEmpty _ }
379355

380356
-- | Zero or more occurrences of 'p', separated by 'sep'
381357
sep_by(p,sep) :: { [ _ ] }
382-
: sep_by1(p,sep) { toList $1 }
383-
| {- empty -} { [] }
358+
: sep_by1(p,sep) { toList $1 }
359+
| {- empty -} { [] }
384360

385361
-- | One or more occurrences of 'p', seperated by 'sep', optionally ending in 'sep'
386362
sep_by1T(p,sep) :: { Reversed NonEmpty _ }
387-
: sep_by1(p,sep) sep { $1 }
388-
| sep_by1(p,sep) { $1 }
363+
: sep_by1(p,sep) sep { $1 }
364+
| sep_by1(p,sep) { $1 }
389365

390366
-- | Zero or more occurences of 'p', seperated by 'sep', optionally ending in 'sep' (only if there
391367
-- is at least one 'p')
392368
sep_byT(p,sep) :: { [ _ ] }
393-
: sep_by1T(p,sep) { toList $1 }
394-
| {- empty -} { [] }
369+
: sep_by1T(p,sep) { toList $1 }
370+
| {- empty -} { [] }
395371

396372

397373
--------------------------
@@ -419,8 +395,8 @@ inner_attribute :: { Attribute Span }
419395

420396
-- TODO: for some precedence related reason, using 'some' here doesn't work
421397
inner_attrs :: { NonEmpty (Attribute Span) }
422-
: inner_attrs inner_attribute { $1 |> $2 }
423-
| inner_attribute { [$1] }
398+
: inner_attrs inner_attribute { $1 |> $2 }
399+
| inner_attribute { [$1] }
424400
425401
426402
--------------
@@ -453,16 +429,16 @@ string :: { Lit Span }
453429
qual_path(segs) :: { Spanned (QSelf Span, Path Span) }
454430
: '<' qual_path_suf(segs) { let Spanned x _ = $2 in Spanned x ($1 # $2) }
455431
| lt_ty_qual_path as ty_path '>' '::' segs {
456-
let Path g segsTy x = $3
457-
in Spanned (QSelf (unspan $1) (length segsTy), Path g (segsTy <> unspan $6) x) ($1 # $>)
432+
let Path g segsTy x = $3 in
433+
Spanned (QSelf (unspan $1) (length segsTy), Path g (segsTy <> unspan $6) x) ($1 # $>)
458434
}
459435
460436
-- Basically a qualified path, but ignoring the very first '<' token
461437
qual_path_suf(segs) :: { Spanned (QSelf Span, Path Span) }
462438
: ty '>' '::' segs { Spanned (QSelf $1 0, Path False (unspan $4) (spanOf $4)) ($1 # $>) }
463439
| ty as ty_path '>' '::' segs {
464-
let Path g segsTy x = $3
465-
in Spanned (QSelf $1 (length segsTy), Path g (segsTy <> unspan $6) x) ($1 # $>)
440+
let Path g segsTy x = $3 in
441+
Spanned (QSelf $1 (length segsTy), Path g (segsTy <> unspan $6) x) ($1 # $>)
466442
}
467443
468444
-- Usually qual_path_suf is for... type paths! This consumes these but with a starting '<<' token.
@@ -547,8 +523,8 @@ mod_path :: { Path Span }
547523
| self_or_ident { Path False [(unspan $1, NoParameters mempty)] (spanOf $1) }
548524
| '::' self_or_ident { Path True [(unspan $2, NoParameters mempty)] ($1 # $>) }
549525
| mod_path '::' ident {
550-
let Path g segs _ = $1
551-
in Path g (segs |> (unspan $3, NoParameters mempty)) ($1 # $>)
526+
let Path g segs _ = $1 in
527+
Path g (segs |> (unspan $3, NoParameters mempty)) ($1 # $>)
552528
}
553529

554530
-----------
@@ -624,8 +600,8 @@ for_ty_no_plus :: { Ty Span }
624600
| for_lts extern abi fn fn_decl(arg_general) { BareFn Normal $3 (unspan $1) $> ($1 # $>) }
625601
| for_lts fn fn_decl(arg_general) { BareFn Normal Rust (unspan $1) $> ($1 # $>) }
626602
| for_lts trait_ref {
627-
let poly = PolyTraitRef (unspan $1) $2 ($1 # $2)
628-
in TraitObject [TraitTyParamBound poly None ($1 # $2)] ($1 # $2)
603+
let poly = PolyTraitRef (unspan $1) $2 ($1 # $2) in
604+
TraitObject [TraitTyParamBound poly None ($1 # $2)] ($1 # $2)
629605
}
630606

631607
impl_ty :: { Ty Span }
@@ -1854,13 +1830,6 @@ parseSourceFile = do
18541830
(as,items) <- parseSourceFileContents
18551831
pure (SourceFile sh as items)
18561832

1857-
-- | Parse a partial source file
1858-
parseSourceFileP :: P (SourceFile Span)
1859-
parseSourceFileP = do
1860-
sh <- lexShebangLine
1861-
(as,items) <- parseSourceFileContentsP
1862-
pure (SourceFile sh as items)
1863-
18641833
-- | Nudge the span endpoints of a 'Span' value
18651834
nudge :: Int -> Int -> Span -> Span
18661835
nudge leftSide rightSide (Span l r) = Span l' r'

src/Language/Rust/Pretty.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,20 @@ import Data.Typeable (Typeable)
4747
writeSourceFile :: (Monoid a, Typeable a) => Handle -> SourceFile a -> IO ()
4848
writeSourceFile hdl = renderIO hdl . PP.layoutPretty (PP.LayoutOptions (PP.AvailablePerLine 100 1.0)) . pretty
4949

50+
-- | Resolve and pretty print. When in doubt, this is probably the function you want to use for
51+
-- pretty-printing.
5052
pretty :: (Resolve a, Pretty a) => a -> Doc b
5153
pretty x = case resolve x of
5254
Left desc -> error ("Failed to resolve: " ++ desc)
5355
Right y -> prettyUnresolved y
5456

57+
-- | Resolve and pretty print with annotations.
5558
prettyAnnotated :: (Resolve (f a), PrettyAnnotated f) => f a -> Doc a
5659
prettyAnnotated x = case resolve x of
5760
Left desc -> error ("Failed to resolve: " ++ desc)
5861
Right y -> prettyAnnUnresolved y
5962

63+
-- | Represents things that can be pretty printed
6064
class Pretty a where
6165
-- | Pretty-print the given value
6266
prettyUnresolved :: a -> Doc b

src/Language/Rust/Pretty/Util.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ neutral element for @<+>@, @hsep@, @<#>@, @vsep@, and @</>@.
2525
module Language.Rust.Pretty.Util where
2626

2727
import Data.Monoid ((<>))
28-
import Data.List (mapAccumL)
2928

3029
import qualified Data.Text.Prettyprint.Doc as PP
3130
import Data.Text.Prettyprint.Doc.Internal.Type (Doc(..))
@@ -156,8 +155,8 @@ block delim p s as xs = group' (lDel # PP.vsep (as' ++ ys) # rDel)
156155
_ -> [ PP.flatAlt (PP.indent n as) (flatten as) ]
157156

158157
-- list of entries
159-
ys = snd $ mapAccumL (\(z:zs) _ -> (zs, PP.flatAlt (PP.indent n z <> s)
160-
(flatten z <> unless (null zs) s)))
161-
xs xs
158+
ys = go xs where go [] = []
159+
go [z] = [ PP.flatAlt (PP.indent n z <> s) (flatten z) ]
160+
go (z:zs) = PP.flatAlt (PP.indent n z <> s) (flatten z <> s) : go zs
162161

163162

test/rustc-tests/Diff.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{-# LANGUAGE CPP, OverloadedStrings, OverloadedLists, InstanceSigs #-}
2-
#if __GASGOW_HASKELL__ >= 800
3-
{-# OPTIONS_GHC -Wall -Wno-orphans #-}
4-
#endif
2+
{-# OPTIONS_GHC -fno-warn-orphans #-}
53
module Diff where
64

75
import Data.Aeson

test/unit-tests/CompleteTest.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Language.Rust.Parser (parse, Span, inputStreamFromString)
1010
import Language.Rust.Pretty (pretty)
1111

1212
import Data.Text.Prettyprint.Doc (layoutPretty, LayoutOptions(..), PageWidth(..))
13-
import Data.Text.Prettyprint.Doc.Render.ShowS (renderShowS)
13+
import Data.Text.Prettyprint.Doc.Render.String (renderShowS)
1414

1515
-- The following tests render with width 50 and ribbon length 50 too.
1616
-- | |

0 commit comments

Comments
 (0)