2020-01-10 23:14:05 +03:00
|
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
2020-01-10 20:22:13 +03:00
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2020-01-08 21:02:53 +03:00
|
|
|
|
module Main (main) where
|
2020-01-08 20:51:37 +03:00
|
|
|
|
|
2020-01-10 23:14:05 +03:00
|
|
|
|
import Control.Algebra
|
|
|
|
|
import Control.Carrier.Sketch.Fresh
|
2020-01-10 20:22:13 +03:00
|
|
|
|
import Control.Monad
|
2020-01-08 21:13:48 +03:00
|
|
|
|
import qualified Data.ByteString as ByteString
|
2020-01-08 21:25:43 +03:00
|
|
|
|
import qualified Data.ScopeGraph as ScopeGraph
|
|
|
|
|
import qualified Language.Python ()
|
2020-01-10 20:22:13 +03:00
|
|
|
|
import Source.Loc
|
|
|
|
|
import qualified Source.Source as Source
|
|
|
|
|
import System.Exit (die)
|
2020-01-13 18:08:07 +03:00
|
|
|
|
import qualified System.Path as Path
|
2020-01-08 21:13:48 +03:00
|
|
|
|
import qualified TreeSitter.Python as TSP
|
2020-01-10 20:22:13 +03:00
|
|
|
|
import qualified TreeSitter.Python.AST as Py
|
2020-01-08 21:13:48 +03:00
|
|
|
|
import qualified TreeSitter.Unmarshal as TS
|
2020-01-10 20:22:13 +03:00
|
|
|
|
|
|
|
|
|
{-
|
|
|
|
|
|
|
|
|
|
The Python code here is
|
|
|
|
|
|
|
|
|
|
hello = ()
|
|
|
|
|
goodbye = ()
|
|
|
|
|
|
|
|
|
|
The graph should be
|
|
|
|
|
|
|
|
|
|
🏁
|
|
|
|
|
|
|
|
|
|
|
1️⃣----"hello"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2️⃣----"goodbye"
|
|
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
2020-01-13 18:08:07 +03:00
|
|
|
|
|
|
|
|
|
runScopeGraph :: ScopeGraph.ToScopeGraph t => Path.AbsRelFile -> Source.Source -> t Loc -> ScopeGraph.ScopeGraph ScopeGraph.Info
|
|
|
|
|
runScopeGraph p _src item = fst . run . runSketch (Just p) $ ScopeGraph.scopeGraph item
|
|
|
|
|
|
2020-01-11 01:46:37 +03:00
|
|
|
|
sampleGraphThing :: (Has (Sketch ScopeGraph.Info) sig m) => m ()
|
2020-01-10 23:14:05 +03:00
|
|
|
|
sampleGraphThing = do
|
2020-01-11 01:46:37 +03:00
|
|
|
|
declare @ScopeGraph.Info "hello" DeclProperties
|
|
|
|
|
declare @ScopeGraph.Info "goodbye" DeclProperties
|
2020-01-10 23:14:05 +03:00
|
|
|
|
|
2020-01-10 20:22:13 +03:00
|
|
|
|
|
|
|
|
|
assertEqual :: (Show a, Eq a) => a -> a -> IO ()
|
|
|
|
|
assertEqual a b = unless (a == b) (die (show a <> "\ndoes not equal\n" <> show b))
|
2020-01-08 21:13:48 +03:00
|
|
|
|
|
2020-01-08 20:51:37 +03:00
|
|
|
|
main :: IO ()
|
2020-01-08 21:13:48 +03:00
|
|
|
|
main = do
|
2020-01-13 18:08:07 +03:00
|
|
|
|
let path = "semantic-python/test/fixtures/1-01-empty-module.py"
|
|
|
|
|
file <- ByteString.readFile path
|
2020-01-08 21:25:43 +03:00
|
|
|
|
tree <- TS.parseByteString @Py.Module @Loc TSP.tree_sitter_python file
|
|
|
|
|
pyModule <- either die pure tree
|
2020-01-13 18:08:07 +03:00
|
|
|
|
let expecto = fst . run $ runSketch Nothing sampleGraphThing
|
|
|
|
|
let result = runScopeGraph (Path.absRel path) (Source.fromUTF8 file) pyModule
|
2020-01-10 20:22:13 +03:00
|
|
|
|
print result
|
|
|
|
|
assertEqual expecto result
|
|
|
|
|
|