[Haskell-cafe] Error recovery in Happy
Tom
tom.ppc at gmail.com
Wed Mar 9 00:27:10 CET 2011
Alright thanks for your comprehensive answer! I think I got something to
work with :)
Cheers,
Tom
On Tue, Mar 8, 2011 at 8:09 PM, Stephen Tetley <stephen.tetley at gmail.com>wrote:
> Hi Tom
>
> Here's how I'd do comment annotation in the Parser:
>
>
> > type Comment = String
> > type Identifier = String
>
> I suspect data carrying tokens need to pair the data and the comment
> so Happy can treat them as a positional reference e.g. $1
>
> > data Token =
> > TK_identifier (Identifier,Comment)
> > | TK_kywd_module Comment
> > | ...
>
>
> Productions now have to use smart constructors:
>
> >
> > module :: { Module }
> > : '%module' mname defs
> > { mkModule $1 $2 $3 }
> >
>
>
>
> > data Module = Module { mod_comment :: Comment
> > , mod_name :: String
> > , mod_body :: [Def]
> > }
>
>
>
> The 'smart' constructor takes the comment before the module
> delacration, any comment between the module start token and the module
> name is ignored...
>
> > mkModule :: Comment -> (String,Comment) -> [Def] -> Module
> > mkModule outer_comment (mname, _) defs =
> > Module outer_comment mname defs
>
>
>
> As for error handling, the strategy is to add error handling
> productions after "good" productions.
>
> Now module can "handle" a missing module name:
>
> >
> > module :: { Module }
> > : '%module' mname defs
> > { mkModule $1 $2 $3 }
> > : ''%module' defs
> > { badModule $1 $2 }
>
>
> >
> > badModule :: Comment [Def] -> Module
> > badModule outer_comment defs = Module outer_comment fake_name defs
> > where
> > fake_name = "ERR - parser error reading module name"
> >
>
> Ideally the smart constructors should be in a monad that supports
> error logging like Writer.
>
> As you can see this isn't a great way of doing things but I'm not sure
> you have any other options. Personally I'd see if I could live with
> "first fail" instead.
>
> Best wishes
>
> Stephen
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110309/327d0ee3/attachment.htm>
More information about the Haskell-Cafe
mailing list