diff --git a/cabal.project b/cabal.project index a0af8c8d0..b066f24cc 100644 --- a/cabal.project +++ b/cabal.project @@ -41,6 +41,8 @@ package semantic-ruby package semantic-tags ghc-options: -Werror +package semantic-scope-graph + package semantic-ast ghc-options: -Werror diff --git a/semantic-python/semantic-python.cabal b/semantic-python/semantic-python.cabal index a8b22230d..f431154cd 100644 --- a/semantic-python/semantic-python.cabal +++ b/semantic-python/semantic-python.cabal @@ -27,6 +27,7 @@ common haskell , semantic-core ^>= 0.0 , semantic-source ^>= 0.0 , semantic-tags ^>= 0.0 + , semantic-scope-graph ^>= 0.0 , text ^>= 1.2.3 , tree-sitter ^>= 0.7.2 , tree-sitter-python ^>= 0.8 @@ -53,6 +54,7 @@ library Language.Python Language.Python.Core Language.Python.Failure + Language.Python.ScopeGraph Language.Python.Tags hs-source-dirs: src diff --git a/semantic-python/src/Language/Python.hs b/semantic-python/src/Language/Python.hs index 732efae42..5d2d085e0 100644 --- a/semantic-python/src/Language/Python.hs +++ b/semantic-python/src/Language/Python.hs @@ -5,7 +5,9 @@ module Language.Python ) where import qualified Language.Python.Tags as PyTags +import qualified Language.Python.ScopeGraph as PyScopeGraph import qualified Tags.Tagging.Precise as Tags +import qualified Data.ScopeGraph as ScopeGraph import qualified TreeSitter.Python (tree_sitter_python) import qualified TreeSitter.Python.AST as Py import qualified TreeSitter.Unmarshal as TS @@ -17,3 +19,7 @@ instance TS.Unmarshal Term where instance Tags.ToTags Term where tags src = Tags.runTagging src . PyTags.tags . getTerm + + +instance ScopeGraph.ToScopeGraph Term where + scopeGraph src = ScopeGraph.runScopeGraph src . PyScopeGraph.scopeGraph . getTerm diff --git a/semantic-python/src/Language/Python/ScopeGraph.hs b/semantic-python/src/Language/Python/ScopeGraph.hs new file mode 100644 index 000000000..2df03adeb --- /dev/null +++ b/semantic-python/src/Language/Python/ScopeGraph.hs @@ -0,0 +1,11 @@ +module Language.Python.ScopeGraph (ToScopeGraph(..)) where + +import Control.Effect.Reader +import Control.Effect.Writer +import Source.Loc +-- import Source.Range +import Source.Source as Source +import Data.ScopeGraph (ScopeGraph, Info) + +class ToScopeGraph t where + scopeGraph :: ( Has (Reader Source) sig m, Has (Writer (ScopeGraph Info)) sig m) => t Loc -> m () diff --git a/semantic-scope-graph/src/Data/ScopeGraph.hs b/semantic-scope-graph/src/Data/ScopeGraph.hs index d84154eaf..9c95240c5 100644 --- a/semantic-scope-graph/src/Data/ScopeGraph.hs +++ b/semantic-scope-graph/src/Data/ScopeGraph.hs @@ -1,6 +1,6 @@ {-# LANGUAGE DuplicateRecordFields #-} -module Data.ScopeGraph () where +module Data.ScopeGraph (ToScopeGraph(..), ScopeGraph, Info, runScopeGraph) where import Algebra.Graph.Labelled (Graph, (-<), (>-)) import qualified Algebra.Graph.Labelled as G @@ -44,18 +44,18 @@ type SGM = class ToScopeGraph t where scopeGraph :: Source -> t Loc -> SGM (ScopeGraph Info) -instance ToScopeGraph Py.Identifier where - scopeGraph _ (Py.Identifier _ t) = ScopeGraph . G.vertex . Node (Ref t) <$> liftIO newUnique +-- instance ToScopeGraph Py.Identifier where +-- scopeGraph _ (Py.Identifier _ t) = ScopeGraph . G.vertex . Node (Ref t) <$> liftIO newUnique -instance ToScopeGraph Py.Module where - scopeGraph src Py.Module { Py.extraChildren = stmts } = do - parent <- ask - self <- ScopeGraph . G.vertex . Node Scope <$> liftIO newUnique - foldr (\item acc -> do { - x <- acc; - y <- scopeGraph src item; - pure (x --> y); - }) (pure (parent --> self)) stmts +-- instance ToScopeGraph Py.Module where +-- scopeGraph src Py.Module { Py.extraChildren = stmts } = do +-- parent <- ask +-- self <- ScopeGraph . G.vertex . Node Scope <$> liftIO newUnique +-- foldr (\item acc -> do { +-- x <- acc; +-- y <- scopeGraph src item; +-- pure (x --> y); +-- }) (pure (parent --> self)) stmts runScopeGraph :: ToScopeGraph t => Source -> t Loc -> IO (ScopeGraph Info) runScopeGraph src item = do diff --git a/semantic.cabal b/semantic.cabal index 5169dd57b..f63de9cc0 100644 --- a/semantic.cabal +++ b/semantic.cabal @@ -290,9 +290,10 @@ library , semantic-json ^>= 0 , semantic-python ^>= 0 , semantic-ruby ^>= 0 + , semantic-scope-graph ^>= 0 + , semantic-tags ^>= 0 , semantic-tsx ^>= 0 , semantic-typescript ^>= 0 - , semantic-tags ^>= 0 , semigroupoids ^>= 5.3.2 , split ^>= 0.2.3.3 , stm-chans ^>= 3.0.0.4