From f0005c982a13293c3c098187b16a503b7a426e30 Mon Sep 17 00:00:00 2001 From: Anton Myasnikov Date: Tue, 25 Aug 2020 17:29:40 +0300 Subject: [PATCH] [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. --- tools/lsp/squirrel/app/Main.hs | 32 ++++++++++++++++++++++++++++++++ tools/lsp/squirrel/package.yaml | 2 ++ 2 files changed, 34 insertions(+) diff --git a/tools/lsp/squirrel/app/Main.hs b/tools/lsp/squirrel/app/Main.hs index 9eaf7107b..708050cca 100644 --- a/tools/lsp/squirrel/app/Main.hs +++ b/tools/lsp/squirrel/app/Main.hs @@ -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 diff --git a/tools/lsp/squirrel/package.yaml b/tools/lsp/squirrel/package.yaml index c6d3dd66c..ad8c0a5e1 100644 --- a/tools/lsp/squirrel/package.yaml +++ b/tools/lsp/squirrel/package.yaml @@ -76,6 +76,8 @@ executables: - interpolate - lens - ligo-squirrel + - directory + - unix - stm main: Main.hs