Earley.hs revision 2bbcdec13d8fd4b862cea292617cba1dca78f513
c63ebf815c8a874525cf18670ad74847f7fc7b26Christian MaederModule : $Header$
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian MaederCopyright : Christian Maeder and Uni Bremen 2003
54ed6a6b1a6c7d27fadb39ec5b59d0806c81f7c8Christian MaederLicence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : hets@tzi.de
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiStability : experimental
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiPortability : portable
c092fcac4b8f5c524c22ca579189c4487c13edf7Christian Maeder generic mixfix analysis
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder -- * special tokens for special ids
dda5ab793f1615c1ba1dcaa97a4346b0878da6b1Christian Maeder varTok, exprTok, typeTok
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder , applId, parenId, typeId, exprId, varId
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder , tupleId, unitId, unknownId, isUnknownId, unToken
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder , Knowns, mkId, protect, listRules, mixRule
0799b5dc3f06d2640e66e9ab54b8b217348fd719Christian Maeder , getTokenPlaceList
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder -- * resolution chart
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder , Chart, mixDiags, ToExpr
43b4c41fbb07705c9df321221ab9cb9832460407Christian Maeder , initChart, nextChart, getResolved)
b10d6cef708b7a659f2d3b367e8e0db0d03ae3f5Till Mossakowskiimport qualified Common.Lib.Set as Set
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maederimport qualified Common.Lib.Map as Map
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- import Control.Exception (assert)
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian Maeder-- import Debug.Trace(trace)
c0c2380bced8159ff0297ece14eba948bd236471Christian Maederassert :: Bool -> a -> a
da333ffa6336cf59a4071fcddad358c5eafd3e61Sonja Gröningassert b a = if b then a else error ("assert")
6352f3c31da3043783a13be6594aacb2147378baRazvan Pascanu-- | reconstruct the token list of an 'Id'.
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross-- Replace top-level places with the input String
fc1a590cd3ee36797c0a032ff41e07f8e2469341Christian MaedergetTokenList :: String -> Id -> [Token]
a2b04db3e156312a8596d8084f7f0f51acf8a96bChristian MaedergetTokenList placeStr (Id ts cs ps) =
0c885f1348fd58f7cb706472a3ff20b52dbef0a7Jonathan von Schroeder let convert = map (\ t -> if isPlace t then t {tokStr = placeStr} else t)
fc1a590cd3ee36797c0a032ff41e07f8e2469341Christian Maeder in if null cs then convert ts else
66a774f13272fde036481edd2298081ab3d04678Razvan Pascanu let (toks, pls) = splitMixToken ts in
834c2e71b8e390e5b05c8d02bb6eb22621125133Markus Gross convert toks ++ getCompoundTokenList cs ps ++ convert pls
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder-- | update token positions.
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder-- return remaining positions
6e52f1dfc0da4bc4a7701cf856641c9dce08fc7dChristian MaedersetToksPos :: [Token] -> [Pos] -> ([Token], [Pos])
923e25bb8c7cf9f2978c7844ad173704482cc3b0Martin KühlsetToksPos (h:ts) (p:ps) =
9f85afecbd79b3df5a0bb17bd28cd0b288dc3213Kristina Sojakova let (rt, rp) = setToksPos ts ps
63da71bfb4226f504944b293fb77177ebcaea7d4Ewaryst Schulz in (h {tokPos = p} : rt, rp)
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian MaedersetToksPos ts ps = (ts, ps)
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder-- | update positions in 'Id'.
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder-- return remaining positions
57026bc09337d158b89775048a9bcc9c17d825caChristian MaedersetPlainIdePos :: Id -> [Pos] -> (Id, [Pos])
9175e29c044318498a40f323f189f9dfd50378efChristian MaedersetPlainIdePos (Id ts cs _) ps =
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian Maeder if null cs then
923e25bb8c7cf9f2978c7844ad173704482cc3b0Martin Kühl let (newTs, restPs) = setToksPos ts ps
f66fcd981f556c238df7dd6dfa42123745e3b1d2Christian Maeder in (Id newTs cs [], restPs)
2b873214c9ab511bbca437c036371ab664aedaceChristian Maeder else let (toks, pls) = splitMixToken ts
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder ttail l = if null l then l else tail l
fe495a0978e5aa70776103c37fb0eb2bd6abea69Eugen Kuksa (front, ps2) = setToksPos toks ps
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder (newCs, ps3, ps4) = foldl ( \ (prevCs, seps, restPs) a ->
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder let (c1, qs) = setPlainIdePos a restPs
d27b1887e61f1dc53d77c37f59dbf5019242a686Christian Maeder in (c1: prevCs, head qs : seps, ttail qs))
52d922076b89f12234f721974e82531bc69a6f69Christian Maeder ([], [head ps2], ttail ps2) cs
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross (newPls, ps7) = setToksPos pls ps4
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus Gross in (Id (front ++ newPls) (reverse newCs) (reverse ps3), ps7)
31bc219bae758272d0f064281b8ce7740a4553e9Till Mossakowski-- | a special index type for more type safety
31bc219bae758272d0f064281b8ce7740a4553e9Till Mossakowskinewtype Index = Index Int deriving (Eq, Ord, Show)
31bc219bae758272d0f064281b8ce7740a4553e9Till Mossakowski-- deriving Num is also possible
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maeder-- but the following functions are sufficient
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maeder-- | the initial index
a31430de8b0632d29f42634d6395e982bf31b14dChristian MaederstartIndex :: Index
a31430de8b0632d29f42634d6395e982bf31b14dChristian MaederstartIndex = Index 0
a31430de8b0632d29f42634d6395e982bf31b14dChristian MaederincrIndex :: Index -> Index
a31430de8b0632d29f42634d6395e982bf31b14dChristian MaederincrIndex (Index i) = Index (i + 1)
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maederdata Item a b = Item
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maeder { rule :: Id -- the rule to match
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maeder , info :: b -- additional info for 'rule'
a31430de8b0632d29f42634d6395e982bf31b14dChristian Maeder , posList :: [Pos] -- positions of Id tokens
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder , args :: [a] -- currently collected arguments
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder -- both in reverse order
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder , ambigArgs :: [[a]] -- field for ambiguities
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder , ambigs :: [[a]] -- field for ambiguities
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder , rest :: [Token] -- part of the rule after the "dot"
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder , index :: Index -- index into the Table/input string
91eeff7b19b22d7e5c5d83fa6e357496e291c718Christian Maederinstance Show (Item a b) where
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder showsPrec _ p =
f63e7684d8db7503c22e5d8d499c94a9405f8f9eChristian Maeder let d = rest p
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian Maeder v = getPlainTokenList (rule p)
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder first = take (length v - length d) v
bdc103981a28a51938de98a956d8a3767f6cf43dAivaras Jakubauskas showToks = showSepList id showTok
c0c2380bced8159ff0297ece14eba948bd236471Christian Maeder Index i = index p
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian Maeder in showChar '['. showToks first
e1ea9a046e9640148ca876dfe47e391559a9fdf3Christian Maeder . showChar '.'
57026bc09337d158b89775048a9bcc9c17d825caChristian Maeder . showString ", "
22b772f8753f0cdb4508ba460356c238de2ee375Jonathan von Schroeder . shows i . showChar ']'
fa388aea9cef5f9734fec346159899a74432ce26Christian Maeder-- | the non-terminal
63719301448519453f66383f4e583d9fd5b89ecbChristian MaedertermStr :: String
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian MaedertermStr = "(__)"
0c885f1348fd58f7cb706472a3ff20b52dbef0a7Jonathan von Schroeder-- | builtin terminals
52d922076b89f12234f721974e82531bc69a6f69Christian MaedercommaTok, termTok, oParenTok, cParenTok, placeTok :: Token
52d922076b89f12234f721974e82531bc69a6f69Christian MaedercommaTok = mkSimpleId "," -- for list elements
923e25bb8c7cf9f2978c7844ad173704482cc3b0Martin KühltermTok = mkSimpleId termStr
9f85afecbd79b3df5a0bb17bd28cd0b288dc3213Kristina SojakovaplaceTok = mkSimpleId place
72079df98b3cb7cc1fd82a0a24984893dcd05ecaEwaryst SchulzoParenTok = mkSimpleId "("
a3a7d8b3cdf05c8040c62dbcf9a15dc5042cd721Christian MaedercParenTok = mkSimpleId ")"
fbc4f8708092d571a45cb483f37cc6b674da45a7Christian MaederlistTok :: Token
8a77240a809197c92c0736c431b4b88947a7bac1Christian MaederlistTok = mkSimpleId "[]" -- impossible token
8a77240a809197c92c0736c431b4b88947a7bac1Christian MaederprotectTok :: Token
1c4dfa148603d4fcf4cdd2ed66c8b6e1de0dd696Till MossakowskiprotectTok = mkSimpleId "()" -- impossible token
b0234f0a84fcd3587073fbc11d38759108997c3cChristian Maeder-- | token for type annotations
b0234f0a84fcd3587073fbc11d38759108997c3cChristian MaedertypeTok :: Token
8ef91a173e69219fc2ebd45c76a35891c7785abdMarkus GrosstypeTok = mkSimpleId ":"
d56ece59c372cb887355825901222b9f3377f7e6Thiemo Wiedemeyer-- | token for a fixed (or recursively resolved) operator expression
d56ece59c372cb887355825901222b9f3377f7e6Thiemo WiedemeyerexprTok :: Token
9175e29c044318498a40f323f189f9dfd50378efChristian MaederexprTok = mkSimpleId "(op )"
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- | token for a fixed (or recursively resolved) argument expression
9175e29c044318498a40f323f189f9dfd50378efChristian MaedervarTok :: Token
f1dec6898638ba1131a9fadbc4d1544c93dfabb0Klaus LuettichvarTok = mkSimpleId "(var )"
9175e29c044318498a40f323f189f9dfd50378efChristian Maeder-- | token for an unknown variable (within patterns)
f77f29e84b3f6e791c82e61b13fbf76582bedd2fChristian MaederunknownTok :: Token
) $ Set.toList lists
type Table a b = Map.Map Index [Item a b]
lookUp ce k = Map.findWithDefault [] k ce
type Knowns = Set.Set String
else if Set.isEmpty ks then []
&& not (tokStr t `Set.member` ks) then
nextTable = Map.insert idx items table
initChart ruleS knownS= Chart { prevTable = Map.empty