[m-users.] use of cc_multi.

Sean Charles (emacstheviking) objitsu at gmail.com
Sat May 1 07:30:41 AEST 2021


%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
%
% File: errors.m
% Main authors: sjc
% Date: 27-04-2021 21:05 Hrs
%
% Outputs an error message to stdout in one of the formats as specified by
% the type: options.error_format
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%

:- module errors.
:- interface.
:- import_module io.
:- use_module options.

:- type felt_error
    --->    error(
                line   :: int,
                col    :: int,
                pos    :: int,
                file   :: string,
                reason :: string
            ).

:- pred write_error(
    options.error_format::in,
    felt_error::in,
    io::di, io::uo
    ) is cc_multi.

:- func make_error(int::in, int::in, int::in, string::in, string::in)
     = (felt_error::out) is det.

:- implementation.
:- import_module list, string.

    % PLAIN -- the default human readable error format.
    %
    write_error(options.plain, error(Pos, Line, Col, File, Reason), !IO) :-
    io.format(
        "*** FELT-ERROR ***A syntax error has been detected in your source:\n" ++
        "file:%s:(%i) at line %i, column %i. The reported error is:\n" ++
        "%s\n",
        [s(File), i(Pos), i(Line), i(Col), s(Reason)], !IO).

    % SIMPLE -- for simple line based parsing applications.
    %
write_error(options.simple, error(Pos, Line, Col, File, Reason), !IO) :-
    io.format(
        "%s\n%i\n%i\n%i\n%s\n",
        [s(File), i(Pos), i(Line), i(Col), s(Reason)], !IO).

    % JSON -- because the internet.
    %
write_error(options.json, error(Pos, Line, Col, File, Reason), !IO) :-
    % poss might need to escape the string but it's MY string!
    io.format(
        "{\"file\": \"%s\", \"pos\": %i, \"line\": %i, \"col\": %i, \"error\": \"%s\"}\n",
        [s(File), i(Pos), i(Line), i(Col), s(Reason)], !IO).

    % XML -- bare minimum xml output
    %
write_error(options.xml, error(Pos, Line, Col, File, Reason), !IO) :-
    io.format(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ++
        "<error file=\"%s\" pos=\"%i\" line=\"%i\" col=\"%i\">\n" ++
        "%s\n</error>\n",
        [s(File), i(Pos), i(Line), i(Col), s(Reason)], !IO).

write_error(_, _, !_IO).

    % Create standard error packet.
    %
make_error(Pos, Line, Col, File, Reason) = Error :-
    Error = error(Pos, Line, Col, File, Reason).


> On 30 Apr 2021, at 22:29, Zoltan Somogyi <zoltan.somogyi at runbox.com> wrote:
> 
> 
> 
> On Fri, 30 Apr 2021 22:24:12 +0100, "Sean Charles (emacstheviking)" <objitsu at gmail.com> wrote:
>> I seek some reassurance that this is the correct reasoning and the correct determinism.
> 
> I cannot answer that without the part of the write_error predicate you deleted.
> 
> Zoltan.
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210430/cdad4e93/attachment-0001.html>


More information about the users mailing list