@@ -194,7 +194,9 @@ func Parse(tokenString string, options *Options) (Token, error) {
194194 }
195195
196196 isKey := func (token string ) bool {
197- return len (token ) > 1 && strings .HasPrefix (token , "'" ) && strings .HasSuffix (token , "'" )
197+ isSingleQuoted := strings .HasPrefix (token , "'" ) && strings .HasSuffix (token , "'" )
198+ isDoubleQuoted := strings .HasPrefix (token , "\" " ) && strings .HasSuffix (token , "\" " )
199+ return len (token ) > 1 && (isSingleQuoted || isDoubleQuoted )
198200 }
199201
200202 tokenString = strings .TrimSpace (tokenString )
@@ -247,7 +249,12 @@ func Parse(tokenString string, options *Options) (Token, error) {
247249 // which would result in the parsing being invalid
248250
249251 openBracketCount , closeBracketCount := 0 , 0
250- openQuote := false
252+ openSingleQuote := false
253+ openDoubleQuote := false
254+
255+ inQuotes := func () bool {
256+ return openSingleQuote || openDoubleQuote
257+ }
251258
252259 args := []interface {}{}
253260
@@ -256,13 +263,13 @@ func Parse(tokenString string, options *Options) (Token, error) {
256263 bufferString += string (rne )
257264 switch rne {
258265 case ' ' :
259- if ! openQuote && openBracketCount == closeBracketCount {
266+ if ! inQuotes () && openBracketCount == closeBracketCount {
260267 // remove whitespace
261268 bufferString = strings .TrimSpace (bufferString )
262269 }
263270 break
264271 case '(' :
265- if openQuote {
272+ if inQuotes () {
266273 continue
267274 }
268275 openBracketCount ++
@@ -281,7 +288,7 @@ func Parse(tokenString string, options *Options) (Token, error) {
281288 }
282289 break
283290 case '\'' :
284- if openBracketCount != closeBracketCount {
291+ if openDoubleQuote || openBracketCount != closeBracketCount {
285292 continue
286293 }
287294
@@ -291,9 +298,9 @@ func Parse(tokenString string, options *Options) (Token, error) {
291298 break
292299 }
293300
294- openQuote = ! openQuote
301+ openSingleQuote = ! openSingleQuote
295302
296- if openQuote {
303+ if openSingleQuote {
297304 // open quote
298305 if bufferString != "'" {
299306 return nil , getInvalidTokenFormatError (tokenString )
@@ -307,8 +314,35 @@ func Parse(tokenString string, options *Options) (Token, error) {
307314 bufferString = ""
308315 }
309316 break
317+ case '"' :
318+ if openSingleQuote || openBracketCount != closeBracketCount {
319+ continue
320+ }
321+
322+ // if last token is escape character, then this is not an open or close
323+ if len (bufferString ) > 1 && bufferString [len (bufferString )- 2 ] == '\\' {
324+ bufferString = bufferString [0 :len (bufferString )- 2 ] + "\" "
325+ break
326+ }
327+
328+ openDoubleQuote = ! openDoubleQuote
329+
330+ if openDoubleQuote {
331+ // open quote
332+ if bufferString != "\" " {
333+ return nil , getInvalidTokenFormatError (tokenString )
334+ }
335+ } else {
336+ // close quote
337+ if ! isKey (bufferString ) {
338+ return nil , getInvalidTokenFormatError (tokenString )
339+ }
340+ args = append (args , bufferString [:])
341+ bufferString = ""
342+ }
343+ break
310344 case ':' :
311- if openQuote || (openBracketCount != closeBracketCount ) {
345+ if inQuotes () || (openBracketCount != closeBracketCount ) {
312346 continue
313347 }
314348 if arg := bufferString [:len (bufferString )- 1 ]; arg != "" {
@@ -326,7 +360,7 @@ func Parse(tokenString string, options *Options) (Token, error) {
326360 bufferString = ""
327361 break
328362 case ',' :
329- if openQuote || (openBracketCount != closeBracketCount ) {
363+ if inQuotes () || (openBracketCount != closeBracketCount ) {
330364 continue
331365 }
332366
0 commit comments