Skip to content

Commit

Permalink
Fix the location in the parser for .juvix.md (#3020)
Browse files Browse the repository at this point in the history
This pr makes it possible to properly hihglight .juvix.md files
  • Loading branch information
janmasrovira authored Sep 11, 2024
1 parent 799d850 commit 8e20463
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/Juvix/Compiler/Backend/Markdown/Data/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 "---"
Expand Down
14 changes: 11 additions & 3 deletions src/Juvix/Compiler/Concrete/Translation/FromSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::
Expand Down
8 changes: 8 additions & 0 deletions src/Juvix/Data/Loc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions tests/positive/Markdown/Test.juvix.md
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8e20463

Please sign in to comment.