never executed always true always false
    1 module GHC.Parser.Utils
    2     ( isStmt
    3     , hasImport
    4     , isImport
    5     , isDecl
    6     )
    7 where
    8 
    9 import GHC.Prelude
   10 import GHC.Hs
   11 import GHC.Data.StringBuffer
   12 import GHC.Data.FastString
   13 import GHC.Types.SrcLoc
   14 
   15 import qualified GHC.Parser.Lexer as Lexer (P (..), ParseResult(..), unP, initParserState)
   16 import GHC.Parser.Lexer (ParserOpts)
   17 import qualified GHC.Parser       as Parser (parseStmt, parseModule, parseDeclaration, parseImport)
   18 
   19 
   20 -- | Returns @True@ if passed string is a statement.
   21 isStmt :: ParserOpts -> String -> Bool
   22 isStmt pflags stmt =
   23   case parseThing Parser.parseStmt pflags stmt of
   24     Lexer.POk _ _ -> True
   25     Lexer.PFailed _ -> False
   26 
   27 -- | Returns @True@ if passed string has an import declaration.
   28 hasImport :: ParserOpts -> String -> Bool
   29 hasImport pflags stmt =
   30   case parseThing Parser.parseModule pflags stmt of
   31     Lexer.POk _ thing -> hasImports thing
   32     Lexer.PFailed _ -> False
   33   where
   34     hasImports = not . null . hsmodImports . unLoc
   35 
   36 -- | Returns @True@ if passed string is an import declaration.
   37 isImport :: ParserOpts -> String -> Bool
   38 isImport pflags stmt =
   39   case parseThing Parser.parseImport pflags stmt of
   40     Lexer.POk _ _ -> True
   41     Lexer.PFailed _ -> False
   42 
   43 -- | Returns @True@ if passed string is a declaration but __/not a splice/__.
   44 isDecl :: ParserOpts -> String -> Bool
   45 isDecl pflags stmt =
   46   case parseThing Parser.parseDeclaration pflags stmt of
   47     Lexer.POk _ thing ->
   48       case unLoc thing of
   49         SpliceD _ _ -> False
   50         _ -> True
   51     Lexer.PFailed _ -> False
   52 
   53 parseThing :: Lexer.P thing -> ParserOpts -> String -> Lexer.ParseResult thing
   54 parseThing parser opts stmt = do
   55   let buf = stringToStringBuffer stmt
   56       loc = mkRealSrcLoc (fsLit "<interactive>") 1 1
   57 
   58   Lexer.unP parser (Lexer.initParserState opts buf loc)