alex icon indicating copy to clipboard operation
alex copied to clipboard

Allow custom error types in monad wrappers

Open anka-213 opened this issue 3 years ago • 7 comments

I want to extract the position information on lexing errors so I can send them to an LSP client, but the error type for the wrappers is hard coded to be string, so it is difficult to send along that extra information. As far as I can tell, the change would mostly consist of replacing Either String a with Either err a, adding a corresponding type parameter to Alex a and adjusting the line

  AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
  1. Is there an easier way to do this?
  2. Am I missing some problems with this approach?

anka-213 avatar Feb 26 '21 04:02 anka-213

Yeah this looks good to me, and something I'd want too. I thought some of the wrappers might affect the error type, but no they don't, only AlexInput not `Alex.

If you make a PR for this I'd be happy to merge it!

Ericson2314 avatar Mar 19 '21 02:03 Ericson2314

I'm happy to make a PR for this but I'd be curious what we do with the line in question:

AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)

Any ideas on what would be best here?

Boarders avatar Jun 18 '21 03:06 Boarders

I'm happy to make a PR for this but I'd be curious what we do with the line in question:

AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)

Any ideas on what would be best here?

What if we did something like Megaparsec where there is a concrete error type plus an additional optional error type:

data LexError e = LexError AlexPosn String | CustomError e
runAlex :: String -> Alex a -> Either (LexError e) a

Then if you don't want a custom error you can do LexError Void.

solomon-b avatar Nov 02 '21 06:11 solomon-b

@Boarders : Still up to a PR?

andreasabel avatar Jan 25 '22 09:01 andreasabel

@andreasabel I'll start having a look in the next couple of weeks!

Boarders avatar Jan 31 '22 20:01 Boarders

Sorry I had intended to work on this but was blocked by #195

solomon-b avatar Feb 01 '22 05:02 solomon-b