Skip to content

Commit a6db54f

Browse files
authored
Initial SyntaxTrivia setup (#12631)
* Initial SyntaxTrivia setup, added SynExprTryWithTrivia. * Add trivia for SynExpr.TryFinally. * Add trivia for SynExpr.IfThenElse. * Add trivia for SynExpr.Lambda. * Add trivia for SynMatchClause. * Add BarRange to SynMatchClauseTrivia. * Add trivia for SynEnumCase and SynUnionCase.
1 parent fdcffa4 commit a6db54f

20 files changed

+695
-252
lines changed

src/fsharp/CheckComputationExpressions.fs

Lines changed: 39 additions & 38 deletions
Large diffs are not rendered by default.

src/fsharp/CheckDeclarations.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ module TcRecdUnionAndEnumDeclarations =
469469
| _ ->
470470
seen.Add(f.LogicalName, sf))
471471

472-
let TcUnionCaseDecl cenv env parent thisTy thisTyInst tpenv (SynUnionCase(Attributes synAttrs, id, args, xmldoc, vis, m)) =
472+
let TcUnionCaseDecl cenv env parent thisTy thisTyInst tpenv (SynUnionCase(Attributes synAttrs, id, args, xmldoc, vis, m, _)) =
473473
let attrs = TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs // the attributes of a union case decl get attached to the generated "static factory" method
474474
let vis, _ = ComputeAccessAndCompPath env None m vis None parent
475475
let vis = CombineReprAccess parent vis
@@ -2911,7 +2911,7 @@ let CheckForDuplicateModule env nm m =
29112911
/// Check 'exception' declarations in implementations and signatures
29122912
module TcExceptionDeclarations =
29132913

2914-
let TcExnDefnCore_Phase1A cenv env parent (SynExceptionDefnRepr(Attributes synAttrs, SynUnionCase(_, id, _, _, _, _), _, doc, vis, m)) =
2914+
let TcExnDefnCore_Phase1A cenv env parent (SynExceptionDefnRepr(Attributes synAttrs, SynUnionCase(ident=id), _, doc, vis, m)) =
29152915
let attrs = TcAttributes cenv env AttributeTargets.ExnDecl synAttrs
29162916
if not (String.isLeadingIdentifierCharacterUpperCase id.idText) then errorR(NotUpperCaseConstructor m)
29172917
let vis, cpath = ComputeAccessAndCompPath env None m vis None parent
@@ -2922,7 +2922,7 @@ module TcExceptionDeclarations =
29222922
let doc = doc.ToXmlDoc(true, Some [])
29232923
Construct.NewExn cpath id vis repr attrs doc
29242924

2925-
let TcExnDefnCore_Phase1G_EstablishRepresentation (cenv: cenv) (env: TcEnv) parent (exnc: Entity) (SynExceptionDefnRepr(_, SynUnionCase(_, _, args, _, _, _), reprIdOpt, _, _, m)) =
2925+
let TcExnDefnCore_Phase1G_EstablishRepresentation (cenv: cenv) (env: TcEnv) parent (exnc: Entity) (SynExceptionDefnRepr(_, SynUnionCase(caseType=args), reprIdOpt, _, _, m)) =
29262926
let g = cenv.g
29272927
let args = match args with SynUnionCaseKind.Fields args -> args | _ -> error(Error(FSComp.SR.tcExplicitTypeSpecificationCannotBeUsedForExceptionConstructors(), m))
29282928
let ad = env.AccessRights
@@ -3111,7 +3111,7 @@ module EstablishTypeDefinitionCores =
31113111
| SynTypeDefnSimpleRepr.None _ -> ()
31123112
| SynTypeDefnSimpleRepr.Union (_, unionCases, _) ->
31133113

