Skip to content

Commit 97a5b65

Browse files
authored
Add a Paren case to SynRationalConst (#15885)
* add ranges for lparen, /, rparen to SynRationalConst.Rational * extend SynRationalConst with a new case "Paren" to capture SynRationalConsts wrapped in parens * use rhs2 to help recovery
1 parent 0f30853 commit 97a5b65

10 files changed

+61
-23
lines changed

src/Compiler/Checking/CheckExpressions.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ let rec TcSynRationalConst c =
757757
| SynRationalConst.Integer(value = i) -> intToRational i
758758
| SynRationalConst.Negate(rationalConst = c2) -> NegRational (TcSynRationalConst c2)
759759
| SynRationalConst.Rational(numerator = p; denominator = q) -> DivRational (intToRational p) (intToRational q)
760+
| SynRationalConst.Paren(rationalConst = c) -> TcSynRationalConst c
760761

761762
/// Typecheck constant terms in expressions and patterns
762763
let TcConst (cenv: cenv) (overallTy: TType) m env synConst =

src/Compiler/Service/ServiceInterfaceStubGenerator.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ type InterfaceData =
106106
let rec (|RationalConst|) =
107107
function
108108
| SynRationalConst.Integer (value = i) -> string i
109-
| SynRationalConst.Rational (numerator = numerator; denominator = denominator) -> sprintf "(%i/%i)" numerator denominator
109+
| SynRationalConst.Rational (numerator = numerator; denominator = denominator) -> sprintf "%i/%i" numerator denominator
110110
| SynRationalConst.Negate (rationalConst = (RationalConst s)) -> sprintf "- %s" s
111+
| SynRationalConst.Paren (rationalConst = (RationalConst s)) -> sprintf "(%s)" s
111112

112113
let rec (|TypeIdent|_|) =
113114
function

src/Compiler/SyntaxTree/SyntaxTree.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ type SynRationalConst =
195195

196196
| Integer of value: int32 * range: range
197197

198-
| Rational of numerator: int32 * numeratorRange: range * denominator: int32 * denominatorRange: range * range: range
198+
| Rational of numerator: int32 * numeratorRange: range * divRange: range * denominator: int32 * denominatorRange: range * range: range
199199

200200
| Negate of rationalConst: SynRationalConst * range: range
201201

202+
| Paren of rationalConst: SynRationalConst * range: range
203+
202204
[<RequireQualifiedAccess>]
203205
type SynAccess =
204206
| Public of range: range

src/Compiler/SyntaxTree/SyntaxTree.fsi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,18 @@ type SynRationalConst =
216216

217217
| Integer of value: int32 * range: range
218218

219-
| Rational of numerator: int32 * numeratorRange: range * denominator: int32 * denominatorRange: range * range: range
219+
| Rational of
220+
numerator: int32 *
221+
numeratorRange: range *
222+
divRange: range *
223+
denominator: int32 *
224+
denominatorRange: range *
225+
range: range
220226

221227
| Negate of rationalConst: SynRationalConst * range: range
222228

229+
| Paren of rationalConst: SynRationalConst * range: range
230+
223231
/// Represents an accessibility modifier in F# syntax
224232
[<RequireQualifiedAccess>]
225233
type SynAccess =

src/Compiler/pars.fsy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,13 +3314,13 @@ rationalConstant:
33143314
{ if $2 <> "/" then reportParseErrorAt (rhs parseState 2) (FSComp.SR.parsUnexpectedOperatorForUnitOfMeasure())
33153315
if fst $3 = 0 then reportParseErrorAt (rhs parseState 3) (FSComp.SR.parsIllegalDenominatorForMeasureExponent())
33163316
if (snd $1) || (snd $3) then errorR(Error(FSComp.SR.lexOutsideThirtyTwoBitSigned(), lhs parseState))
3317-
SynRationalConst.Rational(fst $1, rhs parseState 1, fst $3, rhs parseState 3, lhs parseState) }
3317+
SynRationalConst.Rational(fst $1, rhs parseState 1, rhs parseState 2, fst $3, rhs parseState 3, lhs parseState) }
33183318

