From 6fa183b3bf6d668c4c6847755a8621d63d61c559 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Fri, 27 Dec 2024 12:14:31 +0100 Subject: [PATCH] add `--print-loc` option to `dev import-tree scan` command --- app/Commands/Dev/ImportTree/ScanFile.hs | 3 +++ app/Commands/Dev/ImportTree/ScanFile/Options.hs | 6 ++++++ .../Compiler/Concrete/Translation/ImportScanner/Base.hs | 2 ++ .../Concrete/Translation/ImportScanner/FlatParse.hs | 7 +------ src/Juvix/Prelude/FlatParse/Lexer.hs | 2 -- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Commands/Dev/ImportTree/ScanFile.hs b/app/Commands/Dev/ImportTree/ScanFile.hs index a97ffc0821..00b6f869d2 100644 --- a/app/Commands/Dev/ImportTree/ScanFile.hs +++ b/app/Commands/Dev/ImportTree/ScanFile.hs @@ -16,4 +16,7 @@ runCommand ScanFileOptions {..} = forM_ (scanRes ^. scanResultImports) $ \impor -> do opts <- askGenericOptions renderStdOut (ppOutNoComments opts impor) + when _scanFilePrintLoc $ do + renderStdOut @Text " " + renderStdOut (ppOutNoComments opts (getLoc impor)) newline diff --git a/app/Commands/Dev/ImportTree/ScanFile/Options.hs b/app/Commands/Dev/ImportTree/ScanFile/Options.hs index d9f88cd2c7..4abd43fa4a 100644 --- a/app/Commands/Dev/ImportTree/ScanFile/Options.hs +++ b/app/Commands/Dev/ImportTree/ScanFile/Options.hs @@ -5,6 +5,7 @@ import Juvix.Compiler.Concrete.Translation.ImportScanner data ScanFileOptions = ScanFileOptions { _scanFileFile :: AppPath File, + _scanFilePrintLoc :: Bool, _scanFileStrategy :: ImportScanStrategy } deriving stock (Data) @@ -15,4 +16,9 @@ parseScanFile :: Parser ScanFileOptions parseScanFile = do _scanFileFile <- parseInputFiles (FileExtJuvix :| [FileExtJuvixMarkdown]) _scanFileStrategy <- optImportScanStrategy + _scanFilePrintLoc <- + switch + ( long "print-loc" + <> help "Print the location of each import" + ) pure ScanFileOptions {..} diff --git a/src/Juvix/Compiler/Concrete/Translation/ImportScanner/Base.hs b/src/Juvix/Compiler/Concrete/Translation/ImportScanner/Base.hs index 4353b0a280..3e699dbf4c 100644 --- a/src/Juvix/Compiler/Concrete/Translation/ImportScanner/Base.hs +++ b/src/Juvix/Compiler/Concrete/Translation/ImportScanner/Base.hs @@ -52,3 +52,5 @@ data Token | TokenImport ImportScanParsed | TokenReserved | TokenCode + +makePrisms ''Token diff --git a/src/Juvix/Compiler/Concrete/Translation/ImportScanner/FlatParse.hs b/src/Juvix/Compiler/Concrete/Translation/ImportScanner/FlatParse.hs index f95a69ebcf..bee8afc2c7 100644 --- a/src/Juvix/Compiler/Concrete/Translation/ImportScanner/FlatParse.hs +++ b/src/Juvix/Compiler/Concrete/Translation/ImportScanner/FlatParse.hs @@ -69,14 +69,9 @@ scanner fp bs = do pPreScanResult :: Parser e [ImportScanParsed] pPreScanResult = do whiteSpaceAndComments - imports <- mapMaybe getImport <$> many pToken + imports <- (^.. each . _TokenImport) <$> many pToken eof return imports - where - getImport :: Token -> Maybe ImportScanParsed - getImport = \case - TokenImport i -> Just i - _ -> Nothing bareIdentifier :: ParserT st e String bareIdentifier = do diff --git a/src/Juvix/Prelude/FlatParse/Lexer.hs b/src/Juvix/Prelude/FlatParse/Lexer.hs index 3c0873404d..c898017c22 100644 --- a/src/Juvix/Prelude/FlatParse/Lexer.hs +++ b/src/Juvix/Prelude/FlatParse/Lexer.hs @@ -11,7 +11,5 @@ import Juvix.Prelude.FlatParse whiteSpace1 :: Parser e () whiteSpace1 = skipSome whiteSpaceChar --- TODO is Ascii version much faster? whiteSpaceChar :: Parser e () --- whiteSpaceChar = skipSatisfyAscii L.isWhiteSpace whiteSpaceChar = skipSatisfy L.isWhiteSpace