1
1
mirror of https://github.com/github/semantic.git synced 2024-12-18 12:21:57 +03:00
semantic/semantic-python/app/Main.hs

64 lines
1.6 KiB
Haskell
Raw Normal View History

2020-01-10 20:22:13 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Main (main) where
2020-01-10 20:22:13 +03:00
import Control.Monad
import qualified Data.ByteString as ByteString
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)
import qualified TreeSitter.Python as TSP
2020-01-10 20:22:13 +03:00
import qualified TreeSitter.Python.AST as Py
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))
main :: IO ()
main = do
file <- ByteString.readFile "semantic-python/test/fixtures/1-01-empty-module.py"
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