Skip to content

Commit 7670781

Browse files
auduchinokT-Gro
andauthored
Parser: recover on missing expressions after rArrow (#15045)
Co-authored-by: Tomas Grosup <tomasgrosup@microsoft.com>
1 parent 2542b08 commit 7670781

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+574
-51
lines changed

src/Compiler/pars.fsy

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,10 +3440,13 @@ declExprBlock:
34403440
/* for these it only inserts a trailing ORIGHT_BLOCK_END */
34413441
typedSequentialExprBlockR:
34423442
| typedSequentialExpr ORIGHT_BLOCK_END
3443-
{ $1 }
3443+
{ fun _ -> $1 }
34443444

34453445
| typedSequentialExpr
3446-
{ $1 }
3446+
{ fun _ -> $1 }
3447+
3448+
| recover
3449+
{ fun mStart -> arbExpr ("typedSequentialExprBlockR", mStart) }
34473450

34483451
typedSequentialExpr:
34493452
| sequentialExpr COLON typeWithTypeConstraints
@@ -3573,36 +3576,49 @@ declExpr:
35733576
{ if not $3 then reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnexpectedEndOfFileMatch())
35743577
// Produce approximate expression during error recovery
35753578
exprFromParseError $2 }
3576-
3577-
| TRY typedSequentialExprBlockR withClauses %prec expr_try
3578-
{ let mTry = (rhs parseState 1)
3579+
3580+
| TRY typedSequentialExprBlockR withClauses %prec expr_try
3581+
{ let mTry = rhs parseState 1
3582+
let expr = $2 mTry
35793583
let spTry = DebugPointAtTry.Yes mTry
3580-
let mWith, (clauses, mLast) = $3
3584+
let mWith, (clauses, mLast) = $3
35813585
let spWith = DebugPointAtWith.Yes mWith
3582-
let mTryToWith = unionRanges mTry mWith
3583-
let mWithToLast = unionRanges mWith mLast
3586+
let mTryToWith = unionRanges mTry mWith
3587+
let mWithToLast = unionRanges mWith mLast
35843588
let mTryToLast = unionRanges mTry mLast
3585-
let trivia: SynExprTryWithTrivia =
3589+
let trivia: SynExprTryWithTrivia =
35863590
{ TryKeyword = mTry
35873591
TryToWithRange = mTryToWith
35883592
WithKeyword = mWith
35893593
WithToEndRange = mWithToLast }
3590-
SynExpr.TryWith ($2, clauses, mTryToLast, spTry, spWith, trivia) }
3591-
3592-
| TRY typedSequentialExprBlockR recover %prec expr_try
3593-
{ // Produce approximate expression during error recovery
3594-
// Include any expressions to make sure they gets type checked in case that generates useful results for intellisense
3595-
if not $3 then reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnexpectedEndOfFileTry())
3596-
exprFromParseError $2 }
3594+
SynExpr.TryWith (expr, clauses, mTryToLast, spTry, spWith, trivia) }
35973595

3598-
| TRY typedSequentialExprBlockR FINALLY typedSequentialExprBlock %prec expr_try
3599-
{ let mTry = rhs parseState 1
3596+
| TRY typedSequentialExprBlockR recover %prec expr_try
3597+
{ let mTry = rhs parseState 1
3598+
let spTry = DebugPointAtTry.Yes mTry
3599+
if not $3 then reportParseErrorAt mTry (FSComp.SR.parsUnexpectedEndOfFileTry ())
3600+
let expr = $2 mTry
3601+
let mExpr = expr.Range
3602+
let mEnd = mExpr.EndRange
3603+
let spWith = DebugPointAtWith.Yes mEnd
3604+
let mWhole = unionRanges mTry mEnd
3605+
let trivia: SynExprTryWithTrivia =
3606+
{ TryKeyword = mTry
3607+
TryToWithRange = mWhole
3608+
WithKeyword = mEnd
3609+
WithToEndRange = mWhole }
3610+
SynExpr.TryWith (expr, [], mWhole, spTry, spWith, trivia) }
3611+
3612+
| TRY typedSequentialExprBlockR FINALLY typedSequentialExprBlock %prec expr_try
3613+
{ let mTry = rhs parseState 1
3614+
let tryExpr = $2 mTry
36003615
let spTry = DebugPointAtTry.Yes mTry
36013616
let mFinally = rhs parseState 3
3617+
let finallyExpr = $4
36023618
let spFinally = DebugPointAtFinally.Yes mFinally
3603-
let mTryToLast = unionRanges mTry $4.Range
3619+
let mTryToLast = unionRanges mTry finallyExpr.Range
36043620
let trivia = { TryKeyword = mTry; FinallyKeyword = mFinally }
3605-
SynExpr.TryFinally ($2, $4, mTryToLast, spTry, spFinally, trivia) }
3621+
SynExpr.TryFinally (tryExpr, finallyExpr, mTryToLast, spTry, spFinally, trivia) }
36063622

