Skip to content

Commit f915748

Browse files
committed
Adds escape sequence tracking to acceptUntilUnescaped; fixes #25
1 parent e45f552 commit f915748

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

ungron.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,23 @@ func (l *lexer) acceptUntil(delims string) {
162162
// rune contained in the provided string, unless that rune was
163163
// escaped with a backslash
164164
func (l *lexer) acceptUntilUnescaped(delims string) {
165+
165166
// Read until we hit an unescaped rune or the end of the input
167+
inEscape := false
166168
for {
167-
if strings.ContainsRune(delims, l.next()) && l.prev != '\\' {
169+
r := l.next()
170+
if r == '\\' && !inEscape {
171+
inEscape = true
172+
continue
173+
}
174+
if strings.ContainsRune(delims, r) && !inEscape {
168175
l.backup()
169176
return
170177
}
171178
if l.cur == utf8.RuneError {
172179
return
173180
}
181+
inEscape = false
174182
}
175183
}
176184

ungron_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ func TestLex(t *testing.T) {
4646
{`;`, typSemi},
4747
}},
4848

49+
{`json = "\\";`, []token{
50+
{`json`, typBare},
51+
{`=`, typEquals},
52+
{`"\\"`, typString},
53+
{`;`, typSemi},
54+
}},
55+
56+
{`json = "\\\\";`, []token{
57+
{`json`, typBare},
58+
{`=`, typEquals},
59+
{`"\\\\"`, typString},
60+
{`;`, typSemi},
61+
}},
62+
63+
{`json = "f\oo\\";`, []token{
64+
{`json`, typBare},
65+
{`=`, typEquals},
66+
{`"f\oo\\"`, typString},
67+
{`;`, typSemi},
68+
}},
69+
4970
{`json.value = "\u003c ;";`, []token{
5071
{`json`, typBare},
5172
{`.`, typDot},

0 commit comments

Comments
 (0)