-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_functions.go
207 lines (161 loc) · 4.34 KB
/
lexer_functions.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
200
201
202
203
204
205
206
207
package ganno
import "github.com/brainicorn/goblex"
// LexBegin is the entry point LexFn for lexing java style annotations.
// Parsers should pass this function as the begin parameter when calling goblex.NewLexer
func LexBegin(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CaptureUntil(true, atSymbol) {
lexer.SkipCurrentToken(true)
return lexAtSymbol
}
return nil
}
func lexAtSymbol(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CaptureIdent() {
if lexer.CurrentTokenIs(openParen) {
lexer.Emit(tokenTypeStartAnno)
return lexOpenParen
}
}
return LexBegin
}
func lexOpenParen(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, openParen)
lexer.SkipCurrentToken(true)
if lexer.CurrentTokenIs(closeParen) {
return lexCloseParen
}
return lexKey
}
func lexCloseParen(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, closeParen)
lexer.SkipCurrentToken(true)
lexer.Emit(tokenTypeEndAnno)
return LexBegin
}
func lexKey(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CaptureIdent() {
if lexer.CurrentTokenIs(equalSign) {
lexer.Emit(tokenTypeKey)
return lexEqualSign
}
}
lexer.Errorf("error parsing parameter key")
return LexBegin
}
func lexEqualSign(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, equalSign)
lexer.SkipCurrentToken(true)
return lexValue
}
func lexValue(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CurrentTokenIs(leftBracket) {
return lexLeftBracket
}
return lexSingleValue
}
func lexSingleValue(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CurrentTokenIs(doubleQuote) {
return lexSingleQuotedValue
}
if tkn := lexer.CaptureUntilOneOf(true, comma, closeParen); tkn != "" {
lexer.Emit(tokenTypeValue)
switch tkn {
case comma:
return lexSingleValueComma
case closeParen:
return lexCloseParen
}
}
lexer.Errorf("error parsing single value: comma or close paren missing")
return LexBegin
}
func lexMultiValue(lexer *goblex.Lexer) goblex.LexFn {
if lexer.CurrentTokenIs(doubleQuote) {
return lexMultiQuotedValue
}
if tkn := lexer.CaptureUntilOneOf(true, comma, rightBracket); tkn != "" {
lexer.Emit(tokenTypeValue)
switch tkn {
case comma:
return lexMultiValueComma
case rightBracket:
return lexRightBracket
}
}
lexer.Errorf("error multi value: comma or rbracket missing")
return LexBegin
}
func lexSingleQuotedValue(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, doubleQuote)
lexer.SkipCurrentToken(true)
lexer.RemoveIgnoreTokens(comments...)
if lexer.CaptureUntil(false, doubleQuote) {
lexer.Emit(tokenTypeValue)
lexer.AddIgnoreTokens(comments...)
lexer.SkipCurrentToken(true)
yup, tkn := lexer.CurrentTokenIsOneOf(comma, closeParen)
if yup {
switch tkn {
case comma:
return lexSingleValueComma
case closeParen:
return lexCloseParen
}
}
}
lexer.AddIgnoreTokens(comments...)
lexer.Errorf("error parsing single quoted value: comma or close paren missing")
return LexBegin
}
func lexMultiQuotedValue(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, doubleQuote)
lexer.SkipCurrentToken(true)
lexer.RemoveIgnoreTokens(comments...)
if lexer.CaptureUntil(false, doubleQuote) {
lexer.Emit(tokenTypeValue)
lexer.AddIgnoreTokens(comments...)
lexer.SkipCurrentToken(true)
yup, tkn := lexer.CurrentTokenIsOneOf(comma, rightBracket)
if yup {
switch tkn {
case comma:
return lexMultiValueComma
case rightBracket:
return lexRightBracket
}
}
}
lexer.AddIgnoreTokens(comments...)
lexer.Errorf("error parsing multi quoted value: comma or Rbracket missing")
return LexBegin
}
func lexSingleValueComma(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, comma)
lexer.SkipCurrentToken(true)
return lexKey
}
func lexMultiValueComma(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, comma)
lexer.SkipCurrentToken(true)
return lexMultiValue
}
func lexLeftBracket(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, leftBracket)
lexer.SkipCurrentToken(true)
return lexMultiValue
}
func lexRightBracket(lexer *goblex.Lexer) goblex.LexFn {
lexer.CaptureUntil(true, rightBracket)
lexer.SkipCurrentToken(true)
yup, tkn := lexer.CurrentTokenIsOneOf(comma, closeParen)
if yup {
switch tkn {
case comma:
return lexSingleValueComma
case closeParen:
return lexCloseParen
}
}
lexer.Errorf("error parsing array value")
return LexBegin
}