Source

xilocaml / Position.hs

Full commit
module Position where
import AbsXilocaml


data Position = At Int Int | Somewhere deriving (Eq, Ord, Show)


class Positionable t where
	position :: t -> Position

instance Positionable Exp where
	position exp = case exp of
		ELetIn loc exp1 -> min (position loc) (position exp1)
		ELetRecIn loc exp1 -> min (position loc) (position exp1)
		ELambda args exp1 -> min (position args) (position exp1)
		EIfThenElse exp1 exp2 exp3 -> min (position exp1) $ min (position exp2) (position exp3)
		EOr exp1 exp2 -> min (position exp1) (position exp2)
		EAnd exp1 exp2 -> min (position exp1) (position exp2)
		ECmp exp1 _ exp2 -> min (position exp1) (position exp2)
		EPrepend exp1 exp2 -> min (position exp1) (position exp2)
		EInfixWeak exp1 _ exp2 -> min (position exp1) (position exp2)
		EInfixStrong exp1 _ exp2 -> min (position exp1) (position exp2)
		EUnary _ exp1 -> position exp1
		ENot exp1 -> position exp1
		EHead exp1 -> position exp1
		ETail exp1 -> position exp1
		EAppl exp1 exp2 -> min (position exp1) (position exp2)
		EInt (OInteger ((l,c), _)) -> At l c
		EFloat (OFloat ((l,c), _)) -> At l c
		EBool (OBool ((l,c), _)) -> At l c
		EList elems -> position elems
		EVar (OIdent ((l,c), _)) -> At l c
		EExplType exp1 _ -> position exp1

instance Positionable t => Positionable [t] where
	position [] = Somewhere
	position (x:xs) = foldl (\acc e -> min acc $ position e) (position x) xs

instance Positionable LocDecl where
	position loc = case loc of
		LocalVarDecl (OIdent ((l,c), _)) _ -> At l c
		LocalVarDeclExplType (OIdent ((l,c), _)) _ _ -> At l c

instance Positionable Argument where
	position arg = case arg of
		FunArg (OIdent ((l,c), _)) -> At l c
		FunArgExplType (OIdent ((l,c), _)) _ -> At l c

instance Positionable ListElem where
	position (ListElement exp) = position exp