haskell / alex

A lexical analyser generator for Haskell
https://hackage.haskell.org/package/alex
BSD 3-Clause "New" or "Revised" License
295 stars 82 forks source link

Allow custom error types in monad wrappers #178

Open anka-213 opened 3 years ago

anka-213 commented 3 years ago

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?
Ericson2314 commented 3 years ago

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!

Boarders commented 3 years ago

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?

solomon-b commented 2 years ago

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.

andreasabel commented 2 years ago

@Boarders : Still up to a PR?

Boarders commented 2 years ago

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

solomon-b commented 2 years ago

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

swingbit commented 3 months ago

Just to know, is this abandoned?