assignment = do
varname <- legal_variable_name -- defined elsewhere
_ <- char '='
exp <- expression
_ <- statementTerminator
return $ Assignment varname exp -- returns the corresponding AST node
-- a prioritized list of all possible expressions, each
-- thing in the oneOf is itself a parser expression,
-- obviously missing a lot
expression = oneOf [number, string, queryExpression, ...]
queryExpression = do
_ <- char '{'
sqlValue <- sqlLanguageSelectStatement -- defined elsewhere
_ <- char '}'
return $ QueryExpression sqlValue
although the details would vary wildly depending on the grammars in question, and as clean as this looks it's oversimplified, though parser combinators do seem to offer powerful tools for keeping your grammars clean if you use them properly and keep things well-factored just as you would any other code. (The use of the underscore is a clear way to express "I'm throwing away this value"; they are optional but I find they make my grammars more explicit. YMMV. Also if we were really going to do this there are some tweaks I'd at least consider, like explicitly terminating the sqlLanguageSelectStatement with a semicolon or something.)This really should have come up before now, so I'm assuming there's some blinding flaw in this; could someone enlighten me? (One thing that leaps to mind is that they may not be powerful enough for many cases, I don't know. I've certainly used them to implement arithmetic precedence so I know that's not an intrinsic problem but I have not pushed them to their limits.)