From d92e04215949b7827dd291c89683ef5d79b58c25 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Mon, 17 Sep 2007 11:15:17 +0000 Subject: [PATCH] Moved the functions for converting between Meta and SourcePos (from Parsec) into a common ParseUtils module --- frontends/ParseOccam.hs | 24 +-------------------- frontends/ParseRain.hs | 7 ++---- frontends/ParseUtils.hs | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 frontends/ParseUtils.hs diff --git a/frontends/ParseOccam.hs b/frontends/ParseOccam.hs index 8465823..457a590 100644 --- a/frontends/ParseOccam.hs +++ b/frontends/ParseOccam.hs @@ -27,7 +27,6 @@ import qualified Data.Map as Map import Data.Maybe import Debug.Trace import Text.ParserCombinators.Parsec -import Text.ParserCombinators.Parsec.Pos (newPos) import qualified AST as A import CompState @@ -37,6 +36,7 @@ import EvalLiterals import Intrinsics import LexOccam import Metadata +import ParseUtils import Pass import ShowCode import Types @@ -177,28 +177,6 @@ md = do pos <- getPosition 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* -- 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 diff --git a/frontends/ParseRain.hs b/frontends/ParseRain.hs index f8bef95..a6d7c94 100644 --- a/frontends/ParseRain.hs +++ b/frontends/ParseRain.hs @@ -35,7 +35,6 @@ import Debug.Trace import qualified IO import Numeric (readHex) import Text.ParserCombinators.Parsec -import Text.ParserCombinators.Parsec.Pos (newPos) import Text.Regex import qualified AST as A @@ -45,6 +44,7 @@ import EvalConstants import EvalLiterals import Intrinsics import Metadata +import ParseUtils import Pass import Types 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 test = token (show) (metaToPos . fst) (wrap test) +getToken test = token (show) (metaToSourcePos . fst) (wrap test) where wrap :: (L.TokenType -> Maybe x) -> (Meta,L.TokenType) -> Maybe (Meta,x) wrap f (m,t) = case f t of diff --git a/frontends/ParseUtils.hs b/frontends/ParseUtils.hs new file mode 100644 index 0000000..21d905c --- /dev/null +++ b/frontends/ParseUtils.hs @@ -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 . +-} + +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 + } +--}}} + +