0 | module Text.Parser.Expression
12 | = Prefix (Grammar state k True (a -> a))
13 | | Postfix (Grammar state k True (a -> a))
14 | | Infix (Grammar state k True (a -> a -> a)) Assoc
17 | OperatorTable : Type -> Type -> Type -> Type
18 | OperatorTable state k a = List (List (Op state k a))
21 | buildExpressionParser :
22 | OperatorTable state k a ->
23 | Grammar state k True a ->
24 | Grammar state k True a
25 | buildExpressionParser table term = foldl level term table
27 | level : Grammar state k True a -> List (Op state k a) -> Grammar state k True a
28 | level factor ops = parseThese <|> factor
30 | 0 BinOp, UnOp : Type
31 | BinOp = Grammar state k True (a -> a -> a)
32 | UnOp = Grammar state k True (a -> a)
35 | SortedOps = (List BinOp, List BinOp, List BinOp, List UnOp, List UnOp)
37 | separate : Op state k a -> SortedOps -> SortedOps
38 | separate op (lassoc, rassoc, nassoc, pre, post) = case op of
39 | Infix p AssocLeft => (p::lassoc, rassoc, nassoc, pre, post)
40 | Infix p AssocRight => (lassoc, p::rassoc, nassoc, pre, post)
41 | Infix p AssocNone => (lassoc, rassoc, p::nassoc, pre, post)
42 | Prefix p => (lassoc, rassoc, nassoc, p::pre, post)
43 | Postfix p => (lassoc, rassoc, nassoc, pre, p::post)
45 | sortedOps : SortedOps
46 | sortedOps = foldr separate ([], [], [], [], []) ops
48 | parseThese : Grammar state k True a
50 | let (lassoc, rassoc, nassoc, pre, post) = sortedOps
52 | termP : Grammar state k True a
53 | prefixP : Grammar state k False (a -> a)
54 | postfixP : Grammar state k False (a -> a)
61 | prefixP = choice pre <|> pure id
62 | postfixP = choice post <|> pure id
64 | rassocP : a -> Grammar state k True a
65 | rassocP1 : a -> Grammar state k False a
68 | y <- termP >>= rassocP1
70 | rassocP1 x = rassocP x <|> pure x
72 | lassocP : a -> Grammar state k True a
73 | lassocP1 : a -> Grammar state k False a
78 | lassocP1 x = lassocP x <|> pure x
80 | nassocP : a -> Grammar state k True a
88 | rassocP x <|> lassocP x <|> nassocP x <|> pure x