From 3f3696eb9f294c5494ff32d129decbee7c6fcb33 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Tue, 4 Jul 2023 11:39:12 -0400 Subject: [PATCH] Lexer can now handle multi-line inputs --- lexer_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ states.go | 5 ++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lexer_test.go b/lexer_test.go index 51e589a..e32c292 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -196,6 +196,48 @@ var testCases = []tc{ }, } +func Test_ObsidianWikilinks_LinksEndOfMultiLineInput(t *testing.T) { + for _, tc := range testCases { + mut, test := mutateTestCase( + tc, + " test data please ignore.\nbling blonk more lines\nbling blong\nthis is a", + "", + + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: " test data please ignore.\n"}, + {Typ: wikilink.LexText, Val: "bling blonk more lines\n"}, + {Typ: wikilink.LexText, Val: "bling blong\n"}, + {Typ: wikilink.LexText, Val: "this is a"}, + }, + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: ""}, + }, + ) + t.Run(mut.name, test) + } +} + +func Test_ObsidianWikilinks_LinksStartOfMultiLineInput(t *testing.T) { + for _, tc := range testCases { + mut, test := mutateTestCase( + tc, + "", + " test data please ignore.\nbling blonk more lines\nbling blong\nthis is a", + + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: ""}, + }, + []wikilink.Lexeme{ + {Typ: wikilink.LexText, Val: " test data please ignore.\n"}, + {Typ: wikilink.LexText, Val: "bling blonk more lines\n"}, + {Typ: wikilink.LexText, Val: "bling blong\n"}, + {Typ: wikilink.LexText, Val: "this is a"}, + }, + ) + t.Run(mut.name, test) + } +} + func Test_ObsidianWikilinks_LinksStartOfInput(t *testing.T) { for _, tc := range testCases { mut, test := mutateTestCase( diff --git a/states.go b/states.go index 7ad8451..7f21fb3 100644 --- a/states.go +++ b/states.go @@ -92,9 +92,12 @@ func lexText(l *Lexer) stateFn { } r := l.next() switch { - case r == EOF || r == '\n': + case r == EOF: l.emit(LexText) return nil + case r == '\n': + l.emit(LexText) + return lexText } } }