Skip to content

Commit

Permalink
#2196 Enhance BNF with the ability to specify labels in rule bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
homedirectory committed Feb 23, 2024
1 parent 97a41dc commit 19841e5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,23 @@ private void add(final Token t) {
tokens.add(t);
}

private void add(final LabeledTempSymbol symbol) {
add(symbol.symbol().normalize());
}

private void add(final Symbol symbol) {
switch (symbol) {
case Variable v -> add(v);
case Token tok -> add(tok);
case LabeledTempSymbol s -> add(s);
default -> throw new IllegalStateException("Unexpected value: " + symbol);
}
}

private void add(final Component component) {
switch (component) {
case Quantifier q -> add(q);
case Token tok -> add(tok);
case Variable v -> add(v);
case Symbol sym -> add(sym);
default -> throw new IllegalStateException("Unexpected value: " + component);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package fielden.platform.eql.fling;

import il.ac.technion.cs.fling.internal.grammar.rules.*;

import java.util.Objects;

public abstract sealed class LabeledTempSymbol<T extends TempSymbol> implements TempSymbol {
protected final String label;
protected final T symbol;

LabeledTempSymbol(String label, T symbol) {
this.label = label;
this.symbol = symbol;
}

@Override
public String name() {
return symbol.name();
}

public String label() {
return label;
}

public T symbol() {
return symbol;
}

@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
var that = (LabeledTempSymbol) obj;
return Objects.equals(this.label, that.label) &&
Objects.equals(this.symbol, that.symbol);
}

@Override
public int hashCode() {
return Objects.hash(label, symbol);
}

@Override
public String toString() {
return "%s=%s".formatted(label, symbol);
}

// if the label support needs to be extended to other types (e.g., Token)
// create a corresponding class and a static method in a manner similar to LabeledVariable

public static LabeledVariable label(String label, Variable variable) {
return new LabeledVariable(label, variable);
}

public static final class LabeledVariable extends LabeledTempSymbol<Variable> implements Variable {
public LabeledVariable(final String label, final Variable symbol) {
super(label, symbol);
}

@Override
public Symbol normalize() {
return this;
}
}

public static LabeledTerminal label(String label, Terminal variable) {
return new LabeledTerminal(label, variable);
}

public static final class LabeledTerminal extends LabeledTempSymbol<Terminal> implements Terminal {
public LabeledTerminal(final String label, final Terminal symbol) {
super(label, symbol);
}

@Override
public Token normalize() {
return new Token(this);
}
}

}

0 comments on commit 19841e5

Please sign in to comment.