From ai@solvo.ru Tue Oct  9 18:50:41 2001
Received: from grace.solvo.ru (grace.solvo.ru [195.201.44.100])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f99Goet08913
	for <prolog@swi.psy.uva.nl>; Tue, 9 Oct 2001 18:50:40 +0200 (MET DST)
Received: from elbrus (elbrus.solvo.ru [195.201.44.16])
	by grace.solvo.ru (8.8.8/8.8.8) with SMTP id UAA25260;
	Tue, 9 Oct 2001 20:50:34 +0400
Message-ID: <00ed01c150e2$97adb820$102cc9c3@solvo.ru>
From: "Lyosha" <ai@solvo.ru>
To: <prolog@swi.psy.uva.nl>, "tim menzies" <tim@menzies.com>
References: <00b201c150b1$3b7659d0$102cc9c3@solvo.ru> <002b01c150db$b7de3ea0$6401a8c0@thoughtfox>
Subject: Re: [SWIPL] DCG rules and error reporting
Date: Tue, 9 Oct 2001 20:51:07 +0400
Organization: Solvo
MIME-Version: 1.0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

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" <ai@solvo.ru>
> To: <prolog@swi.psy.uva.nl>
> 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 prolog-request@swi.psy.uva.nl 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/
> >
> >

