Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from puffnfresh/feature/short-byte-literals
Browse files Browse the repository at this point in the history
Add support for literal Byte and Short values.
  • Loading branch information
puffnfresh committed Sep 5, 2014
2 parents 9753f23 + 9d5098f commit 9afc167
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/compiler/scala/tools/nsc/ast/parser/CommonTokens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class CommonTokens {
final val FLOATLIT = 4
final val DOUBLELIT = 5
final val STRINGLIT = 6
final val BYTELIT = 12
final val SHORTLIT = 13

/** keywords */
final val NEW = 20
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ self =>
def isDefIntro = isTemplateIntro || isDclIntro

def isNumericLit: Boolean = in.token match {
case INTLIT | LONGLIT | FLOATLIT | DOUBLELIT => true
case BYTELIT | SHORTLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT => true
case _ => false
}

Expand All @@ -659,7 +659,7 @@ self =>
def isMacro = in.token == IDENTIFIER && in.name == nme.MACROkw

def isLiteralToken(token: Token) = token match {
case CHARLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT |
case BYTELIT | SHORTLIT | CHARLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT |
STRINGLIT | INTERPOLATIONID | SYMBOLLIT | TRUE | FALSE | NULL => true
case _ => false
}
Expand Down Expand Up @@ -1186,6 +1186,8 @@ self =>
case LONGLIT => in.intVal(isNegated)
case FLOATLIT => in.floatVal(isNegated).toFloat
case DOUBLELIT => in.floatVal(isNegated)
case BYTELIT => in.intVal(isNegated).toByte
case SHORTLIT => in.intVal(isNegated).toShort
case STRINGLIT | STRINGPART => in.strVal.intern()
case TRUE => true
case FALSE => false
Expand Down Expand Up @@ -1958,7 +1960,7 @@ self =>
case IDENTIFIER | BACKQUOTED_IDENT | THIS =>
val t = stableId()
in.token match {
case INTLIT | LONGLIT | FLOATLIT | DOUBLELIT =>
case BYTELIT | SHORTLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT =>
t match {
case Ident(nme.MINUS) =>
return literal(isNegated = true, inPattern = true, start = start)
Expand All @@ -1977,7 +1979,7 @@ self =>
case USCORE =>
in.nextToken()
atPos(start, start) { Ident(nme.WILDCARD) }
case CHARLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT |
case BYTELIT | SHORTLIT | CHARLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT |
STRINGLIT | INTERPOLATIONID | SYMBOLLIT | TRUE | FALSE | NULL =>
literal(inPattern = true)
case LPAREN =>
Expand Down
37 changes: 27 additions & 10 deletions src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ trait Scanners extends ScannersCommon {

/** Can token end a statement? */
def inLastOfStat(token: Token) = token match {
case CHARLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT | STRINGLIT | SYMBOLLIT |
case CHARLIT | BYTELIT | SHORTLIT | INTLIT | LONGLIT | FLOATLIT | DOUBLELIT | STRINGLIT | SYMBOLLIT |
IDENTIFIER | BACKQUOTED_IDENT | THIS | NULL | TRUE | FALSE | RETURN | USCORE |
TYPE | XMLSTART | RPAREN | RBRACKET | RBRACE =>
true
Expand Down Expand Up @@ -911,8 +911,12 @@ trait Scanners extends ScannersCommon {
} else {
var value: Long = 0
val divider = if (base == 10) 1 else 2
val limit: Long =
if (token == LONGLIT) Long.MaxValue else Int.MaxValue
val limit: Long = token match {
case LONGLIT => Long.MaxValue
case BYTELIT => Byte.MaxValue
case SHORTLIT => Short.MaxValue
case _ => Int.MaxValue
}
var i = 0
val len = strVal.length
while (i < len) {
Expand Down Expand Up @@ -996,7 +1000,6 @@ trait Scanners extends ScannersCommon {
}
def restOfUncertainToken() = {
def isEfd = ch match { case 'e' | 'E' | 'f' | 'F' | 'd' | 'D' => true ; case _ => false }
def isL = ch match { case 'l' | 'L' => true ; case _ => false }

if (base <= 10 && isEfd)
getFraction()
Expand All @@ -1005,17 +1008,25 @@ trait Scanners extends ScannersCommon {
// as soon as a 0 is read in `case '0'` of method fetchToken.
if (base == 8 && notSingleZero) syntaxError("Non-zero integral values may not have a leading zero.")
setStrVal()
if (isL) {
nextChar()
token = LONGLIT
ch match {
case 'l' | 'L' =>
nextChar()
token = LONGLIT
case 'z' | 'Z' =>
nextChar()
token = BYTELIT
case 's' | 'S' =>
nextChar()
token = SHORTLIT
case _ =>
checkNoLetter()
}
else checkNoLetter()
}
}

if (base > 10 || ch != '.')
if (base > 10 || ch != '.') {
restOfUncertainToken()
else {
} else {
val lookahead = lookaheadReader
val c = lookahead.getc()

Expand Down Expand Up @@ -1095,6 +1106,10 @@ trait Scanners extends ScannersCommon {
"int(" + intVal + ")"
case LONGLIT =>
"long(" + intVal + ")"
case BYTELIT =>
"byte(" + intVal + ")"
case SHORTLIT =>
"short(" + intVal + ")"
case FLOATLIT =>
"float(" + floatVal + ")"
case DOUBLELIT =>
Expand Down Expand Up @@ -1212,6 +1227,8 @@ trait Scanners extends ScannersCommon {
case CHARLIT => "character literal"
case INTLIT => "integer literal"
case LONGLIT => "long literal"
case BYTELIT => "byte literal"
case SHORTLIT => "short literal"
case FLOATLIT => "float literal"
case DOUBLELIT => "double literal"
case STRINGLIT | STRINGPART | INTERPOLATIONID => "string literal"
Expand Down
17 changes: 15 additions & 2 deletions src/compiler/scala/tools/nsc/javac/JavaScanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ trait JavaScanners extends ast.parser.ScannersCommon {
case INTLIT => "integer literal"
case LONGLIT => "long literal"
case STRINGLIT => "string literal"
case BYTELIT => "byte literal"
case SHORTLIT => "short literal"
case EOF => "eof"
case ERROR => "something"
case AMP => "`&'"
Expand Down Expand Up @@ -728,8 +730,13 @@ trait JavaScanners extends ast.parser.ScannersCommon {
} else {
var value: Long = 0
val divider = if (base == 10) 1 else 2
val limit: Long =
if (token == LONGLIT) Long.MaxValue else Int.MaxValue
val limit: Long = token match {
case BYTELIT => Byte.MaxValue
case SHORTLIT => Short.MaxValue
case LONGLIT => Long.MaxValue
case _ => Int.MaxValue
}

var i = 0
val len = name.length
while (i < len) {
Expand Down Expand Up @@ -804,6 +811,12 @@ trait JavaScanners extends ast.parser.ScannersCommon {
if (in.ch == 'l' || in.ch == 'L') {
in.next()
token = LONGLIT
} else if (in.ch == 'z' || in.ch == 'Z') {
in.next()
token = BYTELIT
} else if (in.ch == 's' || in.ch == 'S') {
in.next()
token = SHORTLIT
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/files/run/literals.check
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ test "\0x61\0x62".trim() == "x61\0x62" was successful

test (65 : Byte) == 'A' was successful

test 1z == 1 was successful
test -1z == -1 was successful
test 1Z == 1 was successful
test -1Z == -1 was successful
test 0xffZ == -1 was successful
test 0xFFz == -1 was successful
test 0x0aZ == 0xAz was successful

test 1s == 1 was successful
test -1s == -1 was successful
test 1S == 1 was successful
test -1S == -1 was successful
test 0xffffS == -1 was successful
test 0xFFFFs == -1 was successful
test 0x0aS == 0xAs was successful

test 0X01 == 1 was successful
test 0x01 == 1 was successful
test 0x10 == 16 was successful
Expand Down
22 changes: 22 additions & 0 deletions test/files/run/literals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ object Test {

println

// byte
check_success[Byte]("1z == 1", 1z, 1.toByte)
check_success[Byte]("-1z == -1", -1z, -1.toByte)
check_success[Byte]("1Z == 1", 1Z, 1.toByte)
check_success[Byte]("-1Z == -1", -1Z, -1.toByte)
check_success[Byte]("0xffZ == -1", 0xffZ, -1.toByte)
check_success[Byte]("0xFFz == -1", 0xFFz, -1.toByte)
check_success[Byte]("0x0aZ == 0xAz", 0x0aZ, 0xAz)

println

// short
check_success[Short]("1s == 1", 1s, 1.toShort)
check_success[Short]("-1s == -1", -1s, -1.toShort)
check_success[Short]("1S == 1", 1S, 1.toShort)
check_success[Short]("-1S == -1", -1S, -1.toShort)
check_success[Short]("0xffffS == -1", 0xffffS, -1.toShort)
check_success[Short]("0xFFFFs == -1", 0xFFFFs, -1.toShort)
check_success[Short]("0x0aS == 0xAs", 0x0aS, 0xAs)

println

// int
check_success("0X01 == 1", 0X01, 1)
check_success("0x01 == 1", 0x01, 1)
Expand Down

0 comments on commit 9afc167

Please sign in to comment.