I’d like to write a parser for hashtags. I have been reading the blog
entries on parsing on the opa blog, but they didn’t cover recursive
parsers and constructions of lists a lot.
Hashtags are used by some social networks (Twitter, Diaspora*)
to tag a post. They consist of a hash sign (#) and an alphanumeric
string such as “interesting” or “funny”. One example of a post using
hashtags:
Oh #Opa, you're so #lovely! (Are you a relative of #Haskell?)
Parsing that would result in ["Opa", "lovely", "Haskell"].
I have tried to do it, but it doesn’t quite what I want. (It could
either only parse one hashtag and nothing else, would fail in an endless
loop or fail because there was input it didn’t understand…)
Additionally, here is a Haskell version that implements it.
To begin with a remark: by posing question in Haskell-terms you’re effectively looking for somebody who knows Opa and Haskell hence decreasing chances of finding a person to answer the question ;). Ok, I’m saying it half jokingly as your comments help a lot but still I’d rather see the question phrased in plain English.
I think a solution keeping the structure of the Haskell one would be something like this:
Probably the main ‘trick’ is to use the
Rule.parse_list_sepfunction to parse a list. I suggest you take a look at the implementation of some functions in theRulemodule to get inspiration and learn more about parsing in Opa.Of course I suggest testing this function, for instance with the following code:
which will give the following output:
I suspect that you will need to fine tune this solution to really conform to what you want but it should give you a good head start (I hope).