-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_options.go
123 lines (100 loc) · 2.97 KB
/
lexer_options.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
package golex
import "slices"
type LexerOptionFunc func(*Lexer)
func DebugPrintTokens() LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.DebugPrintTokens = true
})
}
func OmitTokenPosition() LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.OmitTokenPosition = true
})
}
func IgnoreTokens(types ...TokenType) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.IgnoreTokens = append(l.IgnoreTokens, types...)
})
}
func RetainWhitespace() LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.IgnoreWhitespace = false
})
}
func WithKeywords(keywords ...string) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.Keywords = append(l.Keywords, keywords...)
if len(l.Keywords) > 0 {
l.CheckForKeywords = true
}
})
}
func SymbolCharacterMap(startCharMap, continueCharMap string) LexerOptionFunc {
expandedStartMap, err := expandCharacterPattern(startCharMap)
if err != nil {
// TODO: Maybe not panic this...
panic(err)
}
expandedContinueMap, err := expandCharacterPattern(continueCharMap)
if err != nil {
// TODO: Maybe not panic this...
panic(err)
}
return LexerOptionFunc(func(l *Lexer) {
l.SymbolStartCharacterMap = expandedStartMap
l.SymbolContinueCharacterMap = expandedContinueMap
})
}
func WithTokenizer(inserter TokenizerInserter) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.tokenizers, l.tokenizationOrder = inserter.Insert(l.tokenizers, l.tokenizationOrder)
})
}
func WithLiteralTokens(literalTokens ...LiteralToken) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.LiteralTokens = SortLiteralTokens(append(l.LiteralTokens, literalTokens...))
})
}
func WithoutLiteralTokens(literalTokens ...TokenType) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
literals := []LiteralToken{}
for _, t := range l.LiteralTokens {
if !slices.Contains(literalTokens, t.Type) {
literals = append(literals, t)
}
}
l.LiteralTokens = literals
})
}
func WithCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.CommentSyntaxes = append(l.CommentSyntaxes, syntaxes...)
})
}
func WithoutCommentSyntax(syntaxes ...CommentSyntax) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
commentSyntax := []CommentSyntax{}
for _, s := range l.CommentSyntaxes {
if !slices.Contains(syntaxes, s) {
commentSyntax = append(commentSyntax, s)
}
}
l.CommentSyntaxes = commentSyntax
})
}
func WithStringEnclosure(enclosures ...StringEnclosure) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
l.StringEnclosures = append(l.StringEnclosures, enclosures...)
})
}
func WithoutStringEnclosure(enclosures ...string) LexerOptionFunc {
return LexerOptionFunc(func(l *Lexer) {
stringEnclosures := []StringEnclosure{}
for _, e := range l.StringEnclosures {
if !slices.Contains(enclosures, e.Enclosure) {
stringEnclosures = append(stringEnclosures, e)
}
}
l.StringEnclosures = stringEnclosures
})
}