Skip to content
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

Implement string block literals. #11975

Closed
wants to merge 3 commits into from
Closed
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
53 changes: 53 additions & 0 deletions compiler/lexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,59 @@ proc getString(L: var Lexer, tok: var Token, mode: StringMode) =
else:
tok.literal.add(L.buf[pos])
inc(pos)
elif L.buf[pos] == ':' and L.buf[pos + 1] in {CR, LF}:
# string block literal
L.bufpos = pos + 1
tok.tokType = if mode == normal: tkStrLit
else: tkRStrLit
let indent = L.currLineIndent + 2
while true:
# skip indent and/or empty lines without moving lexer
var needIndent = indent
var emptyLines = -1 # account for previous LF still in buffer
var pos = L.bufpos
while needIndent > 0:
var c = L.buf[pos]
if c in {' ', CR, LF}:
if c == ' ':
dec(needIndent)
if c == LF:
inc(emptyLines)
needIndent = indent
inc(pos)
else:
# end of block found -> cancel lookahead
pos = L.bufpos
if c != ')': emptyLines = 0
break
if emptyLines > 0:
add(tok.literal, '\n'.repeat(emptyLines))
# fast-forward lexer to current position
while L.bufpos < pos:
if L.buf[L.bufpos] in {CR, LF}:
L.bufpos = handleCRLF(L, L.bufpos)
else:
inc(L.bufpos)
# EXIT if end of block was reached
if needIndent > 0: break
# parse a line of string, break before EOL
while true:
var c = L.buf[L.bufpos]
if c in {CR, LF, nimlexbase.EndOfFile}:
add(tok.literal, "\n")
break
if (c == '\\') and mode == normal:
if L.buf[L.bufpos + 1] in {CR, LF, nimlexbase.EndOfFile}:
inc(L.bufpos)
break
else:
getEscapedChar(L, tok)
else:
add(tok.literal, c)
inc(L.bufpos)
if tok.literal == "":
lexMessage(L, errGenerated, "string block literal indented by two spaces expected")
tokenEndIgnore(tok, L.bufpos)
else:
# ordinary string literal
if mode != normal: tok.tokType = tkRStrLit
Expand Down