@@ -14,20 +14,33 @@ module Main (main) where
14
14
15
15
import AST.GenerateSyntax
16
16
import Control.Lens (Traversal' , mapped , (%~) )
17
+ import Control.Monad
17
18
import Data.Generics.Product.Typed (typed )
18
19
import Data.Maybe
19
20
import Data.Text (Text )
20
21
import qualified Data.Text as T
21
22
import qualified Data.Text.IO as T
23
+ import Foreign
22
24
import GHC.Generics (Generic )
23
- import Language.Haskell.TH
25
+ import Language.Haskell.TH hiding ( JavaScript )
24
26
import Language.Haskell.TH.Lens
25
27
import NeatInterpolation
26
28
import qualified Options.Generic as Opt
29
+ import Source.Language
27
30
import System.Directory
31
+ import System.Exit
32
+ import qualified TreeSitter.Language
28
33
import System.IO
29
34
import System.Process
35
+ import qualified TreeSitter.Go as Go (tree_sitter_go )
30
36
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 )
31
44
32
45
data Config = Config { language :: Text , path :: FilePath }
33
46
deriving stock (Show , Generic )
@@ -39,21 +52,40 @@ data Config = Config {language :: Text, path :: FilePath}
39
52
-- doesn't like at all. I haven't figured out quite why we get this qualified
40
53
-- name, but for now the easiest thing to do is some nested updates with lens.
41
54
adjust :: Dec -> Dec
42
- adjust = _InstanceD. typed. mapped %~ (values %~ truncate ) . (functions %~ truncate )
55
+ adjust = _InstanceD . typed . mapped %~ (values %~ truncate ) . (functions %~ truncate )
43
56
where
44
57
-- Need to handle functions with no arguments, which are parsed as ValD entities,
45
58
-- as well as those with arguments, which are FunD.
46
59
values , functions :: Traversal' Dec Name
47
- values = _ValD. typed. _VarP
48
- functions = _FunD. typed
60
+ values = _ValD . typed . _VarP
61
+ functions = _FunD . typed
49
62
50
63
truncate :: Name -> Name
51
64
truncate = mkName . nameBase
52
65
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
+
53
83
main :: IO ()
54
84
main = do
55
85
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))
57
89
58
90
let programText =
59
91
[trimming |
0 commit comments