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?










share|improve this question





















  • 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











  • 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















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?










share|improve this question





















  • 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











  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 12:16









Foxy

189110




189110











  • 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











  • 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

















  • 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











  • 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
















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


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線