Handling exceptions with FParsec
up vote
1
down vote
favorite
I would like to be able to parse a file without the program launching an exception and stop the program at the first error. I use this:
let parse input = match run pprog input with
| Success(result, _, _) -> result
| Failure(msg, _, _) -> failwith msg
let program = parse myFile
The program
variable is of the type AST.program
(no need to show the implementation), thanks to the matching pattern of the parse
function. If I do only this:
let program = run pprog myFile
program
is of the type ParserResult<AST.Program, unit>
, which is therefore unanalyzable.
I was wondering if there was a way not to crash the program with the exceptions launched?
.net parsing exception f# fparsec
add a comment |
up vote
1
down vote
favorite
I would like to be able to parse a file without the program launching an exception and stop the program at the first error. I use this:
let parse input = match run pprog input with
| Success(result, _, _) -> result
| Failure(msg, _, _) -> failwith msg
let program = parse myFile
The program
variable is of the type AST.program
(no need to show the implementation), thanks to the matching pattern of the parse
function. If I do only this:
let program = run pprog myFile
program
is of the type ParserResult<AST.Program, unit>
, which is therefore unanalyzable.
I was wondering if there was a way not to crash the program with the exceptions launched?
.net parsing exception f# fparsec
How to not crash with an exception is easy: just use atry ... with
block, and handle the exception in thewith
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in theFailure
branch of yourparse
function, instead of throwing an exception withfailwith
in that branch. So ask yourself this: what do you want to do with invalid input?
– rmunn
Nov 10 at 22:41
@rmunn – FParsec, during its parsing, already displays the errors.failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, theparse
function must be of the following type:string -> AST.Program
. So I must necessarily return a data of the typeAST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.
– Foxy
Nov 10 at 23:16
Does yourAST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then yourparse
function could return the parsed program on success, and an empty program on failure.
– rmunn
2 days ago
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get anAST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.
– Foxy
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would like to be able to parse a file without the program launching an exception and stop the program at the first error. I use this:
let parse input = match run pprog input with
| Success(result, _, _) -> result
| Failure(msg, _, _) -> failwith msg
let program = parse myFile
The program
variable is of the type AST.program
(no need to show the implementation), thanks to the matching pattern of the parse
function. If I do only this:
let program = run pprog myFile
program
is of the type ParserResult<AST.Program, unit>
, which is therefore unanalyzable.
I was wondering if there was a way not to crash the program with the exceptions launched?
.net parsing exception f# fparsec
I would like to be able to parse a file without the program launching an exception and stop the program at the first error. I use this:
let parse input = match run pprog input with
| Success(result, _, _) -> result
| Failure(msg, _, _) -> failwith msg
let program = parse myFile
The program
variable is of the type AST.program
(no need to show the implementation), thanks to the matching pattern of the parse
function. If I do only this:
let program = run pprog myFile
program
is of the type ParserResult<AST.Program, unit>
, which is therefore unanalyzable.
I was wondering if there was a way not to crash the program with the exceptions launched?
.net parsing exception f# fparsec
.net parsing exception f# fparsec
asked Nov 10 at 12:16
Foxy
189110
189110
How to not crash with an exception is easy: just use atry ... with
block, and handle the exception in thewith
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in theFailure
branch of yourparse
function, instead of throwing an exception withfailwith
in that branch. So ask yourself this: what do you want to do with invalid input?
– rmunn
Nov 10 at 22:41
@rmunn – FParsec, during its parsing, already displays the errors.failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, theparse
function must be of the following type:string -> AST.Program
. So I must necessarily return a data of the typeAST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.
– Foxy
Nov 10 at 23:16
Does yourAST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then yourparse
function could return the parsed program on success, and an empty program on failure.
– rmunn
2 days ago
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get anAST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.
– Foxy
yesterday
add a comment |
How to not crash with an exception is easy: just use atry ... with
block, and handle the exception in thewith
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in theFailure
branch of yourparse
function, instead of throwing an exception withfailwith
in that branch. So ask yourself this: what do you want to do with invalid input?
– rmunn
Nov 10 at 22:41
@rmunn – FParsec, during its parsing, already displays the errors.failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, theparse
function must be of the following type:string -> AST.Program
. So I must necessarily return a data of the typeAST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.
– Foxy
Nov 10 at 23:16
Does yourAST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then yourparse
function could return the parsed program on success, and an empty program on failure.
– rmunn
2 days ago
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get anAST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.
– Foxy
yesterday
How to not crash with an exception is easy: just use a
try ... with
block, and handle the exception in the with
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in the Failure
branch of your parse
function, instead of throwing an exception with failwith
in that branch. So ask yourself this: what do you want to do with invalid input?– rmunn
Nov 10 at 22:41
How to not crash with an exception is easy: just use a
try ... with
block, and handle the exception in the with
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in the Failure
branch of your parse
function, instead of throwing an exception with failwith
in that branch. So ask yourself this: what do you want to do with invalid input?– rmunn
Nov 10 at 22:41
@rmunn – FParsec, during its parsing, already displays the errors.
failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, the parse
function must be of the following type: string -> AST.Program
. So I must necessarily return a data of the type AST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.– Foxy
Nov 10 at 23:16
@rmunn – FParsec, during its parsing, already displays the errors.
failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, the parse
function must be of the following type: string -> AST.Program
. So I must necessarily return a data of the type AST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.– Foxy
Nov 10 at 23:16
Does your
AST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then your parse
function could return the parsed program on success, and an empty program on failure.– rmunn
2 days ago
Does your
AST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then your parse
function could return the parsed program on success, and an empty program on failure.– rmunn
2 days ago
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get an
AST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.– Foxy
yesterday
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get an
AST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.– Foxy
yesterday
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238860%2fhandling-exceptions-with-fparsec%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
How to not crash with an exception is easy: just use a
try ... with
block, and handle the exception in thewith
part. BUT... there's something you haven't mentioned in your question, which is: what do you want your program to do if FParsec parsing fails? How do you want your program to handle invalid input? Once you've decided the answer to that question, then all you need to do is to put that logic in theFailure
branch of yourparse
function, instead of throwing an exception withfailwith
in that branch. So ask yourself this: what do you want to do with invalid input?– rmunn
Nov 10 at 22:41
@rmunn – FParsec, during its parsing, already displays the errors.
failwith
launches an exception (which crashes the program on the way), but if I could avoid it it would be fine with me. Only, theparse
function must be of the following type:string -> AST.Program
. So I must necessarily return a data of the typeAST.Program
, or run an exception. But if there is a way to have a function of this type that does not launch an exception, I would be happy to know.– Foxy
Nov 10 at 23:16
Does your
AST.Program
type have some kind of "empty" value? E.g., if it's a list of expressions, the "empty" value would be an empty list. If so, then yourparse
function could return the parsed program on success, and an empty program on failure.– rmunn
2 days ago
@rmunn – I had thought about it at the beginning, and indeed there are no more exceptions, only it doesn't display errors, and stops the first time. What I'm actually looking for is a way to get an
AST.Program
data, and for all errors to be displayed without any exceptions. I also think that my way of doing things in the initial post is not correct in terms of what I would like to do in relation to the match. I looked at the documentation, but it doesn't deal with the subject.– Foxy
yesterday