-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.go
199 lines (173 loc) · 4.14 KB
/
lexer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package traindown
import (
"fmt"
"strings"
"github.com/timtadh/lexmachine"
"github.com/timtadh/lexmachine/machines"
)
// Tokens used in parsing Traindown inputs
var Tokens = []string{
"DATE", "LOAD", "FAILS", "METADATA", "MOVEMENT", "MOVEMENT_SS", "NOTE", "REPS", "SETS",
}
// Token holds information about a token
type Token struct {
t *lexmachine.Token
}
// TokenMap maps is an enum
var TokenMap map[string]int
func init() {
TokenMap = make(map[string]int)
for i, n := range Tokens {
TokenMap[n] = i
}
}
// Name getter
func (tok Token) Name() string {
return Tokens[tok.t.Type]
}
// Type getter
func (tok Token) Type() int {
return tok.t.Type
}
// Value getter
func (tok Token) Value() string {
return tok.t.Value.(string)
}
// Start getter
func (tok Token) Start() (int, int) {
return tok.t.StartLine, tok.t.StartColumn
}
// End getter
func (tok Token) End() (int, int) {
return tok.t.EndLine, tok.t.EndColumn
}
// String override
func (tok *Token) String() string {
return fmt.Sprintf("%q %q (From: r%d, c%d To: r%d c%d)", tok.Name(), tok.t.Value, tok.t.StartLine, tok.t.StartColumn, tok.t.EndLine, tok.t.EndColumn)
}
// Lexer type
type Lexer struct {
l *lexmachine.Lexer
}
// NewLexer returns a new Lexer
func NewLexer() (Lexer, error) {
var lexer = lexmachine.NewLexer()
lexer.Add(
[]byte("@[^\n]*"),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
return scan.Token(
TokenMap["DATE"],
strings.TrimSpace(string(match.Bytes)[1:]),
match),
nil
},
)
lexer.Add(
[]byte(`#[^+@:#*\n]+:[^+@:#*\n]*`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
lR := strings.Split(string(match.Bytes)[1:], ":")
l := strings.TrimSpace(lR[0])
r := strings.TrimSpace(lR[1])
var kvp strings.Builder
kvp.WriteString(l)
kvp.WriteString(": ")
kvp.WriteString(r)
return scan.Token(
TokenMap["METADATA"],
kvp.String(),
match),
nil
},
)
lexer.Add(
[]byte(`\*[^\n|\r]*`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
return scan.Token(
TokenMap["NOTE"],
strings.TrimSpace(string(match.Bytes)[1:]),
match),
nil
},
)
lexer.Add(
[]byte(`[0-9]+[fF]`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
s := string(match.Bytes)
return scan.Token(
TokenMap["FAILS"],
s[:len(s)-1],
match),
nil
},
)
lexer.Add(
[]byte(`[0-9]+[rR]`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
s := string(match.Bytes)
return scan.Token(
TokenMap["REPS"],
s[:len(s)-1],
match),
nil
},
)
lexer.Add(
[]byte(`[0-9]+[sS]`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
s := string(match.Bytes)
return scan.Token(
TokenMap["SETS"],
s[:len(s)-1],
match),
nil
},
)
lexer.Add(
[]byte(`[0-9]*\.?[0-9]+`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
return scan.Token(TokenMap["LOAD"], string(match.Bytes), match), nil
},
)
lexer.Add(
[]byte(`(\+\s*)?([^+@:#*\n ]\s?)+:`),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
s := strings.TrimSuffix(string(match.Bytes), ":")
var tokType int
if strings.HasPrefix(s, "+") {
tokType = TokenMap["MOVEMENT_SS"]
s = strings.TrimPrefix(s, "+")
s = strings.TrimSpace(s)
} else {
tokType = TokenMap["MOVEMENT"]
}
return scan.Token(tokType, s, match), nil
},
)
lexer.Add(
[]byte("( |\t|\n|\r)"),
func(scan *lexmachine.Scanner, match *machines.Match) (interface{}, error) {
return nil, nil
},
)
err := lexer.CompileDFA()
if err != nil {
return Lexer{}, err
}
return Lexer{lexer}, nil
}
// Scan returns the next token
func (lexer Lexer) Scan(text []byte) ([]*Token, error) {
scanner, err := lexer.l.Scanner(text)
var tokens []*Token
if err != nil {
return tokens, err
}
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
if err != nil {
return tokens, err
}
token := &Token{tok.(*lexmachine.Token)}
tokens = append(tokens, token)
}
return tokens, nil
}