Skip to content

Commit

Permalink
Add trivia and service test
Browse files Browse the repository at this point in the history
  • Loading branch information
tboby committed Sep 22, 2022
1 parent 6dbd749 commit ad0743b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Compiler/Checking/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5467,7 +5467,7 @@ and TcExprUndelayed (cenv: cenv) (overallTy: OverallTy) env tpenv (synExpr: SynE
TcNonControlFlowExpr env <| fun env ->
CallExprHasTypeSink cenv.tcSink (m, env.NameEnv, overallTy.Commit, env.AccessRights)
TcConstExpr cenv overallTy env m tpenv synConst
| SynExpr.DotLambda (synExpr, m) ->
| SynExpr.DotLambda (synExpr, m, _) ->
let unaryArg = mkSynId m (cenv.synArgNameGenerator.New())
let svar = mkSynCompGenSimplePatVar unaryArg
let pushedExpr = pushUnaryArg synExpr unaryArg
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/SyntaxTree/SyntaxTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ type SynExpr =

| DotGet of expr: SynExpr * rangeOfDot: range * longDotId: SynLongIdent * range: range

| DotLambda of expr: SynExpr * range: range
| DotLambda of expr: SynExpr * range: range * trivia: SynExprDotLambdaTrivia

| DotSet of targetExpr: SynExpr * longDotId: SynLongIdent * rhsExpr: SynExpr * range: range

Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/SyntaxTree/SyntaxTree.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ type SynExpr =
| DotGet of expr: SynExpr * rangeOfDot: range * longDotId: SynLongIdent * range: range

/// F# syntax: _.ident.ident
| DotLambda of expr: SynExpr * range: range
| DotLambda of expr: SynExpr * range: range * trivia: SynExprDotLambdaTrivia

/// F# syntax: expr.ident...ident <- expr
| DotSet of targetExpr: SynExpr * longDotId: SynLongIdent * rhsExpr: SynExpr * range: range
Expand Down
7 changes: 7 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ type SynExprLambdaTrivia =

static member Zero: SynExprLambdaTrivia = { ArrowRange = None }

[<NoEquality; NoComparison>]
type SynExprDotLambdaTrivia =
{
UnderscoreRange : range
DotRange : range
}

[<NoEquality; NoComparison>]
type SynExprLetOrUseTrivia = { InKeyword: range option }

Expand Down
8 changes: 8 additions & 0 deletions src/Compiler/SyntaxTree/SyntaxTrivia.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ type SynExprLambdaTrivia =

static member Zero: SynExprLambdaTrivia

/// Represents additional information for SynExpr.DotLambda
[<NoEquality; NoComparison>]
type SynExprDotLambdaTrivia =
{
UnderscoreRange : range
DotRange : range
}

/// Represents additional information for SynExpr.LetOrUse
[<NoEquality; NoComparison>]
type SynExprLetOrUseTrivia =
Expand Down
8 changes: 5 additions & 3 deletions src/Compiler/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -4307,9 +4307,11 @@ argExpr:

atomicExpr:
| UNDERSCORE DOT atomicExpr %prec dot_lambda
{ let arg1 = lhs parseState
let arg2, hpa = $3
SynExpr.DotLambda(arg2, arg2.Range), false }
{ let arg1 = rhs parseState 1
let arg2 = rhs parseState 2
let arg3, hpa = $3
let trivia: SynExprDotLambdaTrivia = { UnderscoreRange = arg1; DotRange = arg2 }
SynExpr.DotLambda(arg3, unionRanges arg1 arg3.Range, trivia), false }

| atomicExpr HIGH_PRECEDENCE_BRACK_APP atomicExpr
{ let arg1, _ = $1
Expand Down
35 changes: 26 additions & 9 deletions tests/service/SyntaxTreeTests/ExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open NUnit.Framework

open Xunit
// [<Fact>]
// let ``Thomas`` () =
// let ast = """_.ToString.ToString "b" """ |> getParseResults
// let ast = """_.x""" |> getParseResults
// let ast = """_.x()""" |> getParseResults
// Assert.Fail (ast.ToString())

[<Test>]
let ``SynExpr.Do contains the range of the do keyword`` () =
let ast = """let a =
Expand Down Expand Up @@ -502,3 +493,29 @@ type CFoo() =
assertRange (7,4) (7, 67) m
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"

[<Test>]
let ``SynExpr.DotLambda has correct ranges and trivia`` () =
let ast =
getParseResults """
let e1 : obj [] -> string = _(*test*).(*test*)[5].ToString()
"""

match ast with
| ParsedInput.ImplFile(ParsedImplFileInput(contents = [
SynModuleOrNamespace.SynModuleOrNamespace(decls = [
SynModuleDecl.Let(bindings =
[SynBinding.SynBinding(expr =
SynExpr.Typed(expr =
SynExpr.DotLambda(
SynExpr.App(range = innerRange),
dlRange,
{ UnderscoreRange = urange; DotRange = dRange }
)))])
])
])) ->
assertRange (2, 28) (2, 60) dlRange
assertRange (2, 28) (2, 29) urange
assertRange (2, 37) (2, 38) dRange
assertRange (2, 46) (2, 60) innerRange
Assert.Pass()
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"

0 comments on commit ad0743b

Please sign in to comment.