33193319
| MINUS INT32 INFIX_STAR_DIV_MOD_OP INT32
33203320
{ if $3 <> "/" then reportParseErrorAt (rhs parseState 3) (FSComp.SR.parsUnexpectedOperatorForUnitOfMeasure())
33213321
if fst $4 = 0 then reportParseErrorAt (rhs parseState 4) (FSComp.SR.parsIllegalDenominatorForMeasureExponent())
33223322
if (snd $2) || (snd $4) then errorR(Error(FSComp.SR.lexOutsideThirtyTwoBitSigned(), lhs parseState))
3323-
SynRationalConst.Negate(SynRationalConst.Rational(fst $2, rhs parseState 2, fst $4, rhs parseState 4, lhs parseState), lhs parseState) }
3323+
SynRationalConst.Negate(SynRationalConst.Rational(fst $2, rhs parseState 2, rhs parseState 3, fst $4, rhs parseState 4, lhs parseState), lhs parseState) }
33243324

33253325
| INT32
33263326
{ if snd $1 then errorR(Error(FSComp.SR.lexOutsideThirtyTwoBitSigned(), lhs parseState))
@@ -3335,7 +3335,7 @@ atomicUnsignedRationalConstant:
33353335
SynRationalConst.Integer(fst $1, lhs parseState) }
33363336

33373337
| LPAREN rationalConstant rparen
3338-
{ $2 }
3338+
{ SynRationalConst.Paren($2, rhs2 parseState 1 3) }
33393339

33403340
atomicRationalConstant:
33413341
| atomicUnsignedRationalConstant { $1 }

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.debug.bsl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8238,8 +8238,14 @@ FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Syntax.SynRation
82388238
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Syntax.SynRationalConst rationalConst
82398239
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Text.Range get_range()
82408240
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Text.Range range
8241+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Syntax.SynRationalConst get_rationalConst()
8242+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Syntax.SynRationalConst rationalConst
8243+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Text.Range get_range()
8244+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Text.Range range
82418245
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range denominatorRange
8246+
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range divRange
82428247
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_denominatorRange()
8248+
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_divRange()
82438249
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_numeratorRange()
82448250
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_range()
82458251
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range numeratorRange
@@ -8250,18 +8256,23 @@ FSharp.Compiler.Syntax.SynRationalConst+Rational: Int32 get_numerator()
82508256
FSharp.Compiler.Syntax.SynRationalConst+Rational: Int32 numerator
82518257
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Integer
82528258
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Negate
8259+
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Paren
82538260
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Rational
82548261
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsInteger
82558262
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsNegate
8263+
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsParen
82568264
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsRational
82578265
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsInteger()
82588266
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsNegate()
8267+
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsParen()
82598268
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsRational()
82608269
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewInteger(Int32, FSharp.Compiler.Text.Range)
82618270
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewNegate(FSharp.Compiler.Syntax.SynRationalConst, FSharp.Compiler.Text.Range)
8262-
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewRational(Int32, FSharp.Compiler.Text.Range, Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range)
8271+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewParen(FSharp.Compiler.Syntax.SynRationalConst, FSharp.Compiler.Text.Range)
8272+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewRational(Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range, Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range)
82638273
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Integer
82648274
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Negate
8275+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Paren
82658276
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Rational
82668277
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Tags
82678278
FSharp.Compiler.Syntax.SynRationalConst: Int32 Tag

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.release.bsl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8238,8 +8238,14 @@ FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Syntax.SynRation
82388238
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Syntax.SynRationalConst rationalConst
82398239
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Text.Range get_range()
82408240
FSharp.Compiler.Syntax.SynRationalConst+Negate: FSharp.Compiler.Text.Range range
8241+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Syntax.SynRationalConst get_rationalConst()
8242+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Syntax.SynRationalConst rationalConst
8243+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Text.Range get_range()
8244+
FSharp.Compiler.Syntax.SynRationalConst+Paren: FSharp.Compiler.Text.Range range
82418245
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range denominatorRange
8246+
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range divRange
82428247
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_denominatorRange()
8248+
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_divRange()
82438249
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_numeratorRange()
82448250
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range get_range()
82458251
FSharp.Compiler.Syntax.SynRationalConst+Rational: FSharp.Compiler.Text.Range numeratorRange
@@ -8250,18 +8256,23 @@ FSharp.Compiler.Syntax.SynRationalConst+Rational: Int32 get_numerator()
82508256
FSharp.Compiler.Syntax.SynRationalConst+Rational: Int32 numerator
82518257
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Integer
82528258
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Negate
8259+
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Paren
82538260
FSharp.Compiler.Syntax.SynRationalConst+Tags: Int32 Rational
82548261
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsInteger
82558262
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsNegate
8263+
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsParen
82568264
FSharp.Compiler.Syntax.SynRationalConst: Boolean IsRational
82578265
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsInteger()
82588266
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsNegate()
8267+
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsParen()
82598268
FSharp.Compiler.Syntax.SynRationalConst: Boolean get_IsRational()
82608269
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewInteger(Int32, FSharp.Compiler.Text.Range)
82618270
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewNegate(FSharp.Compiler.Syntax.SynRationalConst, FSharp.Compiler.Text.Range)
8262-
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewRational(Int32, FSharp.Compiler.Text.Range, Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range)
8271+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewParen(FSharp.Compiler.Syntax.SynRationalConst, FSharp.Compiler.Text.Range)
8272+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst NewRational(Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range, Int32, FSharp.Compiler.Text.Range, FSharp.Compiler.Text.Range)
82638273
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Integer
82648274
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Negate
8275+
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Paren
82658276
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Rational
82668277
FSharp.Compiler.Syntax.SynRationalConst: FSharp.Compiler.Syntax.SynRationalConst+Tags
82678278
FSharp.Compiler.Syntax.SynRationalConst: Int32 Tag

