Alright thanks for your comprehensive answer! I think I got something to work with :)<div><br clear="all">Cheers,<br><br>Tom<br>
<br><br><div class="gmail_quote">On Tue, Mar 8, 2011 at 8:09 PM, Stephen Tetley <span dir="ltr"><<a href="mailto:stephen.tetley@gmail.com">stephen.tetley@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Tom<br>
<br>
Here's how I'd do comment annotation in the Parser:<br>
<br>
<br>
> type Comment = String<br>
> type Identifier = String<br>
<br>
I suspect data carrying tokens need to pair the data and the comment<br>
so Happy can treat them as a positional reference e.g. $1<br>
<br>
> data Token =<br>
> TK_identifier (Identifier,Comment)<br>
> | TK_kywd_module Comment<br>
> | ...<br>
<br>
<br>
Productions now have to use smart constructors:<br>
<br>
><br>
> module :: { Module }<br>
> : '%module' mname defs<br>
> { mkModule $1 $2 $3 }<br>
><br>
<br>
<br>
<br>
> data Module = Module { mod_comment :: Comment<br>
> , mod_name :: String<br>
> , mod_body :: [Def]<br>
> }<br>
<br>
<br>
<br>
The 'smart' constructor takes the comment before the module<br>
delacration, any comment between the module start token and the module<br>
name is ignored...<br>
<br>
> mkModule :: Comment -> (String,Comment) -> [Def] -> Module<br>
> mkModule outer_comment (mname, _) defs =<br>
> Module outer_comment mname defs<br>
<br>
<br>
<br>
As for error handling, the strategy is to add error handling<br>
productions after "good" productions.<br>
<br>
Now module can "handle" a missing module name:<br>
<br>
><br>
> module :: { Module }<br>
> : '%module' mname defs<br>
> { mkModule $1 $2 $3 }<br>
> : ''%module' defs<br>
> { badModule $1 $2 }<br>
<br>
<br>
><br>
> badModule :: Comment [Def] -> Module<br>
> badModule outer_comment defs = Module outer_comment fake_name defs<br>
> where<br>
> fake_name = "ERR - parser error reading module name"<br>
><br>
<br>
Ideally the smart constructors should be in a monad that supports<br>
error logging like Writer.<br>
<br>
As you can see this isn't a great way of doing things but I'm not sure<br>
you have any other options. Personally I'd see if I could live with<br>
"first fail" instead.<br>
<br>
Best wishes<br>
<font color="#888888"><br>
Stephen<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br></div>