36073623
| IF declExpr ifExprCases %prec expr_if
36083624
{ let mIf = rhs parseState 1
@@ -3856,9 +3872,11 @@ declExpr:
38563872
| FIXED declExpr
38573873
{ SynExpr.Fixed ($2, (unionRanges (rhs parseState 1) $2.Range)) }
38583874

3859-
| RARROW typedSequentialExprBlockR
3860-
{ errorR(Error(FSComp.SR.parsArrowUseIsLimited(), lhs parseState))
3861-
SynExpr.YieldOrReturn ((true, true), $2, (unionRanges (rhs parseState 1) $2.Range)) }
3875+
| RARROW typedSequentialExprBlockR
3876+
{ errorR(Error(FSComp.SR.parsArrowUseIsLimited(), lhs parseState))
3877+
let mArrow = rhs parseState 1
3878+
let expr = $2 mArrow
3879+
SynExpr.YieldOrReturn ((true, true), expr, (unionRanges mArrow expr.Range)) }
38623880

38633881
| declExpr COLON_QMARK typ
38643882
{ SynExpr.TypeTest ($1, $3, unionRanges $1.Range $3.Range) }
@@ -4156,11 +4174,12 @@ patternGuard:
41564174
| /* EMPTY */
41574175
{ None }
41584176

4159-
patternResult:
4160-
| RARROW typedSequentialExprBlockR
4161-
{ let mArrow = rhs parseState 1
4162-
mArrow, $2 }
4163-
4177+
patternResult:
4178+
| RARROW typedSequentialExprBlockR
4179+
{ let mArrow = rhs parseState 1
4180+
let expr = $2 mArrow
4181+
mArrow, expr }
4182+
41644183
ifExprCases:
41654184
| ifExprThen ifExprElifs
41664185
{ let exprThen, mThen = $1
@@ -4670,8 +4689,10 @@ computationExpr:
46704689
{ $1.Range, (fun mLhs -> SynExpr.ComputationExpr (false, $1, mLhs)) }
46714690

46724691
arrowThenExprR:
4673-
| RARROW typedSequentialExprBlockR
4674-
{ SynExpr.YieldOrReturn ((true, false), $2, unionRanges (rhs parseState 1) $2.Range) }
4692+
| RARROW typedSequentialExprBlockR
4693+
{ let mArrow = rhs parseState 1
4694+
let expr = $2 mArrow
4695+
SynExpr.YieldOrReturn ((true, false), expr, unionRanges mArrow expr.Range) }
46754696

46764697
forLoopBinder:
46774698
| parenPattern IN declExpr
@@ -5012,15 +5033,17 @@ anonLambdaExpr:
50125033
mkSynFunMatchLambdas parseState.SynArgNameGenerator false mAll $2 mArrow (arbExpr("anonLambdaExpr1", (rhs parseState 4))) }
50135034

50145035
| OFUN atomicPatterns RARROW typedSequentialExprBlockR OEND
5015-
{ let mAll = unionRanges (rhs parseState 1) $4.Range
5016-
let mArrow = Some (rhs parseState 3)
5017-
mkSynFunMatchLambdas parseState.SynArgNameGenerator false mAll $2 mArrow $4 }
5036+
{ let mArrow = rhs parseState 3
5037+
let expr = $4 mArrow
5038+
let mAll = unionRanges (rhs parseState 1) expr.Range
5039+
mkSynFunMatchLambdas parseState.SynArgNameGenerator false mAll $2 (Some mArrow) expr }
50185040

50195041
| OFUN atomicPatterns RARROW typedSequentialExprBlockR recover
5020-
{ if not $5 then reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnexpectedEndOfFileFunBody());
5021-
let mAll = unionRanges (rhs parseState 1) $4.Range
5022-
let mArrow = Some (rhs parseState 3)
5023-
exprFromParseError (mkSynFunMatchLambdas parseState.SynArgNameGenerator false mAll $2 mArrow $4) }
5042+
{ if not $5 then reportParseErrorAt (rhs parseState 1) (FSComp.SR.parsUnexpectedEndOfFileFunBody ())
5043+
let mArrow = rhs parseState 3
5044+
let expr = $4 mArrow
5045+
let mAll = unionRanges (rhs parseState 1) expr.Range
5046+
exprFromParseError (mkSynFunMatchLambdas parseState.SynArgNameGenerator false mAll $2 (Some mArrow) expr) }
50245047

