Skip to content

Commit

Permalink
Some refactoring and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin521 committed Aug 9, 2024
1 parent fe8010c commit 6044617
Showing 1 changed file with 56 additions and 39 deletions.
95 changes: 56 additions & 39 deletions src/Compiler/Driver/ParseAndCheckInputs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -219,46 +219,52 @@ let PostParseModuleSpec (_i, defaultNamespace, isLastCompiland, fileName, intf)
let GetScopedPragmas (langVersion: LanguageVersion) hashDirectives =
let supportsNonStringArguments =
langVersion.SupportsFeature(LanguageFeature.ParsedHashDirectiveArgumentNonQuotes)

let getWarnDirectiveInfos hd = [
match hd with
| ParsedHashDirective(("nowarn" | "warnon") as hdIdent, args, m) ->
for arg in args do
let warningNumber =
match supportsNonStringArguments, arg with
| _, ParsedHashDirectiveArgument.SourceIdentifier _ -> None
| true, ParsedHashDirectiveArgument.LongIdent _ -> None
| true, ParsedHashDirectiveArgument.Int32(n, _) -> GetWarningNumber(m, string n, true)
| true, ParsedHashDirectiveArgument.Ident(s, _) -> GetWarningNumber(m, s.idText, true)
| _, ParsedHashDirectiveArgument.String(s, _, _) -> GetWarningNumber(m, s, true)
| _ -> None // emit warning?

match warningNumber with
| None -> ()
| Some n -> (hdIdent, n), m
| _ -> ()
]

let getWarnDirectiveInfos hd =
[
match hd with
| ParsedHashDirective(("nowarn" | "warnon") as hdIdent, args, m) ->
for arg in args do
let warningNumber =
match supportsNonStringArguments, arg with
| _, ParsedHashDirectiveArgument.SourceIdentifier _ -> None
| true, ParsedHashDirectiveArgument.LongIdent _ -> None
| true, ParsedHashDirectiveArgument.Int32(n, _) -> GetWarningNumber(m, string n, true)
| true, ParsedHashDirectiveArgument.Ident(s, _) -> GetWarningNumber(m, s.idText, true)
| _, ParsedHashDirectiveArgument.String(s, _, _) -> GetWarningNumber(m, s, true)
| _ -> None // emit warning?

match warningNumber with
| None -> ()
| Some n -> (hdIdent, n), m
| _ -> ()
]

let processWarnDirectiveInfo (openPragmas, pragmas) ((hdIdent, n), m: range) =
match hdIdent with
| "nowarn" ->
Map.add n m openPragmas, pragmas
| "nowarn" -> Map.add n m openPragmas, pragmas
| "warnon" ->
match Map.tryFind n openPragmas with
| Some mm -> // do we have to add here `when m.FileIndex = mm.FileIndex`? (see comment above DiagnosticsLoggerFilteringByScopedPragmas)
let scope = Range.mkFileIndexRange m.FileIndex (mkPos mm.StartLine 0) (mkPos m.StartLine 0)
| Some mm -> // do we have to add here `when m.FileIndex = mm.FileIndex`? (see comment above DiagnosticsLoggerFilteringByScopedPragmas)
let scope =
Range.mkFileIndexRange m.FileIndex (mkPos mm.StartLine 0) (mkPos m.StartLine 0)

let pragma = ScopedPragma.WarningOff(scope, n)
Map.remove n openPragmas, pragma::pragmas
| None -> openPragmas, pragmas // emit warning?
Map.remove n openPragmas, pragma :: pragmas
| None -> openPragmas, pragmas // emit warning?
| _ -> failwith "unexpected hdIdent in processWarnDirectiveInfo"

let addOpenPragmas (openPragmas, pragmas) =
let addPragma pragmas n (m: range) =
let fileEndLine = 1000000 //TODO!! What is the best way to define a range ending at eof?
let scope = Range.mkFileIndexRange m.FileIndex (mkPos m.StartLine 0) (mkPos fileEndLine 0)
let fileEndLine = 1000000 //TODO!! What is the best way to define a range ending at eof?

let scope =
Range.mkFileIndexRange m.FileIndex (mkPos m.StartLine 0) (mkPos fileEndLine 0)

ScopedPragma.WarningOff(scope, n) :: pragmas

openPragmas |> Map.fold addPragma pragmas

hashDirectives
|> List.collect getWarnDirectiveInfos
|> List.sortBy (snd >> _.StartLine)
Expand Down Expand Up @@ -327,15 +333,16 @@ let PostParseModuleImpls

let scopedPragmas =
let hashDirectives =
[
for SynModuleOrNamespace(decls = decls) in impls do
for d in decls do
match d with
| SynModuleDecl.HashDirective(hd, _) -> yield hd
| _ -> ()
for hd in toplevelHashDirectives do
yield hd
]
let getImplHashDirectives (SynModuleOrNamespace(decls = decls)) =
let getDeclHashDirectives decl =
match decl with
| SynModuleDecl.HashDirective(hd, _) -> Some hd
| _ -> None

decls |> List.choose getDeclHashDirectives

(impls |> List.collect getImplHashDirectives) @ toplevelHashDirectives

hashDirectives |> GetScopedPragmas lexbuf.LanguageVersion

let conditionalDirectives = LexbufIfdefStore.GetTrivia(lexbuf)
Expand All @@ -348,7 +355,17 @@ let PostParseModuleImpls
}

ParsedInput.ImplFile(
ParsedImplFileInput(fileName, isScript, qualName, scopedPragmas, toplevelHashDirectives, impls, isLastCompiland, trivia, identifiers)
ParsedImplFileInput(
fileName,
isScript,
qualName,
scopedPragmas,
toplevelHashDirectives,
impls,
isLastCompiland,
trivia,
identifiers
)
)

let PostParseModuleSpecs
Expand Down

0 comments on commit 6044617

Please sign in to comment.