Skip to content

Commit

Permalink
Make it possible to escape charactors in string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchida committed Jun 15, 2021
1 parent 3b040cc commit 5d67c47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 6 additions & 0 deletions expressions/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ var evaluatorTests = []struct {
{`'abc'`, "abc"},
{`"abc"`, "abc"},

{`"a\\bc"`, "a\\bc"},
{`"a\"bc"`, "a\"bc"},
{`"\u007b\u007b abc \u007d\u007d"`, "{{ abc }}"},
// TODO: single-quoted cannot be escaped
// {`'a\'bc'`, "a'bc"},

// Variables
{`n`, 123},

Expand Down
20 changes: 17 additions & 3 deletions expressions/scanner.rl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,20 @@ func (lex *lexer) Lex(out *yySymType) int {
}
action String {
tok = LITERAL
// TODO unescape \x
out.val = string(lex.data[lex.ts+1:lex.te-1])

var s string
// TODO: single-quoted cannot be escaped
if lex.data[lex.ts] == '\'' {
s = string(lex.data[lex.ts+1:lex.te-1])
} else {
unquotedStr, err := strconv.Unquote(string(lex.data[lex.ts:lex.te]))
if err != nil {
panic(err)
}
s = unquotedStr
}

out.val = s
fbreak;
}
action Relation { tok = RELATION; out.name = lex.token(); fbreak; }
Expand All @@ -76,7 +88,9 @@ func (lex *lexer) Lex(out *yySymType) int {
property = '.' (alpha | '_') . (alnum | '_' | '-')* '?' ? ;
int = '-'? digit+ ;
float = '-'? digit+ ('.' digit+)? ;
string = '"' (any - '"')* '"' | "'" (any - "'")* "'" ; # TODO escapes

dqchar = [^"\\] | ( '\\' any );
string = '"' . dqchar* . '"' | "'" (any - "'")* "'" ;

main := |*
# statement selectors, should match constants in parser.go
Expand Down

0 comments on commit 5d67c47

Please sign in to comment.