From 8e204634b85a710f284198dacd406cdc37470565 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Wed, 11 Sep 2024 14:07:16 +0200 Subject: [PATCH] Fix the location in the parser for .juvix.md (#3020) This pr makes it possible to properly hihglight .juvix.md files --- src/Juvix/Compiler/Backend/Markdown/Data/Types.hs | 3 +-- .../Compiler/Concrete/Translation/FromSource.hs | 14 +++++++++++--- src/Juvix/Data/Loc.hs | 8 ++++++++ tests/positive/Markdown/Test.juvix.md | 5 ++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Juvix/Compiler/Backend/Markdown/Data/Types.hs b/src/Juvix/Compiler/Backend/Markdown/Data/Types.hs index f9e3fc8b9c..19494faca8 100644 --- a/src/Juvix/Compiler/Backend/Markdown/Data/Types.hs +++ b/src/Juvix/Compiler/Backend/Markdown/Data/Types.hs @@ -177,8 +177,7 @@ processCodeBlock info t loc = let b = "```" <> info <> t <> "```" in MkTextBlock TextBlock {_textBlock = b, _textBlockInterval = loc} -instance-- (MK.IsInline TextBlock) => - MK.IsBlock TextBlock Mk where +instance MK.IsBlock TextBlock Mk where paragraph a = MkTextBlock a plain a = MkTextBlock a thematicBreak = toMK "---" diff --git a/src/Juvix/Compiler/Concrete/Translation/FromSource.hs b/src/Juvix/Compiler/Concrete/Translation/FromSource.hs index 77ba8a8737..fc16dc02ef 100644 --- a/src/Juvix/Compiler/Concrete/Translation/FromSource.hs +++ b/src/Juvix/Compiler/Concrete/Translation/FromSource.hs @@ -193,24 +193,32 @@ runMarkdownModuleParser fpath mk = P.sourceColumn = P.mkPos (intervalStartCol i) } + getInitFileLoc :: Interval -> FileLoc + getInitFileLoc = (^. intervalStart) + getInitialParserState :: forall a. MK.JuvixCodeBlock -> P.State Text a getInitialParserState code = - let initPos = + let initPos :: P.SourcePos = maybe (P.initialPos (toFilePath fpath)) getInitPos (code ^. MK.juvixCodeBlockInterval) + initFileLoc :: FileLoc = + maybe + mkInitialFileLoc + getInitFileLoc + (code ^. MK.juvixCodeBlockInterval) in P.State { P.stateInput = code ^. MK.juvixCodeBlock, P.statePosState = P.PosState { P.pstateInput = code ^. MK.juvixCodeBlock, - P.pstateOffset = 0, + P.pstateOffset = fromIntegral (initFileLoc ^. locOffset), P.pstateSourcePos = initPos, P.pstateTabWidth = P.defaultTabWidth, P.pstateLinePrefix = "" }, - P.stateOffset = 0, + P.stateOffset = fromIntegral (initFileLoc ^. locOffset), P.stateParseErrors = [] } parseHelper :: diff --git a/src/Juvix/Data/Loc.hs b/src/Juvix/Data/Loc.hs index 547fcb5860..2140fef45d 100644 --- a/src/Juvix/Data/Loc.hs +++ b/src/Juvix/Data/Loc.hs @@ -67,6 +67,14 @@ mkLoc offset M.SourcePos {..} = mkInitialLoc :: Path Abs File -> Loc mkInitialLoc = mkLoc 0 . M.initialPos . fromAbsFile +mkInitialFileLoc :: FileLoc +mkInitialFileLoc = + FileLoc + { _locLine = 0, + _locCol = 0, + _locOffset = 0 + } + fromPos :: M.Pos -> Pos fromPos = Pos . fromIntegral . M.unPos diff --git a/tests/positive/Markdown/Test.juvix.md b/tests/positive/Markdown/Test.juvix.md index aeb24dfa7e..e4e79d6b02 100644 --- a/tests/positive/Markdown/Test.juvix.md +++ b/tests/positive/Markdown/Test.juvix.md @@ -1,9 +1,9 @@ # Example -What is important is seldom urgent. +What is important is seldom urgent. A Juvix Markdown file name ends with `.juvix.md`. This kind of file must contain -a module declaration at the top, as shown below ---in the first code block. +a module declaration at the top, as shown below ---in the first code block. ```juvix module Test; @@ -18,7 +18,6 @@ import Stdlib.Prelude open; ```juvix fib : Nat → Nat → Nat → Nat | zero x1 _ := x1 - | (suc n) x1 x2 := fib n x2 (x1 + x2); fibonacci (n : Nat) : Nat := fib n 0 1;