{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreigg24qhekkn4qrzvsge3fmwcxqqhns3bddeao73546zlrc6sc5i5a",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mlbplgm7krv2"
},
"path": "/t/how-to-parse-specific-syntax-elements-and-discard-the-rest/14047#post_2",
"publishedAt": "2026-05-07T15:47:26.000Z",
"site": "https://discourse.haskell.org",
"textContent": "What you’re describing sounds very similar to the `find-all-matches` mode of regular expression drivers.\n\nYou can, of course, recreate this with parser combinators.\n\n\n findAll :: Parser a -> Text -> [(a, Word)]\n findAll parser = go 0\n where\n go offset input = case runParser parser input of\n Left _ -> go (succ offset) (Text.drop 1 input)\n Right (result, rest) -> result : go (offset + Text.length input - Text.length rest) rest\n\n\nAssuming, it’s impossible to mistake your token for another token in the target language if context is missing. E.g. it would be impossible to exclusively find all variable names in Rust code because they overlap with Type Identifiers and more.\n\nEdit:\nEfficiency is always questionable when using parser combinators.\nYou are probably a lot better off hand-writing some pattern-matching/parsing code over the input string if performance starts mattering (or does already).",
"title": "How to parse specific syntax elements and discard the rest?"
}