Skip to content

Instantly share code, notes, and snippets.

@eignnx
Last active May 29, 2025 18:49
Show Gist options
  • Select an option

  • Save eignnx/51c526b2aa69a68c10366edb63a763d2 to your computer and use it in GitHub Desktop.

Select an option

Save eignnx/51c526b2aa69a68c10366edb63a763d2 to your computer and use it in GitHub Desktop.
main :-
catch(
( phrase_from_stream(unlimited(parser), user_input) -> true
; format('% parse failure!~n', []) ),
_,
format('% caught exception!~n', [])
),
format('% exiting...~n', []).
unlimited(Goal) -->
call(Goal),
unlimited(Goal).
parser -->
{ format('% trying to parse :NOP...~n', []) },
`:NOP\n`,
{ format('% parsed :NOP!~n', []) }.
parser -->
{ format('% trying to parse :EXIT...~n', []) },
`:EXIT\n`,
{ format('% parsed :EXIT!~n', []) },
{ format('% throwing...~n', []) },
{ throw(error(breakoutplease, _)) }.
/*
?- main.
% trying to parse :NOP...
|: INVALID
% trying to parse :EXIT...
% parse failure!
% exiting...
true.
?- main.
% trying to parse :NOP...
|: :NOP
% parsed :NOP!
% trying to parse :NOP...
:EXIT
% trying to parse :EXIT...
% parsed :EXIT!
% throwing...
% caught exception!
% exiting...
true.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment