| 1 | -- Compiler Toolkit: some basic definitions used all over the place |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 16 February 95 |
| 5 | -- |
| 6 | -- Version $Revision: 1.44 $ from $Date: 2000/10/05 07:51:28 $ |
| 7 | -- |
| 8 | -- Copyright (c) [1995..2000] Manuel M. T. Chakravarty |
| 9 | -- |
| 10 | -- This library is free software; you can redistribute it and/or |
| 11 | -- modify it under the terms of the GNU Library General Public |
| 12 | -- License as published by the Free Software Foundation; either |
| 13 | -- version 2 of the License, or (at your option) any later version. |
| 14 | -- |
| 15 | -- This library is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | -- Library General Public License for more details. |
| 19 | -- |
| 20 | --- DESCRIPTION --------------------------------------------------------------- |
| 21 | -- |
| 22 | -- This module provides some definitions used throughout all modules of a |
| 23 | -- compiler. |
| 24 | -- |
| 25 | --- DOCU ---------------------------------------------------------------------- |
| 26 | -- |
| 27 | -- language: Haskell 98 |
| 28 | -- |
| 29 | -- * May not import anything apart from `Config'. |
| 30 | -- |
| 31 | --- TODO ---------------------------------------------------------------------- |
| 32 | -- |
| 33 | |
| 34 | module Position ( |
| 35 | -- |
| 36 | -- source text positions |
| 37 | -- |
| 38 | Position(Position), Pos (posOf), |
| 39 | nopos, isNopos, |
| 40 | dontCarePos, isDontCarePos, |
| 41 | builtinPos, isBuiltinPos, |
| 42 | internalPos, isInternalPos, |
| 43 | incPos, tabPos, retPos, |
| 44 | ) where |
| 45 | |
| 46 | import Binary (Binary(..), putSharedString, getSharedString) |
| 47 | |
| 48 | -- uniform representation of source file positions; the order of the arguments |
| 49 | -- is important as it leads to the desired ordering of source positions |
| 50 | -- (EXPORTED) |
| 51 | -- |
| 52 | data Position = Position String -- file name |
| 53 | {-# UNPACK #-} !Int -- row |
| 54 | {-# UNPACK #-} !Int -- column |
| 55 | deriving (Eq, Ord) |
| 56 | |
| 57 | instance Show Position where |
| 58 | show (Position fname row col) = show (fname, row, col) |
| 59 | |
| 60 | -- no position (for unknown position information) (EXPORTED) |
| 61 | -- |
| 62 | nopos :: Position |
| 63 | nopos = Position "<no file>" (-1) (-1) |
| 64 | |
| 65 | isNopos :: Position -> Bool |
| 66 | isNopos (Position _ (-1) (-1)) = True |
| 67 | isNopos _ = False |
| 68 | |
| 69 | -- don't care position (to be used for invalid position information) (EXPORTED) |
| 70 | -- |
| 71 | dontCarePos :: Position |
| 72 | dontCarePos = Position "<invalid>" (-2) (-2) |
| 73 | |
| 74 | isDontCarePos :: Position -> Bool |
| 75 | isDontCarePos (Position _ (-2) (-2)) = True |
| 76 | isDontCarePos _ = False |
| 77 | |
| 78 | -- position attached to objects that are hard-coded into the toolkit (EXPORTED) |
| 79 | -- |
| 80 | builtinPos :: Position |
| 81 | builtinPos = Position "<built into the compiler>" (-3) (-3) |
| 82 | |
| 83 | isBuiltinPos :: Position -> Bool |
| 84 | isBuiltinPos (Position _ (-3) (-3)) = True |
| 85 | isBuiltinPos _ = False |
| 86 | |
| 87 | -- position used for internal errors (EXPORTED) |
| 88 | -- |
| 89 | internalPos :: Position |
| 90 | internalPos = Position "<internal error>" (-4) (-4) |
| 91 | |
| 92 | isInternalPos :: Position -> Bool |
| 93 | isInternalPos (Position _ (-4) (-4)) = True |
| 94 | isInternalPos _ = False |
| 95 | |
| 96 | -- instances of the class `Pos' are associated with some source text position |
| 97 | -- don't care position (to be used for invalid position information) (EXPORTED) |
| 98 | -- |
| 99 | class Pos a where |
| 100 | posOf :: a -> Position |
| 101 | |
| 102 | -- advance column |
| 103 | -- |
| 104 | incPos :: Position -> Int -> Position |
| 105 | incPos (Position fname row col) n = Position fname row (col + n) |
| 106 | |
| 107 | -- advance column to next tab positions (tabs are at every 8th column) |
| 108 | -- |
| 109 | tabPos :: Position -> Position |
| 110 | tabPos (Position fname row col) = |
| 111 | Position fname row (col + 8 - (col - 1) `mod` 8) |
| 112 | |
| 113 | -- advance to next line |
| 114 | -- |
| 115 | retPos :: Position -> Position |
| 116 | retPos (Position fname row col) = Position fname (row + 1) 1 |
| 117 | |
| 118 | |
| 119 | instance Binary Position where |
| 120 | put_ bh (Position fname row col) = do |
| 121 | putSharedString bh fname |
| 122 | -- put_ bh fname |
| 123 | put_ bh row |
| 124 | put_ bh col |
| 125 | get bh = do |
| 126 | fname <- getSharedString bh |
| 127 | -- aa <- get bh |
| 128 | row <- get bh |
| 129 | col <- get bh |
| 130 | return (Position fname row col) |