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

70 lines
1.9 KiB
Haskell
Raw Normal View History

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 #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module Main (main) where
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
import Convert.ToScopeGraph
import qualified Data.ByteString as ByteString
import Data.Name (Name)
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 System.Path as Path
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"
-}
runScopeGraph :: ToScopeGraph t => Path.AbsRelFile -> Source.Source -> t Loc -> (ScopeGraph.ScopeGraph Name, Result)
runScopeGraph p _src item = run . runSketch @Name (Just p) $ scopeGraph item
sampleGraphThing :: (Has (Sketch Name) sig m) => m Result
2020-01-10 23:14:05 +03:00
sampleGraphThing = do
declare @Name "hello" DeclProperties
declare @Name "goodbye" DeclProperties
pure Complete
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))
main :: IO ()
main = do
let path = "semantic-python/test/fixtures/1-04-toplevel-assignment.py"
file <- ByteString.readFile path
tree <- TS.parseByteString @Py.Module @Loc TSP.tree_sitter_python file
pyModule <- either die pure tree
let (expecto, Complete) = run $ runSketch @Name Nothing sampleGraphThing
let (result, Complete) = runScopeGraph (Path.absRel path) (Source.fromUTF8 file) pyModule
2020-01-10 20:22:13 +03:00
print result
assertEqual expecto result