aboutsummaryrefslogtreecommitdiff
path: root/mardown/lexer.go
blob: a7b364d3502d9ff2e6bafd79aece892d62ddb631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
}