Skip to content

Commit

Permalink
#19 Allow parsers to be IO functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalh committed Jun 27, 2019
1 parent 63bc293 commit dbad0d0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
8 changes: 7 additions & 1 deletion src/core/Language/Astview/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Language.Astview.Language
, Ast(..)
, Error (..)
, SrcLocException(..)
, Parser(..)
)
where
import Prelude hiding (span)
Expand Down Expand Up @@ -59,6 +60,11 @@ instance Show AstNode where
-- |An (untyped) abstract syntax tree is just a tree of 'AstNode's.
newtype Ast = Ast { ast :: Tree AstNode }

-- |A parser is a function which transforms a string into an untyped abstract
-- syntax tree.
data Parser = PureParser (String -> Either Error Ast)
| IoParser (String -> IO (Either Error Ast))

-- |A value of 'Language' states how files (associated with this language by
-- their file extentions 'exts') are being parsed.
-- The file extentions of all languages known to astview may overlap, since
Expand All @@ -70,7 +76,7 @@ data Language = Language
-- ^ (kate) syntax highlighter name. Use @[]@ if no highlighting is desired.
, exts :: [String]
-- ^ file extentions which should be associated with this language
, parse :: String -> Either Error Ast -- ^ parse function
, parser :: Parser -- ^ parse function
}

-- |Since parsers return different
Expand Down
4 changes: 2 additions & 2 deletions src/core/Language/Astview/Languages/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Language.Astview.Languages.Haskell (haskellExts) where
import Prelude hiding (span)

import Language.Astview.DataTree (dataToAstIgnoreByExample)
import Language.Astview.Language hiding (parse)
import Language.Astview.Language

import Data.Generics (Data, extQ)
import Data.Generics.Zipper (down', query, toZipper)
Expand All @@ -11,7 +11,7 @@ import Language.Haskell.Exts.Parser (ParseResult (..),parseM
import qualified Language.Haskell.Exts.SrcLoc as HsSrcLoc

haskellExts :: Language
haskellExts = Language "Haskell" "Haskell" [".hs"] parsehs
haskellExts = Language "Haskell" "Haskell" [".hs"] (PureParser parsehs)

parsehs :: String -> Either Error Ast
parsehs s = case parseModule s of
Expand Down
24 changes: 12 additions & 12 deletions src/core/Language/Astview/Languages/HaskellCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Generics.Zipper (down', query, toZipper)
import Data.Tree (Tree (Node))

import Language.Astview.DataTree (manual)
import Language.Astview.Language hiding (parse)
import Language.Astview.Language

import qualified DynFlags as GHC
import FastString
Expand All @@ -24,16 +24,16 @@ import StringBuffer

import GHC.Paths (libdir)

import System.IO.Unsafe

haskellCore :: Language
haskellCore = Language "HaskellCore" "Haskell" [".hs"] parsehs
haskellCore = Language "HaskellCore" "Haskell" [".hs"] (IoParser parsehs)

parsehs :: String -> Either Error Ast
parsehs s =
case runParser s parseModule of
POk _ parsed -> Right (coreToAst parsed)
PFailed ss msg -> Left $ makeError ss (showSDoc (unsafePerformIO getDynFlags) msg)
parsehs :: String -> IO (Either Error Ast)
parsehs s = do
dynFlags <- getDynFlags
return $
case runParser dynFlags s parseModule of
POk _ parsed -> Right (coreToAst parsed)
PFailed ss msg -> Left $ makeError ss (showSDoc dynFlags msg)

makeError :: GHC.SrcSpan -> String -> Error
makeError ss s =
Expand All @@ -51,13 +51,13 @@ ghcss2ss real
(GHC.srcLocLine end)
(GHC.srcLocCol end)

runParser :: String -> P a -> ParseResult a
runParser str parser = unP parser parseState
runParser :: GHC.DynFlags -> String -> P a -> ParseResult a
runParser flags str parser = unP parser parseState
where
filename = "<interactive>"
location = GHC.mkRealSrcLoc (mkFastString filename) 1 1
buffer = stringToStringBuffer str
parseState = mkPState (unsafePerformIO getDynFlags) buffer location
parseState = mkPState flags buffer location

getDynFlags :: IO GHC.DynFlags
getDynFlags =
Expand Down
2 changes: 1 addition & 1 deletion src/core/Language/Astview/Languages/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.Generics (Data,extQ)
import Data.Generics.Zipper(toZipper,down,query)

python :: Language
python = Language "Python" "Python" [".py"] parsePy
python = Language "Python" "Python" [".py"] (PureParser parsePy)

parsePy :: String -> Either Error Ast
parsePy s = case parseModule s [] of
Expand Down
5 changes: 4 additions & 1 deletion src/gui/Language/Astview/Gui/Actions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ actionGetAst :: Language -> AstAction (Either Error Ast)
actionGetAst l = do
plain <- getText
flattening <- getFlattenLists
return $ (if flattening then flatten else id) <$> parse l plain
errOrAst <- case parser l of
PureParser p -> return $ p plain
IoParser p -> liftIO $ p plain
return $ (if flattening then flatten else id) <$> errOrAst

-- | parses the contents of the sourceview with the selected language
actionParse :: Language -> AstAction (Tree String)
Expand Down

0 comments on commit dbad0d0

Please sign in to comment.