Skip to content

Commit

Permalink
Parse nested structure
Browse files Browse the repository at this point in the history
Adds
* parsing rules for StructStructures
* name to StructStructure used for data reference notation
* test for parsing a nested structure
  • Loading branch information
Raoul Hidalgo Charman authored and mrd committed Aug 19, 2019
1 parent e278e82 commit cb0cb91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Language/Fortran/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ data DataGroup a =
data StructureItem a =
StructFields a SrcSpan (TypeSpec a) (Maybe (AList Attribute a)) (AList Declarator a)
| StructUnion a SrcSpan (AList UnionMap a)
| StructStructure a SrcSpan (Maybe String) (AList StructureItem a)
| StructStructure a SrcSpan (Maybe String) String (AList StructureItem a)
deriving (Eq, Show, Data, Typeable, Generic, Functor)

data UnionMap a =
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Fortran/Parser/Fortran77.y
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ STRUCTURE_DECLARATION_STATEMENT
in StructFields () s t attrs decls }
| union NEWLINE UNION_MAPS endunion NEWLINE
{ StructUnion () (getTransSpan $1 $5) (fromReverseList $3) }
| structure MAYBE_NAME NAME NEWLINE STRUCTURE_DECLARATIONS endstructure NEWLINE
{ StructStructure () (getTransSpan $1 $7) $2 $3 (fromReverseList $5) }

UNION_MAPS :: { [ UnionMap A0 ] }
UNION_MAPS
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Fortran/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ instance IndentablePretty (StructureItem a) where
"union" <> newline <>
foldl' (\doc item -> doc <> pprint v item (incIndentation i) <> newline) empty (aStrip maps) <>
"end union"
pprint v (StructStructure a s mName items) _ = pprint' v (StStructure a s mName items)
pprint v (StructStructure a s mName _ items) _ = pprint' v (StStructure a s mName items)

instance IndentablePretty (UnionMap a) where
pprint v (UnionMap _ _ items) i =
Expand Down
14 changes: 14 additions & 0 deletions test/Language/Fortran/Parser/Fortran77/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,20 @@ spec =
st = StStructure () u (Just "foo") $ AList () u [StructUnion () u $ AList () u ds]
resetSrcSpan (slParser src) `shouldBe` st

it "parses nested structure blocks" $ do
let src = init
$ unlines [ " structure /foo/"
, " structure /bar/ baz"
, " integer qux"
, " end structure"
, " end structure"]
var = DeclVariable () u (varGen "qux") Nothing Nothing
innerst = StructStructure () u (Just "bar") ("baz")
$ AList () u [StructFields () u (TypeSpec () u TypeInteger Nothing) Nothing
$ AList () u [var]]
st = StStructure () u (Just "foo") $ AList () u [innerst]
resetSrcSpan (slParser src) `shouldBe` st

it "parses character declarations with unspecfied lengths" $ do
let src = " character s*(*)"
st = StDeclaration () u (TypeSpec () u (TypeCharacter Nothing Nothing) Nothing) Nothing $
Expand Down

0 comments on commit cb0cb91

Please sign in to comment.