I’m writing a simple HTTP server and I have code like this to parse request string
val method :: url :: version :: rest = tokenize(requestString)
if (rest != Nil)
throw new IncorrectRequestException("Extra data after HTTP version")
The tokenize() function returns a list of strings which is matched against a list of variables. That works just fine.
However, I want to add nice error reporting. For example, if HTTP version is missing I want to throw the IncorrectRequestException(“HTTP version is missing”) instead of simple MatchError exception.
Is there any good way to achieve this, short of checking the length of the result of tokenize() call?
Combinator Parsers are probably the most powerful technique you could introduce here. You might also try getting an
Iteratorfrom the list, and making successivenextcalls to retrieve the values.