MixfixParser.hs revision 75a6279dbae159d018ef812185416cf6df386c10
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederModule : $Header$
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederCopyright : Christian Maeder and Uni Bremen 2002-2003
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian MaederLicence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
ffd01020a4f35f434b912844ad6e0d6918fadffdChristian MaederMaintainer : hets@tzi.de
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederStability : experimental
66267bcb678a9c341272c323b299337bcfdb7cc5Christian MaederPortability : portable
fb69cd512eab767747f109e40322df7cae2f7bdfChristian Maeder Mixfix analysis of terms
fb69cd512eab767747f109e40322df7cae2f7bdfChristian Maeder Missing features:
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder - the positions of ids from string, list, number and floating annotations
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder is not changed within applications (and might be misleading)
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maedermodule CASL.MixfixParser ( resolveFormula, resolveMixfix)
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maederimport qualified Common.Lib.Set as Set
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- import Control.Exception (assert)
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maederassert :: Bool -> a -> a
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maederassert b a = if b then a else error ("assert")
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maedertype Rule = (Id, Bool, [Token]) -- True means predicate
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermkRule :: Id -> Rule
024621f43239cfe9629e35d35a8669fad7acbba2Christian MaedermkRule = mixRule False
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermkSingleArgRule :: Bool -> Id -> Rule
36c6cc568751e4235502cfee00ba7b597dae78dcChristian MaedermkSingleArgRule b ide = (protect ide, b, getPlainTokenList ide ++ [varTok])
27912d626bf179b82fcb337077e5cd9653bb71cfChristian MaedermkSingleOpArgRule :: Bool -> Id -> Rule
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermkSingleOpArgRule b ide = (protect ide, b, getPlainTokenList ide ++ [exprTok])
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaedermkArgsRule :: Bool -> Id -> Rule
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaedermkArgsRule b ide = (protect ide, b, getPlainTokenList ide
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder ++ getTokenPlaceList tupleId)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersingleArgId, singleOpArgId, multiArgsId :: Id
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersingleArgId = mkId (getPlainTokenList exprId ++ [varTok])
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedersingleOpArgId = mkId (getPlainTokenList exprId ++ [exprTok])
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaedermultiArgsId = mkId (getPlainTokenList exprId ++
07b1bf56f3a486f26d69514d05b73100abb25a0eChristian Maeder getPlainTokenList tupleId)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinitRules :: GlobalAnnos -> IdSet -> Bool -> [Rule]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederinitRules ga (opS, predS, _) maybeFormula =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder preds = if maybeFormula then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in concat [ mkRule typeId :
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder mkRule exprId :
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder mkRule varId :
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder mkRule singleArgId :
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder mkRule singleOpArgId :
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder mkRule multiArgsId :
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder listRules False ga,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mixRule True) preds,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkSingleArgRule True) preds,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkSingleOpArgRule True) preds,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkArgsRule True) preds,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map mkRule ops,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkSingleArgRule False) ops,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkSingleOpArgRule False) ops,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder map (mkArgsRule False) ops]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | meaningful position of a term
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederposOfTerm :: TERM -> Pos
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederposOfTerm trm =
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder Mixfix_token t -> tokPos t
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder Mixfix_term ts -> posOfTerm (head ts)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder Simple_id i -> tokPos i
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_qual_pred p ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Pred_name i -> posOfId i
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Qual_pred_name _ _ ps -> first (Just ps)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Application o [] [] ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Op_name i -> posOfId i
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Qual_op_name _ _ ps -> first (Just ps)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> first $ get_pos_l trm
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder where first ps = case ps of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Nothing -> nullPos
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Just l -> if null l then nullPos else head l
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder-- | construct application
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaederasAppl :: Id -> [TERM] -> [Pos] -> TERM
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederasAppl f as ps = Application (Op_name f) as ps
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian Maeder-- | constructing the parse tree from (the final) parser state(s)
5c933f3c61d2cfa7e76e4eb610a4b0bac988be47Christian MaedertoAppl :: Id -> Bool -> [TERM] -> [Pos] -> TERM
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedertoAppl ide _ ar qs =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if ide == singleArgId || ide == multiArgsId
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder then assert (length ar > 1) $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let har:tar = ar
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder ps = posOfTerm har : qs
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in case har of
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Application q ts _ -> assert (null ts) $
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Application q tar ps
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_qual_pred _ -> Mixfix_term [har,
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_parenthesized tar ps]
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> error "stateToAppl"
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder else asAppl ide ar qs
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maedertype IdSet = (Set.Set Id, Set.Set Id, Set.Set Id)
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaederaddType :: TERM -> TERM -> TERM
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaederaddType tt t =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_sorted_term s ps -> Sorted_term t s ps
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder Mixfix_cast s ps -> Cast t s ps
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian Maeder _ -> error "addType"
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaederfilterByPredicate :: Bool -> Bool -> Maybe Bool
8aea46773664711e0910accc5cf80ef9ee1bcfbfChristian MaederfilterByPredicate bArg bOp =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if bArg then Just False else
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder if bOp then Just True else Nothing
61091743da1a9ed6dfd5e077fdcc972553358962Christian Maedertype TermChart = Chart TERM Bool
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederiterateCharts :: GlobalAnnos -> IdSet -> Bool -> [TERM] -> TermChart
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian MaederiterateCharts g ids maybeFormula terms c =
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder let self = iterateCharts g ids maybeFormula
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder expand = expandPos Mixfix_token
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder oneStep = nextChart addType filterByPredicate toAppl g c
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder resolveTerm = resolveMixTrm g ids False
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in if null terms then c
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder else case head terms of
5d7e4bf173534e7eb3fc84dce7bb0151079d3f8aChristian Maeder Mixfix_term ts -> self (ts ++ tail terms) c
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_bracketed ts ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder self (expand ("[", "]") ts ps ++ tail terms) c
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_braced ts ps ->
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder self (expand ("{", "}") ts ps ++ tail terms) c
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder Mixfix_parenthesized ts ps ->
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder if isSingle ts then
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let Result mds v = resolveMixTrm g ids maybeFormula
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder tNew = case v of Nothing -> head ts
717686b54b9650402e2ebfbaadf433eab8ba5171Christian Maeder c2 = self (tail terms) (oneStep (tNew, varTok))
405b95208385572f491e1e0207d8d14e31022fa6Christian Maeder in mixDiags mds c2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder else self (expand ("(", ")") ts ps ++ tail terms) c
8c81b727b788d90ff3b8cbda7b0900c9009243bbChristian Maeder Conditional t1 f2 t3 ps ->
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder let Result mds v =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder do t4 <- resolveTerm t1
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder f5 <- resolveMixFrm g ids f2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder t6 <- resolveTerm t3
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder return (Conditional t4 f5 t6 ps)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder tNew = case v of Nothing -> head terms
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder c2 = self (tail terms)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (oneStep (tNew, varTok {tokPos = posOfTerm tNew}))
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in mixDiags mds c2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder Mixfix_token t -> let (ds1, trm) =
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder convertMixfixToken (literal_annos g) t
05e2a3161e4589a717c6fe5c7306820273a473c5Christian Maeder c2 = self (tail terms) $ oneStep $
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Mixfix_token tok -> (trm, tok)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder _ -> (trm, varTok
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder {tokPos = tokPos t})
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder in mixDiags ds1 c2
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder t@(Mixfix_sorted_term _ (p:_)) -> self (tail terms)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (oneStep (t, typeTok {tokPos = p}))
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder t@(Mixfix_cast _ (p:_)) -> self (tail terms)
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian Maeder (oneStep (t, typeTok {tokPos = p}))
341d00318de2d0ea9b6f0ab43f7e4d10ee4fb454Christian Maeder t@(Qual_var _ _ (p:_)) -> self (tail terms)
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder (oneStep (t, varTok {tokPos = p}))
5a13581acc5a76d392c1dec01657bb3efd4dcf2dChristian Maeder t@(Application (Qual_op_name _ _ (p:_)) _ _) ->
let both = Set.intersection ops preds in
(ops, Set.difference preds both, preds)
initChart (initRules ga ids maybeFormula) Set.empty
let varIds = Set.fromList $ concatMap (\ (Var_decl va _ _) ->
newIds = (Set.union ops varIds,
if ide `Set.member` preds