Moved the functions for converting between Meta and SourcePos (from Parsec) into a common ParseUtils module
This commit is contained in:
parent
fd2109d548
commit
d92e042159
|
@ -27,7 +27,6 @@ import qualified Data.Map as Map
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Debug.Trace
|
import Debug.Trace
|
||||||
import Text.ParserCombinators.Parsec
|
import Text.ParserCombinators.Parsec
|
||||||
import Text.ParserCombinators.Parsec.Pos (newPos)
|
|
||||||
|
|
||||||
import qualified AST as A
|
import qualified AST as A
|
||||||
import CompState
|
import CompState
|
||||||
|
@ -37,6 +36,7 @@ import EvalLiterals
|
||||||
import Intrinsics
|
import Intrinsics
|
||||||
import LexOccam
|
import LexOccam
|
||||||
import Metadata
|
import Metadata
|
||||||
|
import ParseUtils
|
||||||
import Pass
|
import Pass
|
||||||
import ShowCode
|
import ShowCode
|
||||||
import Types
|
import Types
|
||||||
|
@ -177,28 +177,6 @@ md
|
||||||
= do pos <- getPosition
|
= do pos <- getPosition
|
||||||
return $ sourcePosToMeta pos
|
return $ sourcePosToMeta pos
|
||||||
|
|
||||||
--{{{ Meta to/from SourcePos
|
|
||||||
-- | Convert source position into Parsec's format.
|
|
||||||
metaToSourcePos :: Meta -> SourcePos
|
|
||||||
metaToSourcePos meta
|
|
||||||
= newPos filename (metaLine meta) (metaColumn meta)
|
|
||||||
where
|
|
||||||
filename = case metaFile meta of
|
|
||||||
Just s -> s
|
|
||||||
Nothing -> ""
|
|
||||||
|
|
||||||
-- | Convert source position out of Parsec's format.
|
|
||||||
sourcePosToMeta :: SourcePos -> Meta
|
|
||||||
sourcePosToMeta pos
|
|
||||||
= emptyMeta {
|
|
||||||
metaFile = case sourceName pos of
|
|
||||||
"" -> Nothing
|
|
||||||
s -> Just s,
|
|
||||||
metaLine = sourceLine pos,
|
|
||||||
metaColumn = sourceColumn pos
|
|
||||||
}
|
|
||||||
--}}}
|
|
||||||
|
|
||||||
--{{{ try*
|
--{{{ try*
|
||||||
-- These functions let you try a sequence of productions and only retrieve the
|
-- These functions let you try a sequence of productions and only retrieve the
|
||||||
-- results from some of them. In the function name, X represents a value
|
-- results from some of them. In the function name, X represents a value
|
||||||
|
|
|
@ -35,7 +35,6 @@ import Debug.Trace
|
||||||
import qualified IO
|
import qualified IO
|
||||||
import Numeric (readHex)
|
import Numeric (readHex)
|
||||||
import Text.ParserCombinators.Parsec
|
import Text.ParserCombinators.Parsec
|
||||||
import Text.ParserCombinators.Parsec.Pos (newPos)
|
|
||||||
import Text.Regex
|
import Text.Regex
|
||||||
|
|
||||||
import qualified AST as A
|
import qualified AST as A
|
||||||
|
@ -45,6 +44,7 @@ import EvalConstants
|
||||||
import EvalLiterals
|
import EvalLiterals
|
||||||
import Intrinsics
|
import Intrinsics
|
||||||
import Metadata
|
import Metadata
|
||||||
|
import ParseUtils
|
||||||
import Pass
|
import Pass
|
||||||
import Types
|
import Types
|
||||||
import Utils
|
import Utils
|
||||||
|
@ -113,11 +113,8 @@ monadicArithOp
|
||||||
|
|
||||||
--}}}
|
--}}}
|
||||||
|
|
||||||
metaToPos :: Meta -> SourcePos
|
|
||||||
metaToPos m = newPos (fromMaybe "" $ metaFile m) (metaLine m) (metaColumn m)
|
|
||||||
|
|
||||||
getToken :: (L.TokenType -> Maybe x) -> RainParser (Meta, x)
|
getToken :: (L.TokenType -> Maybe x) -> RainParser (Meta, x)
|
||||||
getToken test = token (show) (metaToPos . fst) (wrap test)
|
getToken test = token (show) (metaToSourcePos . fst) (wrap test)
|
||||||
where
|
where
|
||||||
wrap :: (L.TokenType -> Maybe x) -> (Meta,L.TokenType) -> Maybe (Meta,x)
|
wrap :: (L.TokenType -> Maybe x) -> (Meta,L.TokenType) -> Maybe (Meta,x)
|
||||||
wrap f (m,t) = case f t of
|
wrap f (m,t) = case f t of
|
||||||
|
|
47
frontends/ParseUtils.hs
Normal file
47
frontends/ParseUtils.hs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
{-
|
||||||
|
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/>.
|
||||||
|
-}
|
||||||
|
|
||||||
|
module ParseUtils where
|
||||||
|
|
||||||
|
import Text.ParserCombinators.Parsec
|
||||||
|
import Text.ParserCombinators.Parsec.Pos (newPos)
|
||||||
|
import Metadata
|
||||||
|
|
||||||
|
--{{{ Meta to/from SourcePos
|
||||||
|
-- | Convert source position into Parsec's format.
|
||||||
|
metaToSourcePos :: Meta -> SourcePos
|
||||||
|
metaToSourcePos meta
|
||||||
|
= newPos filename (metaLine meta) (metaColumn meta)
|
||||||
|
where
|
||||||
|
filename = case metaFile meta of
|
||||||
|
Just s -> s
|
||||||
|
Nothing -> ""
|
||||||
|
|
||||||
|
-- | Convert source position out of Parsec's format.
|
||||||
|
sourcePosToMeta :: SourcePos -> Meta
|
||||||
|
sourcePosToMeta pos
|
||||||
|
= emptyMeta {
|
||||||
|
metaFile = case sourceName pos of
|
||||||
|
"" -> Nothing
|
||||||
|
s -> Just s,
|
||||||
|
metaLine = sourceLine pos,
|
||||||
|
metaColumn = sourceColumn pos
|
||||||
|
}
|
||||||
|
--}}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user