1
1
mirror of https://github.com/github/semantic.git synced 2024-11-26 09:07:39 +03:00

nothing that some text munging and 'sed' can't fix

This commit is contained in:
Patrick Thomson 2020-09-09 14:15:33 -04:00
parent d3a94a478a
commit 3163a3e262
4 changed files with 60 additions and 6 deletions

View File

@ -94,9 +94,9 @@ stack_snapshot(
"hspec",
"hspec-core",
"hspec-expectations",
"proto-lens-jsonpb",
"lens",
"lingo",
"neat-interpolation",
"network",
"network-uri",
"optparse-applicative",
@ -109,6 +109,7 @@ stack_snapshot(
"prettyprinter-ansi-terminal",
"process",
"proto-lens",
"proto-lens-jsonpb",
"proto-lens-runtime",
"raw-strings-qq",
"recursion-schemes",

View File

@ -56,8 +56,11 @@ haskell_binary(
":semantic-ast",
"//:base",
"//:filepath",
"//:process",
"//:template-haskell",
"//:text",
"@stackage//:directory",
"@stackage//:neat-interpolation",
"@stackage//:optparse-generic",
"@tree-sitter-json//:tree-sitter-json",
],

View File

@ -1,28 +1,77 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
module Main (main) where
import AST.GenerateSyntax
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import GHC.Generics (Generic)
import Language.Haskell.TH
import NeatInterpolation
import qualified Options.Generic as Opt
import System.Directory
import System.IO
import System.Process
import qualified TreeSitter.JSON as JSON (tree_sitter_json)
data Config = Config
{ language :: String,
{ language :: Text,
path :: FilePath
}
deriving (Show, Generic)
instance Opt.ParseRecord Config
header :: Text
header =
[trimming|
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|]
main :: IO ()
main = do
args <- Opt.getRecord "generate-ast"
print @Config args
absolute <- makeAbsolute (path args)
Config language path <- Opt.getRecord "generate-ast"
absolute <- makeAbsolute path
decls <- runQ (astDeclarationsRelative JSON.tree_sitter_json absolute)
putStrLn (pprint decls)
let modheader =
[trimming| module Language.$language.AST (module Language.$language.AST) where
-- Language definition for $language, enerated by ast-generate. Do not edit!
import Prelude ()
|]
let programText = T.unlines [header, modheader, T.pack (pprint decls)]
hasOrmolu <- findExecutable "ormolu"
if isNothing hasOrmolu
then T.putStrLn programText
else do
(path, tf) <- openTempFile "/tmp" "generated.hs"
print path
T.hPutStrLn tf programText
hClose tf
callProcess "sed" ["-i", "-e", "s/AST.Traversable1.Class.Traversable1 someConstraint/(AST.Traversable1.Class.Traversable1 someConstraint)/g", path]
callProcess "ormolu" ["--mode", "inplace", path]
readFile path >>= putStrLn

View File

@ -8,6 +8,7 @@ packages:
- fused-effects-readline-0.1.0.0
- semilattices-0.0.0.4
- haskeline-0.8.0.0
- Interpolation-0.3.0
- tree-sitter-0.9.0.2
- unliftio-core-0.2.0.1
- lingo-0.5.0.1