Skip to content

Commit

Permalink
Allow more escape runes to be skipped over when parsing string litera…
Browse files Browse the repository at this point in the history
…l. (#2734)

- `\v`
- `\xnn`

Inspiration from: https://golang.org/src/strconv/quote.go
  • Loading branch information
hoonmin authored and manishrjain committed Nov 9, 2018
1 parent 6b474ff commit 70e07e8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lex/iri.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ func HasUChars(r rune, l *Lexer) bool {
return times == l.AcceptRunTimes(isHex, times)
}

// XCHAR ::= '\x' HEX HEX
func HasXChars(r rune, l *Lexer) bool {
if r != 'x' {
return false
}
times := 2
return times == l.AcceptRunTimes(isHex, times)
}

// HEX ::= [0-9] | [A-F] | [a-f]
func isHex(r rune) bool {
switch {
Expand Down
4 changes: 2 additions & 2 deletions lex/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ const (
quote = '"'
)

// ECHAR ::= '\' [tbnrf"'\]
// ECHAR ::= '\' [vtbnrf"'\]
func (l *Lexer) IsEscChar(r rune) bool {
switch r {
case 't', 'b', 'n', 'r', 'f', '"', '\'', '\\':
case 'v', 't', 'b', 'n', 'r', 'f', '"', '\'', '\\':
return true
}
return false
Expand Down
17 changes: 15 additions & 2 deletions rdf/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,30 @@ var testNQuads = []struct {
input: `<alice> <lives> "\u004 wonderland" .`,
expectedErr: true, // should have 4 hex values after \u
},
{
input: `<alice> <lives> "\x02 wonderland" .`,
nq: api.NQuad{
Subject: "alice",
Predicate: "lives",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "\x02 wonderland"}},
},
expectedErr: false,
},
{
input: `<alice> <lives> "\x2 wonderland" .`,
expectedErr: true, // should have 2 hex values after \x
},
{
input: `<alice> <lives> "wonderful land"@a- .`,
expectedErr: true, // object langtag can not end with -
},
{
input: `<alice> <lives> "\t\b\n\r\f\"\\"@a-b .`,
input: `<alice> <lives> "\v\t\b\n\r\f\"\\"@a-b .`,
nq: api.NQuad{
Subject: "alice",
Predicate: "lives",
Lang: "a-b",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "\t\b\n\r\f\"\\"}},
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "\v\t\b\n\r\f\"\\"}},
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion rdf/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func lexLiteral(l *lex.Lexer) lex.StateFn {
r := l.Next()
if r == '\u005c' { // backslash
r = l.Next()
if l.IsEscChar(r) || lex.HasUChars(r, l) {
if l.IsEscChar(r) || lex.HasUChars(r, l) || lex.HasXChars(r, l) {
continue // This would skip over the escaped rune.
}
return l.Errorf("Invalid escape character : '%c' in literal", r)
Expand Down

0 comments on commit 70e07e8

Please sign in to comment.