Skip to content

Commit bd4f498

Browse files
author
Patrick Thomson
committed
work for all languages (hopefully)
1 parent 42b0839 commit bd4f498

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

semantic-ast/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ haskell_binary(
6767
compiler_flags = GHC_FLAGS + EXECUTABLE_FLAGS + ["-XStrictData"],
6868
deps = [
6969
":semantic-ast",
70+
"//semantic-source",
7071
"//:base",
7172
"//:filepath",
7273
"//:process",
@@ -75,6 +76,7 @@ haskell_binary(
7576
"@stackage//:directory",
7677
"@stackage//:generic-lens",
7778
"@stackage//:lens",
79+
"@stackage//:tree-sitter",
7880
"@stackage//:neat-interpolation",
7981
"@stackage//:optparse-generic",
8082
] + all_ts_deps,

semantic-ast/app/Main.hs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,33 @@ module Main (main) where
1414

1515
import AST.GenerateSyntax
1616
import Control.Lens (Traversal', mapped, (%~))
17+
import Control.Monad
1718
import Data.Generics.Product.Typed (typed)
1819
import Data.Maybe
1920
import Data.Text (Text)
2021
import qualified Data.Text as T
2122
import qualified Data.Text.IO as T
23+
import Foreign
2224
import GHC.Generics (Generic)
23-
import Language.Haskell.TH
25+
import Language.Haskell.TH hiding (JavaScript)
2426
import Language.Haskell.TH.Lens
2527
import NeatInterpolation
2628
import qualified Options.Generic as Opt
29+
import Source.Language
2730
import System.Directory
31+
import System.Exit
32+
import qualified TreeSitter.Language
2833
import System.IO
2934
import System.Process
35+
import qualified TreeSitter.Go as Go (tree_sitter_go)
3036
import qualified TreeSitter.JSON as JSON (tree_sitter_json)
37+
import qualified TreeSitter.Java as Java (tree_sitter_java)
38+
import qualified TreeSitter.PHP as PHP (tree_sitter_php)
39+
import qualified TreeSitter.Python as Python (tree_sitter_python)
40+
import qualified TreeSitter.QL as CodeQL (tree_sitter_ql)
41+
import qualified TreeSitter.Ruby as Ruby (tree_sitter_ruby)
42+
import qualified TreeSitter.TSX as TSX (tree_sitter_tsx)
43+
import qualified TreeSitter.TypeScript as TypeScript (tree_sitter_typescript)
3144

3245
data Config = Config {language :: Text, path :: FilePath}
3346
deriving stock (Show, Generic)
@@ -39,21 +52,40 @@ data Config = Config {language :: Text, path :: FilePath}
3952
-- doesn't like at all. I haven't figured out quite why we get this qualified
4053
-- name, but for now the easiest thing to do is some nested updates with lens.
4154
adjust :: Dec -> Dec
42-
adjust = _InstanceD.typed.mapped %~ (values %~ truncate) . (functions %~ truncate)
55+
adjust = _InstanceD . typed . mapped %~ (values %~ truncate) . (functions %~ truncate)
4356
where
4457
-- Need to handle functions with no arguments, which are parsed as ValD entities,
4558
-- as well as those with arguments, which are FunD.
4659
values, functions :: Traversal' Dec Name
47-
values = _ValD.typed._VarP
48-
functions = _FunD.typed
60+
values = _ValD . typed . _VarP
61+
functions = _FunD . typed
4962

5063
truncate :: Name -> Name
5164
truncate = mkName . nameBase
5265

66+
parserForLanguage :: Language -> Ptr TreeSitter.Language.Language
67+
parserForLanguage = \case
68+
Unknown -> error "Unknown language encountered"
69+
CodeQL -> CodeQL.tree_sitter_ql
70+
Go -> Go.tree_sitter_go
71+
Haskell -> error "Haskell backend not implemented yet"
72+
Java -> Java.tree_sitter_java
73+
JavaScript -> TypeScript.tree_sitter_typescript
74+
JSON -> JSON.tree_sitter_json
75+
JSX -> TSX.tree_sitter_tsx
76+
Markdown -> error "Markdown backend deprecated"
77+
PHP -> PHP.tree_sitter_php
78+
Python -> Python.tree_sitter_python
79+
Ruby -> Ruby.tree_sitter_ruby
80+
TypeScript -> TypeScript.tree_sitter_typescript
81+
TSX -> TSX.tree_sitter_tsx
82+
5383
main :: IO ()
5484
main = do
5585
Config language path <- Opt.getRecord "generate-ast"
56-
decls <- T.pack . pprint . fmap adjust <$> astDeclarationsIO JSON.tree_sitter_json path
86+
let lang = textToLanguage language
87+
decls <- T.pack . pprint . fmap adjust <$> astDeclarationsIO (parserForLanguage lang) path
88+
when (lang == Unknown) (die ("Couldn't determine language for " <> T.unpack language))
5789

5890
let programText =
5991
[trimming|

0 commit comments

Comments
 (0)