1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00
semantic/semantic-ast/src/Main.hs

69 lines
2.0 KiB
Haskell
Raw Normal View History

2019-10-02 00:01:50 +03:00
{-# LANGUAGE TypeApplications #-}
2019-10-10 21:59:56 +03:00
module Main (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)
import Options.Applicative hiding (style)
import Data.Semigroup ((<>))
import Text.Pretty.Simple (pPrint, pPrintNoColor)
import Data.Traversable (for)
2019-10-01 18:25:14 +03:00
2019-10-10 20:14:15 +03:00
data SemanticAST = SemanticAST
2019-10-17 01:18:11 +03:00
{ format :: Format
, color :: Bool
2019-10-20 18:51:02 +03:00
, source :: Either [FilePath] Prelude.String
2019-10-16 22:15:55 +03:00
}
2019-10-10 20:14:15 +03:00
2019-10-21 19:57:16 +03:00
-- Usage: semantic-ast --format ARG [--no-color] [--sourceString STRING] [FILEPATHS…]
2019-10-10 20:14:15 +03:00
parseAST :: Parser SemanticAST
parseAST = SemanticAST
2019-10-16 21:17:07 +03:00
<$> option auto
( long "format"
<> help "Specify desired output: show, json, sexpression" )
<*> switch
( long "no-color"
<> help "Print with color: --color"
)
2019-10-21 19:57:31 +03:00
<*> (Left <$> some
(Options.Applicative.argument str (metavar "FILEPATH(S)"))
2019-10-16 21:17:07 +03:00
<|> Right <$> strOption
( long "sourceString"
<> metavar "STRING"
<> help "Specify source input to parse"
))
2019-10-10 20:14:15 +03:00
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 ()
2019-10-19 00:20:06 +03:00
generateAST (SemanticAST format color source) = do
2019-10-20 18:50:15 +03:00
bytestrings <- case source of
Left filePaths -> do
traverse Data.ByteString.readFile filePaths
2019-10-15 20:27:54 +03:00
Right source -> do
2019-10-20 18:50:43 +03:00
pure [Data.ByteString.Char8.pack source]
ast <- for bytestrings $ \bytestring -> do parseByteString @TreeSitter.Python.AST.Module @(Range, Span) tree_sitter_python bytestring
2019-10-18 18:14:56 +03:00
case format of
2019-10-19 00:20:31 +03:00
Show -> print ast
2019-10-18 18:23:37 +03:00
Pretty -> pPrint ast
2019-10-19 00:20:31 +03:00
case color of
Prelude.True -> pPrintNoColor ast
Prelude.False -> pPrint ast
2019-10-10 20:14:15 +03:00
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
-- TODO: Define formats for json, sexpression, etc.
2019-10-18 18:23:23 +03:00
data Format = Show | Pretty
2019-10-10 20:13:51 +03:00
deriving (Read)