Skip to content
Merged
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
12 changes: 12 additions & 0 deletions codegen/e2e_strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ func TestE2E_StringWithEscapes(t *testing.T) {
t.Errorf("expected %q, got %q", expected, output)
}
}

func TestE2E_StringWithEscapedQuote(t *testing.T) {
// *" inside a string literal should produce a literal double-quote
occam := `SEQ
print.string("He said *"hello*"")
`
output := transpileCompileRun(t, occam)
expected := "He said \"hello\"\n"
if output != expected {
t.Errorf("expected %q, got %q", expected, output)
}
}
14 changes: 13 additions & 1 deletion lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,21 @@ func (l *Lexer) readHexNumber() string {

func (l *Lexer) readString() string {
position := l.position + 1
escaped := false
for {
l.readChar()
if l.ch == '"' || l.ch == 0 {
if l.ch == 0 {
break
}
if escaped {
escaped = false
continue
}
if l.ch == '*' {
escaped = true
continue
}
if l.ch == '"' {
break
}
}
Expand Down
14 changes: 14 additions & 0 deletions lexer/lexer_test2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ func TestStringEscapeSequences(t *testing.T) {
}
}

func TestStringEscapeQuote(t *testing.T) {
// *" inside a string should not terminate the string
input := `"He said *"hello*""` + "\n"
l := New(input)
tok := l.NextToken()
if tok.Type != STRING {
t.Fatalf("expected STRING, got %q", tok.Type)
}
expected := `He said *"hello*"`
if tok.Literal != expected {
t.Fatalf("expected literal %q, got %q", expected, tok.Literal)
}
}

func TestByteLiteralToken(t *testing.T) {
input := "'A'\n"
l := New(input)
Expand Down