Skip to content

Commit

Permalink
Fixes for grammar transitions (#58)
Browse files Browse the repository at this point in the history
Fixes for grammar transitions
  • Loading branch information
afroozeh authored Nov 19, 2023
1 parent 0b39372 commit 1d6c8b9
Show file tree
Hide file tree
Showing 24 changed files with 413 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/org/iguana/grammar/GrammarGraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ private void visitSymbol(Symbol symbol) {
Conditions preconditions = i == 0 ? ConditionsFactory.DEFAULT : getConditions(symbol.getPreConditions());

if (symbol.getLabel() != null) {
BodyGrammarSlot declared = getBodyGrammarSlot(rule, i + 1, rule.getPosition(i + 1), null, null, null);
BodyGrammarSlot declared = getBodyGrammarSlot(rule, i + 1, rule.getPosition(i), null, null, null);
EpsilonTransition transition = new EpsilonTransition(Type.DECLARE_LABEL, symbol.getLabel(),
preconditions, currentSlot, declared);
setTransition(transition);
currentSlot = declared;
} else {
BodyGrammarSlot checked = getBodyGrammarSlot(rule, i + 1, rule.getPosition(i + 1), null, null, null);
BodyGrammarSlot checked = getBodyGrammarSlot(rule, i + 1, rule.getPosition(i), null, null, null);
EpsilonTransition transition = new EpsilonTransition(preconditions, currentSlot, checked);
setTransition(transition);
currentSlot = checked;
Expand Down
14 changes: 5 additions & 9 deletions src/org/iguana/grammar/runtime/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
public class Position {

private final RuntimeRule rule;

private final int posInRule;
private final int posInSymbol;

Expand Down Expand Up @@ -69,15 +68,12 @@ public boolean isLast() {

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;

if (!(obj instanceof Position))
return false;

if (this == obj) return true;
if (!(obj instanceof Position)) return false;
Position other = (Position) obj;

return rule.equals(other.rule) && posInRule == other.posInRule && posInSymbol == other.posInSymbol;
return rule.equals(other.rule)
&& posInRule == other.posInRule
&& posInSymbol == other.posInSymbol;
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/org/iguana/grammar/slot/AbstractTransition.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public abstract class AbstractTransition implements Transition {
protected final BodyGrammarSlot origin;

public AbstractTransition(BodyGrammarSlot origin, BodyGrammarSlot dest) {
if (origin.equals(dest)) {
throw new IllegalArgumentException("Origin " + origin + " and Destination " + dest + " are equal");
}
this.origin = origin;
this.dest = dest;
if (origin == dest) {
throw new IllegalArgumentException("Origin " + origin + " and Destination " + dest + " are equal");
}
}

@Override
Expand Down
24 changes: 1 addition & 23 deletions src/org/iguana/grammar/slot/BodyGrammarSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.iguana.utils.input.Input;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

public class BodyGrammarSlot implements GrammarSlot {
Expand Down Expand Up @@ -113,8 +112,7 @@ public <T extends Result> T getIntermediateNode(
T rightResult,
Environment env,
IguanaRuntime<T> runtime) {
if (isFirst())
return rightResult;
if (isFirst()) return rightResult;

Key key = Keys.from(destinationIndex, rightResult.getRightExtent(), env);

Expand Down Expand Up @@ -268,24 +266,4 @@ public boolean isEnd() {
public FollowTest getFollowTest() {
return followTest;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BodyGrammarSlot that = (BodyGrammarSlot) o;
return i1 == that.i1
&& i2 == that.i2
&& Objects.equals(position, that.position)
&& Objects.equals(conditions, that.conditions)
&& Objects.equals(label, that.label)
&& Objects.equals(variable, that.variable)
&& Objects.equals(state, that.state)
&& Objects.equals(followTest, that.followTest);
}

@Override
public int hashCode() {
return Objects.hash(position, conditions, label, i1, variable, i2, state, followTest);
}
}
7 changes: 6 additions & 1 deletion src/org/iguana/grammar/slot/EpsilonTransition.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.iguana.util.Tuple;
import org.iguana.utils.input.Input;

import java.util.Objects;

public class EpsilonTransition extends AbstractTransition {

private final Type type;
Expand All @@ -63,7 +65,10 @@ public EpsilonTransition(
BodyGrammarSlot origin,
BodyGrammarSlot dest) {
super(origin, dest);

if (Objects.equals(origin.position, dest.position)) {
throw new IllegalArgumentException("Origin and destination positions must be the same for epsilon "
+ "transitions: " + origin.position + " is not equal to " + dest.position);
}
assert label != null && (type == Type.DECLARE_LABEL || type == Type.STORE_LABEL);

this.type = type;
Expand Down
6 changes: 3 additions & 3 deletions src/org/iguana/grammar/slot/NonterminalGrammarSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
import org.iguana.result.Result;
import org.iguana.util.Configuration.EnvironmentImpl;
import org.iguana.util.ParserLogger;
import org.iguana.utils.collections.IntHashMap;
import org.iguana.utils.collections.Keys;
import org.iguana.utils.collections.OpenAddressingHashMap;
import org.iguana.utils.collections.OpenAddressingIntHashMap;
import org.iguana.utils.collections.key.Key;
import org.iguana.utils.collections.primitive.IntHashMap;
import org.iguana.utils.collections.primitive.OpenAddressingIntHashMap;
import org.iguana.utils.collections.rangemap.RangeMap;
import org.iguana.utils.input.Input;

Expand Down Expand Up @@ -206,7 +206,7 @@ public <T extends Result> void create(

int inputIndex = result.isDummy() ? gssNode.getInputIndex() : result.getRightExtent();
if (!slot.getConditions().execute(input, returnSlot, gssNode, inputIndex, runtime.getEvaluatorContext(),
runtime)) {
runtime)) {
runtime.scheduleDescriptor(slot, gssNode, runtime.getResultOps().dummy(), runtime.getEnvironment());
}

Expand Down
8 changes: 3 additions & 5 deletions src/org/iguana/grammar/slot/TerminalGrammarSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.iguana.regex.matcher.Matcher;
import org.iguana.regex.matcher.MatcherFactory;
import org.iguana.result.Result;
import org.iguana.utils.collections.IntHashMap;
import org.iguana.utils.collections.OpenAddressingIntHashMap;
import org.iguana.utils.collections.primitive.IntHashMap;
import org.iguana.utils.collections.primitive.OpenAddressingIntHashMap;
import org.iguana.utils.input.Input;

public class TerminalGrammarSlot implements GrammarSlot {
Expand All @@ -55,9 +55,7 @@ public <T extends Result> T getResult(Input input, int i, IguanaRuntime<T> runti
terminalNodes = new OpenAddressingIntHashMap<>();
}
Object node = terminalNodes.get(i);
if (node == failure) {
return null;
}
if (node == failure) return null;

@SuppressWarnings("unchecked")
T terminalNode = (T) node;
Expand Down
3 changes: 2 additions & 1 deletion src/org/iguana/grammar/slot/TerminalTransition.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public <T extends Result> void execute(
return;
}

if (dest.getLabel() != null)
if (dest.getLabel() != null) {
runtime.getEvaluatorContext().declareVariable(dest.getLabel(), cr);
}

if (postConditions.execute(input, origin, u, cr.getLeftExtent(), cr.getRightExtent(),
runtime.getEvaluatorContext(), runtime)) {
Expand Down
4 changes: 2 additions & 2 deletions src/org/iguana/gss/StartGSSNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.iguana.parser.IguanaRuntime;
import org.iguana.result.Result;
import org.iguana.util.ParserLogger;
import org.iguana.utils.collections.IntHashMap;
import org.iguana.utils.collections.OpenAddressingIntHashMap;
import org.iguana.utils.collections.primitive.IntHashMap;
import org.iguana.utils.collections.primitive.OpenAddressingIntHashMap;
import org.iguana.utils.input.Input;

import java.util.Collections;
Expand Down
25 changes: 9 additions & 16 deletions src/org/iguana/regex/matcher/DFAMatcherFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,14 @@ public class DFAMatcherFactory implements MatcherFactory {
private final Map<RegularExpression, Matcher> matcherCache = new HashMap<>();
private final Map<RegularExpression, Matcher> backwardsMatcherCache = new HashMap<>();

public org.iguana.regex.matcher.Matcher getMatcher(RegularExpression regex) {

if (regex == Epsilon.getInstance())
return epsilonMatcher();

if (regex instanceof Char)
return characterMatcher((Char) regex);

if (regex instanceof CharRange)
return characterRangeMatcher((CharRange) regex);

public Matcher getMatcher(RegularExpression regex) {
if (regex == Epsilon.getInstance()) return epsilonMatcher();
if (regex instanceof Char) return characterMatcher((Char) regex);
if (regex instanceof CharRange) return characterRangeMatcher((CharRange) regex);
return matcherCache.computeIfAbsent(regex, DFAMatcher::new);
}

public org.iguana.regex.matcher.Matcher getBackwardsMatcher(RegularExpression regex) {
public Matcher getBackwardsMatcher(RegularExpression regex) {

if (regex instanceof Char)
return characterBackwardsMatcher((Char) regex);
Expand All @@ -65,19 +58,19 @@ public org.iguana.regex.matcher.Matcher getBackwardsMatcher(RegularExpression re
return backwardsMatcherCache.computeIfAbsent(regex, DFABackwardsMatcher::new);
}

public static org.iguana.regex.matcher.Matcher characterMatcher(Char c) {
public static Matcher characterMatcher(Char c) {
return (input, i) -> input.charAt(i) == c.getValue() ? 1 : -1;
}

public static org.iguana.regex.matcher.Matcher characterBackwardsMatcher(Char c) {
public static Matcher characterBackwardsMatcher(Char c) {
return (input, i) -> i == 0 ? -1 : (input.charAt(i - 1) == c.getValue() ? 1 : -1);
}

public static org.iguana.regex.matcher.Matcher characterRangeMatcher(CharRange range) {
public static Matcher characterRangeMatcher(CharRange range) {
return (input, i) -> input.charAt(i) >= range.getStart() && input.charAt(i) <= range.getEnd() ? 1 : -1;
}

public static org.iguana.regex.matcher.Matcher characterRangeBackwardsMatcher(CharRange range) {
public static Matcher characterRangeBackwardsMatcher(CharRange range) {
return (input, i) -> i == 0 ? -1
: (input.charAt(i - 1) >= range.getStart() && input.charAt(i - 1) <= range.getEnd() ? 1 : -1);
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/iguana/traversal/idea/GenerateJFlex.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ public String visit(org.iguana.regex.Opt o) {

@Override
public <E extends RegularExpression> String visit(org.iguana.regex.Alt<E> symbol) {
Map<Boolean, List<E>> parition = symbol.getSymbols().stream().collect(
Map<Boolean, List<E>> partition = symbol.getSymbols().stream().collect(
Collectors.partitioningBy(s -> isCharClass(s)));
List<E> charClasses = parition.get(true);
List<E> other = parition.get(false);
List<E> charClasses = partition.get(true);
List<E> other = partition.get(false);

StringBuilder sb = new StringBuilder();

Expand Down
10 changes: 5 additions & 5 deletions src/org/iguana/util/visualization/GrammarGraphToDot.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ private static void toDot(NonterminalGrammarSlot slot, DotGraph dotGraph, Set<In
dotGraph.addNode(node);

slot.getFirstSlots().forEach(s -> dotGraph.addEdge(newEdge(slotId, getId(s), "")));
slot.getFirstSlots().forEach(s -> toDot(s, dotGraph, visited));
slot.getFirstSlots().forEach(s -> toDot(s, dotGraph));
}

private static void toDot(BodyGrammarSlot slot, DotGraph dotGraph, Set<Integer> visited) {
private static void toDot(BodyGrammarSlot slot, DotGraph dotGraph) {
if (slot instanceof EndGrammarSlot) {
dotGraph.addNode(newNode(getId(slot)).setShape(DotGraph.Shape.DOUBLE_CIRCLE));
dotGraph.addNode(newNode(getId(slot), slot.toString()).setShape(DotGraph.Shape.ROUNDED_RECTANGLE));
} else {
dotGraph.addNode(newNode(getId(slot), slot.toString()).setShape(DotGraph.Shape.CIRCLE));
dotGraph.addNode(newNode(getId(slot), slot.toString()).setShape(DotGraph.Shape.ROUNDED_RECTANGLE));

// TODO: improve this code
Transition t = slot.getOutTransition();
Expand All @@ -110,7 +110,7 @@ private static void toDot(BodyGrammarSlot slot, DotGraph dotGraph, Set<Integer>
dotGraph.addEdge(newEdge(getId(slot), getId(t.destination()), t.getLabel()));
}

toDot(t.destination(), dotGraph, visited);
toDot(t.destination(), dotGraph);
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/org/iguana/utils/collections/CollectionsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package org.iguana.utils.collections;

import org.iguana.util.Tuple;
import org.iguana.utils.collections.primitive.IntIterable;
import org.iguana.utils.collections.primitive.IntIterator;

import java.util.ArrayList;
Expand Down Expand Up @@ -129,11 +128,7 @@ public static <T> T last(List<T> list) {
return list.get(list.size() - 1);
}

public static boolean isEqual(
org.iguana.utils.collections.primitive.IntIterable iterables1,
IntIterable iterables2) {
org.iguana.utils.collections.primitive.IntIterator it1 = iterables1.iterator();
IntIterator it2 = iterables2.iterator();
public static boolean isEqual(IntIterator it1, IntIterator it2) {
while (it1.hasNext()) {
if (!it2.hasNext()) return false;
int t1 = it1.next();
Expand Down
Loading

0 comments on commit 1d6c8b9

Please sign in to comment.