Skip to content

Commit

Permalink
Adds escape sequence tracking to acceptUntilUnescaped; fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Jan 31, 2017
1 parent e45f552 commit f915748
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ungron.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,23 @@ func (l *lexer) acceptUntil(delims string) {
// rune contained in the provided string, unless that rune was
// escaped with a backslash
func (l *lexer) acceptUntilUnescaped(delims string) {

// Read until we hit an unescaped rune or the end of the input
inEscape := false
for {
if strings.ContainsRune(delims, l.next()) && l.prev != '\\' {
r := l.next()
if r == '\\' && !inEscape {
inEscape = true
continue
}
if strings.ContainsRune(delims, r) && !inEscape {
l.backup()
return
}
if l.cur == utf8.RuneError {
return
}
inEscape = false
}
}

Expand Down
21 changes: 21 additions & 0 deletions ungron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ func TestLex(t *testing.T) {
{`;`, typSemi},
}},

{`json = "\\";`, []token{
{`json`, typBare},
{`=`, typEquals},
{`"\\"`, typString},
{`;`, typSemi},
}},

{`json = "\\\\";`, []token{
{`json`, typBare},
{`=`, typEquals},
{`"\\\\"`, typString},
{`;`, typSemi},
}},

{`json = "f\oo\\";`, []token{
{`json`, typBare},
{`=`, typEquals},
{`"f\oo\\"`, typString},
{`;`, typSemi},
}},

{`json.value = "\u003c ;";`, []token{
{`json`, typBare},
{`.`, typDot},
Expand Down

0 comments on commit f915748

Please sign in to comment.