Skip to content

Commit 4ececfe

Browse files
committed
begin defining main exposed parsers
1 parent ea8579f commit 4ececfe

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

src/Language/Fortran/Parser.hs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,55 @@ import qualified Language.Fortran.Parser.Free.Fortran95 as F95
2121
import qualified Language.Fortran.Parser.Free.Fortran2003 as F2003
2222
import qualified Language.Fortran.Parser.Fixed.Lexer as Fixed
2323
import qualified Language.Fortran.Parser.Free.Lexer as Free
24+
import Language.Fortran.Version
25+
import Language.Fortran.Util.Position
2426

2527
import 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 ] }

src/Language/Fortran/Parser/Fixed/Lexer.x

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,8 @@ instance LastToken AlexInput Token where
887887

888888
type LexAction a = Parse AlexInput Token a
889889

890-
vanillaAlexInput :: B.ByteString -> FortranVersion -> String -> AlexInput
891-
vanillaAlexInput bs fv fn = AlexInput
890+
vanillaAlexInput :: String -> FortranVersion -> B.ByteString -> AlexInput
891+
vanillaAlexInput fn fv bs = AlexInput
892892
{ aiSourceBytes = bs
893893
, aiEndOffset = B.length bs
894894
, aiPosition = initPosition { filePath = fn }

src/Language/Fortran/Parser/Free/Lexer.x

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{-# LANGUAGE DeriveGeneric #-}
1111

1212
module Language.Fortran.Parser.Free.Lexer
13-
( lexer
13+
( lexer, Token(..), vanillaAlexInput, AlexInput, LexAction
1414
) where
1515

1616
import Prelude hiding (span)
@@ -888,8 +888,8 @@ instance LastToken AlexInput Token where
888888

889889
type LexAction a = Parse AlexInput Token a
890890

891-
vanillaAlexInput :: B.ByteString -> FortranVersion -> String -> AlexInput
892-
vanillaAlexInput bs fv fn = AlexInput
891+
vanillaAlexInput :: String -> B.ByteString -> AlexInput
892+
vanillaAlexInput fn bs = AlexInput
893893
{ aiSourceBytes = bs
894894
, aiPosition = initPosition { filePath = fn }
895895
, aiEndOffset = B.length bs

0 commit comments

Comments
 (0)