
ErrorReport is of type (Maybe Meta, String), thereby adding an optional code position to error messages. Die has been changed so that die and dieP are now implemented in terms of dieReport (:: ErrorReport -> m a). This involved changing less code than changing die to be of type ErrorReport -> m a. All that had to be changed directly was that Die instances now implement dieReport instead of die. Any bits of code that "caught" errors has been changed so that it handles ErrorReport instead of String. This ErrorReport is eventually, in Main, passed to dieIO, which will soon be changed to read the file in and provide the context. Accordingly, MonadIO m has been added as a constraint to dieIO, and dieInternal has been changed to no longer use dieIO (because really we can't add the MonadIO constraint to dieInternal). Various error messages have been changed. Notably, all instances of fail in ParseOccam have been changed to use die or, wherever possible, dieP. A similar thing has been done in EvalConstants and EvalLiterals.
86 lines
2.5 KiB
Haskell
86 lines
2.5 KiB
Haskell
{-
|
|
Tock: a compiler for parallel languages
|
|
Copyright (C) 2007 University of Kent
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU General Public License as published by the
|
|
Free Software Foundation, either version 2 of the License, or (at your
|
|
option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
-}
|
|
|
|
-- | Metadata -- i.e. source position.
|
|
module Metadata where
|
|
|
|
{-! global : Haskell2Xml !-}
|
|
|
|
import Data.Generics
|
|
import Text.Printf
|
|
import Text.Read
|
|
|
|
import Utils
|
|
|
|
data Meta = Meta {
|
|
metaFile :: Maybe String,
|
|
metaLine :: Int,
|
|
metaColumn :: Int
|
|
}
|
|
deriving (Typeable, Data)
|
|
|
|
emptyMeta :: Meta
|
|
emptyMeta = Meta {
|
|
metaFile = Nothing,
|
|
metaLine = 0,
|
|
metaColumn = 0
|
|
}
|
|
|
|
instance Show Meta where
|
|
show m =
|
|
case metaFile m of
|
|
Just s -> s ++ ":" ++ show (metaLine m) ++ ":" ++ show (metaColumn m)
|
|
Nothing -> "no source position"
|
|
|
|
--emptyMeta is equal to any meta tag:
|
|
instance Eq Meta where
|
|
(==) a b =
|
|
if ((metaFile a == Nothing) && (metaLine a == 0) && (metaColumn a == 0)) then True else
|
|
if ((metaFile b == Nothing) && (metaLine b == 0) && (metaColumn b == 0)) then True else
|
|
((metaFile a == metaFile b) && (metaLine a == metaLine b) && (metaColumn a == metaColumn b))
|
|
|
|
-- | Encode a Meta as the prefix of a string.
|
|
packMeta :: Meta -> String -> String
|
|
packMeta m s
|
|
= case metaFile m of
|
|
Nothing -> s
|
|
Just fn -> printf "~%d\0%d\0%s\0%s"
|
|
(metaLine m) (metaColumn m) fn s
|
|
|
|
-- | Extract a Meta (encoded by packMeta) from a String.
|
|
unpackMeta :: String -> (Maybe Meta, String)
|
|
unpackMeta ('~':s) = (Just m, rest)
|
|
where
|
|
(ls, _:s') = break (== '\0') s
|
|
(cs, _:s'') = break (== '\0') s'
|
|
(fn, _:rest) = break (== '\0') s''
|
|
m = emptyMeta {
|
|
metaFile = Just fn,
|
|
metaLine = read ls,
|
|
metaColumn = read cs
|
|
}
|
|
unpackMeta s = (Nothing, s)
|
|
|
|
-- | Find the first Meta value in some part of the AST.
|
|
findMeta :: (Data t, Typeable t) => t -> Meta
|
|
findMeta e = if null metaList then emptyMeta else head metaList
|
|
where
|
|
metaList = gmapQ (mkQ emptyMeta findMeta') e
|
|
findMeta' :: Meta -> Meta
|
|
findMeta' m = m
|