Skip to content

Commit

Permalink
Finished config for real numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 5, 2023
1 parent f1deeda commit 42da6a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class ErrorConfig {
def labelRealNumberEnd: Option[String] = None

def labelRealDot = None
def labelRealExponent = None
def labelRealExponentEnd = None

private [token] def labelDecimal(bits: Int, signed: Boolean): Option[String] = {
if (signed) labelIntegerSignedDecimal(bits) else labelIntegerUnsignedDecimal(bits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import parsley.internal.deepembedding.singletons
private [token] final class SignedReal(desc: NumericDesc, unsigned: Real, err: ErrorConfig) extends Real(err) {
private val sign = new Parsley(new singletons.Sign[DoubleType.resultType](DoubleType, desc.positiveSign))

override lazy val _decimal: Parsley[BigDecimal] = attempt(sign <*> unsigned._decimal)
override lazy val _hexadecimal: Parsley[BigDecimal] = attempt(sign <*> unsigned._hexadecimal)
override lazy val _octal: Parsley[BigDecimal] = attempt(sign <*> unsigned._octal)
override lazy val _binary: Parsley[BigDecimal] = attempt(sign <*> unsigned._binary)
override lazy val _number: Parsley[BigDecimal] = attempt(sign <*> unsigned._number)
override lazy val _decimal: Parsley[BigDecimal] = attempt(sign <*> ErrorConfig.label(err.labelRealDecimalEnd)(unsigned._decimal))
override lazy val _hexadecimal: Parsley[BigDecimal] = attempt(sign <*> ErrorConfig.label(err.labelRealHexadecimalEnd)(unsigned._hexadecimal))
override lazy val _octal: Parsley[BigDecimal] = attempt(sign <*> ErrorConfig.label(err.labelRealOctalEnd)(unsigned._octal))
override lazy val _binary: Parsley[BigDecimal] = attempt(sign <*> ErrorConfig.label(err.labelRealBinaryEnd)(unsigned._binary))
override lazy val _number: Parsley[BigDecimal] = attempt(sign <*> ErrorConfig.label(err.labelRealNumberEnd)(unsigned._number))

override def decimal: Parsley[BigDecimal] = ErrorConfig.label(err.labelRealDecimal)(_decimal)
override def hexadecimal: Parsley[BigDecimal] = ErrorConfig.label(err.labelRealHexadecimal)(_hexadecimal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ private [token] final class UnsignedReal(desc: NumericDesc, natural: UnsignedInt
case true => err.explainRealNoDoubleDroppedZero
}
val expDesc = desc.exponentDescForRadix(radix)
val endLabel = radix match {
case 10 => err.labelRealDecimalEnd
case 16 => err.labelRealHexadecimalEnd
case 8 => err.labelRealOctalEnd
case 2 => err.labelRealBinaryEnd
}
val whole = radix match {
case 10 => generic.plainDecimal(desc, None)
case 16 => generic.plainHexadecimal(desc, None)
case 8 => generic.plainOctal(desc, None)
case 2 => generic.plainBinary(desc, None)
case 10 => generic.plainDecimal(desc, endLabel)
case 16 => generic.plainHexadecimal(desc, endLabel)
case 8 => generic.plainOctal(desc, endLabel)
case 2 => generic.plainBinary(desc, endLabel)
}
val f = (d: Char, x: BigDecimal) => x/radix + d.asDigit
def broken(c: Char) = lift2(f, digit, (optional(c) *> digit).foldRight[BigDecimal](0)(f))
val fractional = amend {
'.' *> {
ErrorConfig.label(err.labelRealDot.orElse(endLabel))('.') *> {
desc.literalBreakChar match {
case BreakCharDesc.NoBreakChar if desc.trailingDotAllowed =>
if (!leadingDotAllowed) entrench(digit.foldRight[BigDecimal](0)(f))
Expand All @@ -91,11 +97,11 @@ private [token] final class UnsignedReal(desc: NumericDesc, natural: UnsignedInt
case ExponentDesc.Supported(compulsory, exp, base, sign) =>
val expErr = new ErrorConfig {
override def pleaseDontValidateConfig: Boolean = true
override def labelIntegerSignedDecimal(bits: Int): Option[String] = None
override def labelIntegerBinaryEnd: Option[String] = None
override def labelIntegerSignedDecimal(bits: Int): Option[String] = err.labelRealExponentEnd.orElse(endLabel)
override def labelIntegerDecimalEnd: Option[String] = err.labelRealExponentEnd.orElse(endLabel)
}
val integer = new SignedInteger(desc.copy(positiveSign = sign), natural, expErr)
val exponent = oneOf(exp) *> integer.decimal32
val exponent = ErrorConfig.label(err.labelRealExponent.orElse(endLabel))(oneOf(exp)) *> integer.decimal32
if (compulsory) (exponent, exponent, base)
else (exponent, exponent <|> pure(0), base)
// this can't fail for non-required, it has to be the identity exponent
Expand Down

0 comments on commit 42da6a4

Please sign in to comment.