I was following Megaparsec documentation to parse multidimensional array.
opSubscript = Postfix $ foldr1 (.) <$> some singleIndex
singleIndex = do
index < brackets expr
return $ \l -> ArrayIndex l index
array[1][2] is expected to be parsed as
ArrayIndex (ArrayIndex (Var "array") 1)) 2
However, it was
ArrayIndex (ArrayIndex (Var "array") 2)) 1
What is going wrong here?
some singleIndexhas parsed a list containing two functions,[f, g], whereAnd then composed them with
foldr1. And sinceyour resulting function is
f . g. Naturally this appliesgfirst, yieldingArrayIndex (Var "array") 2, and then appliesfnext, yieldingArrayIndex (ArrayIndex (Var "array") 2) 1.So your parsing is fine, but you are composing the results wrong. You want a different associativity, or order or something.