Skip to content

Commit

Permalink
Change to 'prettyprinter', remove 'InlineAsm'
Browse files Browse the repository at this point in the history
Move pretty printing facility to 'prettyprinter' library.

Remove 'InlineAsm' since we have no good way of parsing it, we don't
represent it properly, and we don't support 'GlobalAsm'. This is
tracked in #24.
  • Loading branch information
harpocrates committed Jun 21, 2017
1 parent 0459efc commit 845e631
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 292 deletions.
5 changes: 3 additions & 2 deletions language-rust.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ library
Language.Rust.Parser.Reversed
Language.Rust.Pretty.Resolve
Language.Rust.Pretty.Literals
Language.Rust.Pretty.Util
Language.Rust.Syntax.AST
Language.Rust.Syntax.Ident
Language.Rust.Syntax.Token
Expand All @@ -62,7 +63,7 @@ library
, DeriveFunctor

build-depends: base >=4.9 && <5.0
, wl-pprint-annotated >=0.1.0.0 && <0.2.0.0
, prettyprinter >=1.1
, transformers >=0.5 && <0.6
, array >=0.5 && <0.6
, deepseq >=1.4.2.0
Expand All @@ -89,7 +90,7 @@ test-suite unit-tests
default-language: Haskell2010
build-depends: base >=4.9 && <5.0
, HUnit >=1.5.0.0
, wl-pprint-annotated >=0.1.0.0 && <0.2.0.0
, prettyprinter >=1.1
, test-framework >=0.8.0
, test-framework-hunit >= 0.3.0
, language-rust
Expand Down
1 change: 0 additions & 1 deletion src/Language/Rust/Parser/Internal.y
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,6 @@ addAttrs as (AddrOf as' m e s) = AddrOf (as ++ as') m e s
addAttrs as (Break as' l e s) = Break (as ++ as') l e s
addAttrs as (Continue as' l s) = Continue (as ++ as') l s
addAttrs as (Ret as' e s) = Ret (as ++ as') e s
addAttrs as (InlineAsmExpr as' a s) = InlineAsmExpr (as ++ as') a s
addAttrs as (MacExpr as' m s) = MacExpr (as ++ as') m s
addAttrs as (Struct as' p f e a) = Struct (as ++ as') p f e a
addAttrs as (Repeat as' e1 e2 s) = Repeat (as ++ as') e1 e2 s
Expand Down
83 changes: 37 additions & 46 deletions src/Language/Rust/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub fn foo(&self) -> ! { }
it :: Doc a
-}
{-# OPTIONS_GHC -Wall -fno-warn-orphans #-}

module Language.Rust.Pretty (
PrettyAnnotated(..), Pretty(..), Resolve(..), Doc, writeSourceFile
Pretty(..), PrettyAnnotated(..), Resolve(..), Doc, writeSourceFile
) where

import Language.Rust.Data.Position
Expand All @@ -35,19 +36,13 @@ import Language.Rust.Syntax.Ident
import Language.Rust.Pretty.Internal
import Language.Rust.Pretty.Resolve

import Text.PrettyPrint.Annotated.WL (Doc, noAnnotate, text, displayIO, renderPretty)
import Data.Text.Prettyprint.Doc (Doc, Pretty(..), unAnnotate, layoutPretty, LayoutOptions(..), PageWidth(..))
import Data.Text.Prettyprint.Doc.Render.Text (renderIO)
import System.IO (Handle)

-- | Given a handle, write into it the given 'SourceFile' (with file width will be 100).
writeSourceFile :: Handle -> SourceFile a -> IO ()
writeSourceFile hdl = displayIO hdl . renderPretty 1.0 100 . pretty

-- | Class of things that can be pretty printed (without any annotations). The is very similar to
-- the class defined in 'wl-pprint-annotated' itself. However, in order to avoid having orphan
-- instances or extra instance that don't make sense, we are redefining it.
class Pretty p where
-- | Pretty-print the given value without any annotations.
pretty :: p -> Doc a
writeSourceFile hdl = renderIO hdl . layoutPretty (LayoutOptions (AvailablePerLine 100 1.0)) . pretty

instance Pretty Abi where pretty = printAbi
instance Pretty BindingMode where pretty = printBindingMode
Expand All @@ -62,40 +57,38 @@ instance Pretty TokenTree where pretty = printTt
instance Pretty TokenStream where pretty = printTokenStream
instance Pretty UnOp where pretty = printUnOp
instance Pretty Unsafety where pretty = printUnsafety
instance Pretty (Attribute a) where pretty = noAnnotate . prettyAnn
instance Pretty (Block a) where pretty = noAnnotate . prettyAnn
instance Pretty (SourceFile a) where pretty = noAnnotate . prettyAnn
instance Pretty (Expr a) where pretty = noAnnotate . prettyAnn
instance Pretty (Field a) where pretty = noAnnotate . prettyAnn
instance Pretty (FieldPat a) where pretty = noAnnotate . prettyAnn
instance Pretty (FnDecl a) where pretty = noAnnotate . prettyAnn
instance Pretty (ForeignItem a) where pretty = noAnnotate . prettyAnn
instance Pretty (Generics a) where pretty = noAnnotate . prettyAnn
instance Pretty (ImplItem a) where pretty = noAnnotate . prettyAnn
instance Pretty (InlineAsm a) where pretty = noAnnotate . prettyAnn
instance Pretty (InlineAsmOutput a) where pretty = noAnnotate . prettyAnn
instance Pretty (Item a) where pretty = noAnnotate . prettyAnn
instance Pretty (Lifetime a) where pretty = noAnnotate . prettyAnn
instance Pretty (LifetimeDef a) where pretty = noAnnotate . prettyAnn
instance Pretty (Lit a) where pretty = noAnnotate . prettyAnn
instance Pretty (Nonterminal a) where pretty = noAnnotate . prettyAnn
instance Pretty (Pat a) where pretty = noAnnotate . prettyAnn
instance Pretty (Path a) where pretty = noAnnotate . prettyAnn
instance Pretty (PolyTraitRef a) where pretty = noAnnotate . prettyAnn
instance Pretty (Stmt a) where pretty = noAnnotate . prettyAnn
instance Pretty (StructField a) where pretty = noAnnotate . prettyAnn
instance Pretty (TraitItem a) where pretty = noAnnotate . prettyAnn
instance Pretty (TraitRef a) where pretty = noAnnotate . prettyAnn
instance Pretty (Ty a) where pretty = noAnnotate . prettyAnn
instance Pretty (TyParam a) where pretty = noAnnotate . prettyAnn
instance Pretty (TyParamBound a) where pretty = noAnnotate . prettyAnn
instance Pretty (Variant a) where pretty = noAnnotate . prettyAnn
instance Pretty (ViewPath a) where pretty = noAnnotate . prettyAnn
instance Pretty (Visibility a) where pretty = noAnnotate . prettyAnn
instance Pretty (WhereClause a) where pretty = noAnnotate . prettyAnn
instance Pretty (WherePredicate a) where pretty = noAnnotate . prettyAnn
instance Pretty Position where pretty = text . prettyPosition
instance Pretty Span where pretty = text . prettySpan
instance Pretty (Attribute a) where pretty = unAnnotate . prettyAnn
instance Pretty (Block a) where pretty = unAnnotate . prettyAnn
instance Pretty (SourceFile a) where pretty = unAnnotate . prettyAnn
instance Pretty (Expr a) where pretty = unAnnotate . prettyAnn
instance Pretty (Field a) where pretty = unAnnotate . prettyAnn
instance Pretty (FieldPat a) where pretty = unAnnotate . prettyAnn
instance Pretty (FnDecl a) where pretty = unAnnotate . prettyAnn
instance Pretty (ForeignItem a) where pretty = unAnnotate . prettyAnn
instance Pretty (Generics a) where pretty = unAnnotate . prettyAnn
instance Pretty (ImplItem a) where pretty = unAnnotate . prettyAnn
instance Pretty (Item a) where pretty = unAnnotate . prettyAnn
instance Pretty (Lifetime a) where pretty = unAnnotate . prettyAnn
instance Pretty (LifetimeDef a) where pretty = unAnnotate . prettyAnn
instance Pretty (Lit a) where pretty = unAnnotate . prettyAnn
instance Pretty (Nonterminal a) where pretty = unAnnotate . prettyAnn
instance Pretty (Pat a) where pretty = unAnnotate . prettyAnn
instance Pretty (Path a) where pretty = unAnnotate . prettyAnn
instance Pretty (PolyTraitRef a) where pretty = unAnnotate . prettyAnn
instance Pretty (Stmt a) where pretty = unAnnotate . prettyAnn
instance Pretty (StructField a) where pretty = unAnnotate . prettyAnn
instance Pretty (TraitItem a) where pretty = unAnnotate . prettyAnn
instance Pretty (TraitRef a) where pretty = unAnnotate . prettyAnn
instance Pretty (Ty a) where pretty = unAnnotate . prettyAnn
instance Pretty (TyParam a) where pretty = unAnnotate . prettyAnn
instance Pretty (TyParamBound a) where pretty = unAnnotate . prettyAnn
instance Pretty (Variant a) where pretty = unAnnotate . prettyAnn
instance Pretty (ViewPath a) where pretty = unAnnotate . prettyAnn
instance Pretty (Visibility a) where pretty = unAnnotate . prettyAnn
instance Pretty (WhereClause a) where pretty = unAnnotate . prettyAnn
instance Pretty (WherePredicate a) where pretty = unAnnotate . prettyAnn
instance Pretty Position where pretty = pretty . prettyPosition
instance Pretty Span where pretty = pretty . prettySpan

-- | Similar to 'Pretty', but for types which are parametrized over an annotation type.
class PrettyAnnotated p where
Expand All @@ -113,8 +106,6 @@ instance PrettyAnnotated FnDecl where prettyAnn = printFnArgsAndRet
instance PrettyAnnotated ForeignItem where prettyAnn = printForeignItem
instance PrettyAnnotated Generics where prettyAnn = printGenerics
instance PrettyAnnotated ImplItem where prettyAnn = printImplItem
instance PrettyAnnotated InlineAsm where prettyAnn = printInlineAsm
instance PrettyAnnotated InlineAsmOutput where prettyAnn = printInlineAsmOutput
instance PrettyAnnotated Item where prettyAnn = printItem
instance PrettyAnnotated Lifetime where prettyAnn = printLifetime
instance PrettyAnnotated LifetimeDef where prettyAnn = printLifetimeDef
Expand Down
Loading

0 comments on commit 845e631

Please sign in to comment.