[Haskell-cafe] Error recovery in Happy

Stephen Tetley stephen.tetley at gmail.com
Tue Mar 8 20:09:17 CET 2011


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



More information about the Haskell-Cafe mailing list