Skip to content

Add support for multiline string literals #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parser/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (l *lexer) scanEscape(quote rune) rune {
func (l *lexer) scanString(quote rune) (n int) {
ch := l.next() // read character after quote
for ch != quote {
if ch == '\n' || ch == eof {
if ch == eof || ch == '\n' && quote != '`' {
l.error("literal not terminated")
return
}
Expand Down
3 changes: 2 additions & 1 deletion parser/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ var lexTests = []lexTest{
},
},
{
`"double" 'single' "abc \n\t\"\\" '"\'' "'\"" "\xC3\xBF\u263A\U000003A8" '❤️'`,
`"double" 'single'` + "`multi\nline\n`" + `"abc \n\t\"\\" '"\'' "'\"" "\xC3\xBF\u263A\U000003A8" '❤️'`,
[]Token{
{Kind: String, Value: "double"},
{Kind: String, Value: "single"},
{Kind: String, Value: "multi\nline\n"},
{Kind: String, Value: "abc \n\t\"\\"},
{Kind: String, Value: "\"'"},
{Kind: String, Value: "'\""},
Expand Down
2 changes: 1 addition & 1 deletion parser/lexer/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func root(l *lexer) stateFn {
case IsSpace(r):
l.ignore()
return root
case r == '\'' || r == '"':
case r == '\'' || r == '"' || r == '`':
l.scanString(r)
str, err := unescape(l.word())
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions parser/lexer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func unescape(value string) (string, error) {
}

// Quoted string of some form, must have same first and last char.
if value[0] != value[n-1] || (value[0] != '"' && value[0] != '\'') {
if value[0] != value[n-1] || (value[0] != '"' && value[0] != '\'' && value[0] != '`') {
return value, fmt.Errorf("unable to unescape string")
}

Expand Down Expand Up @@ -63,10 +63,10 @@ func unescape(value string) (string, error) {

// unescapeChar takes a string input and returns the following info:
//
// value - the escaped unicode rune at the front of the string.
// multibyte - whether the rune value might require multiple bytes to represent.
// tail - the remainder of the input string.
// err - error value, if the character could not be unescaped.
// value - the escaped unicode rune at the front of the string.
// multibyte - whether the rune value might require multiple bytes to represent.
// tail - the remainder of the input string.
// err - error value, if the character could not be unescaped.
//
// When multibyte is true the return value may still fit within a single byte,
// but a multibyte conversion is attempted which is more expensive than when the
Expand Down
4 changes: 4 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func TestParse(t *testing.T) {
`"str"`,
&StringNode{Value: "str"},
},
{
"`multi\nline\nstring`",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for raw strings.

&StringNode{Value: "multi\nline\nstring"},
},
{
"3",
&IntegerNode{Value: 3},
Expand Down