Improved lexer funcs

This commit is contained in:
Igor Ranieri 2025-09-21 10:36:25 +02:00
parent f586b90434
commit e81f1ea4f7
2 changed files with 118 additions and 67 deletions

View file

@ -12,8 +12,87 @@ import Data.String (IsString (..))
import Data.Text (Text)
import Text.Parsec.Pos
shouldParseTo :: Text -> DocMarkup mod Identifier -> Expectation
shouldParseTo input ast = parseText input `shouldBe` ast
main :: IO ()
main = hspec $ do
describe "Lexer" do
describe "minimal" do
it "space chars" space
it "bare string" someString
it "emphasis" emphatic
it "monospace" monospace
it "bird tracks" birdTracks
it "quotes" quotes
it "ignores nesting" ignoreNesting
describe "Parser" do
it "Bold" do
"__bold__" `shouldParseTo` (DocBold (DocString "bold"))
it "Emphasis" do
"/emphasis/" `shouldParseTo` (DocEmphasis (DocString "emphasis"))
------------
-- Tests
------------
ignoreNesting :: Expectation
ignoreNesting =
">/foo/"
`shouldLexTo` [ (1, 1, Token ">/foo/")
]
birdTracks :: Expectation
birdTracks =
">> code"
`shouldLexTo` [ (1, 1, BirdTrack)
, (1, 2, Token "code")
]
quotes :: Expectation
quotes =
"\"quoted\""
`shouldLexTo` [ (1, 1, QuoteOpen)
, (1, 2, Token "quoted")
, (1, 8, QuoteClose)
]
space :: Expectation
space = do
"\n "
`shouldLexTo` [ (1, 1, Newline)
, (2, 1, Space)
]
" \n"
`shouldLexTo` [ (1, 1, Space)
, (1, 2, Newline)
]
monospace :: Expectation
monospace =
"@mono@"
`shouldLexTo` [ (1, 1, MonospaceOpen)
, (1, 2, Token "mono")
, (1, 6, MonospaceClose)
]
emphatic :: Expectation
emphatic =
"/emphatic/"
`shouldLexTo` [ (1, 1, EmphasisOpen)
, (1, 2, Token "emphatic")
, (1, 10, EmphasisClose)
]
someString :: Expectation
someString =
"some string"
`shouldLexTo` [ (1, 1, Token "some")
, (1, 5, Space)
, (1, 6, Token "string")
]
--------------
-- Helpers
--------------
type Doc id = DocMarkup () id
@ -28,46 +107,5 @@ shouldLexTo input expected =
actual `shouldBe` expected
Left err -> expectationFailure $ "Parse error: " <> show err
main :: IO ()
main = hspec $ do
describe "Lexer" do
describe "minimal" do
it "bare string" someString
it "emphasis" emphatic
it "monospace" monospace
it "ignores nesting" ignoreNesting
describe "Parser" do
it "Bold" do
"__bold__" `shouldParseTo` (DocBold (DocString "bold"))
it "Emphasis" do
"/emphasis/" `shouldParseTo` (DocEmphasis (DocString "emphasis"))
monospace :: Expectation
monospace =
"@mono@"
`shouldLexTo` [ (1, 7, MonospaceOpen)
, (1, 7, Token "mono")
, (1, 7, MonospaceClose)
]
ignoreNesting :: Expectation
ignoreNesting =
">/foo/"
`shouldLexTo` [ (1, 1, Token ">/foo/")
]
emphatic :: Expectation
emphatic =
"/emphatic/"
`shouldLexTo` [ (1, 11, EmphasisOpen)
, (1, 11, Token "emphatic")
, (1, 11, EmphasisClose)
]
someString :: Expectation
someString =
"some string"
`shouldLexTo` [ (1, 1, Token "some")
, (1, 5, Space)
, (1, 6, Token "string")
]
shouldParseTo :: Text -> DocMarkup mod Identifier -> Expectation
shouldParseTo input ast = parseText input `shouldBe` ast