@@ -21,34 +21,55 @@ import qualified Language.Fortran.Parser.Free.Fortran95 as F95
2121import qualified Language.Fortran.Parser.Free.Fortran2003 as F2003
2222import qualified Language.Fortran.Parser.Fixed.Lexer as Fixed
2323import qualified Language.Fortran.Parser.Free.Lexer as Free
24+ import Language.Fortran.Version
25+ import Language.Fortran.Util.Position
2426
2527import qualified Data.ByteString.Char8 as B
2628
2729-- | Our common Fortran parser type takes a filename and input, and returns
2830-- either a normalized error (tokens are printed) or an untransformed
2931-- 'ProgramFile'.
30- type Parser = String -> B. ByteString -> Either ParseErrorSimple (ProgramFile A0 )
31-
32- {-
33-
34- f66ProgramFile :: Parser
35- f66ProgramFile fn bs = fromParseResult . (F66.programParser :: _)
36-
37- --fpr = fromParseResult
38-
39- initParseState :: B.ByteString -> FortranVersion -> String -> ParseState AlexInput
40- initParseState srcBytes fortranVersion filename =
41- _vanillaParseState { psAlexInput = _vanillaAlexInput }
42- where
43- _vanillaParseState = ParseState
44- { psAlexInput = undefined
45- , psVersion = fortranVersion
46- , psFilename = filename
47- , psParanthesesCount = ParanthesesCount 0 False
48- , psContext = [ ConStart ] }
49- _vanillaAlexInput = vanillaAlexInput
50- { aiSourceBytes = srcBytes
51- , aiEndOffset = B.length srcBytes
52- , aiPosition = initPosition {filePath = filename} }
32+ type Parser a = String -> B. ByteString -> Either ParseErrorSimple a
33+ type StateInit s = String -> FortranVersion -> B. ByteString -> ParseState s
5334
54- -}
35+ f66ProgramFile :: Parser (ProgramFile A0 )
36+ f66ProgramFile = makeParserFixed F66. programParser Fortran66
37+
38+ f66Statement :: Parser (Statement A0 )
39+ f66Statement = makeParserFixed F66. statementParser Fortran66
40+
41+ makeParser
42+ :: (Loc s , LastToken s a , Show a )
43+ => StateInit s -> Parse s a b -> FortranVersion -> Parser b
44+ makeParser fInitState p fv fn bs = fromParseResult $ runParse p $ fInitState fn fv bs
45+
46+ makeParserFixed
47+ :: Parse Fixed. AlexInput Fixed. Token b -> FortranVersion -> Parser b
48+ makeParserFixed = makeParser initParseStateFixed
49+
50+ makeParserFree
51+ :: Parse Free. AlexInput Free. Token b -> FortranVersion -> Parser b
52+ makeParserFree = makeParser initParseStateFree
53+
54+ initParseStateFixed :: StateInit Fixed. AlexInput
55+ initParseStateFixed fn fv bs = initParseStatePartial
56+ { psAlexInput = Fixed. vanillaAlexInput fn fv bs
57+ , psVersion = fv
58+ , psFilename = fn }
59+
60+ initParseStateFree :: StateInit Free. AlexInput
61+ initParseStateFree fn fv bs = initParseStatePartial
62+ { psAlexInput = Free. vanillaAlexInput fn bs
63+ , psVersion = fv
64+ , psFilename = fn }
65+
66+ --------------------------------------------------------------------------------
67+
68+ -- | Helper for preparing initial parser state for the different lexers.
69+ initParseStatePartial :: ParseState a
70+ initParseStatePartial = ParseState
71+ { psAlexInput = undefined
72+ , psVersion = undefined
73+ , psFilename = undefined
74+ , psParanthesesCount = ParanthesesCount 0 False
75+ , psContext = [ ConStart ] }
0 commit comments