[LIGO-39] Parse contracts in directory

Problem: We want to be able to parse whole directory for ligo
contracts for testing purposes.

Solution: Add `parseContracts` function that returns `ParsedContract`
data that ignores every file which is not a ligo contract.
This commit is contained in:
Anton Myasnikov 2020-08-25 17:29:40 +03:00
parent fd862676b3
commit f0005c982a
No known key found for this signature in database
GPG Key ID: FEB685E6DAA0A95F
2 changed files with 34 additions and 0 deletions

View File

@ -32,8 +32,15 @@ import Duplo.Tree (collect)
import AST hiding (def)
import qualified AST.Find as Find
import Data.Maybe (fromMaybe)
import Extension
import Parser
import Product
import Range
import System.Directory
import System.FilePath
import System.Posix.Files
-- import Error
main :: IO ()
@ -283,6 +290,31 @@ collectErrors funs uri path version = do
Nothing -> error "TODO: implement URI file loading"
data ParsedContract = ParsedContract
{ cPath :: FilePath
, cTree :: LIGO Info
, cErr :: [Msg]
}
-- | Parse whole directory for ligo contracts and collect the results.
-- This ignores every other file which is not a contract.
parseContracts :: FilePath -> IO [ParsedContract]
parseContracts top = let
exclude p = p /= "." && p /= ".." in do
ds <- getDirectoryContents top
contracts <- forM (filter exclude ds) $ \d -> do
let p = top </> d
s <- getFileStatus p
if isDirectory s
then parseContracts p
else do
putStrLn $ "parsing: " ++ show p
contract <- try @UnsupportedExtension $ parse (Path p)
case contract of
Right (tree, errs) -> return $ [ParsedContract p tree errs]
Left _ -> return []
return (concat contracts)
errorToDiag :: (Range, Err Text a) -> J.Diagnostic
errorToDiag (getRange -> (Range (sl, sc, _) (el, ec, _) _), Err what) =
J.Diagnostic

View File

@ -76,6 +76,8 @@ executables:
- interpolate
- lens
- ligo-squirrel
- directory
- unix
- stm
main: Main.hs