From f0a002c5b5a577d2c0ff207e0faa29ca61df249d Mon Sep 17 00:00:00 2001 From: Gauthier Segay Date: Mon, 13 Nov 2023 02:48:38 +0100 Subject: [PATCH] define names for ParseResult fields, in lieu of Item1, Item2, Item3, which shows in debugger and prevent deconstruction via name rather than positional. --- FParsec/CharParsers.fs | 8 ++++---- FParsec/CharParsers.fsi | 8 ++++---- Samples/Calculator/calculator.fs | 6 +++--- Samples/FSharpParsingSample/FParsecVersion/main.fs | 4 ++-- Samples/JSON/main.fs | 4 ++-- Samples/PEG/main.fs | 4 ++-- Samples/Tutorial/tutorial.fs | 4 ++-- Test/CharParsersTests.fs | 10 +++++----- Test/IdentifierValidatorTests.fs | 4 ++-- 9 files changed, 26 insertions(+), 26 deletions(-) diff --git a/FParsec/CharParsers.fs b/FParsec/CharParsers.fs index 9986710..926e124 100644 --- a/FParsec/CharParsers.fs +++ b/FParsec/CharParsers.fs @@ -43,15 +43,15 @@ let float32OfHexString = HexFloat.SingleFromHexString [] type ParserResult<'Result,'UserState> = - | Success of 'Result * 'UserState * Position - | Failure of string * ParserError * 'UserState + | Success of result: 'Result * state: 'UserState * endPosition: Position + | Failure of message: string * error: ParserError * state: 'UserState with member private t.StructuredFormatDisplay = match t with - | Success(r,_,_) -> + | Success(result=r) -> if typeof<'Result> = typeof then "Success: ()" else sprintf "Success: %A" r - | Failure(msg,_,_) -> + | Failure(message=msg) -> sprintf "Failure:\n%s" msg let internal applyParser (parser: Parser<'Result,'UserState>) (stream: CharStream<'UserState>) = diff --git a/FParsec/CharParsers.fsi b/FParsec/CharParsers.fsi index 46edd12..e5bbf20 100644 --- a/FParsec/CharParsers.fsi +++ b/FParsec/CharParsers.fsi @@ -13,12 +13,12 @@ open Primitives /// Values of this type are returned by the runParser functions (not by `Parser<_,_>` functions). type ParserResult<'Result,'UserState> = - /// Success(result, userState, endPos) holds the result and the user state returned by a successful parser, + /// Success(result, state, endPosition) holds the result and the user state returned by a successful parser, /// together with the position where the parser stopped. - | Success of 'Result * 'UserState * Position - /// Failure(errorAsString, error, suserState) holds the parser error and the user state returned by a failing parser, + | Success of result: 'Result * state: 'UserState * endPosition: Position + /// Failure(message, error, suserState) holds the parser error and the user state returned by a failing parser, /// together with a string representation of the parser error. - | Failure of string * ParserError * 'UserState + | Failure of message: string * error: ParserError * state: 'UserState /// `runParserOnString p ustate streamName str` runs the parser `p` directly on the content of the string `str`, /// starting with the initial user state `ustate`. The `streamName` is used in error messages to describe diff --git a/Samples/Calculator/calculator.fs b/Samples/Calculator/calculator.fs index 4e5c12f..323d9d5 100644 --- a/Samples/Calculator/calculator.fs +++ b/Samples/Calculator/calculator.fs @@ -47,9 +47,9 @@ let calculate s = run completeExpression s let equals expectedValue r = match r with - | Success (v, _, _) when v = expectedValue -> () - | Success (v, _, _) -> failwith "Math is hard, let's go shopping!" - | Failure (msg, err, _) -> printf "%s" msg; failwith msg + | Success (result=v) when v = expectedValue -> () + | Success _ -> failwith "Math is hard, let's go shopping!" + | Failure (message=msg) -> printf "%s" msg; failwith msg let test() = calculate "10.5 + 123.25 + 877" |> equals 1010.75 diff --git a/Samples/FSharpParsingSample/FParsecVersion/main.fs b/Samples/FSharpParsingSample/FParsecVersion/main.fs index 3cf5d61..65ebca3 100644 --- a/Samples/FSharpParsingSample/FParsecVersion/main.fs +++ b/Samples/FSharpParsingSample/FParsecVersion/main.fs @@ -23,8 +23,8 @@ let main(argv: string[]) = let myProg = match result with - | Success (v, _, _) -> v - | Failure (msg, _, _) -> + | Success (result=v) -> v + | Failure (message=msg) -> System.Console.WriteLine(msg) exit 1 diff --git a/Samples/JSON/main.fs b/Samples/JSON/main.fs index e861c4f..f4f80bf 100644 --- a/Samples/JSON/main.fs +++ b/Samples/JSON/main.fs @@ -22,10 +22,10 @@ let main(args: string[]) = let result = parseJsonFile args[0] System.Text.Encoding.UTF8 // for the moment we just print out the AST match result with - | Success (v, _, _) -> + | Success (result=v) -> printf "The AST of the input file is:\n%A\n" v 0 - | Failure (msg, err, _) -> + | Failure (message=msg) -> printfn "%s" msg 1 diff --git a/Samples/PEG/main.fs b/Samples/PEG/main.fs index bf144b9..c02a37b 100644 --- a/Samples/PEG/main.fs +++ b/Samples/PEG/main.fs @@ -22,6 +22,6 @@ let main(args: string[]) = let result = runParserOnFile Parser.pGrammar () fileName System.Text.Encoding.UTF8 // for the moment we just print out the AST match result with - | Success (v, _, _) -> printf "The ast for the input file is:\n%A\n" v - | Failure (msg, err, _) -> printf "%s\n" msg + | Success (result=v) -> printf "The ast for the input file is:\n%A\n" v + | Failure (message=msg) -> printf "%s\n" msg 0 diff --git a/Samples/Tutorial/tutorial.fs b/Samples/Tutorial/tutorial.fs index 107c05c..ec89968 100644 --- a/Samples/Tutorial/tutorial.fs +++ b/Samples/Tutorial/tutorial.fs @@ -9,8 +9,8 @@ open FParsec let test p str = match run p str with - | Success(result, _, _) -> printfn "Success: %A" result - | Failure(errorMsg, _, _) -> printfn "Failure: %s" errorMsg + | Success(result=result) -> printfn "Success: %A" result + | Failure(message=errorMsg) -> printfn "Failure: %s" errorMsg test pfloat "1.25" test pfloat "1.25E 2" diff --git a/Test/CharParsersTests.fs b/Test/CharParsersTests.fs index 38bee19..745096e 100644 --- a/Test/CharParsersTests.fs +++ b/Test/CharParsersTests.fs @@ -144,7 +144,7 @@ let testSpecialCharParsers() = let count p = many p |>> List.fold (fun c x -> c + 1) 0 match run (count unicodeNewline) "\n\r\r\n\u0085\u2028\u2029\r\n" with - | Success(c,_,pos) -> c |> Equal 7; pos.Index |> Equal 9L; pos.Line |> Equal 8L; pos.Column |> Equal 1L + | Success(result=c;endPosition=pos) -> c |> Equal 7; pos.Index |> Equal 9L; pos.Line |> Equal 8L; pos.Column |> Equal 1L | Failure _ -> Fail() spaces |> ROk "" 0 () @@ -162,17 +162,17 @@ let testSpecialCharParsers() = unicodeSpaces1 |> ROk " \u200A" 2 () match run spaces "\n \r\t\t\r\n\n " with - | Success(_, _, pos) -> pos.Index |> Equal 9L; pos.Line |> Equal 5L; pos.Column |> Equal 2L + | Success(endPosition=pos) -> pos.Index |> Equal 9L; pos.Line |> Equal 5L; pos.Column |> Equal 2L | _ -> Fail() match run spaces1 "\n \r\t\t\r\n\n " with - | Success(_, _, pos) -> pos.Index |> Equal 9L; pos.Line |> Equal 5L; pos.Column |> Equal 2L + | Success(endPosition=pos) -> pos.Index |> Equal 9L; pos.Line |> Equal 5L; pos.Column |> Equal 2L | _ -> Fail() match run unicodeSpaces "\n \r\t\t\r\n\n \u0085\u000C\u2028\u2029 \r\n\t\u200A" with - | Success(_, _, pos) -> pos.Index |> Equal 18L; pos.Line |> Equal 9L; pos.Column |> Equal 3L + | Success(endPosition=pos) -> pos.Index |> Equal 18L; pos.Line |> Equal 9L; pos.Column |> Equal 3L | _ -> Fail() match run unicodeSpaces1 "\n \r\t\t\r\n\n \u0085\u000C\u2028\u2029 \r\n\t\u200A" with - | Success(_, _, pos) -> pos.Index |> Equal 18L; pos.Line |> Equal 9L; pos.Column |> Equal 3L + | Success(endPosition=pos) -> pos.Index |> Equal 18L; pos.Line |> Equal 9L; pos.Column |> Equal 3L | _ -> Fail() eof |> ROk "" 0 () diff --git a/Test/IdentifierValidatorTests.fs b/Test/IdentifierValidatorTests.fs index 0f2ce4c..71d6b8d 100644 --- a/Test/IdentifierValidatorTests.fs +++ b/Test/IdentifierValidatorTests.fs @@ -1812,8 +1812,8 @@ open FParsec let testCharPredicates() = let xidStartRanges, xidContinueRanges = match Parser.parseXIdRanges() with - | CharParsers.Success(ranges, _, _ ) -> ranges - | CharParsers.Failure(msg,_,_) -> failwith msg + | CharParsers.Success(result=ranges) -> ranges + | CharParsers.Failure(message=msg) -> failwith msg let checkPredicate fBmp fSmp ranges = let mutable lastLast = -1