Skip to content

Commit 418d95b

Browse files
committed
Adds support of nullable alternatives.
1 parent fa046f3 commit 418d95b

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/org/rascalmpl/parser/gtd/SGTDBF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ else if (!stack.isExpandable()) { // A 'normal' non-terminal.
13011301
expanded = true;
13021302
}
13031303

1304-
if(stack.canBeEmpty()){ // Star list, optional or empty sequence.
1304+
if(stack.canBeEmpty()){ // Star list, optional or empty sequence / alternative.
13051305
AbstractStackNode<P> empty =
13061306
stack.getEmptyChild().getCleanCopyWithResult(location, EpsilonStackNode.EPSILON_RESULT);
13071307
empty.initEdges();

src/org/rascalmpl/parser/gtd/stack/AlternativeStackNode.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class AlternativeStackNode<P> extends AbstractExpandableStackNode<P>{
2020
private final String name;
2121

2222
private final AbstractStackNode<P>[] children;
23+
private final AbstractStackNode<P> emptyChild;
2324

2425
public AlternativeStackNode(int id, int dot, P production, AbstractStackNode<P>[] alternatives){
2526
super(id, dot);
@@ -28,6 +29,7 @@ public AlternativeStackNode(int id, int dot, P production, AbstractStackNode<P>[
2829
this.name = String.valueOf(id);
2930

3031
this.children = generateAlternatives(alternatives);
32+
this.emptyChild = children.length == 0 ? generateEmptyChild() : null;
3133
}
3234

3335
public AlternativeStackNode(int id, int dot, P production, AbstractStackNode<P>[] alternatives, IEnterFilter[] enterFilters, ICompletionFilter[] completionFilters){
@@ -37,6 +39,7 @@ public AlternativeStackNode(int id, int dot, P production, AbstractStackNode<P>[
3739
this.name = String.valueOf(id);
3840

3941
this.children = generateAlternatives(alternatives);
42+
this.emptyChild = children.length == 0 ? generateEmptyChild() : null;
4043
}
4144

4245
private AlternativeStackNode(AlternativeStackNode<P> original, int startLocation){
@@ -46,6 +49,7 @@ private AlternativeStackNode(AlternativeStackNode<P> original, int startLocation
4649
name = original.name;
4750

4851
children = original.children;
52+
this.emptyChild = original.emptyChild;
4953
}
5054

5155
/**
@@ -67,6 +71,16 @@ private AbstractStackNode<P>[] generateAlternatives(AbstractStackNode<P>[] alter
6771

6872
return children;
6973
}
74+
75+
/**
76+
* Generates and initializes the empty child for usage in empty alternatives.
77+
*/
78+
@SuppressWarnings("unchecked")
79+
private AbstractStackNode<P> generateEmptyChild(){
80+
AbstractStackNode<P> empty = (AbstractStackNode<P>) EMPTY.getCleanCopy(DEFAULT_START_LOCATION);
81+
empty.setAlternativeProduction(production);
82+
return empty;
83+
}
7084

7185
public String getName(){
7286
return name;
@@ -81,11 +95,14 @@ public AbstractStackNode<P>[] getChildren(){
8195
}
8296

8397
public boolean canBeEmpty(){
84-
return false;
98+
return children.length == 0;
8599
}
86100

87101
public AbstractStackNode<P> getEmptyChild(){
88-
throw new UnsupportedOperationException();
102+
if(children.length > 0) {
103+
throw new UnsupportedOperationException();
104+
}
105+
return emptyChild;
89106
}
90107

91108
@Override
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009-2011 CWI
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
10+
* * Arnold Lankamp - [email protected]
11+
*******************************************************************************/
12+
package org.rascalmpl.test.parser;
13+
14+
import java.io.IOException;
15+
import java.io.StringReader;
16+
17+
import org.rascalmpl.parser.gtd.SGTDBF;
18+
import org.rascalmpl.parser.gtd.result.out.DefaultNodeFlattener;
19+
import org.rascalmpl.parser.gtd.stack.AbstractStackNode;
20+
import org.rascalmpl.parser.gtd.stack.AlternativeStackNode;
21+
import org.rascalmpl.parser.gtd.stack.NonTerminalStackNode;
22+
import org.rascalmpl.parser.uptr.UPTRNodeFactory;
23+
import io.usethesource.vallang.IConstructor;
24+
import io.usethesource.vallang.ISourceLocation;
25+
import io.usethesource.vallang.IValue;
26+
import io.usethesource.vallang.io.StandardTextReader;
27+
28+
import org.rascalmpl.values.RascalValueFactory;
29+
import org.rascalmpl.values.ValueFactoryFactory;
30+
import org.rascalmpl.values.parsetrees.ITree;
31+
32+
@SuppressWarnings({"unchecked", "cast"})
33+
public class Alternative2 extends SGTDBF<IConstructor, ITree, ISourceLocation> implements IParserTest{
34+
private final static IConstructor SYMBOL_START_S = VF.constructor(RascalValueFactory.Symbol_Sort, VF.string("S"));
35+
private final static IConstructor SYMBOL_ALT = VF.constructor(RascalValueFactory.Symbol_Alt, VF.set());
36+
37+
private final static IConstructor PROD_S_ALT = VF.constructor(RascalValueFactory.Production_Default, SYMBOL_START_S, VF.list(SYMBOL_ALT), VF.set());
38+
private final static IConstructor PROD_ALT = VF.constructor(RascalValueFactory.Production_Default, SYMBOL_ALT, VF.list(), VF.set());
39+
40+
private final static AbstractStackNode<IConstructor> NONTERMINAL_START_S = new NonTerminalStackNode<IConstructor>(AbstractStackNode.START_SYMBOL_ID, 0, "S");
41+
private final static AbstractStackNode<IConstructor>[] S_EXPECT_1 = (AbstractStackNode<IConstructor>[]) new AbstractStackNode[1];
42+
static{
43+
S_EXPECT_1[0] = new AlternativeStackNode<IConstructor>(4, 0, PROD_ALT, (AbstractStackNode<IConstructor>[]) new AbstractStackNode[]{});
44+
S_EXPECT_1[0].setProduction(S_EXPECT_1);
45+
S_EXPECT_1[0].setAlternativeProduction(PROD_S_ALT);
46+
}
47+
48+
public Alternative2(){
49+
super();
50+
}
51+
52+
public AbstractStackNode<IConstructor>[] S(){
53+
return new AbstractStackNode[]{S_EXPECT_1[0]};
54+
}
55+
56+
public ITree executeParser(){
57+
return parse(NONTERMINAL_START_S, null, "".toCharArray(), new DefaultNodeFlattener<IConstructor, ITree, ISourceLocation>(), new UPTRNodeFactory(true));
58+
}
59+
60+
public IValue getExpectedResult() throws IOException{
61+
String expectedInput = "appl(prod(sort(\"S\"),[alt({})],{}),[appl(prod(alt({})),[])])";
62+
return new StandardTextReader().read(ValueFactoryFactory.getValueFactory(), RascalValueFactory.uptr, RascalValueFactory.Tree, new StringReader(expectedInput));
63+
}
64+
65+
public static void main(String[] args){
66+
Alternative2 a2 = new Alternative2();
67+
IConstructor result = a2.executeParser();
68+
System.out.println(result);
69+
}
70+
}

0 commit comments

Comments
 (0)