Commit 6038faf1 authored by Andrew Gibiansky's avatar Andrew Gibiansky

fixed issues with decl parsing

parent 11130856
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"celltoolbar": "Hiding", "celltoolbar": "Hiding",
"language": "haskell", "language": "haskell",
"name": "", "name": "",
"signature": "sha256:1a476f19eca338ce4e01b81006d0a165e437bdcebbbea6d1ac10e0cd217740ee" "signature": "sha256:15cf13f0b51aedf4b16cbd0be35d8f1f0e7cca10ef18411c270d26f993eda4a7"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
...@@ -44,22 +44,16 @@ ...@@ -44,22 +44,16 @@
"f :: Int -> Int\n", "f :: Int -> Int\n",
"f x = x + x\n", "f x = x + x\n",
"\n", "\n",
"f 3 <<>> f 3" "f 3 <<>> f 3\n",
"\n",
"f :: Int -> Int\n",
"f x = x + x"
], ],
"language": "python", "language": "python",
"metadata": { "metadata": {
"hidden": false "hidden": false
}, },
"outputs": [ "outputs": []
{
"metadata": {},
"output_type": "display_data",
"text": [
"18"
]
}
],
"prompt_number": 2
}, },
{ {
"cell_type": "code", "cell_type": "code",
......
...@@ -25,6 +25,7 @@ import Bag ...@@ -25,6 +25,7 @@ import Bag
import ErrUtils hiding (ErrMsg) import ErrUtils hiding (ErrMsg)
import FastString import FastString
import GHC hiding (Located) import GHC hiding (Located)
import GhcMonad
import Lexer import Lexer
import OrdList import OrdList
import Outputable hiding ((<>)) import Outputable hiding ((<>))
...@@ -66,7 +67,7 @@ data DirectiveType ...@@ -66,7 +67,7 @@ data DirectiveType
deriving (Show, Eq) deriving (Show, Eq)
-- | Parse a string into code blocks. -- | Parse a string into code blocks.
parseString :: GhcMonad m => String -> m [Located CodeBlock] parseString :: String -> Ghc [Located CodeBlock]
parseString codeString = do parseString codeString = do
-- Try to parse this as a single module. -- Try to parse this as a single module.
flags <- getSessionDynFlags flags <- getSessionDynFlags
...@@ -77,6 +78,7 @@ parseString codeString = do ...@@ -77,6 +78,7 @@ parseString codeString = do
-- Split input into chunks based on indentation. -- Split input into chunks based on indentation.
let chunks = layoutChunks $ removeComments codeString let chunks = layoutChunks $ removeComments codeString
result <- joinFunctions <$> processChunks [] chunks result <- joinFunctions <$> processChunks [] chunks
liftIO $ print result
-- Return to previous flags. When parsing, flags can be set to make -- Return to previous flags. When parsing, flags can be set to make
-- sure parsing works properly. But we don't want those flags to be -- sure parsing works properly. But we don't want those flags to be
...@@ -190,7 +192,10 @@ parseCodeChunk code startLine = do ...@@ -190,7 +192,10 @@ parseCodeChunk code startLine = do
-- signature, which is also joined with the subsequent declarations. -- signature, which is also joined with the subsequent declarations.
joinFunctions :: [Located CodeBlock] -> [Located CodeBlock] joinFunctions :: [Located CodeBlock] -> [Located CodeBlock]
joinFunctions [] = [] joinFunctions [] = []
joinFunctions blocks = Located lnum (conjoin $ map unloc decls) : joinFunctions rest joinFunctions blocks =
if signatureOrDecl $ unloc $ head blocks
then Located lnum (conjoin $ map unloc decls) : joinFunctions rest
else head blocks : joinFunctions (tail blocks)
where where
decls = takeWhile (signatureOrDecl . unloc) blocks decls = takeWhile (signatureOrDecl . unloc) blocks
rest = drop (length decls) blocks rest = drop (length decls) blocks
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment