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 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-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"
|
|
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
needed :: IO (ScopeGraph.ScopeGraph ScopeGraph.Info)
|
|
|
|
|
needed = do
|
|
|
|
|
let start = ScopeGraph.root
|
|
|
|
|
one <- ScopeGraph.scope
|
|
|
|
|
hello <- ScopeGraph.ref "hello"
|
|
|
|
|
two <- ScopeGraph.scope
|
|
|
|
|
goodb <- ScopeGraph.ref "goodbye"
|
|
|
|
|
pure . ScopeGraph.edges $ [ (start, one)
|
|
|
|
|
, (one, hello)
|
|
|
|
|
, (one, two)
|
|
|
|
|
, (two, goodb)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
file <- ByteString.readFile "semantic-python/test/fixtures/1-01-empty-module.py"
|
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-10 20:22:13 +03:00
|
|
|
|
expecto <- needed
|
|
|
|
|
result <- ScopeGraph.runScopeGraph (Source.fromUTF8 file) pyModule
|
|
|
|
|
print result
|
|
|
|
|
assertEqual expecto result
|
|
|
|
|
|