-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.go
73 lines (63 loc) · 1.47 KB
/
scanner.go
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
73
package lit
import (
"bytes"
)
var (
TAB = []byte("\t")
NEWLINE = []byte("\n")
SPACE = []byte(" ")
FOUR_SPACES = []byte(" ")
THREE_BACKTICKS = []byte("```")
)
const (
TYPE_DOC = "DOC"
TYPE_CODE = "CODE"
TYPE_CODESIGN = "CODESIGN"
)
type Token struct {
Type string
Decl string
Value []byte
}
func Scanner(source []byte) []Token {
lines := bytes.Split(source, NEWLINE)
tokens := make([]Token, len(lines))
lastType := ""
for i := 0; i < len(lines); i++ {
if bytes.HasPrefix(lines[i], FOUR_SPACES) {
tokens[i].Type = TYPE_CODE
tokens[i].Value = bytes.Replace(lines[i], FOUR_SPACES, []byte(""), 1)
continue
}
if bytes.HasPrefix(lines[i], TAB) {
tokens[i].Type = TYPE_CODE
tokens[i].Value = bytes.Replace(lines[i], TAB, []byte(""), 1)
continue
}
tokens[i].Value = lines[i]
if len(lines[i]) >= 3 {
if bytes.Equal(lines[i][0:3], THREE_BACKTICKS) {
tokens[i].Type = TYPE_CODESIGN
if lastType == "" {
lastType = TYPE_CODESIGN
} else {
// reset the lastType
lastType = ""
}
// get the declaration.
// the declaration is the second string aster the three backticks.
codesignDecl := bytes.Split(lines[i][3:], SPACE)
if len(codesignDecl) > 1 {
tokens[i].Decl = string(bytes.Join(codesignDecl[1:], SPACE))
}
continue
}
}
if lastType == TYPE_CODESIGN {
tokens[i].Type = TYPE_CODE
} else {
tokens[i].Type = TYPE_DOC
}
}
return tokens
}