Subject: Re: [SWIPL] DCG rules and error reporting

From: "Lyosha" <[email-masked]>
Date: Tue, 9 Oct 2001 20:51:07 +0400

Unfortunately I may not read it from a file.
Let's say a list is being passed into my parsing module
like:
parse_it( Error, Input, Output ).
% Input = "line1 \n line2 \n line3 \n ..."

If my parser detect an error I want to be able to say where it happened
like: Error = error( LineNum, ErrorDescription )

** Note that my parser uses DCG rules and I don't want to detect line
    number in *each* DCG rule and then pass it through all rules.

Here is an example:

parse_it(Result, In, []) :- 
        catch( phrase( parse_go(Result), In), Result, true ).

parse_go --> parse_a, parse_b.

parse_a --> ...
parse_b --> parse_b1, parse_b2.
...
parse_b2 --> p1, p2 ; { get_err_line(EL), throw( error( EL, 'cannot parse b2') ) }.

If parsing succeeds then Result will be bound to the result.
If parsing fails I would like to get Result = error( LineNum, Description ).

Question:  where would get_err_line/1 get an error line number?

I thought I could do:
newline --> "\n", { flag( linenum, V, V+1) }.
newline --> "\n", { flag( linenum, V, V-1), fail }.

But this won't work because of used '!' (cut) in upper rules.

> in swi prolog, the predicate here/2 will return the name and line numebr of
> the current file being read.
> 
> here(File,Line) :-
>  source_location(Path,Line),
>  file_base_name(Path,File).
> 
> ----- Original Message -----
> From: "Lyosha" <[email-masked]>
> To: <[email-masked]>
> Sent: Tuesday, October 09, 2001 3:57 AM
> Subject: [SWIPL] DCG rules and error reporting
> 
> 
> > Hi,
> >
> > I have a question about parsing with DCG rules.
> > Maybe someone can suggest me a solution. I know
> > it's something simple, but I just can't see it.
> >
> > I use DCG rules in my parser. Is there a way to implement
> > error reporting mechanism refering to lines in the source stream?
> > I want my parser to point me at the specific line where the error
> > happened.
> >
> > For example, I could define:
> > newline --> "\n".
> > and put a counter for lines in there, but it won't work with
> > combination of rollback and cut that I use in my other DCG rules.
> >
> > Thanks,
> > Lyosha
> >
> >
> > ----------------
> > * To UNSUBSCRIBE, please use the HTML form at
> >
> >     http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
> >
> > or send mail to [email-masked] using the Subject:
> "unsubscribe"
> > (without the quotes) and *no* message body.
> >
> > ** An ARCHIVE of this list is maintained at
> >
> >     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
> >
> >