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-10 21:09:35 +03:00
|
|
|
import Data.ByteString.Char8
|
|
|
|
import Data.ByteString (pack, readFile, ByteString)
|
2019-10-02 01:36:00 +03:00
|
|
|
import System.IO (FilePath)
|
2019-10-02 21:34:58 +03:00
|
|
|
import Options.Applicative hiding (style)
|
|
|
|
import Data.Semigroup ((<>))
|
2019-10-01 18:25:14 +03:00
|
|
|
|
2019-10-10 20:14:15 +03:00
|
|
|
data SemanticAST = SemanticAST
|
|
|
|
{ sourceFilePath :: Input
|
|
|
|
, format :: Format
|
2019-10-10 21:08:41 +03:00
|
|
|
} deriving (Read)
|
2019-10-10 20:14:15 +03:00
|
|
|
|
|
|
|
parseAST :: Parser SemanticAST
|
|
|
|
parseAST = SemanticAST
|
|
|
|
<$> option auto
|
|
|
|
( long "sourcefile"
|
|
|
|
<> metavar "FILEPATH"
|
|
|
|
<> help "Specify filepath containing source code to parse" )
|
|
|
|
<*> option auto
|
|
|
|
( long "format"
|
|
|
|
<> help "Specify desired output: show, json, sexpression" )
|
|
|
|
|
2019-10-04 01:11:28 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = generateAST =<< execParser opts
|
|
|
|
|
2019-10-10 20:14:15 +03:00
|
|
|
generateAST :: SemanticAST -> IO ()
|
|
|
|
generateAST (SemanticAST sourceInput _) = do
|
|
|
|
case sourceInput of
|
|
|
|
FileInput filePath -> do
|
|
|
|
bytestring <- Data.ByteString.readFile filePath
|
|
|
|
print =<< parseByteString @TreeSitter.Python.AST.Module @(Range, Span) tree_sitter_python bytestring
|
|
|
|
StdInput -> do
|
2019-10-10 21:08:59 +03:00
|
|
|
bytestring <- Data.ByteString.Char8.pack . Prelude.head <$> getArgs
|
2019-10-10 20:14:15 +03:00
|
|
|
print =<< parseByteString @TreeSitter.Python.AST.Module @(Range, Span) tree_sitter_python bytestring
|
|
|
|
|
2019-10-04 01:11:28 +03:00
|
|
|
opts :: ParserInfo SemanticAST
|
|
|
|
opts = info (parseAST <**> helper)
|
|
|
|
( fullDesc
|
2019-10-10 20:14:15 +03:00
|
|
|
<> progDesc "Parse source code and produce an AST"
|
|
|
|
<> header "semantic-ast is a package used to parse source code" )
|
2019-10-04 01:56:58 +03:00
|
|
|
|
2019-10-04 01:12:04 +03:00
|
|
|
-- TODO: Define formats for json, sexpression, etc.
|
2019-10-10 20:13:51 +03:00
|
|
|
data Format = Show
|
|
|
|
deriving (Read)
|
2019-10-02 22:24:33 +03:00
|
|
|
|
2019-10-04 01:22:50 +03:00
|
|
|
data Input
|
|
|
|
= FileInput FilePath
|
|
|
|
| StdInput
|
2019-10-10 21:08:41 +03:00
|
|
|
deriving (Read)
|
2019-10-04 01:22:50 +03:00
|
|
|
|
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" )
|
|
|
|
|
2019-10-10 20:13:26 +03:00
|
|
|
-- Read from stdin
|
2019-10-04 00:56:38 +03:00
|
|
|
stdInput :: Parser Input
|
|
|
|
stdInput = flag' StdInput
|
|
|
|
( long "stdin"
|
|
|
|
<> help "Read from stdin" )
|