50255048
| OFUN atomicPatterns RARROW ORIGHT_BLOCK_END OEND
50265049
{ let mLambda = rhs2 parseState 1 3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Module
2+
3+
(fun _ -> )
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
ImplFile
2+
(ParsedImplFileInput
3+
("/root/Expression/Lambda - Missing expr 01.fs", false,
4+
QualifiedNameOfFile Module, [], [],
5+
[SynModuleOrNamespace
6+
([Module], false, NamedModule,
7+
[Expr
8+
(Paren
9+
(Lambda
10+
(false, false,
11+
SimplePats
12+
([Id (_arg1, None, true, false, false, (3,5--3,6))],
13+
(3,5--3,6)),
14+
ArbitraryAfterError ("anonLambdaExpr2", (3,9--3,9)),
15+
Some
16+
([Wild (3,5--3,6)],
17+
ArbitraryAfterError ("anonLambdaExpr2", (3,9--3,9))),
18+
(3,1--3,9), { ArrowRange = Some (3,7--3,9) }), (3,0--3,1),
19+
Some (3,10--3,11), (3,0--3,11)), (3,0--3,11))],
20+
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
21+
(1,0--3,11), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
22+
{ ConditionalDirectives = []
23+
CodeComments = [] }, set []))
24+
25+
(3,1)-(3,9) parse error Missing function body
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Module
2+
3+
fun _ ->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ImplFile
2+
(ParsedImplFileInput
3+
("/root/Expression/Lambda - Missing expr 02.fs", false,
4+
QualifiedNameOfFile Module, [], [],
5+
[SynModuleOrNamespace
6+
([Module], false, NamedModule,
7+
[Expr
8+
(Lambda
9+
(false, false,
10+
SimplePats
11+
([Id (_arg1, None, true, false, false, (3,4--3,5))],
12+
(3,4--3,5)),
13+
ArbitraryAfterError ("anonLambdaExpr2", (3,8--3,8)),
14+
Some
15+
([Wild (3,4--3,5)],
16+
ArbitraryAfterError ("anonLambdaExpr2", (3,8--3,8))),
17+
(3,0--3,8), { ArrowRange = Some (3,6--3,8) }), (3,0--3,8))],
18+
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
19+
(1,0--3,8), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
20+
{ ConditionalDirectives = []
21+
CodeComments = [] }, set []))
22+
23+
(4,0)-(4,0) parse warning Possible incorrect indentation: this token is offside of context started at position (1:1). Try indenting this token further or using standard formatting conventions.
24+
(3,0)-(3,8) parse error Missing function body
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Module
2+
3+
(fun _ -> ())
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
ImplFile
2+
(ParsedImplFileInput
3+
("/root/Expression/Lambda 01.fs", false, QualifiedNameOfFile Module, [], [],
4+
[SynModuleOrNamespace
5+
([Module], false, NamedModule,
6+
[Expr
7+
(Paren
8+
(Lambda
9+
(false, false,
10+
SimplePats
11+
([Id (_arg1, None, true, false, false, (3,5--3,6))],
12+
(3,5--3,6)), Const (Unit, (3,10--3,12)),
13+
Some ([Wild (3,5--3,6)], Const (Unit, (3,10--3,12))),
14+
(3,1--3,12), { ArrowRange = Some (3,7--3,9) }), (3,0--3,1),
15+
Some (3,12--3,13), (3,0--3,13)), (3,0--3,13))],
16+
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
17+
(1,0--3,13), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
18+
{ ConditionalDirectives = []
19+
CodeComments = [] }, set []))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Module
2+
3+
fun _ -> ()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
ImplFile
2+
(ParsedImplFileInput
3+
("/root/Expression/Lambda 02.fs", false, QualifiedNameOfFile Module, [], [],
4+
[SynModuleOrNamespace
5+
([Module], false, NamedModule,
6+
[Expr
7+
(Lambda
8+
(false, false,
9+
SimplePats
10+
([Id (_arg1, None, true, false, false, (3,4--3,5))],
11+
(3,4--3,5)), Const (Unit, (3,9--3,11)),
12+
Some ([Wild (3,4--3,5)], Const (Unit, (3,9--3,11))),
13+
(3,0--3,11), { ArrowRange = Some (3,6--3,8) }), (3,0--3,11))],
14+
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None,
15+
(1,0--3,11), { LeadingKeyword = Module (1,0--1,6) })], (true, true),
16+
{ ConditionalDirectives = []
17+
CodeComments = [] }, set []))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Module
2+
3+
[ for x in [] -> () ]

0 commit comments

Comments
 (0)