Skip to content

Commit cc1f245

Browse files
committed
update 'Main's
1 parent b3b1cb9 commit cc1f245

File tree

3 files changed

+119
-13
lines changed

3 files changed

+119
-13
lines changed

language-powerquery/app/Main.hs

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,105 @@
1+
{-# LANGUAGE PackageImports #-}
12
module Main where
23

3-
import qualified Language.PowerQuery.Main as PQ
4+
import Prelude hiding (lex)
5+
import "base" Data.Semigroup ((<>))
6+
import "base" System.IO (stdin)
7+
import qualified "bytestring" Data.ByteString.Lazy.Char8 as B (ByteString, readFile, putStrLn, hGetContents, unpack)
8+
import "aeson" Data.Aeson (encode)
9+
import "optparse-applicative" Options.Applicative
10+
import "language-powerquery-ast" Language.PowerQuery.AST
11+
import "language-powerquery" Language.PowerQuery
12+
13+
14+
data Lex
15+
= Lex
16+
{ _lex_path :: !(Maybe FilePath)
17+
}
18+
19+
data Parse
20+
= Parse
21+
{ _parse_path :: !(Maybe FilePath)
22+
, _parse_json :: !Bool
23+
}
24+
25+
data PPrint
26+
= PPrint
27+
{ _print_path :: !(Maybe FilePath)
28+
}
29+
30+
data Command
31+
= C1 Lex
32+
| C2 Parse
33+
| C3 PPrint
34+
35+
lexParser :: Parser Command
36+
lexParser = C1 <$> (Lex
37+
<$> optional (strOption
38+
( long "path"
39+
<> short 'p'
40+
<> metavar "PATH"
41+
<> help "Lex PowerQuery Text"
42+
))
43+
)
44+
45+
parseParser :: Parser Command
46+
parseParser = C2 <$> (Parse
47+
<$> optional (strOption
48+
( long "path"
49+
<> short 'p'
50+
<> metavar "PATH"
51+
<> help "Parse PowerQuery Text"
52+
))
53+
<*> switch
54+
( long "json"
55+
<> short 'j'
56+
<> help "print AST as JSON"
57+
)
58+
)
59+
60+
combined :: Parser Command
61+
combined = subparser
62+
( command "lex" (info lexParser (progDesc "Lex PowerQuery Text"))
63+
<> command "parse" (info parseParser (progDesc "Parse PowerQuery Text"))
64+
)
65+
66+
fromRight (Right x) = x
67+
fromRight (Left err) = error err
68+
69+
70+
readPathOrStdin :: Maybe FilePath -> IO B.ByteString
71+
readPathOrStdin Nothing = B.hGetContents stdin
72+
readPathOrStdin (Just path) = B.readFile path
73+
74+
75+
lex :: Lex -> IO ()
76+
lex (Lex mPath) = do
77+
bs <- readPathOrStdin mPath
78+
let tokens = lexer
79+
. B.unpack
80+
$ bs
81+
print tokens
82+
return ()
83+
84+
85+
parse :: Parse -> IO ()
86+
parse (Parse mPath shouldJSON) = do
87+
bs <- readPathOrStdin mPath
88+
let ast = parseDocument
89+
. fromRight
90+
. lexer
91+
. B.unpack
92+
$ bs
93+
if shouldJSON
94+
then B.putStrLn . encode $ ast
95+
else print $ ast
96+
return ()
97+
498

599
main :: IO ()
6-
main = PQ.main
100+
main = do
101+
options <- execParser (info (combined <**> helper) (fullDesc <> progDesc "PowerQuery Lexer/Parser/Pretty Printer"))
102+
case options of
103+
C1 config -> lex config
104+
C2 config -> parse config
105+
C3 config -> undefined

language-powerquery/language-powerquery.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ executable language-powerquery
5353
-- other-modules:
5454
-- other-extensions:
5555
build-depends: base
56+
, bytestring
57+
, aeson
5658
, optparse-applicative
5759
, language-powerquery-ast
5860
, language-powerquery

pbix/app/Main.hs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
module Main where
33

44
import "base" Data.Semigroup ((<>))
5+
import "base" System.IO (stdin)
56
import "lens" Control.Lens
6-
import qualified "bytestring" Data.ByteString.Lazy.Char8 as B (readFile, putStrLn)
7+
import qualified "bytestring" Data.ByteString.Lazy.Char8 as B (ByteString, readFile, hGetContents, putStrLn)
78
import "aeson" Data.Aeson (encode)
89
import "optparse-applicative" Options.Applicative
910
import "pbix" Codec.Pbix.Types
@@ -12,12 +13,12 @@ import "language-powerquery-ast" Language.PowerQuery.AST
1213

1314
data ListFormulas
1415
= ListFormulas
15-
{ _listFormula_path :: !FilePath
16+
{ _listFormula_path :: !(Maybe FilePath)
1617
}
1718

1819
data PrintFormula
1920
= PrintFormula
20-
{ _printFormula_path :: !FilePath
21+
{ _printFormula_path :: !(Maybe FilePath)
2122
, _printFormula_formula :: !String
2223
, _printFormula_parse :: !Bool
2324
, _printFormaul_json :: !Bool
@@ -29,22 +30,22 @@ data Command
2930

3031
listFormulasParser :: Parser Command
3132
listFormulasParser = C1 <$> (ListFormulas
32-
<$> strOption
33+
<$> optional (strOption
3334
( long "path"
3435
<> short 'p'
3536
<> metavar "PATH"
3637
<> help ".pbix file path"
3738
)
38-
)
39+
))
3940

4041
printFormulaParser :: Parser Command
4142
printFormulaParser = C2 <$> (PrintFormula
42-
<$> strOption
43+
<$> optional (strOption
4344
( long "path"
4445
<> short 'p'
4546
<> metavar "PATH"
4647
<> help ".pbix file path"
47-
)
48+
))
4849
<*> strOption
4950
( long "formula"
5051
<> short 'f'
@@ -70,14 +71,18 @@ combined = subparser
7071
<> command "print" (info printFormulaParser (progDesc "Print a formula in a .pbix"))
7172
)
7273

74+
readPathOrStdin :: Maybe FilePath -> IO B.ByteString
75+
readPathOrStdin Nothing = B.hGetContents stdin
76+
readPathOrStdin (Just path) = B.readFile path
77+
7378
listFormulas :: ListFormulas -> IO ()
74-
listFormulas (ListFormulas path) = do
75-
bs <- B.readFile path
79+
listFormulas (ListFormulas mPath) = do
80+
bs <- readPathOrStdin mPath
7681
print $ bs ^. pbix . dataMashup . formulas
7782

7883
printFormula :: PrintFormula -> IO ()
79-
printFormula (PrintFormula path name shouldParse shouldJSON) = do
80-
bs <- B.readFile path
84+
printFormula (PrintFormula mPath name shouldParse shouldJSON) = do
85+
bs <- readPathOrStdin mPath
8186
let script = bs ^. pbix . dataMashup . formula name
8287
if shouldParse
8388
then if shouldJSON

0 commit comments

Comments
 (0)