1
1
mirror of https://github.com/github/semantic.git synced 2024-11-22 14:20:24 +03:00

Add a semantic-parse package.

This commit is contained in:
Rob Rix 2020-02-04 14:25:39 -05:00
parent 0291afcd98
commit 0e5c97038f
No known key found for this signature in database
GPG Key ID: 2BE643E01DC032AE
8 changed files with 163 additions and 0 deletions

View File

@ -9,6 +9,7 @@ packages: .
semantic-go
semantic-java
semantic-json
semantic-parse
semantic-python
semantic-ruby
semantic-scope-graph

View File

@ -9,6 +9,7 @@ packages: .
semantic-go
semantic-java
semantic-json
semantic-parse
semantic-python
semantic-ruby
semantic-scope-graph

View File

@ -53,6 +53,7 @@ function flags {
echo "-isemantic-go/src"
echo "-isemantic-java/src"
echo "-isemantic-json/src"
echo "-isemantic-parse/src"
echo "-isemantic-python/src"
echo "-isemantic-python/test"
echo "-isemantic-ruby/src"

View File

@ -0,0 +1,5 @@
# Revision history for semantic-parse
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

21
semantic-parse/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2019 GitHub
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2
semantic-parse/Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,75 @@
{-# LANGUAGE TypeApplications #-}
module Main (main) where
import AST.Unmarshal
import qualified Language.Python.AST as AST
import qualified Language.Python.Grammar as Python
import Source.Range
import Source.Span
import Data.Aeson (toJSON)
import Data.ByteString.Char8
import Data.ByteString (readFile)
import Options.Applicative hiding (style)
import Text.Pretty.Simple (pPrint, pPrintNoColor)
import Data.Foldable (traverse_)
import Control.Monad ((>=>))
import Marshal.JSON (marshal)
import Data.ByteString.Lazy.Char8 (putStrLn)
import Data.Aeson.Encode.Pretty (encodePretty)
data SemanticAST = SemanticAST
{ _format :: Format
, _noColor :: Bool
, _source :: Either [FilePath] String
}
-- Usage: semantic-ast --format ARG [--no-color] (--sourceString STRING | FILEPATHS…)
parseAST :: Parser SemanticAST
parseAST = SemanticAST
<$> option auto
( long "format"
<> help "Specify desired output: show, json, sexpression" )
<*> switch
( long "no-color"
<> help "Print with color: --color"
)
<*> (Left <$> some
(Options.Applicative.argument str (metavar "FILEPATH(S)"))
<|> Right <$> strOption
( long "sourceString"
<> metavar "STRING"
<> help "Specify source input to parse"
))
main :: IO ()
main = generateAST =<< execParser opts
generateAST :: SemanticAST -> IO ()
generateAST (SemanticAST format noColor source) =
getByteStrings >>= traverse_ go
where getByteStrings = case source of
Left filePaths -> traverse Data.ByteString.readFile filePaths
Right source -> pure [Data.ByteString.Char8.pack source]
go = ast >=> display
ast = parseByteString @AST.Module @(Range, Span) Python.tree_sitter_python -- TODO: generalize for all languages
display = case format of
Json -> Data.ByteString.Lazy.Char8.putStrLn . encodePretty . either toJSON (marshal . fmap (const ())) -- TODO: replacing range and span annotations with () for which there is a ToJSON instance for now, deal with this later
Show -> print
Pretty | noColor -> pPrintNoColor
| otherwise -> pPrint
opts :: ParserInfo SemanticAST
opts = info (parseAST <**> helper)
( fullDesc
<> progDesc "Parse source code and produce an AST"
<> header "semantic-ast is a package used to parse source code" )
-- TODO: Define formats for json, sexpression, etc.
data Format = Show
| Pretty
| Json
deriving (Read)

View File

@ -0,0 +1,57 @@
cabal-version: 2.4
-- Initial package description 'semantic-ast.cabal' generated by 'cabal
-- init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: semantic-parse
version: 0.1.0.0
-- synopsis:
-- description:
-- bug-reports:
license: MIT
license-file: LICENSE
author: The Semantic Authors
maintainer: opensource+semantic@github.com
copyright: (c) 2019 GitHub, Inc.
category: Language
extra-source-files: CHANGELOG.md
tested-with: GHC == 8.6.5
common haskell
default-language: Haskell2010
ghc-options:
-Weverything
-Wno-missing-local-signatures
-Wno-missing-import-lists
-Wno-implicit-prelude
-Wno-safe
-Wno-unsafe
-Wno-name-shadowing
-Wno-monomorphism-restriction
-Wno-missed-specialisations
-Wno-all-missed-specialisations
-Wno-star-is-type
if (impl(ghc >= 8.8))
ghc-options: -Wno-missing-deriving-strategies
executable semantic-parse
import: haskell
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base
, semantic-ast
, tree-sitter
, semantic-source
, tree-sitter-python ^>= 0.8.1
, bytestring
, optparse-applicative
, pretty-simple
, aeson
, bytestring
, aeson-pretty
, semantic-python
, text
hs-source-dirs: app
default-language: Haskell2010