Skip to content

Commit b78b6cb

Browse files
committed
restructure parsers, various refactoring
1 parent a240060 commit b78b6cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1084
-1436
lines changed

src/Main.hs renamed to app/Main.hs

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
{-# LANGUAGE FlexibleContexts, FlexibleInstances, ScopedTypeVariables, OverloadedStrings #-}
1+
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
22
{-# OPTIONS_GHC -Wno-orphans #-}
33

4-
module Main where
4+
module Main ( main ) where
55

66
import Prelude hiding (readFile, mod)
77
import qualified Data.ByteString.Char8 as B
88
import qualified Data.ByteString.Lazy.Char8 as LB
9-
import Language.Fortran.Util.Files
109

1110
import Text.PrettyPrint (render)
1211

@@ -23,17 +22,19 @@ import Data.Char (toLower)
2322
import Data.Maybe (listToMaybe, fromMaybe, maybeToList)
2423
import Data.Data
2524
import Data.Generics.Uniplate.Data
25+
import Data.Graph.Inductive hiding (trc, mf, version)
26+
import Data.Either.Combinators ( fromRight' )
2627

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
3332

33+
import Language.Fortran.Parser
34+
import Language.Fortran.Version
3435
import Language.Fortran.Util.ModFile
3536
import Language.Fortran.Util.Position
36-
37+
import Language.Fortran.Util.Files
3738
import Language.Fortran.PrettyPrint
3839
import Language.Fortran.Analysis
3940
import Language.Fortran.AST
@@ -42,12 +43,9 @@ import Language.Fortran.Analysis.ModGraph
4243
import Language.Fortran.Analysis.BBlocks
4344
import Language.Fortran.Analysis.DataFlow
4445
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
5149

5250
programName :: String
5351
programName = "fortran-src"
@@ -104,14 +102,13 @@ main = do
104102
mapM_ (\ p -> compileFileToMod (fortranVersion opts) mods p (outputFile opts)) paths
105103
(path:_, actionOpt) -> do
106104
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
111105
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
115112

116113
let runTypes = analyseAndCheckTypesWithEnv tenv . analyseRenamesWithModuleMap mmap . initAnalysis
117114
let runRenamer = stripAnalysis . rename . analyseRenamesWithModuleMap mmap . initAnalysis
@@ -129,18 +126,18 @@ main = do
129126
, insLabel (getAnnotation b) == Just astBlockId ]
130127
case actionOpt of
131128
Lex | version `elem` [ Fortran66, Fortran77, Fortran77Extended, Fortran77Legacy ] ->
132-
print $ FixedForm.collectFixedTokens version contents
129+
print $ Parser.collectTokens Fixed.lexer' $ initParseStateFixed "<unknown>" version contents
133130
Lex | version `elem` [Fortran90, Fortran2003, Fortran2008] ->
134-
print $ FreeForm.collectFreeTokens version contents
131+
print $ Parser.collectTokens Free.lexer' $ initParseStateFree "<unknown>" version contents
135132
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
138135
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
142139
Reprint ->
143-
let prettyContents = render . flip (pprint version) (Just 0) $ parserF mods contents path
140+
let prettyContents = render . flip (pprint version) (Just 0) $ parsedPF
144141
in putStrLn $
145142
if useContinuationReformatter opts
146143
then reformatMixedFormInsertContinuations prettyContents
@@ -162,7 +159,7 @@ main = do
162159
let pf = analyseParameterVars pvm .
163160
analyseBBlocks .
164161
analyseRenamesWithModuleMap mmap .
165-
initAnalysis $ parserF mods contents path
162+
initAnalysis $ parsedPF
166163
let bbm = genBBlockMap pf
167164
case (isSuper, findBlockPU pf astBlockId) of
168165
(False, Nothing) -> fail "Couldn't find given AST block ID number."
@@ -178,7 +175,7 @@ main = do
178175
ShowBlocks mlinenum -> do
179176
let pf = analyseBBlocks .
180177
analyseRenamesWithModuleMap mmap .
181-
initAnalysis $ parserF mods contents path
178+
initAnalysis $ parsedPF
182179
let f :: ([ASTBlockNode], Int) -> ([ASTBlockNode], Int) -> ([ASTBlockNode], Int)
183180
f (nodes1, len1) (nodes2, len2)
184181
| len1 < len2 = (nodes1, len1)
@@ -244,13 +241,12 @@ compileFileToMod :: Maybe FortranVersion -> ModFiles -> FilePath -> Maybe FilePa
244241
compileFileToMod mvers mods path moutfile = do
245242
contents <- flexReadFile path
246243
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
254250
LB.writeFile fspath $ encodeModFile [mod]
255251
return mod
256252

@@ -457,26 +453,26 @@ compileArgs args =
457453
where
458454
header = "Usage: " ++ programName ++ " [OPTION...] <file...>"
459455

460-
instance {-# OVERLAPPING #-} Show [ FixedForm.Token ] where
456+
instance {-# OVERLAPPING #-} Show [ Fixed.Token ] where
461457
show = unlines . lines'
462458
where
463459
lines' [] = []
464460
lines' xs =
465461
let (x, xs') = break isNewline xs
466462
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''
468464
xs'' -> [ show xs'' ]
469-
isNewline (FixedForm.TNewline _) = True
465+
isNewline (Fixed.TNewline _) = True
470466
isNewline _ = False
471467

472-
instance {-# OVERLAPPING #-} Show [ FreeForm.Token ] where
468+
instance {-# OVERLAPPING #-} Show [ Free.Token ] where
473469
show = unlines . lines'
474470
where
475471
lines' [] = []
476472
lines' xs =
477473
let (x, xs') = break isNewline xs
478474
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''
480476
xs'' -> [ show xs'' ]
481-
isNewline (FreeForm.TNewline _) = True
477+
isNewline (Free.TNewline _) = True
482478
isNewline _ = False

fortran-src.cabal

+101-38
Original file line numberDiff line numberDiff line change
@@ -62,46 +62,67 @@ source-repository head
6262

6363
library
6464
exposed-modules:
65-
Language.Fortran.Analysis.SemanticTypes
6665
Language.Fortran.Analysis
67-
Language.Fortran.Analysis.Renaming
68-
Language.Fortran.Analysis.ModGraph
69-
Language.Fortran.Analysis.Types
7066
Language.Fortran.Analysis.BBlocks
7167
Language.Fortran.Analysis.DataFlow
68+
Language.Fortran.Analysis.ModGraph
69+
Language.Fortran.Analysis.Renaming
70+
Language.Fortran.Analysis.SemanticTypes
71+
Language.Fortran.Analysis.Types
7272
Language.Fortran.AST
7373
Language.Fortran.AST.AList
74-
Language.Fortran.AST.RealLit
7574
Language.Fortran.AST.Boz
76-
Language.Fortran.Version
77-
Language.Fortran.LValue
75+
Language.Fortran.AST.RealLit
7876
Language.Fortran.Intrinsics
79-
Language.Fortran.Lexer.FixedForm
80-
Language.Fortran.Lexer.FixedForm.Utils
81-
Language.Fortran.Lexer.FreeForm
82-
Language.Fortran.ParserMonad
83-
Language.Fortran.Parser.Any
84-
Language.Fortran.Parser.Fortran66
85-
Language.Fortran.Parser.Fortran77
86-
Language.Fortran.Parser.Fortran90
87-
Language.Fortran.Parser.Fortran95
88-
Language.Fortran.Parser.Fortran2003
89-
Language.Fortran.Parser.Utils
77+
Language.Fortran.LValue
78+
Language.Fortran.Parser
79+
Language.Fortran.Parser.Fixed.Fortran66
80+
Language.Fortran.Parser.Fixed.Fortran77
81+
Language.Fortran.Parser.Fixed.Lexer
82+
Language.Fortran.Parser.Fixed.Utils
83+
Language.Fortran.Parser.Free.Fortran2003
84+
Language.Fortran.Parser.Free.Fortran90
85+
Language.Fortran.Parser.Free.Fortran95
86+
Language.Fortran.Parser.Free.Lexer
87+
Language.Fortran.Parser.Free.Utils
88+
Language.Fortran.Parser.LexerUtils
89+
Language.Fortran.Parser.Monad
9090
Language.Fortran.PrettyPrint
91+
Language.Fortran.Rewriter
92+
Language.Fortran.Rewriter.Internal
9193
Language.Fortran.Transformation.Disambiguation.Function
9294
Language.Fortran.Transformation.Disambiguation.Intrinsic
9395
Language.Fortran.Transformation.Grouping
94-
Language.Fortran.Transformation.TransformMonad
95-
Language.Fortran.Transformer
96-
Language.Fortran.Util.Position
96+
Language.Fortran.Transformation.Monad
97+
Language.Fortran.Util.Files
9798
Language.Fortran.Util.FirstParameter
98-
Language.Fortran.Util.SecondParameter
9999
Language.Fortran.Util.ModFile
100-
Language.Fortran.Util.Files
101-
Language.Fortran.Rewriter
102-
Language.Fortran.Rewriter.Internal
100+
Language.Fortran.Util.Position
101+
Language.Fortran.Util.SecondParameter
102+
Language.Fortran.Version
103+
other-modules:
104+
Paths_fortran_src
103105
hs-source-dirs:
104106
src
107+
default-extensions:
108+
EmptyCase
109+
FlexibleContexts
110+
FlexibleInstances
111+
InstanceSigs
112+
MultiParamTypeClasses
113+
PolyKinds
114+
LambdaCase
115+
DerivingStrategies
116+
StandaloneDeriving
117+
DeriveAnyClass
118+
DeriveGeneric
119+
DeriveDataTypeable
120+
DeriveFunctor
121+
DeriveFoldable
122+
DeriveTraversable
123+
DeriveLift
124+
BangPatterns
125+
TupleSections
105126
ghc-options: -Wall -fno-warn-tabs
106127
build-tools:
107128
alex >=3.1
@@ -115,6 +136,7 @@ library
115136
, containers >=0.5 && <0.7
116137
, deepseq ==1.4.*
117138
, directory >=1.2 && <2
139+
, either
118140
, fgl ==5.*
119141
, filepath ==1.4.*
120142
, mtl >=2.2 && <3
@@ -125,9 +147,30 @@ library
125147
default-language: Haskell2010
126148

127149
executable fortran-src
128-
main-is: src/Main.hs
150+
main-is: Main.hs
129151
other-modules:
130152
Paths_fortran_src
153+
hs-source-dirs:
154+
app
155+
default-extensions:
156+
EmptyCase
157+
FlexibleContexts
158+
FlexibleInstances
159+
InstanceSigs
160+
MultiParamTypeClasses
161+
PolyKinds
162+
LambdaCase
163+
DerivingStrategies
164+
StandaloneDeriving
165+
DeriveAnyClass
166+
DeriveGeneric
167+
DeriveDataTypeable
168+
DeriveFunctor
169+
DeriveFoldable
170+
DeriveTraversable
171+
DeriveLift
172+
BangPatterns
173+
TupleSections
131174
ghc-options: -Wall -fno-warn-tabs
132175
build-depends:
133176
GenericPretty >=1.2.2 && <2
@@ -138,6 +181,7 @@ executable fortran-src
138181
, containers >=0.5 && <0.7
139182
, deepseq ==1.4.*
140183
, directory >=1.2 && <2
184+
, either
141185
, fgl ==5.*
142186
, filepath ==1.4.*
143187
, fortran-src
@@ -160,18 +204,17 @@ test-suite spec
160204
Language.Fortran.AnalysisSpec
161205
Language.Fortran.AST.BozSpec
162206
Language.Fortran.AST.RealLitSpec
163-
Language.Fortran.Lexer.FixedFormSpec
164-
Language.Fortran.Lexer.FreeFormSpec
165-
Language.Fortran.Parser.Fortran2003Spec
166-
Language.Fortran.Parser.Fortran2008Spec
167-
Language.Fortran.Parser.Fortran66Spec
168-
Language.Fortran.Parser.Fortran77.IncludeSpec
169-
Language.Fortran.Parser.Fortran77.ParserSpec
170-
Language.Fortran.Parser.Fortran90Spec
171-
Language.Fortran.Parser.Fortran95Spec
172-
Language.Fortran.Parser.FreeFormCommon
173-
Language.Fortran.Parser.UtilsSpec
174-
Language.Fortran.ParserMonadSpec
207+
Language.Fortran.Parser.Fixed.Fortran66Spec
208+
Language.Fortran.Parser.Fixed.Fortran77.IncludeSpec
209+
Language.Fortran.Parser.Fixed.Fortran77.ParserSpec
210+
Language.Fortran.Parser.Fixed.LexerSpec
211+
Language.Fortran.Parser.Free.Common
212+
Language.Fortran.Parser.Free.Fortran2003Spec
213+
Language.Fortran.Parser.Free.Fortran2008Spec
214+
Language.Fortran.Parser.Free.Fortran90Spec
215+
Language.Fortran.Parser.Free.Fortran95Spec
216+
Language.Fortran.Parser.Free.LexerSpec
217+
Language.Fortran.Parser.MonadSpec
175218
Language.Fortran.PrettyPrintSpec
176219
Language.Fortran.Rewriter.InternalSpec
177220
Language.Fortran.RewriterSpec
@@ -183,6 +226,25 @@ test-suite spec
183226
Paths_fortran_src
184227
hs-source-dirs:
185228
test
229+
default-extensions:
230+
EmptyCase
231+
FlexibleContexts
232+
FlexibleInstances
233+
InstanceSigs
234+
MultiParamTypeClasses
235+
PolyKinds
236+
LambdaCase
237+
DerivingStrategies
238+
StandaloneDeriving
239+
DeriveAnyClass
240+
DeriveGeneric
241+
DeriveDataTypeable
242+
DeriveFunctor
243+
DeriveFoldable
244+
DeriveTraversable
245+
DeriveLift
246+
BangPatterns
247+
TupleSections
186248
ghc-options: -Wall
187249
build-tool-depends:
188250
hspec-discover:hspec-discover
@@ -196,6 +258,7 @@ test-suite spec
196258
, containers >=0.5 && <0.7
197259
, deepseq ==1.4.*
198260
, directory >=1.2 && <2
261+
, either
199262
, fgl ==5.*
200263
, filepath ==1.4.*
201264
, fortran-src

0 commit comments

Comments
 (0)