forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashIfExpression.fs
259 lines (208 loc) · 9.3 KB
/
HashIfExpression.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.Service.Tests
open System
open System.Text
open Xunit
open FSharp.Test
open Internal.Utilities
open Internal.Utilities.Text.Lexing
open FSharp.Compiler
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.Lexhelp
open FSharp.Compiler.DiagnosticsLogger
open FSharp.Compiler.Features
open FSharp.Compiler.ParseHelpers
type public HashIfExpression() =
let preludes = [|"#if "; "#elif "|]
let epilogues = [|""; " // Testing"|]
let ONE = IfdefId "ONE"
let TWO = IfdefId "TWO"
let THREE = IfdefId "THREE"
let isSet l r = (l &&& r) <> 0
let (!!) e = IfdefNot(e)
let (&&&) l r = IfdefAnd(l,r)
let (|||) l r = IfdefOr(l,r)
let exprAsString (e : LexerIfdefExpression) : string =
let sb = StringBuilder()
let append (s : string) = ignore <| sb.Append s
let rec build (e : LexerIfdefExpression) : unit =
match e with
| IfdefAnd (l,r) -> append "("; build l; append " && "; build r; append ")"
| IfdefOr (l,r) -> append "("; build l; append " || "; build r; append ")"
| IfdefNot ee -> append "!"; build ee
| IfdefId nm -> append nm
build e
sb.ToString ()
let createParser () =
let errors = ResizeArray<PhasedDiagnostic>()
let warnings = ResizeArray<PhasedDiagnostic>()
let diagnosticsLogger =
{
new DiagnosticsLogger("TestDiagnosticsLogger") with
member _.DiagnosticSink(e, sev) = if sev = FSharpDiagnosticSeverity.Error then errors.Add e else warnings.Add e
member _.ErrorCount = errors.Count
}
let indentationSyntaxStatus = IndentationAwareSyntaxStatus(true, false)
let resourceManager = LexResourceManager ()
let defines = []
let applyLineDirectives = true
let startPos = Position.Empty
let args = mkLexargs (defines, indentationSyntaxStatus, resourceManager, [], diagnosticsLogger, PathMap.empty, applyLineDirectives)
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
let parser (s : string) =
let lexbuf = LexBuffer<char>.FromChars (true, LanguageVersion.Default, None, s.ToCharArray ())
lexbuf.StartPos <- startPos
lexbuf.EndPos <- startPos
let tokenStream = PPLexer.tokenstream args
PPParser.start tokenStream lexbuf
errors, warnings, parser
do // Setup
DiagnosticsThreadStatics.BuildPhase <- BuildPhase.Compile
interface IDisposable with // Teardown
member _.Dispose() =
DiagnosticsThreadStatics.BuildPhase <- BuildPhase.DefaultPhase
DiagnosticsThreadStatics.DiagnosticsLogger <- DiagnosticsThreadStatics.DiagnosticsLogger
[<Fact>]
member _.PositiveParserTestCases()=
let errors, warnings, parser = createParser ()
let positiveTestCases =
[|
"ONE" , ONE
"ONE//" , ONE
"ONE // Comment" , ONE
"!ONE" , !!ONE
"!!ONE" , !! (!!ONE)
"DEBUG" , (IfdefId "DEBUG")
"!DEBUG" , !! (IfdefId "DEBUG")
"O_s1" , IfdefId "O_s1"
"(ONE)" , (ONE)
"ONE&&TWO" , ONE &&& TWO
"ONE||TWO" , ONE ||| TWO
"( ONE && TWO )" , ONE &&& TWO
"ONE && TWO && THREE" , (ONE &&& TWO) &&& THREE
"ONE || TWO || THREE" , (ONE ||| TWO) ||| THREE
"ONE || TWO && THREE" , ONE ||| (TWO &&& THREE)
"ONE && TWO || THREE" , (ONE &&& TWO) ||| THREE
"ONE || (TWO && THREE)" , ONE ||| (TWO &&& THREE)
"ONE && (TWO || THREE)" , ONE &&& (TWO ||| THREE)
"!ONE || TWO && THREE" , (!!ONE) ||| (TWO &&& THREE)
"ONE && !TWO || THREE" , (ONE &&& (!!TWO)) ||| THREE
"ONE || !(TWO && THREE)" , ONE ||| (!!(TWO &&& THREE))
"true" , IfdefId "true"
"false" , IfdefId "false"
|]
let failures = ResizeArray<string> ()
let fail = failures.Add
for test,expected in positiveTestCases do
for prelude in preludes do
let test = prelude + test
for epilogue in epilogues do
let test = test + epilogue
try
let expr = parser test
if expected <> expr then
fail <| sprintf "'%s', expected %A, actual %A" test (exprAsString expected) (exprAsString expr)
with e ->
fail <| sprintf "'%s', expected %A, actual %s,%A" test (exprAsString expected) (e.GetType().Name) e.Message
let fs =
failures
|> Seq.append (errors |> Seq.map (fun pe -> pe.DebugDisplay ()))
|> Seq.append (warnings |> Seq.map (fun pe -> pe.DebugDisplay ()))
|> Seq.toArray
let failure = String.Join ("\n", fs)
Assert.shouldBe "" failure
()
[<Fact>]
member _.NegativeParserTestCases()=
let errors, _warnings, parser = createParser ()
let negativeTests =
[|
""
"!"
"&&"
"||"
"@"
"ONE ONE"
"ONE@"
"@ONE"
"$"
"ONE$"
"$ONE"
"ONE!"
"(ONE"
"ONE)"
// TODO: Investigate why this raises a parse failure
// "(ONE ||)"
"ONE&&"
"ONE ||"
"&& ONE"
"||ONE"
"ONE TWO"
"ONE(* Comment"
"ONE(* Comment *)"
"ONE(**)"
"ONE (* Comment"
"ONE (* Comment *)"
"ONE (**)"
"ONE )(@$&%*@^#%#!$)"
|]
let failures = ResizeArray<string> ()
let fail = failures.Add
for test in negativeTests do
for prelude in preludes do
let test = prelude + test
for epilogue in epilogues do
let test = test + epilogue
try
let bec = errors.Count
let expr = parser test
let aec = errors.Count
if bec = aec then // No new errors discovered
fail <| sprintf "'%s', expected 'parse error', actual %A" test (exprAsString expr)
with
| e -> fail <| sprintf "'%s', expected 'parse error', actual %s,%A" test (e.GetType().Name) e.Message
let fs = failures |> Seq.toArray
let fails = String.Join ("\n", fs)
Assert.shouldBe "" fails
[<Fact>]
member _.LexerIfdefEvalTestCases()=
let failures = ResizeArray<string> ()
let fail = failures.Add
for i in 0..7 do
let one = isSet i 1
let two = isSet i 2
let three = isSet i 4
let lookup s =
match s with
| "ONE" -> one
| "TWO" -> two
| "THREE" -> three
| _ -> false
let testCases =
[|
ONE , one
!!ONE , not one
!! (!!ONE) , not (not one)
TWO , two
!!TWO , not two
!! (!!TWO) , not (not two)
ONE &&& TWO , one && two
ONE ||| TWO , one || two
(ONE &&& TWO) &&& THREE , (one && two) && three
(ONE ||| TWO) ||| THREE , (one || two) || three
ONE ||| (TWO &&& THREE) , one || (two && three)
(ONE &&& TWO) ||| THREE , (one && two) || three
ONE ||| (TWO &&& THREE) , one || (two && three)
ONE &&& (TWO ||| THREE) , one && (two || three)
(!!ONE) ||| (TWO &&& THREE) , (not one) || (two && three)
(ONE &&& (!!TWO)) ||| THREE , (one && (not two)) || three
ONE ||| (!!(TWO &&& THREE)) , one || (not (two && three))
|]
let eval = LexerIfdefEval lookup
for expr, expected in testCases do
let actual = eval expr
if actual <> expected then
fail <| sprintf "For ONE=%A, TWO=%A, THREE=%A the expression %A is expected to be %A but was %A" one two three (exprAsString expr) expected actual
let fs = failures |> Seq.toArray
let fails = String.Join ("\n", fs)
Assert.shouldBe "" fails