tests/service/data/SyntaxTree/Measure/Constant - 07.fs.bsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ ImplFile
1010
Seq
1111
([Power
1212
(Named ([kg], (3,3--3,5)), (3,8--3,9),
13-
Rational
14-
(-12345, (3,21--3,27), 123, (3,28--3,31),
15-
(3,21--3,31)), (3,3--3,32))], (3,3--3,32)),
13+
Paren
14+
(Rational
15+
(-12345, (3,21--3,27), (3,27--3,28), 123,
16+
(3,28--3,31), (3,21--3,31)), (3,20--3,32)),
17+
(3,3--3,32))], (3,3--3,32)),
1618
{ LessRange = (3,2--3,3)
1719
GreaterRange = (3,32--3,33) }), (3,0--3,33)), (3,0--3,33))],
1820
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,

tests/service/data/SyntaxTree/Measure/Constant - 08.fs.bsl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ ImplFile
1111
([Power
1212
(Named ([kg], (3,3--3,5)), (3,5--3,6),
1313
Negate
14-
(Rational
15-
(12345, (3,13--3,18), 123, (3,19--3,22),
16-
(3,13--3,22)), (3,6--3,23)), (3,3--3,23))],
17-
(3,3--3,23)), { LessRange = (3,2--3,3)
18-
GreaterRange = (3,23--3,24) }),
19-
(3,0--3,24)), (3,0--3,24))],
14+
(Paren
15+
(Rational
16+
(12345, (3,13--3,18), (3,18--3,19), 123,
17+
(3,19--3,22), (3,13--3,22)), (3,12--3,23)),
18+
(3,6--3,23)), (3,3--3,23))], (3,3--3,23)),
19+
{ LessRange = (3,2--3,3)
20+
GreaterRange = (3,23--3,24) }), (3,0--3,24)), (3,0--3,24))],
2021
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
2122
(1,0--3,24), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
2223
{ ConditionalDirectives = []

tests/service/data/SyntaxTree/Measure/Constant - 09.fs.bsl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ ImplFile
1111
([Power
1212
(Named ([kg], (3,3--3,5)), (3,5--3,6),
1313
Negate
14-
(Rational
15-
(12345, (3,10--3,15), 123, (3,16--3,19),
16-
(3,10--3,19)), (3,7--3,20)), (3,3--3,20))],
17-
(3,3--3,20)), { LessRange = (3,2--3,3)
18-
GreaterRange = (3,20--3,21) }),
19-
(3,0--3,21)), (3,0--3,21))],
14+
(Paren
15+
(Rational
16+
(12345, (3,10--3,15), (3,15--3,16), 123,
17+
(3,16--3,19), (3,10--3,19)), (3,9--3,20)),
18+
(3,7--3,20)), (3,3--3,20))], (3,3--3,20)),
19+
{ LessRange = (3,2--3,3)
20+
GreaterRange = (3,20--3,21) }), (3,0--3,21)), (3,0--3,21))],
2021
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
2122
(1,0--3,21), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
2223
{ ConditionalDirectives = []

0 commit comments

Comments
 (0)