3114-
for SynUnionCase (_, _, args, _, _, m) in unionCases do
3114+
for SynUnionCase (caseType=args; range=m) in unionCases do
31153115
match args with
31163116
| SynUnionCaseKind.Fields flds ->
31173117
for SynField(_, _, _, ty, _, _, _, m) in flds do
@@ -5225,7 +5225,7 @@ and TcSignatureElementsMutRec cenv parent typeNames m mutRecNSInfo envInitial (d
52255225
decls, (openOk, moduleAbbrevOk)
52265226

52275227
| SynModuleSigDecl.Exception (exnSig=SynExceptionSig(exnRepr=exnRepr; withKeyword=withKeyword; members=members)) ->
5228-
let ( SynExceptionDefnRepr(synAttrs, SynUnionCase(_, id, _args, _, _, _), _, doc, vis, m)) = exnRepr
5228+
let ( SynExceptionDefnRepr(synAttrs, SynUnionCase(ident=id), _, doc, vis, m)) = exnRepr
52295229
let compInfo = SynComponentInfo(synAttrs, None, [], [id], doc, false, vis, id.idRange)
52305230
let decls = [ MutRecShape.Tycon(SynTypeDefnSig.SynTypeDefnSig(compInfo, None, SynTypeDefnSigRepr.Exception exnRepr, withKeyword, members, m)) ]
52315231
decls, (false, false)
@@ -5562,7 +5562,7 @@ and TcModuleOrNamespaceElementsMutRec (cenv: cenv) parent typeNames m envInitial
55625562
decls, (openOk, moduleAbbrevOk, attrs)
55635563

55645564
| SynModuleDecl.Exception (SynExceptionDefn(repr, _, members, _), _m) ->
5565-
let (SynExceptionDefnRepr(synAttrs, SynUnionCase(_, id, _args, _, _, _), _repr, doc, vis, m)) = repr
5565+
let (SynExceptionDefnRepr(synAttrs, SynUnionCase(ident=id), _repr, doc, vis, m)) = repr
55665566
let compInfo = SynComponentInfo(synAttrs, None, [], [id], doc, false, vis, id.idRange)
55675567
let decls = [ MutRecShape.Tycon(SynTypeDefn(compInfo, None, SynTypeDefnRepr.Exception repr, None, members, None, m)) ]
55685568
decls, (false, false, attrs)

src/fsharp/CheckExpressions.fs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ module EventDeclarationNormalization =
25592559

25602560
match rhsExpr with
25612561
// Detect 'fun () -> e' which results from the compilation of a property getter
2562-
| SynExpr.Lambda (_, _, SynSimplePats.SimplePats([], _), _, trueRhsExpr, _, m) ->
2562+
| SynExpr.Lambda (args=SynSimplePats.SimplePats([], _); body=trueRhsExpr; range=m) ->
25632563
let rhsExpr = mkSynApp1 (SynExpr.DotGet (SynExpr.Paren (trueRhsExpr, range0, None, m), range0, LongIdentWithDots([ident(target, m)], []), m)) (SynExpr.Ident (ident(argName, m))) m
25642564

25652565
// reconstitute rhsExpr
@@ -5766,10 +5766,10 @@ and TcExprUndelayed cenv (overallTy: OverallTy) env tpenv (synExpr: SynExpr) =
57665766
| SynExpr.LetOrUse _ ->
57675767
TcLinearExprs (TcExprThatCanBeCtorBody cenv) cenv env overallTy tpenv false synExpr (fun x -> x)
57685768

5769-
| SynExpr.TryWith (_mTry, synBodyExpr, _mTryToWith, _mWith, synWithClauses, mWithToLast, mTryToLast, spTry, spWith) ->
5770-
TcExprTryWith cenv overallTy env tpenv (synBodyExpr, _mTryToWith, synWithClauses, mWithToLast, mTryToLast, spTry, spWith)
5769+
| SynExpr.TryWith (synBodyExpr, synWithClauses, mTryToLast, spTry, spWith, trivia) ->
5770+
TcExprTryWith cenv overallTy env tpenv (synBodyExpr, synWithClauses, trivia.WithToEndRange, mTryToLast, spTry, spWith)
57715771

5772-
| SynExpr.TryFinally (synBodyExpr, synFinallyExpr, mTryToLast, spTry, spFinally) ->
5772+
| SynExpr.TryFinally (synBodyExpr, synFinallyExpr, mTryToLast, spTry, spFinally, _trivia) ->
57735773
TcExprTryFinally cenv overallTy env tpenv (synBodyExpr, synFinallyExpr, mTryToLast, spTry, spFinally)
57745774

57755775
| SynExpr.JoinIn (e1, mInToken, e2, mAll) ->
@@ -6036,14 +6036,14 @@ and TcExprIntegerForLoop cenv overallTy env tpenv (spBind, id, start, dir, finis
60366036
let bodyExpr, tpenv = TcStmt cenv envinner tpenv body
60376037
mkFastForLoop cenv.g (spBind, m, idv, startExpr, dir, finishExpr, bodyExpr), tpenv
60386038

6039-
and TcExprTryWith cenv overallTy env tpenv (synBodyExpr, _mTryToWith, synWithClauses, mWithToLast, mTryToLast, spTry, spWith) =
6039+
and TcExprTryWith cenv overallTy env tpenv (synBodyExpr, synWithClauses, mWithToLast, mTryToLast, spTry, spWith) =
60406040
let bodyExpr, tpenv = TcExpr cenv overallTy env tpenv synBodyExpr
60416041
// Compile the pattern twice, once as a List.filter with all succeeding targets returning "1", and once as a proper catch block.
60426042
let filterClauses =
60436043
synWithClauses |> List.map (fun clause ->
6044-
let (SynMatchClause(pat, optWhenExpr, arrow, _, m, _)) = clause
6044+
let (SynMatchClause(pat, optWhenExpr, _, m, _, trivia)) = clause
60456045
let oneExpr = SynExpr.Const (SynConst.Int32 1, m)
6046-
SynMatchClause(pat, optWhenExpr, arrow, oneExpr, m, DebugPointAtTarget.No))
6046+
SynMatchClause(pat, optWhenExpr, oneExpr, m, DebugPointAtTarget.No, trivia))
60476047
let checkedFilterClauses, tpenv = TcMatchClauses cenv cenv.g.exn_ty (MustEqual cenv.g.int_ty) env tpenv filterClauses
60486048
let checkedHandlerClauses, tpenv = TcMatchClauses cenv cenv.g.exn_ty overallTy env tpenv synWithClauses
60496049
let v1, filterExpr = CompilePatternForMatchClauses cenv env mWithToLast mWithToLast true FailFilter None cenv.g.exn_ty cenv.g.int_ty checkedFilterClauses
@@ -6215,7 +6215,7 @@ and RewriteRangeExpr expr =
62156215
/// Check lambdas as a group, to catch duplicate names in patterns
62166216
and TcIteratedLambdas cenv isFirst (env: TcEnv) overallTy takenNames tpenv e =
62176217
match e with
6218-
| SynExpr.Lambda (isMember, isSubsequent, spats, _, bodyExpr, _, m) when isMember || isFirst || isSubsequent ->
6218+
| SynExpr.Lambda (isMember, isSubsequent, spats, bodyExpr, _, m, _) when isMember || isFirst || isSubsequent ->
62196219
let domainTy, resultTy = UnifyFunctionType None cenv env.DisplayEnv m overallTy.Commit
62206220
let vs, (tpenv, names, takenNames) = TcSimplePats cenv isMember CheckCxs domainTy env (tpenv, Map.empty, takenNames) spats
62216221
let envinner, _, vspecMap = MakeAndPublishSimpleValsForMergedScope cenv env m names
@@ -8484,7 +8484,7 @@ and TcImplicitOpItemThen cenv overallTy env id sln tpenv mItem delayed =
84848484
| SynExpr.ArrayOrList (_, synExprs, _) -> synExprs |> List.forall isSimpleArgument
84858485
| SynExpr.Record (copyInfo=copyOpt; recordFields=fields) -> copyOpt |> Option.forall (fst >> isSimpleArgument) && fields |> List.forall ((fun (SynExprRecordField(expr=e)) -> e) >> Option.forall isSimpleArgument)
84868486
| SynExpr.App (_, _, synExpr, synExpr2, _) -> isSimpleArgument synExpr && isSimpleArgument synExpr2
8487-
| SynExpr.IfThenElse (_, _, synExpr, _, synExpr2, _, synExprOpt, _, _, _, _) -> isSimpleArgument synExpr && isSimpleArgument synExpr2 && Option.forall isSimpleArgument synExprOpt
8487+
| SynExpr.IfThenElse (ifExpr=synExpr; thenExpr=synExpr2; elseExpr=synExprOpt) -> isSimpleArgument synExpr && isSimpleArgument synExpr2 && Option.forall isSimpleArgument synExprOpt
84888488
| SynExpr.DotIndexedGet (synExpr, _, _, _) -> isSimpleArgument synExpr
84898489
| SynExpr.ObjExpr _
84908490
| SynExpr.AnonRecd _
@@ -9711,7 +9711,7 @@ and TcLinearExprs bodyChecker cenv env overallTy tpenv isCompExpr expr cont =
97119711
TcLinearExprs bodyChecker cenv envinner overallTy tpenv isCompExpr body (fun (x, tpenv) ->
97129712
cont (fst (mkf (x, overallTy.Commit)), tpenv))
97139713

9714-
| SynExpr.IfThenElse (_, _, synBoolExpr, _, synThenExpr, _, synElseExprOpt, spIfToThen, isRecovery, mIfToThen, m) when not isCompExpr ->
9714+
| SynExpr.IfThenElse (synBoolExpr, synThenExpr, synElseExprOpt, spIfToThen, isRecovery, m, trivia) when not isCompExpr ->
97159715
let boolExpr, tpenv = TcExprThatCantBeCtorBody cenv (MustEqual cenv.g.bool_ty) env tpenv synBoolExpr
97169716
let thenExpr, tpenv =
97179717
let env =
@@ -9729,7 +9729,7 @@ and TcLinearExprs bodyChecker cenv env overallTy tpenv isCompExpr expr cont =
97299729

97309730
match synElseExprOpt with
97319731
| None ->
9732-
let elseExpr = mkUnit cenv.g mIfToThen
9732+
let elseExpr = mkUnit cenv.g trivia.IfToThenRange
97339733
let spElse = DebugPointAtTarget.No // the fake 'unit' value gets exactly the same range as spIfToThen
97349734
let overallExpr = primMkCond spIfToThen DebugPointAtTarget.Yes spElse m overallTy.Commit boolExpr thenExpr elseExpr
97359735
cont (overallExpr, tpenv)
@@ -9769,7 +9769,7 @@ and TcMatchClauses cenv inputTy (resultTy: OverallTy) env tpenv clauses =
97699769
List.mapFold (fun clause -> TcMatchClause cenv inputTy resultTy env (isFirst()) clause) tpenv clauses
97709770

97719771
and TcMatchClause cenv inputTy (resultTy: OverallTy) env isFirst tpenv synMatchClause =
9772-
let (SynMatchClause(pat, optWhenExpr, _, e, patm, spTgt)) = synMatchClause
9772+
let (SynMatchClause(pat, optWhenExpr, e, patm, spTgt, _)) = synMatchClause
97739773
let pat', optWhenExprR, vspecs, envinner, tpenv = TcMatchPattern cenv inputTy env tpenv (pat, optWhenExpr)
97749774
let resultEnv = if isFirst then envinner else { envinner with eContextInfo = ContextInfo.FollowingPatternMatchClause e.Range }
97759775
let e', tpenv = TcExprThatCanBeCtorBody cenv resultTy resultEnv tpenv e

src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@
447447
<Compile Include="..\XmlDoc.fs">
448448
<Link>ParserAndUntypedAST\XmlDoc.fs</Link>
449449
</Compile>
450+
<Compile Include="..\SyntaxTrivia.fsi">
451+
<Link>ParserAndUntypedAST\SyntaxTrivia.fsi</Link>
452+
</Compile>
453+
<Compile Include="..\SyntaxTrivia.fs">
454+
<Link>ParserAndUntypedAST\SyntaxTrivia.fs</Link>
455+
</Compile>
450456
<Compile Include="..\SyntaxTree.fsi">
451457
<Link>ParserAndUntypedAST\SyntaxTree.fsi</Link>
452458
</Compile>

src/fsharp/SyntaxTree.fs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open FSharp.Compiler.Syntax
88
open FSharp.Compiler.Text
99
open FSharp.Compiler.Text.Range
1010
open FSharp.Compiler.Xml
11+
open FSharp.Compiler.SyntaxTrivia
1112

1213
[<Struct; NoEquality; NoComparison; DebuggerDisplay("{idText}")>]
1314
type Ident (text: string, range: range) =
@@ -563,10 +564,10 @@ type SynExpr =
563564
fromMethod: bool *
564565
inLambdaSeq: bool *
565566
args: SynSimplePats *
566-
arrow: range option *
567567
body: SynExpr *
568568
parsedData: (SynPat list * SynExpr) option *
569-
range: range
569+
range: range *
570+
trivia: SynExprLambdaTrivia
570571

571572
| MatchLambda of
572573
isExnMatch: bool *
@@ -615,22 +616,20 @@ type SynExpr =
615616
range: range
616617

617618
| TryWith of
618-
tryKeywordRange: range *
619619
tryExpr: SynExpr *
620-
tryRange: range *
621-
withKeywordRange: range *
622620
withCases: SynMatchClause list *
623-
withRange: range *
624621
range: range *
625622
tryDebugPoint: DebugPointAtTry *
626-
withDebugPoint: DebugPointAtWith
623+
withDebugPoint: DebugPointAtWith *
624+
trivia: SynExprTryWithTrivia
627625

628626
| TryFinally of
629627
tryExpr: SynExpr *
630628
finallyExpr: SynExpr *
631629
range: range *
632630
tryDebugPoint: DebugPointAtTry *
633-
finallyDebugPoint: DebugPointAtFinally
631+
finallyDebugPoint: DebugPointAtFinally *
632+
trivia: SynExprTryFinallyTrivia
634633

635634
| Lazy of
636635
expr: SynExpr *
@@ -644,17 +643,13 @@ type SynExpr =
644643
range: range
645644

646645
| IfThenElse of
647-
ifKeyword: range *
648-
isElif: bool *
649646
ifExpr: SynExpr *
650-
thenKeyword: range *
651647
thenExpr: SynExpr *
652-
elseKeyword: range option *
653648
elseExpr: SynExpr option *
654649
spIfToThen: DebugPointAtBinding *
655650
isFromErrorRecovery: bool *
656-
ifToThenRange: range *
657-
range: range
651+
range: range *
652+
trivia: SynExprIfThenElseTrivia
658653

659654
| Ident of
660655
ident: Ident
@@ -1187,14 +1182,14 @@ type SynMatchClause =
11871182
| SynMatchClause of
11881183
pat: SynPat *
11891184
whenExpr: SynExpr option *
1190-
arrow: range option *
11911185
resultExpr: SynExpr *
11921186
range: range *
1193-
debugPoint: DebugPointAtTarget
1187+
debugPoint: DebugPointAtTarget *
1188+
trivia: SynMatchClauseTrivia
11941189

11951190
member this.RangeOfGuardAndRhs =
11961191
match this with
1197-
| SynMatchClause(_, eo, _, e, _, _) ->
1192+
| SynMatchClause(whenExpr=eo; resultExpr=e) ->
11981193
match eo with
11991194
| None -> e.Range
12001195
| Some x -> unionRanges e.Range x.Range
@@ -1403,11 +1398,11 @@ type SynEnumCase =
14031398
| SynEnumCase of
14041399
attributes: SynAttributes *
14051400
ident: Ident *
1406-
equalsRange: range *
14071401
value: SynConst *
14081402
valueRange: range *
14091403
xmlDoc: PreXmlDoc *
1410-
range: range
1404+
range: range *
1405+
trivia: SynEnumCaseTrivia
14111406

14121407
member this.Range =
14131408
match this with
@@ -1422,7 +1417,8 @@ type SynUnionCase =
14221417
caseType: SynUnionCaseKind *
14231418
xmlDoc: PreXmlDoc *
14241419
accessibility: SynAccess option *
1425-
range: range
1420+
range: range *
1421+
trivia: SynUnionCaseTrivia
14261422

14271423
member this.Range =
14281424
match this with

src/fsharp/SyntaxTree.fsi

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace rec FSharp.Compiler.Syntax
55
open FSharp.Compiler.Syntax
66
open FSharp.Compiler.Text
77
open FSharp.Compiler.Xml
8+
open FSharp.Compiler.SyntaxTrivia
89

910
/// Represents an identifier in F# code
1011
[<Struct; NoEquality; NoComparison>]
@@ -693,10 +694,10 @@ type SynExpr =
693694
fromMethod: bool *
694695
inLambdaSeq: bool *
695696
args: SynSimplePats *
696-
arrow: range option *
697697
body: SynExpr *
698698
parsedData: (SynPat list * SynExpr) option *
699-
range: range
699+
range: range *
700+
trivia: SynExprLambdaTrivia
700701

701702
/// F# syntax: function pat1 -> expr | ... | patN -> exprN
702703
| MatchLambda of
@@ -760,23 +761,21 @@ type SynExpr =
760761

761762
/// F# syntax: try expr with pat -> expr
762763
| TryWith of
763-
tryKeywordRange: range *
764764
tryExpr: SynExpr *
765-
tryRange: range *
766-
withKeywordRange: range *
767765
withCases: SynMatchClause list *
768-
withRange: range *
769766
range: range *
770767
tryDebugPoint: DebugPointAtTry *
771-
withDebugPoint: DebugPointAtWith
768+
withDebugPoint: DebugPointAtWith *
769+
trivia: SynExprTryWithTrivia
772770

773771
/// F# syntax: try expr finally expr
774772
| TryFinally of
775773
tryExpr: SynExpr *
776774
finallyExpr: SynExpr *
777775
range: range *
778776
tryDebugPoint: DebugPointAtTry *
779-
finallyDebugPoint: DebugPointAtFinally
777+
finallyDebugPoint: DebugPointAtFinally *
778+
trivia: SynExprTryFinallyTrivia
780779

781780
/// F# syntax: lazy expr
782781
| Lazy of
@@ -796,17 +795,13 @@ type SynExpr =
796795
/// F# syntax: if expr then expr
797796
/// F# syntax: if expr then expr else expr
798797
| IfThenElse of
799-
ifKeyword: range *
800-
isElif: bool *
801798
ifExpr: SynExpr *
802-
thenKeyword: range *
803799
thenExpr: SynExpr *
804-
elseKeyword: range option *
805800
elseExpr: SynExpr option *
806801
spIfToThen: DebugPointAtBinding *
807802
isFromErrorRecovery: bool *
808-
ifToThenRange: range *
809-
range: range
803+
range: range *
804+
trivia: SynExprIfThenElseTrivia
810805

811806
/// F# syntax: ident
812807
/// Optimized representation for SynExpr.LongIdent (false, [id], id.idRange)
@@ -1321,10 +1316,10 @@ type SynMatchClause =
13211316
| SynMatchClause of
13221317
pat: SynPat *
13231318
whenExpr: SynExpr option *
1324-
arrow: range option *
13251319
resultExpr: SynExpr *
13261320
range: range *
1327-
debugPoint: DebugPointAtTarget
1321+
debugPoint: DebugPointAtTarget *
1322+
trivia: SynMatchClauseTrivia
13281323

13291324
/// Gets the syntax range of part of this construct
13301325
member RangeOfGuardAndRhs: range
@@ -1567,11 +1562,11 @@ type SynEnumCase =
15671562
| SynEnumCase of
15681563
attributes: SynAttributes *
15691564
ident: Ident *
1570-
equalsRange: range *
15711565
value: SynConst *
15721566
valueRange: range *
15731567
xmlDoc: PreXmlDoc *
1574-
range: range
1568+
range: range *
1569+
trivia: SynEnumCaseTrivia
15751570

15761571
/// Gets the syntax range of this construct
15771572
member Range: range
@@ -1586,7 +1581,8 @@ type SynUnionCase =
15861581
caseType: SynUnionCaseKind *
15871582
xmlDoc: PreXmlDoc *
15881583
accessibility: SynAccess option *
1589-
range: range
1584+
range: range *
1585+
trivia: SynUnionCaseTrivia
15901586

15911587
/// Gets the syntax range of this construct
15921588
member Range: range

0 commit comments

Comments
 (0)