Skip to content

Megaparsec 7 Compatibility Fix #21

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ lambdacube-compiler-test-suite.tix
.ghc.environment.*
.dir-locals.el
.stack-work
cabal.project
20 changes: 13 additions & 7 deletions src/LambdaCube/Compiler/Lexer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module LambdaCube.Compiler.Lexer
) where

import Data.List
import Data.List.NonEmpty (fromList)
import Data.List.NonEmpty (NonEmpty, fromList)
import Data.Char
import qualified Data.Set as Set
import Data.Void
Expand All @@ -34,6 +34,7 @@ import LambdaCube.Compiler.DesugaredSource

-------------------------------------------------------------------------------- utils


-- try with error handling
-- see http://blog.ezyang.com/2014/05/parsec-try-a-or-b-considered-harmful/comment-page-1/#comment-6602
try_ :: String -> Parse r w a -> Parse r w a
Expand All @@ -43,7 +44,7 @@ toSPos :: SourcePos -> SPos
toSPos p = SPos (fromIntegral $ unPos $ sourceLine p) (fromIntegral $ unPos $ sourceColumn p)

getSPos :: Parse r w SPos
getSPos = toSPos <$> getPosition
getSPos = toSPos <$> getSourcePos

-------------------------------------------------------------------------------- literals

Expand Down Expand Up @@ -113,21 +114,26 @@ data ParseEnv r = ParseEnv
, indentationLevel :: SPos
}

type ParseState r = (ParseEnv r, P.State String)
type ParseState r = (ParseEnv r, P.State String (ErrorFancy Void))

parseState :: FileInfo -> r -> ParseState r
parseState fi di = (ParseEnv fi di ExpNS (SPos 0 0), either (error "impossible") id $ runParser (getParserState :: Parsec (ErrorFancy Void) String (P.State String)) (filePath fi) (fileContent fi))
parseState fi di = (ParseEnv fi di ExpNS (SPos 0 0), either (error "impossible") id $ runParser (getParserState :: Parsec (ErrorFancy Void) String (P.State String (ErrorFancy Void))) (filePath fi) (fileContent fi))

--type Parse r w = ReaderT (ParseEnv r) (WriterT [w] (StateT SPos (Parsec String)))
type Parse r w = RWST (ParseEnv r) [w] SPos (Parsec (ErrorFancy Void) String)

newtype ParseError = ParseErr (P.ParseError (Token String) (ErrorFancy Void))
newtype ParseError = ParseErr (P.ParseError String (ErrorFancy Void))

instance (ShowErrorComponent v, Show v) =>
ShowErrorComponent (ErrorFancy v) where
showErrorComponent (ErrorCustom e) = showErrorComponent e
showErrorComponent x = show x

instance Show ParseError where
show (ParseErr e) = parseErrorPretty e

runParse :: Parse r w a -> ParseState r -> Either ParseError (a, [w])
runParse p (env, st) = left ParseErr . snd . flip runParser' st $ evalRWST p env (error "spos")
runParse :: Parse r w a -> ParseState r -> Either (NonEmpty ParseError) (a, [w])
runParse p (env, st) = left (fmap ParseErr . bundleErrors) . snd . flip runParser' st $ evalRWST p env (error "spos")

getParseState :: Parse r w (ParseState r)
getParseState = (,) <$> ask <*> getParserState
Expand Down
9 changes: 5 additions & 4 deletions src/LambdaCube/Compiler/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module LambdaCube.Compiler.Parser
import Data.Monoid
import Data.Maybe
import Data.List
import Data.List.NonEmpty (NonEmpty, fromList)
import Data.Char
import qualified Data.Map as Map
import qualified Data.Set as Set
Expand Down Expand Up @@ -53,7 +54,7 @@ data LCParseError
= MultiplePatternVars [[SIName]]
| OperatorMismatch SIName SIName
| UndefinedConstructor SIName
| ParseError ParseError
| ParseError (NonEmpty ParseError)

data ParseWarning
= Unreachable Range
Expand Down Expand Up @@ -621,7 +622,7 @@ parseExtensions

type Module = Module_ DefParser

type DefParser = DesugarInfo -> Either ParseError ([Stmt], [PostponedCheck])
type DefParser = DesugarInfo -> Either (NonEmpty ParseError) ([Stmt], [PostponedCheck])

type HeaderParser = Parse () ()

Expand Down Expand Up @@ -655,15 +656,15 @@ parseModule = do
, definitions = \ge -> runParse (parseDefs SLHS <* eof) (env { desugarInfo = ge }, st)
}

parseLC :: FileInfo -> Either ParseError Module
parseLC :: FileInfo -> Either (NonEmpty ParseError) Module
parseLC fi
= fmap fst $ runParse parseModule $ parseState fi ()

runDefParser :: (MonadFix m, MonadError LCParseError m) => DesugarInfo -> DefParser -> m ([Stmt], [ParseWarning], DesugarInfo)
runDefParser ds_ dp = do

(defs, dns, ds) <- mfix $ \ ~(_, _, ds) -> do
let x :: Either ParseError ([Stmt], [PostponedCheck])
let x :: Either (NonEmpty ParseError) ([Stmt], [PostponedCheck])
x = dp (ds <> ds_)
(defs, dns) <- either (throwError . ParseError) return x
return (defs, dns, mkDesugarInfo defs)
Expand Down