diff --git a/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ModuleBuilderTest.java b/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ModuleBuilderTest.java index 2103cb282..43e5a62d9 100644 --- a/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ModuleBuilderTest.java +++ b/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ModuleBuilderTest.java @@ -119,7 +119,36 @@ public void testGroupDataDeclarationConstructors() { assertSame(cons, dataDecl.getConstructors()[0]); } - private IConstructor createConstructor() { + public void testDistributeConstructorsBetweenDataDeclarations() { + final IConstructor fstCons = createConstructor(); + final IConstructor sndCons = createConstructor(); + + fBuilder.startModule(); + fBuilder.startDataDeclaration(); + fBuilder.addConstructor(fstCons); + + fBuilder.startDataDeclaration(); + fBuilder.addConstructor(sndCons); + + final IModule module = fBuilder.getResult(); + + final IDeclaration[] decls = module.getDeclarations(); + assertEquals(2, decls.length); + assertTrue(decls[0] instanceof IDataDeclaration); + assertTrue(decls[1] instanceof IDataDeclaration); + + IDataDeclaration fstDataDecl = (IDataDeclaration) decls[0]; + IConstructor[] fstConstructorGroup = fstDataDecl.getConstructors(); + assertEquals(1, fstConstructorGroup.length); + assertSame(fstCons, fstConstructorGroup[0]); + + IDataDeclaration sndDataDecl = (IDataDeclaration) decls[1]; + IConstructor[] sndConstructorGroup = sndDataDecl.getConstructors(); + assertEquals(1, sndConstructorGroup.length); + assertSame(sndCons, sndConstructorGroup[0]); + } + + private IConstructor createConstructor() { return new Constructor(); } diff --git a/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ParserIntegrationTest.java b/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ParserIntegrationTest.java index da1c0632a..0e9145f17 100644 --- a/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ParserIntegrationTest.java +++ b/net.sf.eclipsefp.haskell.core.jparser.test/src/net/sf/eclipsefp/haskell/core/jparser/test/ParserIntegrationTest.java @@ -528,6 +528,32 @@ public void testDataLocationRecording() throws RecognitionException, TokenStream assertEquals(3, srcLoc.getLine()); assertEquals(2, srcLoc.getColumn()); } + + public void testConsecutiveADTDeclarations() throws RecognitionException, TokenStreamException { + final String input = "module Nature where\n" + + "\n" + + "data Gender = Male | Female\n" + + "data Temperature = Cold | Warm | Hot"; + IModule module = parse(input); + + IDeclaration[] decls = module.getDeclarations(); + assertEquals(2, decls.length); + assertTrue(decls[0] instanceof IDataDeclaration); + assertTrue(decls[1] instanceof IDataDeclaration); + + IDataDeclaration fstDataDecl = (IDataDeclaration) decls[0]; + IConstructor[] fstConstructorGroup = fstDataDecl.getConstructors(); + assertEquals(2, fstConstructorGroup.length); + assertEquals("Male", fstConstructorGroup[0].getName()); + assertEquals("Female", fstConstructorGroup[1].getName()); + + IDataDeclaration sndDataDecl = (IDataDeclaration) decls[1]; + IConstructor[] sndConstructorGroup = sndDataDecl.getConstructors(); + assertEquals(3, sndConstructorGroup.length); + assertEquals("Cold", sndConstructorGroup[0].getName()); + assertEquals("Warm", sndConstructorGroup[1].getName()); + assertEquals("Hot", sndConstructorGroup[2].getName()); + } public void testNewtypeLocationRecording() throws RecognitionException, TokenStreamException { // sample code from darcs source code diff --git a/net.sf.eclipsefp.haskell.core.jparser/antlr-src/haskell-parser.g b/net.sf.eclipsefp.haskell.core.jparser/antlr-src/haskell-parser.g index b29b10344..d2d11ac4f 100644 --- a/net.sf.eclipsefp.haskell.core.jparser/antlr-src/haskell-parser.g +++ b/net.sf.eclipsefp.haskell.core.jparser/antlr-src/haskell-parser.g @@ -393,12 +393,12 @@ constrs constr { - Constructor aConstructor = createNode(Constructor.class); - fBuilder.addConstructor(aConstructor); - + Constructor aConstructor = null; String name = null; } : + { aConstructor = createNode(Constructor.class); + fBuilder.addConstructor(aConstructor); } name=con { aConstructor.setName(name); } (block | ~(SEMICOLON|ALT|RIGHT_CURLY))* ; diff --git a/net.sf.eclipsefp.haskell.core.jparser/src/net/sf/eclipsefp/haskell/core/jparser/NullDataDeclaration.java b/net.sf.eclipsefp.haskell.core.jparser/src/net/sf/eclipsefp/haskell/core/jparser/NullDataDeclaration.java index 98cd8da5c..d36e9f9df 100644 --- a/net.sf.eclipsefp.haskell.core.jparser/src/net/sf/eclipsefp/haskell/core/jparser/NullDataDeclaration.java +++ b/net.sf.eclipsefp.haskell.core.jparser/src/net/sf/eclipsefp/haskell/core/jparser/NullDataDeclaration.java @@ -1,7 +1,13 @@ package net.sf.eclipsefp.haskell.core.jparser; +import de.leiffrenzel.fp.haskell.core.halamo.IConstructor; import net.sf.eclipsefp.haskell.core.jparser.ast.DataDeclaration; class NullDataDeclaration extends DataDeclaration { + @Override + public boolean accepts(IConstructor cons) { + return false; + } + }