aboutsummaryrefslogtreecommitdiff
path: root/mardown
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-09-30 16:35:46 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-09-30 16:35:46 +0200
commit34d16f28f2d85bc1762e65a73d426de4ac8c30ed (patch)
tree86c7d574aaafe68895cc39e322d461133262082c /mardown
parent802bcb5d2c815f89f5651adbe63707599f1d443f (diff)
feat(markdown): simple lexer
Diffstat (limited to 'mardown')
-rw-r--r--mardown/lexer.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/mardown/lexer.go b/mardown/lexer.go
new file mode 100644
index 0000000..a7b364d
--- /dev/null
+++ b/mardown/lexer.go
@@ -0,0 +1,72 @@
+package mardown
+
+import "fmt"
+
+type lexerType string
+
+const (
+ lexerBreak lexerType = "break"
+
+ lexerModifier lexerType = "modifier"
+ lexerCode lexerType = "code"
+ lexerHeader lexerType = "header"
+ lexerQuote lexerType = "quote"
+
+ lexerLiteral lexerType = "literal"
+)
+
+type lexer struct {
+ Type lexerType
+ Value string
+}
+
+func (l *lexer) String() string {
+ return fmt.Sprintf("%s(%s)", l.Type, l.Value)
+}
+
+type lexers struct {
+ current int
+ lexers []lexer
+}
+
+func (l *lexers) Next() lexer {
+ l.current++
+ return l.lexers[l.current]
+}
+
+func (l *lexers) Finished() bool {
+ return l.current+1 >= len(l.lexers)
+}
+
+func lex(s string) *lexers {
+ lxs := &lexers{current: -1}
+ var lexs []lexer
+ var currentType lexerType
+ var previous string
+ fn := func(c rune, t lexerType) {
+ if currentType != t && len(previous) > 0 {
+ lexs = append(lexs, lexer{Type: currentType, Value: previous})
+ previous = ""
+ }
+ currentType = t
+ previous += string(c)
+ }
+ for _, c := range []rune(s) {
+ switch c {
+ case '*', '_':
+ fn(c, lexerModifier)
+ case '`':
+ fn(c, lexerCode)
+ case '\n':
+ fn(c, lexerBreak)
+ case '#':
+ fn(c, lexerHeader)
+ case '>':
+ fn(c, lexerQuote)
+ default:
+ fn(c, lexerLiteral)
+ }
+ }
+ lxs.lexers = lexs
+ return lxs
+}