Search code examples
exceptionf#fparsec

failwith causes an error when used in a calculation expression - FParsec


I use a function:

let identifier kind =
    (many1Satisfy2L isLetter
        (fun c -> isLetter c || isDigit c) "identifier"
     >>= fun s -> preturn s) >>= fun s -> identifierKind s kind

The kind argument is of this type:

type KindOfIdentifier =
    | Data
    | Type
    | Module

And here is my function that analyzes the kind argument:

let private identifierKind (id: string) kind =
    match kind with
    | KindOfIdentifier.Data ->
        if id.ToUpper() = id && id.Length > 1 then preturn id
        elif System.Char.IsUpper id.[0] = false then preturn id
        else failwith "Error 1"
    | KindOfIdentifier.Module ->
        if System.Char.IsUpper id.[0] then preturn id
        else failwith "Error 2"
    | KindOfIdentifier.Type ->
        preturn id

I would therefore like to analyze an identifier to verify whether it meets the criteria of the identifier type. If identifying it does not meet the criterion, I return an error with failwith. But, when I use this parser (identify) with a deliberate error in my text to be analyzed, to check if everything works, I get a long error:

enter image description here

(Sorry, I'm French, so there's a little french in the error message ^^.)

How to prevent all this, and only display the error message in the classic way with FParsec?


Solution

  • The failwith function throws a .NET exception - a catasprophic failure that is supposed to indicate that the program broke in an unexpected way. Or, in other words, in an exceptional way - hence the name "exception". This is not what you're trying to do.

    What you're trying to do here is to indicate to FParsec that the current parsing attempt has failed, and possibly provide an explanation of what exactly happened.

    To do this, you need to create an error-producing instance of Parser - the same type that is returned by preturn.

    While preturn creates a successful instance of Parser, there is another function that creates an error-producing instance. This function is called fail. Just use it:

        | KindOfIdentifier.Data ->
            if id.ToUpper() = id && id.Length > 1 then preturn id
            elif System.Char.IsUpper id.[0] = false then preturn id
            else fail "Error 1"