-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
(Infix|Prefix)Operator
(#910)
- Loading branch information
Showing
10 changed files
with
126 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module HIndent.Ast.Operator.Infix | ||
( InfixOperator | ||
, mkInfixOperator | ||
) where | ||
|
||
import Data.Maybe | ||
import qualified GHC.Types.Name as GHC | ||
import qualified GHC.Types.Name.Reader as GHC | ||
import qualified GHC.Unit.Module as GHC | ||
import HIndent.Ast.NodeComments | ||
import {-# SOURCE #-} HIndent.Pretty | ||
import HIndent.Pretty.Combinators | ||
import HIndent.Pretty.NodeComments | ||
|
||
data InfixOperator = InfixOperator | ||
{ name :: GHC.OccName | ||
, moduleName :: Maybe GHC.ModuleName | ||
, backtick :: Bool | ||
} | ||
|
||
instance CommentExtraction InfixOperator where | ||
nodeComments InfixOperator {} = NodeComments [] [] [] | ||
|
||
instance Pretty InfixOperator where | ||
pretty' InfixOperator {..} = | ||
wrap $ hDotSep $ catMaybes [pretty <$> moduleName, Just $ pretty name] | ||
where | ||
wrap = | ||
if backtick | ||
then backticks | ||
else id | ||
|
||
mkInfixOperator :: GHC.RdrName -> InfixOperator | ||
mkInfixOperator (GHC.Unqual name) = | ||
InfixOperator name Nothing (backticksNeeded name) | ||
mkInfixOperator (GHC.Qual modName name) = | ||
InfixOperator name (Just modName) (backticksNeeded name) | ||
mkInfixOperator (GHC.Orig {}) = | ||
error "This AST node should not appear in the parser output." | ||
mkInfixOperator (GHC.Exact name) = | ||
InfixOperator (GHC.occName name) Nothing (backticksNeeded $ GHC.occName name) | ||
|
||
backticksNeeded :: GHC.OccName -> Bool | ||
backticksNeeded = not . GHC.isSymOcc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module HIndent.Ast.Operator.Prefix | ||
( PrefixOperator | ||
, mkPrefixOperator | ||
) where | ||
|
||
import Data.Maybe | ||
import qualified GHC.Types.Name as GHC | ||
import qualified GHC.Types.Name.Reader as GHC | ||
import qualified GHC.Unit.Module as GHC | ||
import HIndent.Ast.NodeComments | ||
import {-# SOURCE #-} HIndent.Pretty | ||
import HIndent.Pretty.Combinators | ||
import HIndent.Pretty.NodeComments | ||
|
||
data PrefixOperator = PrefixOperator | ||
{ name :: String | ||
, moduleName :: Maybe GHC.ModuleName | ||
, parentheses :: Bool | ||
} | ||
|
||
instance CommentExtraction PrefixOperator where | ||
nodeComments PrefixOperator {} = NodeComments [] [] [] | ||
|
||
instance Pretty PrefixOperator where | ||
pretty' PrefixOperator {..} = | ||
wrap $ hDotSep $ catMaybes [pretty <$> moduleName, Just $ string name] | ||
where | ||
wrap = | ||
if parentheses | ||
then parens | ||
else id | ||
|
||
mkPrefixOperator :: GHC.RdrName -> PrefixOperator | ||
mkPrefixOperator (GHC.Unqual name) = | ||
PrefixOperator (showOutputable name) Nothing (parensNeeded name) | ||
mkPrefixOperator (GHC.Qual modName name) = | ||
PrefixOperator (showOutputable name) (Just modName) (parensNeeded name) | ||
mkPrefixOperator (GHC.Orig {}) = | ||
error "This AST node should not appear in the parser output." | ||
mkPrefixOperator (GHC.Exact name) = | ||
PrefixOperator (showOutputable name) Nothing (parensNeeded $ GHC.occName name) | ||
|
||
parensNeeded :: GHC.OccName -> Bool | ||
parensNeeded = GHC.isSymOcc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters