patternModerate
Parsing strings with escaped characters using Parsec
Viewed 0 times
withescapedparsingusingcharactersparsecstrings
Problem
I have been working through the exercises in a Write Yourself a Scheme in 48 Hours/Parsing and hacked together something to get parseString to comprehend escaped characters. Also had some inspiration from Real World Haskell chapter 16 minus the applicative parts.
This works but I am not fond of my 'tryEscaped' definition. What are the alternatives?
parseString :: Parser LispVal
parseString = do char '"'
x noneOf "\""
escaped = choice $ map tryEscaped escapedChars
tryEscaped c = try $ char '\\' >> char (fst c) >> return (snd c)
escapedChars = zip "bnfrt\\\"/" "\b\n\f\r\t\\\"/"This works but I am not fond of my 'tryEscaped' definition. What are the alternatives?
Solution
Some suggestions:
verify that the escape codes are correctly matched with their replacements. Consider
spelling them out and aligning them to make this easy to see.
Here's what I came up with:
- Replace
fstandsndwith a pattern match or explicit function arguments.
- Extract the common
char '\\'parser. You can then avoid thetry.
- Strings with lots of escaped characters are hard to read and it's hard to visually
verify that the escape codes are correctly matched with their replacements. Consider
spelling them out and aligning them to make this easy to see.
Here's what I came up with:
escaped = char '\\' >> choice (zipWith escapedChar codes replacements)
escapedChar code replacement = char code >> return replacement
codes = ['b', 'n', 'f', 'r', 't', '\\', '\"', '/']
replacements = ['\b', '\n', '\f', '\r', '\t', '\\', '\"', '/']Code Snippets
escaped = char '\\' >> choice (zipWith escapedChar codes replacements)
escapedChar code replacement = char code >> return replacement
codes = ['b', 'n', 'f', 'r', 't', '\\', '\"', '/']
replacements = ['\b', '\n', '\f', '\r', '\t', '\\', '\"', '/']Context
StackExchange Code Review Q#2406, answer score: 11
Revisions (0)
No revisions yet.