|
| 1 | +" |
| 2 | +An ExtendedNumberParser is extending Squeak number syntax with these rules |
| 3 | +
|
| 4 | +- allow partial specification of integer and fraction parts: |
| 5 | +1.e2 .1e3 are both 100.0 |
| 6 | +- allow plus sign before number and in exponent |
| 7 | +
|
| 8 | +
|
| 9 | +" |
| 10 | +Class { |
| 11 | + #name : #ExtendedNumberParser, |
| 12 | + #superclass : #NumberParser, |
| 13 | + #category : 'ExtendedNumberParser' |
| 14 | +} |
| 15 | + |
| 16 | +{ #category : #accessing } |
| 17 | +ExtendedNumberParser >> allowPlusSign [ |
| 18 | + ^true |
| 19 | +] |
| 20 | + |
| 21 | +{ #category : #accessing } |
| 22 | +ExtendedNumberParser >> exponentLetters [ |
| 23 | + "Allow uppercase exponent letter." |
| 24 | + |
| 25 | + ^'edqEDQ' |
| 26 | +] |
| 27 | + |
| 28 | +{ #category : #'parsing-public' } |
| 29 | +ExtendedNumberParser >> nextFraction [ |
| 30 | + | numerator denominator numberOfTrailingZeroInIntegerPart | |
| 31 | + base := 10. |
| 32 | + neg := self peekSignIsMinus. |
| 33 | + (integerPart := self nextUnsignedIntegerOrNilBase: base) |
| 34 | + ifNil: [numberOfTrailingZeroInIntegerPart := 0] |
| 35 | + ifNotNil: [ |
| 36 | + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. |
| 37 | + (sourceStream peekFor: $r) |
| 38 | + ifTrue: ["<base>r<integer>" |
| 39 | + (base := integerPart) < 2 |
| 40 | + ifTrue: [ |
| 41 | + sourceStream skip: -1. |
| 42 | + ^ self expected: 'an integer greater than 1 as valid radix']. |
| 43 | + self peekSignIsMinus |
| 44 | + ifTrue: [neg := neg not]. |
| 45 | + integerPart := self nextUnsignedIntegerBase: base. |
| 46 | + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero]]. |
| 47 | + (sourceStream peekFor: $.) |
| 48 | + ifTrue: |
| 49 | + [^self readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart]. |
| 50 | + integerPart |
| 51 | + ifNil: |
| 52 | + ["No integerPart, raise an error" |
| 53 | + ^ self expected: 'a digit']. |
| 54 | + numerator := neg |
| 55 | + ifTrue: [integerPart negated] |
| 56 | + ifFalse: [integerPart]. |
| 57 | + self readExponent ifTrue: [numerator := numerator * (base raisedToInteger: exponent)]. |
| 58 | + (sourceStream peekFor: $/) ifFalse: [^numerator]. |
| 59 | + base := 10. |
| 60 | + (denominator := self nextUnsignedIntegerOrNilBase: base) |
| 61 | + ifNil: |
| 62 | + [sourceStream skip: -1. "Not a valid denominator, ungobble / and return numerator" |
| 63 | + ^numerator]. |
| 64 | + (sourceStream peekFor: $r) |
| 65 | + ifTrue: ["<base>r<integer>" |
| 66 | + (base := denominator) < 2 |
| 67 | + ifTrue: [ |
| 68 | + sourceStream skip: -1. |
| 69 | + ^ self expected: 'an integer greater than 1 as valid radix']. |
| 70 | + denominator := self nextUnsignedIntegerBase: base]. |
| 71 | + self readExponent ifTrue: [denominator := denominator * (base raisedToInteger: exponent)]. |
| 72 | + ^numerator / denominator |
| 73 | +] |
| 74 | + |
| 75 | +{ #category : #'parsing-public' } |
| 76 | +ExtendedNumberParser >> nextNumber [ |
| 77 | + "main method for reading a number. |
| 78 | + This one can read Float Integer and ScaledDecimal" |
| 79 | + |
| 80 | + | numberOfTrailingZeroInIntegerPart | |
| 81 | + base := 10. |
| 82 | + neg := self peekSignIsMinus. |
| 83 | + integerPart := self nextUnsignedIntegerOrNilBase: base. |
| 84 | + integerPart ifNil: [(sourceStream peekFor: $.) |
| 85 | + ifTrue: [ |
| 86 | + "Try .1 syntax" |
| 87 | + ^self readNumberWithoutIntegerPart] |
| 88 | + ifFalse: [ |
| 89 | + "This is not a regular number beginning with a digit |
| 90 | + It is time to check for exceptional condition NaN and Infinity" |
| 91 | + ^self readNamedFloatOrFail]]. |
| 92 | + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero. |
| 93 | + (sourceStream peekFor: $r) |
| 94 | + ifTrue: ["<base>r<integer>" |
| 95 | + | oldNeg pos | |
| 96 | + pos := sourceStream position. |
| 97 | + (base := integerPart) < 2 |
| 98 | + ifTrue: ["A radix currently need to be greater than 1, ungobble the r and return the integer part" |
| 99 | + sourceStream skip: -1. |
| 100 | + ^neg |
| 101 | + ifTrue: [base negated] |
| 102 | + ifFalse: [base]]. |
| 103 | + oldNeg := neg. |
| 104 | + self peekSignIsMinus ifTrue: [neg := neg not]. |
| 105 | + integerPart := self nextUnsignedIntegerOrNilBase: base. |
| 106 | + integerPart ifNil: [ |
| 107 | + (sourceStream peekFor: $.) ifTrue: [self readNumberWithoutIntegerPartOrNil ifNotNil: [:aNumber | ^aNumber]]. |
| 108 | + sourceStream position: pos. |
| 109 | + ^oldNeg |
| 110 | + ifTrue: [base negated] |
| 111 | + ifFalse: [base]]. |
| 112 | + numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero]. |
| 113 | + ^ (sourceStream peekFor: $.) |
| 114 | + ifTrue: [self readNumberWithFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart] |
| 115 | + ifFalse: [self makeIntegerOrScaledInteger] |
| 116 | +] |
| 117 | + |
| 118 | +{ #category : #'parsing-private' } |
| 119 | +ExtendedNumberParser >> readFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart [ |
| 120 | + "at this stage, sign integerPart and a fraction point have been read. |
| 121 | + try and form a number with a fractionPart" |
| 122 | + |
| 123 | + | numberOfNonZeroFractionDigits numberOfTrailingZeroInFractionPart mantissa value | |
| 124 | + fractionPart := self nextUnsignedIntegerOrNilBase: base. |
| 125 | + fractionPart |
| 126 | + ifNil: [ |
| 127 | + "No fractionPart found, but can be an extended 1.e2 syntax" |
| 128 | + integerPart ifNil: ["No integerPart, nor fractionPart found, ungobble the fraction point and raise an error" |
| 129 | + sourceStream skip: -1. |
| 130 | + ^self expected: 'a digit']. |
| 131 | + fractionPart := 0. |
| 132 | + numberOfNonZeroFractionDigits := 0. |
| 133 | + numberOfTrailingZeroInFractionPart := 0] |
| 134 | + ifNotNil: [. |
| 135 | + numberOfNonZeroFractionDigits := lastNonZero. |
| 136 | + numberOfTrailingZeroInFractionPart := nDigits - lastNonZero]. |
| 137 | + self readExponent. |
| 138 | + integerPart ifNil: [integerPart := 0]. |
| 139 | + |
| 140 | + fractionPart isZero |
| 141 | + ifTrue: [mantissa := integerPart |
| 142 | + // (base raisedToInteger: numberOfTrailingZeroInIntegerPart). |
| 143 | + exponent := exponent + numberOfTrailingZeroInIntegerPart] |
| 144 | + ifFalse: [mantissa := integerPart |
| 145 | + * (base raisedToInteger: numberOfNonZeroFractionDigits) + (fractionPart // (base raisedToInteger: numberOfTrailingZeroInFractionPart)). |
| 146 | + exponent := exponent - numberOfNonZeroFractionDigits]. |
| 147 | + |
| 148 | + value := exponent positive |
| 149 | + ifTrue: [mantissa * (base raisedToInteger: exponent)] |
| 150 | + ifFalse: [mantissa / (base raisedToInteger: exponent negated)]. |
| 151 | + ^ neg |
| 152 | + ifTrue: [value negated] |
| 153 | + ifFalse: [value] |
| 154 | +] |
| 155 | + |
| 156 | +{ #category : #'parsing-private' } |
| 157 | +ExtendedNumberParser >> readNumberWithFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart [ |
| 158 | + "at this stage, sign integerPart and a decimal point have been read. |
| 159 | + try and form a number with a fractionPart" |
| 160 | + |
| 161 | + | numberOfNonZeroFractionDigits numberOfTrailingZeroInFractionPart mantissa value | |
| 162 | + fractionPart := self nextUnsignedIntegerOrNilBase: base. |
| 163 | + fractionPart |
| 164 | + ifNil: [ |
| 165 | + "No fractionPart found, but can be a 1.e2 syntax" |
| 166 | + fractionPart := 0. |
| 167 | + numberOfNonZeroFractionDigits := 0. |
| 168 | + numberOfTrailingZeroInFractionPart := 0] |
| 169 | + ifNotNil: [. |
| 170 | + numberOfNonZeroFractionDigits := lastNonZero. |
| 171 | + numberOfTrailingZeroInFractionPart := nDigits - lastNonZero]. |
| 172 | + self readExponent |
| 173 | + ifFalse: [self readScale |
| 174 | + ifTrue: [^self makeScaledDecimalWithNumberOfNonZeroFractionDigits: numberOfNonZeroFractionDigits |
| 175 | + andNumberOfTrailingZeroInFractionPart: numberOfTrailingZeroInFractionPart]]. |
| 176 | + |
| 177 | + fractionPart isZero |
| 178 | + ifTrue: [mantissa := integerPart |
| 179 | + // (base raisedToInteger: numberOfTrailingZeroInIntegerPart). |
| 180 | + exponent := exponent + numberOfTrailingZeroInIntegerPart] |
| 181 | + ifFalse: [mantissa := integerPart |
| 182 | + * (base raisedToInteger: numberOfNonZeroFractionDigits) + (fractionPart // (base raisedToInteger: numberOfTrailingZeroInFractionPart)). |
| 183 | + exponent := exponent - numberOfNonZeroFractionDigits]. |
| 184 | + |
| 185 | + value := self makeFloatFromMantissa: mantissa exponent: exponent base: base. |
| 186 | + ^ neg |
| 187 | + ifTrue: [value isZero |
| 188 | + ifTrue: [Float negativeZero] |
| 189 | + ifFalse: [value negated]] |
| 190 | + ifFalse: [value] |
| 191 | +] |
| 192 | + |
| 193 | +{ #category : #'parsing-private' } |
| 194 | +ExtendedNumberParser >> readNumberWithoutIntegerPart [ |
| 195 | + "at this stage, sign followed by a decimal point have been read, but no intergerPart |
| 196 | + try and form a number with a fractionPart" |
| 197 | + |
| 198 | + ^self readNumberWithoutIntegerPartOrNil ifNil: [ |
| 199 | + "No integer part, no fractionPart, this does not look like a number..." |
| 200 | + ^self expected: 'a digit between 0 and 9']. |
| 201 | +] |
| 202 | + |
| 203 | +{ #category : #'parsing-private' } |
| 204 | +ExtendedNumberParser >> readNumberWithoutIntegerPartOrNil [ |
| 205 | + "at this stage, sign followed by a decimal point have been read, but no intergerPart |
| 206 | + try and form a number with a fractionPart" |
| 207 | + |
| 208 | + | numberOfNonZeroFractionDigits numberOfTrailingZeroInFractionPart mantissa value | |
| 209 | + integerPart := 0. |
| 210 | + fractionPart := self nextUnsignedIntegerOrNilBase: base. |
| 211 | + fractionPart ifNil: [ |
| 212 | + "No integer part, no fractionPart, this does not look like a number..." |
| 213 | + ^nil]. |
| 214 | + numberOfNonZeroFractionDigits := lastNonZero. |
| 215 | + numberOfTrailingZeroInFractionPart := nDigits - lastNonZero. |
| 216 | + self readExponent |
| 217 | + ifFalse: [self readScale |
| 218 | + ifTrue: [^self makeScaledDecimalWithNumberOfNonZeroFractionDigits: numberOfNonZeroFractionDigits |
| 219 | + andNumberOfTrailingZeroInFractionPart: numberOfTrailingZeroInFractionPart]]. |
| 220 | + |
| 221 | + fractionPart isZero |
| 222 | + ifTrue: [mantissa := 0] |
| 223 | + ifFalse: [mantissa := (fractionPart // (base raisedToInteger: numberOfTrailingZeroInFractionPart)). |
| 224 | + exponent := exponent - numberOfNonZeroFractionDigits]. |
| 225 | + |
| 226 | + value := self makeFloatFromMantissa: mantissa exponent: exponent base: base. |
| 227 | + ^ neg |
| 228 | + ifTrue: [value isZero |
| 229 | + ifTrue: [Float negativeZero] |
| 230 | + ifFalse: [value negated]] |
| 231 | + ifFalse: [value] |
| 232 | +] |
0 commit comments