Skip to content
Open
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
29 changes: 16 additions & 13 deletions effekt/shared/src/main/scala/effekt/Lexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum LexerError {
case MultipleCodePointsInChar
case InvalidIntegerFormat
case InvalidDoubleFormat
case InvalidByteFormat
case InvalidHexLiteralFormat
case UnterminatedInterpolation(depth: Int)

def message: String = this match {
Expand All @@ -36,7 +36,7 @@ enum LexerError {
case MultipleCodePointsInChar => "Character literal consists of multiple code points"
case InvalidIntegerFormat => "Invalid integer format, not a 64bit integer literal"
case InvalidDoubleFormat => "Invalid float format, not a double literal"
case InvalidByteFormat => "Invalid byte format, byte has to be exactly two hex digits"
case InvalidHexLiteralFormat => "Invalid hex literal format, has to be exactly two or eight hex digits for byte or int respectively"
case UnterminatedInterpolation(depth) =>
s"Unterminated string interpolation ($depth unclosed splices)"
}
Expand Down Expand Up @@ -402,7 +402,7 @@ class Lexer(source: Source) extends Iterator[Token] {
case (c, _) if c.isWhitespace => advanceSpaces()

// Numbers
case ('0', 'x') if isHexDigit(peekAhead(2)) => advance2With(byte())
case ('0', 'x') => advance2With(hexliteral())
case (c, _) if c.isDigit => number()

// Identifiers and keywords
Expand Down Expand Up @@ -554,25 +554,28 @@ class Lexer(source: Source) extends Iterator[Token] {
case None => TokenKind.Error(LexerError.InvalidIntegerFormat)
}

private def byte(): TokenKind =
private def hexliteral(): TokenKind =
// Consume hex digits
advanceWhile { (curr, _) => isHexDigit(curr) }

// Get the hex string
val hexString = getCurrentSlice(skipAfterStart = 2)

if hexString.length < 2 then
return TokenKind.Error(LexerError.InvalidByteFormat)

if hexString.length > 2 then
return TokenKind.Error(LexerError.InvalidByteFormat)
if hexString.length != 2 && hexString.length != 8 then
return TokenKind.Error(LexerError.InvalidHexLiteralFormat)

try {
val byte = java.lang.Integer.parseInt(hexString, 16)
assert(byte >= 0 && byte <= 255)
TokenKind.Byt(UByte.unsafeFromInt(byte))
if hexString.length == 2 then {
val byte = java.lang.Integer.parseInt(hexString, 16)
assert(byte >= 0 && byte <= 255)
TokenKind.Byt(UByte.unsafeFromInt(byte))
} else {
val long = java.lang.Long.parseLong(hexString, 16)
assert(long >= Long.MinValue && long <= Long.MaxValue)
TokenKind.Integer(long)
}
} catch {
case e: NumberFormatException => TokenKind.Error(LexerError.InvalidByteFormat)
case e: NumberFormatException => TokenKind.Error(LexerError.InvalidHexLiteralFormat)
}

private def identifier(): TokenKind =
Expand Down
3 changes: 3 additions & 0 deletions examples/neg/hexliteral_wrong_ndigits.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[error] examples/neg/hexliteral_wrong_ndigits.effekt:1:9: Invalid hex literal format, has to be exactly two or eight hex digits for byte or int respectively
val a = 0x000
^^^^^
1 change: 1 addition & 0 deletions examples/neg/hexliteral_wrong_ndigits.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val a = 0x000
4 changes: 4 additions & 0 deletions examples/pos/hexliteral.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0
4294967295
12345678
255
6 changes: 6 additions & 0 deletions examples/pos/hexliteral.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main() = {
println(0x00000000)
println(0xffffffff)
println(0x00bc614e)
println(0x000000ff)
}
Loading