Parsec permutation parser with separators
I want to parse assembly programs. I have a fixed format for parsing an
assembly address: [ register + offset + label ] I implemented parsers for
registers, offsets and labels. Now I want to create a parser which parses
the whole address.
The combinations I want to accept:
[register]
[offset]
[label]
[register + offset]
[register + label]
[offset + label]
[register + offset + label]
And what I don't want to accept:
[]
[register offset]
[register + ]
...
Of course the simple solution is to have something like:
choice $ try (parseRegister >>= \r -> Address (Just r) Nothing Nothing)
<|> try ...
But it is ugly and does not scale well with more types of elements. So I'm
looking for a cleaner solution.
No comments:
Post a Comment