1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00

Serialize terms to s-expressions.

This commit is contained in:
Rob Rix 2018-05-11 14:53:22 -04:00
parent bf334d24a5
commit 6872baa090
2 changed files with 3 additions and 4 deletions

View File

@ -55,7 +55,7 @@ data TermRenderer output where
-- | Render to JSON with the format documented in docs/json-format.md under “Term.” -- | Render to JSON with the format documented in docs/json-format.md under “Term.”
JSONTermRenderer :: TermRenderer [Value] JSONTermRenderer :: TermRenderer [Value]
-- | Render to a 'ByteString' formatted as nested s-expressions. -- | Render to a 'ByteString' formatted as nested s-expressions.
SExpressionTermRenderer :: TermRenderer ByteString SExpressionTermRenderer :: TermRenderer Builder
-- | Render to a list of tags (deprecated). -- | Render to a list of tags (deprecated).
TagsTermRenderer :: TermRenderer [Value] TagsTermRenderer :: TermRenderer [Value]
-- | Render to a list of symbols. -- | Render to a list of symbols.

View File

@ -8,7 +8,6 @@ import Analysis.PackageDef (HasPackageDef, packageDefAlgebra)
import Data.Blob import Data.Blob
import Data.JSON.Fields import Data.JSON.Fields
import Data.Output import Data.Output
import Data.Record
import Parsing.Parser import Parsing.Parser
import Prologue hiding (MonadError(..)) import Prologue hiding (MonadError(..))
import Rendering.Graph import Rendering.Graph
@ -30,7 +29,7 @@ parseBlob renderer blob@Blob{..}
| Just (SomeParser parser) <- someParser (Proxy :: Proxy '[ConstructorName, HasPackageDef, HasDeclaration, IdentifierName, Foldable, Functor, ToJSONFields1]) <$> blobLanguage | Just (SomeParser parser) <- someParser (Proxy :: Proxy '[ConstructorName, HasPackageDef, HasDeclaration, IdentifierName, Foldable, Functor, ToJSONFields1]) <$> blobLanguage
= parse parser blob >>= case renderer of = parse parser blob >>= case renderer of
JSONTermRenderer -> decorate constructorLabel >=> decorate identifierLabel >=> render (renderJSONTerm blob) JSONTermRenderer -> decorate constructorLabel >=> decorate identifierLabel >=> render (renderJSONTerm blob)
SExpressionTermRenderer -> decorate constructorLabel . (Nil <$) >=> render renderSExpressionTerm SExpressionTermRenderer -> serialize SExpression
TagsTermRenderer -> decorate (declarationAlgebra blob) >=> render (renderToTags blob) TagsTermRenderer -> decorate (declarationAlgebra blob) >=> render (renderToTags blob)
ImportsTermRenderer -> decorate (declarationAlgebra blob) >=> decorate (packageDefAlgebra blob) >=> render (renderToImports blob) ImportsTermRenderer -> decorate (declarationAlgebra blob) >=> decorate (packageDefAlgebra blob) >=> render (renderToImports blob)
SymbolsTermRenderer fields -> decorate (declarationAlgebra blob) >=> render (renderToSymbols fields blob) SymbolsTermRenderer fields -> decorate (declarationAlgebra blob) >=> render (renderToSymbols fields blob)
@ -49,7 +48,7 @@ astParseBlobs renderer blobs = toOutput' <$> distributeFoldMap (WrapTask . astPa
astParseBlob renderer blob@Blob{..} astParseBlob renderer blob@Blob{..}
| Just (SomeASTParser parser) <- someASTParser <$> blobLanguage | Just (SomeASTParser parser) <- someASTParser <$> blobLanguage
= parse parser blob >>= case renderer of = parse parser blob >>= case renderer of
SExpressionTermRenderer -> render renderSExpressionAST SExpressionTermRenderer -> serialize SExpression
JSONTermRenderer -> render (renderJSONTerm' blob) JSONTermRenderer -> render (renderJSONTerm' blob)
_ -> pure $ throwError (SomeException (FormatNotSupported "Only SExpression and JSON output supported for tree-sitter ASTs.")) _ -> pure $ throwError (SomeException (FormatNotSupported "Only SExpression and JSON output supported for tree-sitter ASTs."))
| otherwise = throwError (SomeException (NoLanguageForBlob blobPath)) | otherwise = throwError (SomeException (NoLanguageForBlob blobPath))