fixed issues with decl parsing

This commit is contained in:
Andrew Gibiansky 2014-05-18 00:11:42 -07:00
parent 11130856b6
commit 6038faf1a3
2 changed files with 13 additions and 14 deletions

View File

@ -3,7 +3,7 @@
"celltoolbar": "Hiding",
"language": "haskell",
"name": "",
"signature": "sha256:1a476f19eca338ce4e01b81006d0a165e437bdcebbbea6d1ac10e0cd217740ee"
"signature": "sha256:15cf13f0b51aedf4b16cbd0be35d8f1f0e7cca10ef18411c270d26f993eda4a7"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -44,22 +44,16 @@
"f :: Int -> Int\n",
"f x = x + x\n",
"\n",
"f 3 <<>> f 3"
"f 3 <<>> f 3\n",
"\n",
"f :: Int -> Int\n",
"f x = x + x"
],
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"18"
]
}
],
"prompt_number": 2
"outputs": []
},
{
"cell_type": "code",

View File

@ -25,6 +25,7 @@ import Bag
import ErrUtils hiding (ErrMsg)
import FastString
import GHC hiding (Located)
import GhcMonad
import Lexer
import OrdList
import Outputable hiding ((<>))
@ -66,7 +67,7 @@ data DirectiveType
deriving (Show, Eq)
-- | Parse a string into code blocks.
parseString :: GhcMonad m => String -> m [Located CodeBlock]
parseString :: String -> Ghc [Located CodeBlock]
parseString codeString = do
-- Try to parse this as a single module.
flags <- getSessionDynFlags
@ -77,6 +78,7 @@ parseString codeString = do
-- Split input into chunks based on indentation.
let chunks = layoutChunks $ removeComments codeString
result <- joinFunctions <$> processChunks [] chunks
liftIO $ print result
-- 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
@ -190,7 +192,10 @@ parseCodeChunk code startLine = do
-- signature, which is also joined with the subsequent declarations.
joinFunctions :: [Located CodeBlock] -> [Located CodeBlock]
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
decls = takeWhile (signatureOrDecl . unloc) blocks
rest = drop (length decls) blocks