1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 05:41:54 +03:00
semantic/semantic-ast/src/CLI.hs

66 lines
1.7 KiB
Haskell
Raw Normal View History

2019-10-01 18:25:14 +03:00
{-# LANGUAGE ApplicativeDo #-}
2019-10-02 00:01:50 +03:00
{-# LANGUAGE TypeApplications #-}
2019-10-02 00:02:08 +03:00
module CLI (main) where
2019-10-01 18:25:14 +03:00
2019-10-02 00:03:02 +03:00
import System.Environment
2019-10-02 00:03:37 +03:00
import TreeSitter.Unmarshal
import TreeSitter.Python.AST
import TreeSitter.Python
2019-10-02 00:03:18 +03:00
import Source.Range
import Source.Span
2019-10-02 01:35:50 +03:00
import Data.ByteString (readFile, ByteString)
2019-10-02 01:36:00 +03:00
import System.IO (FilePath)
import Options.Applicative hiding (style)
import Data.Semigroup ((<>))
2019-10-01 18:25:14 +03:00
2019-10-04 01:11:28 +03:00
main :: IO ()
main = generateAST =<< execParser opts
opts :: ParserInfo SemanticAST
opts = info (parseAST <**> helper)
( fullDesc
<> progDesc "Read a file, output an AST"
<> header "semantic-ast - generates ASTs" )
generateAST :: SemanticAST -> IO ()
generateAST (SemanticAST file _) = do
bytestring <- Data.ByteString.readFile file
print =<< parseByteString @TreeSitter.Python.AST.Module @(Range, Span) tree_sitter_python bytestring
-- TODO: Define formats for json, sexpression, etc.
2019-10-04 01:56:20 +03:00
data Format = Show deriving (Read)
2019-10-02 22:24:17 +03:00
data SemanticAST = SemanticAST
{ sourceFilePath :: Prelude.String
2019-10-04 01:56:34 +03:00
, format :: Format
2019-10-02 22:24:17 +03:00
}
parseAST :: Parser SemanticAST
parseAST = SemanticAST
<$> strOption
( long "semantic-ast"
<> metavar "FILEPATH"
<> help "Specify filepath containing source code to parse" )
2019-10-04 01:56:11 +03:00
<*> option auto
( long "format"
<> help "The format you want to display")
2019-10-04 01:22:50 +03:00
data Input
= FileInput FilePath
| StdInput
2019-10-04 00:57:21 +03:00
input :: Parser Input
input = fileInput <|> stdInput
2019-10-04 00:56:54 +03:00
-- pass in a file as an argument and parse its contents
fileInput :: Parser Input
fileInput = FileInput <$> strOption
( long "file"
<> short 'f'
<> metavar "FILENAME"
<> help "Input file" )
-- cat something in
stdInput :: Parser Input
stdInput = flag' StdInput
( long "stdin"
<> help "Read from stdin" )