1
- {-# LANGUAGE FlexibleContexts, FlexibleInstances, ScopedTypeVariables, OverloadedStrings #-}
1
+ {-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
2
2
{-# OPTIONS_GHC -Wno-orphans #-}
3
3
4
- module Main where
4
+ module Main ( main ) where
5
5
6
6
import Prelude hiding (readFile , mod )
7
7
import qualified Data.ByteString.Char8 as B
8
8
import qualified Data.ByteString.Lazy.Char8 as LB
9
- import Language.Fortran.Util.Files
10
9
11
10
import Text.PrettyPrint (render )
12
11
@@ -23,17 +22,19 @@ import Data.Char (toLower)
23
22
import Data.Maybe (listToMaybe , fromMaybe , maybeToList )
24
23
import Data.Data
25
24
import Data.Generics.Uniplate.Data
25
+ import Data.Graph.Inductive hiding (trc , mf , version )
26
+ import Data.Either.Combinators ( fromRight' )
26
27
27
- import Language.Fortran.Version (FortranVersion (.. ), selectFortranVersion , deduceFortranVersion )
28
- import Language.Fortran.ParserMonad (fromRight )
29
- import qualified Language.Fortran.Lexer.FixedForm as FixedForm (collectFixedTokens , Token (.. ))
30
- import qualified Language.Fortran.Lexer.FreeForm as FreeForm (collectFreeTokens , Token (.. ))
31
-
32
- import Language.Fortran.Parser.Any (parserWithModFilesVersions )
28
+ import qualified Data.IntMap as IM
29
+ import qualified Data.Map as M
30
+ import Control.Monad
31
+ import Text.Printf
33
32
33
+ import Language.Fortran.Parser
34
+ import Language.Fortran.Version
34
35
import Language.Fortran.Util.ModFile
35
36
import Language.Fortran.Util.Position
36
-
37
+ import Language.Fortran.Util.Files
37
38
import Language.Fortran.PrettyPrint
38
39
import Language.Fortran.Analysis
39
40
import Language.Fortran.AST
@@ -42,12 +43,9 @@ import Language.Fortran.Analysis.ModGraph
42
43
import Language.Fortran.Analysis.BBlocks
43
44
import Language.Fortran.Analysis.DataFlow
44
45
import Language.Fortran.Analysis.Renaming
45
- import Data.Graph.Inductive hiding (trc , mf , version )
46
-
47
- import qualified Data.IntMap as IM
48
- import qualified Data.Map as M
49
- import Control.Monad
50
- import Text.Printf
46
+ import qualified Language.Fortran.Parser as Parser
47
+ import qualified Language.Fortran.Parser.Fixed.Lexer as Fixed
48
+ import qualified Language.Fortran.Parser.Free.Lexer as Free
51
49
52
50
programName :: String
53
51
programName = " fortran-src"
@@ -104,14 +102,13 @@ main = do
104
102
mapM_ (\ p -> compileFileToMod (fortranVersion opts) mods p (outputFile opts)) paths
105
103
(path: _, actionOpt) -> do
106
104
contents <- flexReadFile path
107
- let version = fromMaybe (deduceFortranVersion path) (fortranVersion opts)
108
- let parserF0 = parserWithModFilesVersions version
109
- let parserF m b s = fromRight (parserF0 m b s)
110
- let outfmt = outputFormat opts
111
105
mods <- decodeModFiles $ includeDirs opts
112
- let mmap = combinedModuleMap mods
113
- let tenv = combinedTypeEnv mods
114
- let pvm = combinedParamVarMap mods
106
+ let version = fromMaybe (deduceFortranVersion path) (fortranVersion opts)
107
+ parsedPF = fromRight' $ (Parser. byVerWithMods mods version) path contents
108
+ outfmt = outputFormat opts
109
+ mmap = combinedModuleMap mods
110
+ tenv = combinedTypeEnv mods
111
+ pvm = combinedParamVarMap mods
115
112
116
113
let runTypes = analyseAndCheckTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
117
114
let runRenamer = stripAnalysis . rename . analyseRenamesWithModuleMap mmap . initAnalysis
@@ -129,18 +126,18 @@ main = do
129
126
, insLabel (getAnnotation b) == Just astBlockId ]
130
127
case actionOpt of
131
128
Lex | version `elem` [ Fortran66 , Fortran77 , Fortran77Extended , Fortran77Legacy ] ->
132
- print $ FixedForm. collectFixedTokens version contents
129
+ print $ Parser. collectTokens Fixed. lexer' $ initParseStateFixed " <unknown> " version contents
133
130
Lex | version `elem` [Fortran90 , Fortran2003 , Fortran2008 ] ->
134
- print $ FreeForm. collectFreeTokens version contents
131
+ print $ Parser. collectTokens Free. lexer' $ initParseStateFree " <unknown> " version contents
135
132
Lex -> ioError $ userError $ usageInfo programName options
136
- Parse -> pp $ parserF mods contents path
137
- Typecheck -> let (pf, _, errs) = runTypes (parserF mods contents path) in
133
+ Parse -> pp parsedPF
134
+ Typecheck -> let (pf, _, errs) = runTypes parsedPF in
138
135
printTypeErrors errs >> printTypes (extractTypeEnv pf)
139
- Rename -> pp . runRenamer $ parserF mods contents path
140
- BBlocks -> putStrLn . runBBlocks $ parserF mods contents path
141
- SuperGraph -> putStrLn . runSuperGraph $ parserF mods contents path
136
+ Rename -> pp $ runRenamer parsedPF
137
+ BBlocks -> putStrLn $ runBBlocks parsedPF
138
+ SuperGraph -> putStrLn $ runSuperGraph parsedPF
142
139
Reprint ->
143
- let prettyContents = render . flip (pprint version) (Just 0 ) $ parserF mods contents path
140
+ let prettyContents = render . flip (pprint version) (Just 0 ) $ parsedPF
144
141
in putStrLn $
145
142
if useContinuationReformatter opts
146
143
then reformatMixedFormInsertContinuations prettyContents
@@ -162,7 +159,7 @@ main = do
162
159
let pf = analyseParameterVars pvm .
163
160
analyseBBlocks .
164
161
analyseRenamesWithModuleMap mmap .
165
- initAnalysis $ parserF mods contents path
162
+ initAnalysis $ parsedPF
166
163
let bbm = genBBlockMap pf
167
164
case (isSuper, findBlockPU pf astBlockId) of
168
165
(False , Nothing ) -> fail " Couldn't find given AST block ID number."
@@ -178,7 +175,7 @@ main = do
178
175
ShowBlocks mlinenum -> do
179
176
let pf = analyseBBlocks .
180
177
analyseRenamesWithModuleMap mmap .
181
- initAnalysis $ parserF mods contents path
178
+ initAnalysis $ parsedPF
182
179
let f :: ([ASTBlockNode ], Int ) -> ([ASTBlockNode ], Int ) -> ([ASTBlockNode ], Int )
183
180
f (nodes1, len1) (nodes2, len2)
184
181
| len1 < len2 = (nodes1, len1)
@@ -244,13 +241,12 @@ compileFileToMod :: Maybe FortranVersion -> ModFiles -> FilePath -> Maybe FilePa
244
241
compileFileToMod mvers mods path moutfile = do
245
242
contents <- flexReadFile path
246
243
let version = fromMaybe (deduceFortranVersion path) mvers
247
- let parserF0 = parserWithModFilesVersions version
248
- let parserF m b s = fromRight (parserF0 m b s)
249
- let mmap = combinedModuleMap mods
250
- let tenv = combinedTypeEnv mods
251
- let runCompile = genModFile . fst . analyseTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
252
- let mod = runCompile $ parserF mods contents path
253
- let fspath = path -<.> modFileSuffix `fromMaybe` moutfile
244
+ mmap = combinedModuleMap mods
245
+ tenv = combinedTypeEnv mods
246
+ runCompile = genModFile . fst . analyseTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
247
+ parsedPF = fromRight' $ (Parser. byVerWithMods mods version) path contents
248
+ mod = runCompile parsedPF
249
+ fspath = path -<.> modFileSuffix `fromMaybe` moutfile
254
250
LB. writeFile fspath $ encodeModFile [mod ]
255
251
return mod
256
252
@@ -457,26 +453,26 @@ compileArgs args =
457
453
where
458
454
header = " Usage: " ++ programName ++ " [OPTION...] <file...>"
459
455
460
- instance {-# OVERLAPPING #-} Show [ FixedForm . Token ] where
456
+ instance {-# OVERLAPPING #-} Show [ Fixed . Token ] where
461
457
show = unlines . lines'
462
458
where
463
459
lines' [] = []
464
460
lines' xs =
465
461
let (x, xs') = break isNewline xs
466
462
in case xs' of
467
- (nl@ (FixedForm . TNewline _): xs'') -> (' \t ' : (intercalate " , " . map show $ x ++ [nl])) : lines' xs''
463
+ (nl@ (Fixed . TNewline _): xs'') -> (' \t ' : (intercalate " , " . map show $ x ++ [nl])) : lines' xs''
468
464
xs'' -> [ show xs'' ]
469
- isNewline (FixedForm . TNewline _) = True
465
+ isNewline (Fixed . TNewline _) = True
470
466
isNewline _ = False
471
467
472
- instance {-# OVERLAPPING #-} Show [ FreeForm . Token ] where
468
+ instance {-# OVERLAPPING #-} Show [ Free . Token ] where
473
469
show = unlines . lines'
474
470
where
475
471
lines' [] = []
476
472
lines' xs =
477
473
let (x, xs') = break isNewline xs
478
474
in case xs' of
479
- (nl@ (FreeForm . TNewline _): xs'') -> (' \t ' : (intercalate " , " . map show $ x ++ [nl])) : lines' xs''
475
+ (nl@ (Free . TNewline _): xs'') -> (' \t ' : (intercalate " , " . map show $ x ++ [nl])) : lines' xs''
480
476
xs'' -> [ show xs'' ]
481
- isNewline (FreeForm . TNewline _) = True
477
+ isNewline (Free . TNewline _) = True
482
478
isNewline _ = False
0 commit comments