Skip to content

Commit

Permalink
Fix minor issues mentioned in the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
chengluyu committed Jan 1, 2024
1 parent c07ac62 commit 3f1b9a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
8 changes: 4 additions & 4 deletions shared/src/main/scala/mlscript/NewLexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class NewLexer(origin: Origin, raise: Diagnostic => Unit, dbg: Bool) {
else (cur.reverseIterator.mkString, i)

final def num(i: Int): (Lit, Int) = {
def test(i: Int, p: Char => Bool): Bool = i < length && p(bytes(i))
def zero: IntLit = IntLit(BigInt(0))
/** Take a sequence of digits interleaved with underscores. */
def takeDigits(i: Int, pred: Char => Bool): (Opt[Str], Int) = {
Expand Down Expand Up @@ -101,7 +102,7 @@ class NewLexer(origin: Origin, raise: Diagnostic => Unit, dbg: Bool) {
def isDecimalStart(ch: Char) = ch === '.' || ch === 'e' || ch === 'E'
/** Take a fraction part with an optional exponent part. Call at periods. */
def decimal(i: Int, integral: Str): (DecLit, Int) = {
val (fraction, j) = if (i < length && bytes(i) === '.') {
val (fraction, j) = if (test(i, _ === '.')) {
takeDigits(i + 1, isDigit) match {
case (N, j) =>
raise(ErrorReport(msg"Expect at least one digit after the decimal point" -> S(loc(i + 1, i + 2)) :: Nil,
Expand All @@ -110,8 +111,8 @@ class NewLexer(origin: Origin, raise: Diagnostic => Unit, dbg: Bool) {
case (S(digits), j) => ("." + digits, j)
}
} else ("", i)
val (exponent, k) = if (j < length && (bytes(j) === 'e' || bytes(j) === 'E')) {
val (sign, k) = if (j + 1 < length && (bytes(j + 1) === '+' || bytes(j + 1) === '-')) {
val (exponent, k) = if (test(j, ch => ch === 'e' || ch === 'E')) {
val (sign, k) = if (test(j + 1, ch => ch === '+' || ch === '-')) {
(bytes(j + 1), j + 2)
} else {
('+', j + 1)
Expand All @@ -138,7 +139,6 @@ class NewLexer(origin: Origin, raise: Diagnostic => Unit, dbg: Bool) {
case _ => integer(i, 10, "decimal", isDigit)
}
case '0' => (zero, i + 1)
// case '.' => decimal(i, "0")
case _ => takeDigits(i, isDigit) match {
case (N, j) =>
raise(ErrorReport(msg"Expect a numeric literal" -> S(loc(i, i + 1)) :: Nil,
Expand Down
13 changes: 11 additions & 2 deletions shared/src/test/diff/nu/DecLit.mls
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,22 @@
//│ res
//│ = 4378

:pe
5.
//│ ╔══[LEXICAL ERROR] Expect at least one digit after the decimal point
//│ ║ l.121: 5.
//│ ╙── ^
//│ 5
//│ res
//│ = 5

:pe
789.E
//│ ╔══[LEXICAL ERROR] Expect at least one digit after the decimal point
//│ ║ l.121: 789.E
//│ ║ l.130: 789.E
//│ ╙── ^
//│ ╔══[LEXICAL ERROR] Expect at least one digit after the exponent sign
//│ ║ l.121: 789.E
//│ ║ l.130: 789.E
//│ ╙── ^
//│ 789
//│ res
Expand Down
15 changes: 15 additions & 0 deletions shared/src/test/diff/nu/IntLit.mls
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@
//│ = 2
//│ res
//│ = 51966

// Extra leading zeros are discarded.
0123
023456
00
0
//│ 0
//│ res
//│ = 123
//│ res
//│ = 23456
//│ res
//│ = 0
//│ res
//│ = 0

0 comments on commit 3f1b9a5

Please sign in to comment.