Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define a parser that consumes a single character #2

Open
mrange opened this issue Oct 31, 2016 · 3 comments
Open

Define a parser that consumes a single character #2

mrange opened this issue Oct 31, 2016 · 3 comments

Comments

@mrange
Copy link
Collaborator

mrange commented Oct 31, 2016

If the input is non empty the parser should return the first character.

If the input is empty the parser return a failure.

let p : Parser<char> = char
@vlukash
Copy link
Owner

vlukash commented Nov 1, 2016

Pushed a few *fs files to the repo with implementation for the single character parser but not sure if that is correct. Could you please take a look?

@mrange
Copy link
Collaborator Author

mrange commented Nov 1, 2016

What is missing is that it should return the unconsumed part of the string. In addition error message should be associated with a position in the string in order to help the user

On Tue, Nov 1, 2016 at 8:51 AM +0100, "Volodymyr Lukashevych" [email protected] wrote:

Pushed a few *fs files to the repo with implementation for the single character parser but not sure if that is correct. Could you please take a look?

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#2 (comment)

@vlukash
Copy link
Owner

vlukash commented Nov 2, 2016

Pushed changes to a hardcoded char parser so now returning an unconsumed string on Success.

But not sure if I got the point about providing information on a position in the string in a Failure case.
As if there is no matching character in a string then we can't provide details on position. Or Probably I've missed something

type Result<'T> =
     | Success of 'T*string 
     | Failure of string 

let parseFirstCharHardcodedA str =
    let charToMatch = 'a'
    if String.IsNullOrEmpty(str) then
        Failure "Input is empty"
    else
        let firstChar = str.[0]
        if firstChar = charToMatch then
            Success(charToMatch,str.[1..])
        else
            let errorMessage = sprintf "Expected character is: '%c' but received '%c'" charToMatch firstChar
            Failure errorMessage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants