Skip to content

Commit

Permalink
avoiding Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin521 committed Nov 1, 2024
1 parent 86c372c commit 0f4a215
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
39 changes: 22 additions & 17 deletions src/Compiler/Driver/CompilerConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,37 +99,42 @@ type WarningNumberSource =

let GetWarningNumber (m, numStr: string, langVersion: LanguageVersion, source: WarningNumberSource) =
let argFeature = LanguageFeature.ParsedHashDirectiveArgumentNonQuotes
let rxOptions = RegexOptions.CultureInvariant

let warnInvalid () =
warning (Error(FSComp.SR.buildInvalidWarningNumber numStr, m))

let getNumber regexString failAction =
let mtch = Regex(regexString, rxOptions).Match(numStr)

if mtch.Success then
Some(int mtch.Groups[2].Captures[0].Value)
else
let tryParseIntWithFailAction (s: string) (failAction: unit -> unit) =
match Int32.TryParse s with
| true, n -> Some n
| false, _ ->
failAction ()
None

let matches regexString =
Regex(regexString, rxOptions).Match(numStr).Success

if source = WarningNumberSource.CommandLineOption then
getNumber """^(FS)?(\d+)$""" (fun () ->
if not (matches """^([A-Z]+)(\d+)$""") then
warnInvalid ())
let s =
if numStr.StartsWithOrdinal "FS" then
numStr[2..]
else
numStr

tryParseIntWithFailAction s id
elif langVersion.SupportsFeature(argFeature) then
getNumber """^("?)(?:(?:FS)?(\d+))\1$""" warnInvalid
elif matches """^[^"].+$""" then
let s =
if numStr[0] = '"' && numStr[numStr.Length - 1] = '"' then
numStr[1 .. numStr.Length - 2]
else
numStr

let s = if s.StartsWithOrdinal "FS" then s[2..] else s
tryParseIntWithFailAction s warnInvalid
elif numStr[0] <> '"' || numStr[numStr.Length - 1] <> '"' then
errorR (languageFeatureError langVersion argFeature m)
None
elif matches """^"FS.*"$""" then
elif numStr.StartsWith "\"FS" && numStr[numStr.Length - 1] = '"' then
warnInvalid ()
None
else
getNumber """^(")(\d+)"$""" (fun () -> ())
tryParseIntWithFailAction numStr id

let ComputeMakePathAbsolute implicitIncludeDir (path: string) =
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ match None with None -> () // creates FS0025 - ignored due to flag
|> compile
|> shouldFail
|> withDiagnostics [
(Warning 203, Line 0, Col 1, Line 0, Col 1, "Invalid warning number 'FS'")
(Warning 203, Line 0, Col 1, Line 0, Col 1, "Invalid warning number 'FSBLAH'")
(Warning 988, Line 3, Col 3, Line 3, Col 3, "Main module of program is empty: nothing will happen when it is run")
]

Expand Down

0 comments on commit 0f4a215

Please sign in to comment.