-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer_strings.go
157 lines (121 loc) · 3.93 KB
/
tokenizer_strings.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
package golex
import "fmt"
var (
DoubleQuoteStringEnclosure StringEnclosure = StringEnclosure{
Type: TypeDoubleQuoteString,
Enclosure: "\"",
Escapable: true,
}
SingleQuoteStringEnclosure StringEnclosure = StringEnclosure{
Type: TypeSingleQuoteString,
Enclosure: "'",
}
BacktickStringEnclosure StringEnclosure = StringEnclosure{
Type: TypeBacktickString,
Enclosure: "`",
}
TripleBacktickStringEnclosure StringEnclosure = StringEnclosure{
Type: TypeTripleBacktickString,
Enclosure: "```",
}
cachedStringEnclosure *StringEnclosure
)
type StringTokenizer struct{}
func (s StringTokenizer) CanTokenize(l *Lexer) bool {
for _, enclosure := range l.StringEnclosures {
if l.NextCharsAre([]rune(enclosure.Enclosure)) {
cachedStringEnclosure = &enclosure
return true
}
}
return false
}
func (s StringTokenizer) Tokenize(l *Lexer) (Token, error) {
if cachedStringEnclosure == nil {
if !s.CanTokenize(l) {
return Token{Type: TypeInvalid, Position: l.GetPosition()},
NewError(fmt.Sprintf("Invalid character '%c' found", l.CharAtCursor()), l.GetPosition(), l.state.Content)
} else {
return s.Tokenize(l)
}
}
token, err := cachedStringEnclosure.Tokenize(l)
cachedStringEnclosure = nil
return token, err
}
// ###################################################
// # StringEnclosures
// ###################################################
type StringEnclosure struct {
Type TokenType
Enclosure string
Escapable bool
}
func (se StringEnclosure) Tokenize(l *Lexer) (Token, error) {
if len(se.Enclosure) > 1 {
return se.TokenizeNotEscapableMultiChar(l)
}
if !se.Escapable {
return se.TokenizeNotEscapableSingleChar(l)
}
return se.TokenizeEscapable(l)
}
func (se StringEnclosure) TokenizeEscapable(l *Lexer) (Token, error) {
enclosureChar := []rune(se.Enclosure)[0]
token := Token{Type: se.Type, Position: l.GetPosition()}
start := l.GetCursor()
token.AppendChar(l.CharAtCursor())
l.IncrementCursor(1)
nextEnclosureCharIsEscaped := false
for !l.CursorIsOutOfBounds() && (l.CharAtCursor() != enclosureChar || nextEnclosureCharIsEscaped) {
if nextEnclosureCharIsEscaped {
nextEnclosureCharIsEscaped = false
}
if l.CharAtCursor() == '\\' && l.CharAtRelativePosition(1) == enclosureChar {
nextEnclosureCharIsEscaped = true
}
token.AppendChar(l.CharAtCursor())
l.IncrementCursor(1)
if l.CharAtCursor() == EOF {
return token, NewError("Unterminated string literal", token.Position, l.state.Content)
}
}
token.AppendChar(l.CharAtCursor())
token.Value = l.GetSourceSubsString(start+1, l.GetCursor())
return token, nil
}
func (se StringEnclosure) TokenizeNotEscapableSingleChar(l *Lexer) (Token, error) {
enclosureChar := []rune(se.Enclosure)[0]
token := Token{Type: se.Type, Position: l.GetPosition()}
start := l.GetCursor()
token.AppendChar(l.CharAtCursor())
l.IncrementCursor(1)
for !l.CursorIsOutOfBounds() && l.CharAtCursor() != enclosureChar {
token.AppendChar(l.CharAtCursor())
l.IncrementCursor(1)
if l.CharAtCursor() == EOF {
return token, NewError("Unterminated string literal", token.Position, l.state.Content)
}
}
token.AppendChar(l.CharAtCursor())
token.Value = l.GetSourceSubsString(start+1, l.GetCursor())
return token, nil
}
func (se StringEnclosure) TokenizeNotEscapableMultiChar(l *Lexer) (Token, error) {
enclosureLen := len(se.Enclosure)
token := Token{Type: se.Type, Position: l.GetPosition()}
start := l.GetCursor()
token.AppendChar([]rune(se.Enclosure)...)
l.IncrementCursor(enclosureLen)
for !l.CursorIsOutOfBounds() && !l.NextCharsAre([]rune(se.Enclosure)) {
token.AppendChar(l.CharAtCursor())
l.IncrementCursor(1)
if l.CharAtCursor() == EOF {
return token, NewError("Unterminated string literal", token.Position, l.state.Content)
}
}
token.AppendChar([]rune(se.Enclosure)...)
token.Value = l.GetSourceSubsString(start+enclosureLen, l.GetCursor())
l.IncrementCursor(enclosureLen - 1)
return token, nil
}