1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Merge pull request #243 from github/semantic-ast

semantic-ast
This commit is contained in:
Ayman Nadeem 2019-10-18 11:52:15 -04:00 committed by GitHub
commit 44db35aba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 167 additions and 0 deletions

View File

@ -1,5 +1,6 @@
packages: .
semantic-analysis
semantic-ast
semantic-core
semantic-java
semantic-json

View File

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

21
semantic-ast/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.

32
semantic-ast/README.md Normal file
View File

@ -0,0 +1,32 @@
# semantic-ast
This package has two goals:
1. Develop a library that will produce ASTs;
2. Provide a command line tool that will output ASTs in supported formats.
#### CLI
To output ASTs, run the `semantic-ast` command, specifying two mandatory options: 1) the format you'd like to return (ex., `Show`, `JSON`, etc.) and 2) the option specifying whether the source code will be passed in directly via command line (using `--sourceString`) or via providing the file path `--sourceFile`.
Filepath:
```
semantic-ast --format [FORMAT] --sourceFile [FILEPATH]
```
Source string:
```
semantic-ast --format [FORMAT] --sourceString [SOURCE]
```
An example command is:
```
semantic-ast -- --format Show --sourceString "a"
```
This will generate an AST
```
Right (Module {ann = (Range {start = 0, end = 1},Span {start = Pos {line = 0, column = 0}, end = Pos {line = 0, column = 1}}), extraChildren = [R1 (ExpressionStatementSimpleStatement (ExpressionStatement {ann = (Range {start = 0, end = 1},Span {start = Pos {line = 0, column = 0}, end = Pos {line = 0, column = 1}}), extraChildren = L1 (PrimaryExpressionExpression (IdentifierPrimaryExpression (Identifier {ann = (Range {start = 0, end = 1},Span {start = Pos {line = 0, column = 0}, end = Pos {line = 0, column = 1}}), bytes = "a"}))) :| []}))]})
```

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

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

View File

@ -0,0 +1,46 @@
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-ast
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
library
exposed-modules:
-- other-modules:
-- other-extensions:
build-depends: base ^>=4.12.0.0
, tree-sitter ^>= 0.5
, semantic-source ^>= 0.0
, tree-sitter-python ^>= 0.6
, bytestring ^>= 0.10.8.2
, optparse-applicative ^>= 0.14.3.0
, pretty-simple ^>= 3.1.0.0
hs-source-dirs: src
default-language: Haskell2010
executable semantic-ast
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base ^>=4.12.0.0
, semantic-ast
, tree-sitter ^>= 0.5
, semantic-source ^>= 0.0
, tree-sitter-python ^>= 0.6
, bytestring ^>= 0.10.8.2
, optparse-applicative ^>= 0.14.3.0
, pretty-simple ^>= 3.1.0.0
hs-source-dirs: src
default-language: Haskell2010

60
semantic-ast/src/Main.hs Normal file
View File

@ -0,0 +1,60 @@
{-# LANGUAGE TypeApplications #-}
module Main (main) where
import System.Environment
import TreeSitter.Unmarshal
import TreeSitter.Python.AST
import TreeSitter.Python
import Source.Range
import Source.Span
import Data.ByteString.Char8
import Data.ByteString (pack, readFile, ByteString)
import System.IO (FilePath)
import Options.Applicative hiding (style)
import Data.Semigroup ((<>))
import Text.Pretty.Simple (pPrint)
data SemanticAST = SemanticAST
{ format :: Format
, source :: Either FilePath Prelude.String
}
parseAST :: Parser SemanticAST
parseAST = SemanticAST
<$> option auto
( long "format"
<> help "Specify desired output: show, json, sexpression" )
<*> (Left <$> strOption
( long "sourceFile"
<> metavar "FILEPATH"
<> help "Specify filepath containing source code to parse" )
<|> Right <$> strOption
( long "sourceString"
<> metavar "STRING"
<> help "Specify source input to parse"
))
main :: IO ()
main = generateAST =<< execParser opts
generateAST :: SemanticAST -> IO ()
generateAST (SemanticAST format source) = do
bytestring <- case source of
Left filePath -> do
Data.ByteString.readFile filePath
Right source -> do
pure $ Data.ByteString.Char8.pack source
ast <- parseByteString @TreeSitter.Python.AST.Module @(Range, Span) tree_sitter_python bytestring
case format of
Show -> print ast
Pretty -> pPrint ast
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
deriving (Read)