
Hi, I'm using parsec to parse something which is either a "name" or a "type". The particular test string I'm using is a type, but isn't a name. I want things to default to "name" before "type". Some examples of the parsec function, and the result when applied to a test string:
parsecQuery = do spaces ; types Right Query {scope = [], names = [], typeSig = Just [a], items = [], flags = []}
parsecQuery = do spaces ; names Left (line 1, column 1): unexpected "[" expecting white space, "--", "/", "(", letter, "::" or end of input
parsecQuery = do spaces ; try names <|> types Left (line 1, column 1): unexpected "[" expecting white space, "--", "/", "(", letter, "::" or end of input
I would have expected try names <|> types to return a Right (since types matches), but it doesn't. I assumed this would be a property of Parsec, but it doesn't appear to hold. Can anyone shed any light? Thanks Neil

Neil Mitchell schrieb:
Hi,
I'm using parsec to parse something which is either a "name" or a "type". The particular test string I'm using is a type, but isn't a name. I want things to default to "name" before "type".
Some examples of the parsec function, and the result when applied to a test string:
parsecQuery = do spaces ; types Right Query {scope = [], names = [], typeSig = Just [a], items = [], flags = []}
parsecQuery = do spaces ; names Left (line 1, column 1): unexpected "[" expecting white space, "--", "/", "(", letter, "::" or end of input
parsecQuery = do spaces ; try names <|> types Left (line 1, column 1): unexpected "[" expecting white space, "--", "/", "(", letter, "::" or end of input
I would have expected try names <|> types to return a Right (since types matches), but it doesn't. I assumed this would be a property of Parsec, but it doesn't appear to hold. Can anyone shed any light?
I suppose "names" or "try names" succeeds without consuming input, but calling parsecQuery fails for another reason that you haven't shown, C.

Hi
I suppose "names" or "try names" succeeds without consuming input, but calling parsecQuery fails for another reason that you haven't shown,
I assume (from the docs) that "try names" doesn't consume input (the try is meant to take care of that). I also know that "spaces ; types" works on its own, so what other reason is there? The code is at: http://www.cs.york.ac.uk/fp/darcs/hoogle/src/Hoogle/Query/Parser.hs Darcs repo at: http://www.cs.york.ac.uk/fp/darcs/hoogle/ And the test case that is failing is: parseQuery "[a]" Thanks Neil

Neil Mitchell wrote:
The code is at:
http://www.cs.york.ac.uk/fp/darcs/hoogle/src/Hoogle/Query/Parser.hs
My guess: names can never fail, so types is never tried, and eof fails. Tillmann

Hi Tillmann
http://www.cs.york.ac.uk/fp/darcs/hoogle/src/Hoogle/Query/Parser.hs
My guess: names can never fail, so types is never tried, and eof fails.
You are correct. Thanks very much! Neil

Neil Mitchell schrieb:
I assume (from the docs) that "try names" doesn't consume input (the try is meant to take care of that).
"try names" does not consume input when names fails, but it may also not consume input when names succeeds on the empty input. In that (latter) case the other alternative (type) is not tried. HTH Christian

On 06/06/07, Christian Maeder
"try names" does not consume input when names fails, but it may also not consume input when names succeeds on the empty input. In that (latter) case the other alternative (type) is not tried.
But in that case it'd be successful and return Right. -- -David House, dmhouse@gmail.com

Neil Mitchell wrote:
Hi,
I'm using parsec to parse something which is either a "name" or a "type". The particular test string I'm using is a type, but isn't a name. I want things to default to "name" before "type".
I just finished a parsec grammar for C99, and found this very useful while bringing it up: mtrace_enabled = False mtrace n p = if mtrace_enabled then trace ("-> trying to match: " ++ n) $ choice [do {a <- p; return (trace ("<- matched: " ++ n ++ "(" ++ show a ++ ")") a)}, fail ("<- failed to match " ++ n)] else p Usage: expression = mtrace "expression" (do {a <- chainl1 assignment_expression comma_op; return a}) Oh, and hello-and-delurk :) - Derek
participants (5)
-
Christian Maeder
-
David House
-
Derek Gladding
-
Neil Mitchell
-
Tillmann Rendel