never executed always true always false
1 {-# LANGUAGE DataKinds #-}
2 module Parse.Binop (binops) where
3
4 import Text.Parsec ((<|>), choice, try)
5
6 import AST.V0_16
7 import AST.Structure (FixAST)
8 import Data.Coapplicative
9 import qualified Data.Indexed as I
10 import Parse.Helpers (commitIf, addLocation, multilineToBool)
11 import Parse.IParser
12 import Parse.Whitespace
13 import Reporting.Annotation (Located)
14
15
16 binops
17 :: IParser (FixAST Located typeRef ctorRef varRef 'ExpressionNK)
18 -> IParser (FixAST Located typeRef ctorRef varRef 'ExpressionNK)
19 -> IParser varRef
20 -> IParser (FixAST Located typeRef ctorRef varRef 'ExpressionNK)
21 binops term last anyOp =
22 fmap I.Fix $ addLocation $
23 do ((e, ops), multiline) <- trackNewline ((,) <$> term <*> nextOps)
24 return $
25 case ops of
26 [] ->
27 extract $ I.unFix e
28 _ ->
29 Binops e ops $ multilineToBool multiline
30 where
31 nextOps =
32 choice
33 [ commitIf (whitespace >> anyOp) $
34 do preOpComments <- whitespace
35 op <- anyOp
36 preExpressionComments <- whitespace
37 expr <- Left <$> try term <|> Right <$> last
38 case expr of
39 Left t -> (:) (BinopsClause preOpComments op preExpressionComments t) <$> nextOps
40 Right e -> return [BinopsClause preOpComments op preExpressionComments e]
41 , return []
42 ]