------------------------------------------------------------------------------
-- The ADP Compiler 
-- Copyright (C) 2001-2007 Peter Steffen, Marco Ruether, Christian Lang,
-- Georg Sauthoff
--
-- Send comments/bug reports to: P.Steffen <psteffen@techfak.uni-bielefeld.de>.
-- Updates: http://www.techfak.uni-bielefeld.de/~psteffen/
------------------------------------------------------------------------------

> module Track where

> import Tools
> import Parse
> import Syntax
> import PrettyPrint

> rev_Track =  "$Revision: 718 $"

Single-/Two-Track
------------------

> data TMode = MST | MTT   deriving (Show, Eq)
> type TModes = Assoc Nt TMode
> getTMode assoc n = lookupA1 assoc n "TMode"

> instance Pretty TMode where
>     pretty = ppTMode

> ppTMode MST  = "single track"
> ppTMode MTT  = "two track"
> ppTModes :: TModes -> String
> ppTModes = ppAssoc ppTMode
> ppTModesElem :: (Nt, TMode) -> String
> ppTModesElem = ppAssocElem ppTMode

> maxTM _ MTT = MTT
> maxTM MTT _ = MTT
> maxTM _   _ = MST 

Track mode analysis
---------------------

> tmaProds :: [String] -> [Prod] -> TModes
> tmaProds crfilter ps = (fixit tmaProd init count ps $ "Track mode analysis does not terminate!\n" ++ 
>                                                       "Probably your input grammar is incorrect.\n")
>                      ++ map (\s -> ("contains_" ++ s, MST)) crfilter
>   where
>     init  = initTM ps
>     count = length ps

>     initTM ::  [Prod] -> TModes
>     initTM [] = []
>     initTM ((n :=== (_, _, _, _)):ps) = (n,MST) : initTM ps 

>     tmaProd :: TModes -> Prod -> (Nt, TMode)
>     tmaProd tmodes (n :=== (_, _, _, u)) = (n, tma_u tmodes u)

> tma_u :: TModes -> Unit -> TMode
> tma_u tmodes (Nonterminal nt)    = tm_nt
>    where  tm_nt = getTMode tmodes nt 
> tma_u tmodes (TTUnit  _ _)          = MTT
> tma_u params x                      = wrkUnit (tma_u params) maxTM MST x

extract maximal track mode from TModes:
------------------------------------------

> maxTrackMode :: TModes -> TMode
> maxTrackMode [] = MST
> maxTrackMode ((_,mode):ps) = maxTM mode (maxTrackMode ps)


