0 | module Text.Markdown.Lexer
 1 |
 2 | import Data.List1
 3 |
 4 | import Text.Lexer
 5 | import Text.Token
 6 |
 7 | import Text.Markdown.Errors
 8 | import Text.Markdown.String
 9 | import public Text.Markdown.Tokens
10 |
11 | %default total
12 |
13 | private
14 | markdownTokenMap : TokenMap MarkdownToken
15 | markdownTokenMap = toTokenMap $
16 |   [ (horizontalRules, MdHorizontalRules)
17 |   , (blankLines, BlankLine)
18 |   , (code, MdCodeBlock)
19 |   , (pre, MdPre)
20 |   , (headingSym, HeadingSym)
21 |     -- Blank lines are captured before and single list symbol after, so it
22 |     -- implies that the list separation is captured only from the second item
23 |     -- of a list.
24 |   , (uListSepSym, UListSepSym)
25 |   , (uListSym, UListSym)
26 |   , (italicsSym, ItalicsSym)
27 |   , (boldSym, BoldSym)
28 |   , (imageStart, ImageStart)
29 |   , (openingBracket, OpeningBracket)
30 |   , (closingBracket, ClosingBracket)
31 |   , (openingParenthesis, OpeningParenthesis)
32 |   , (closingParenthesis, ClosingParenthesis)
33 |   , (htmlCloseTag, HtmlCloseTag)
34 |   , (htmlOpenTag, HtmlOpenTag)
35 |   , (newline, MdNewLine)
36 |   , (text, MdText)
37 |   ]
38 |
39 | ||| Lexes the provided markdown string to Markdown tokens.
40 | export
41 | lexMarkdown : String -> Either MdError (List (WithBounds MarkdownToken))
42 | lexMarkdown str =
43 |   case lex markdownTokenMap str of
44 |     (tokens, line, col, "") => Right tokens
45 |     (_, line, col, remain)  => Left $ MkMdError
46 |                                  LexingError
47 |                                  line
48 |                                  col
49 |                                  "cannot lex: \{remain}"
50 |