Skip to content

Commit

Permalink
Char escape mild refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jul 6, 2021
1 parent 2457b67 commit 468dd35
Showing 1 changed file with 39 additions and 42 deletions.
81 changes: 39 additions & 42 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Expand Up @@ -1017,9 +1017,8 @@ trait Scanners extends ScannersCommon {
next.name = newTermName(cbuf.toString)
cbuf.clear()
val idx = next.name.start - kwOffset
if (idx >= 0 && idx < kwArray.length) {
if (idx >= 0 && idx < kwArray.length)
next.token = kwArray(idx)
}
}
nextRawChar()
if (ch == '$' || ch == '"') {
Expand Down Expand Up @@ -1052,13 +1051,11 @@ trait Scanners extends ScannersCommon {
}
} else {
val isUnclosedLiteral = (ch == SU || (!multiLine && (ch == CR || ch == LF)))
if (isUnclosedLiteral) {
if (isUnclosedLiteral)
if (multiLine)
incompleteInputError("unclosed multi-line string literal")
else {
else
unclosedStringLit(seenEscapedQuote)
}
}
else {
putChar(ch)
nextRawChar()
Expand Down Expand Up @@ -1104,44 +1101,25 @@ trait Scanners extends ScannersCommon {
nextChar()
}

private def charEscape(): Unit =
if ('0' <= ch && ch <= '7') {
val start = charOffset - 2
val leadch: Char = ch
var oct: Int = digit2int(ch, 8)
nextChar()
if ('0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
if (leadch <= '3' && '0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
}
}
val alt = if (oct == LF) "\\n" else "\\u%04x" format oct
syntaxError(start, s"octal escape literals are unsupported: use $alt instead")
putChar(oct.toChar)
} else {
if (ch == 'u') {
if (getUEscape()) nextChar()
}
else {
ch match {
case 'b' => putChar('\b')
case 't' => putChar('\t')
case 'n' => putChar('\n')
case 'f' => putChar('\f')
case 'r' => putChar('\r')
case '\"' => putChar('\"')
case '\'' => putChar('\'')
case '\\' => putChar('\\')
case _ => invalidEscape()
}
nextChar()
}
private def charEscape(): Unit = {
var bump = true
ch match {
case 'b' => putChar('\b')
case 't' => putChar('\t')
case 'n' => putChar('\n')
case 'f' => putChar('\f')
case 'r' => putChar('\r')
case '\"' => putChar('\"')
case '\'' => putChar('\'')
case '\\' => putChar('\\')
case 'u' => bump = uEscape()
case x if '0' <= x && x <= '7' => bump = octalEscape()
case _ => invalidEscape()
}
if (bump) nextChar()
}

private def getUEscape(): Boolean = {
private def uEscape(): Boolean = {
while (ch == 'u') nextChar()
var codepoint = 0
var digitsRead = 0
Expand All @@ -1163,6 +1141,25 @@ trait Scanners extends ScannersCommon {
true
}

private def octalEscape(): Boolean = {
val start = charOffset - 2
val leadch: Char = ch
var oct: Int = digit2int(ch, 8)
nextChar()
if ('0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
if (leadch <= '3' && '0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
}
}
val alt = if (oct == LF) "\\n" else f"\\u$oct%04x"
syntaxError(start, s"octal escape literals are unsupported: use $alt instead")
putChar(oct.toChar)
false
}

protected def invalidEscape(): Unit = {
syntaxError(charOffset - 1, "invalid escape character")
putChar(ch)
Expand Down

0 comments on commit 468dd35

Please sign in to comment.