-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2178 Enhance the BNF abstraction with the Alternation type
- Loading branch information
1 parent
a0ff944
commit 51fa045
Showing
17 changed files
with
223 additions
and
96 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
platform-eql-grammar/src/main/java/fielden/platform/bnf/Alternation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fielden.platform.bnf; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
public record Alternation(List<Sequence> options, TermMetadata metadata) implements Notation { | ||
|
||
public Alternation(final List<Sequence> options, final TermMetadata metadata) { | ||
this.options = List.copyOf(options); | ||
this.metadata = metadata; | ||
} | ||
|
||
public Alternation(List<Sequence> options) { | ||
this(options, TermMetadata.EMPTY_METADATA); | ||
} | ||
|
||
@Override | ||
public <V> Alternation annotate(TermMetadata.Key<V> key, V value) { | ||
return new Alternation(options, TermMetadata.merge(metadata(), key, value)); | ||
} | ||
|
||
@Override | ||
public Alternation normalize() { | ||
return metadata == TermMetadata.EMPTY_METADATA ? this : new Alternation(options); | ||
} | ||
|
||
@Override | ||
public Alternation recMap(final Function<? super Term, ? extends Term> mapper) { | ||
return new Alternation(options.stream().map(seq -> seq.recMap(mapper)).toList(), metadata); | ||
} | ||
|
||
@Override | ||
public Stream<Term> flatten() { | ||
return options.stream().flatMap(Sequence::flatten); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return options().stream().map(Term::toString).collect(joining(" | ")); | ||
} | ||
|
||
} |
48 changes: 5 additions & 43 deletions
48
platform-eql-grammar/src/main/java/fielden/platform/bnf/Derivation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,17 @@ | ||
package fielden.platform.bnf; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
public record Derivation(Variable lhs, Alternation rhs) implements Rule { | ||
|
||
public final class Derivation implements Rule { | ||
|
||
private final Variable lhs; | ||
private final List<Sequence> rhs; | ||
|
||
public Derivation(Variable lhs, List<Sequence> rhs) { | ||
this.lhs = lhs; | ||
this.rhs = rhs; | ||
} | ||
|
||
@Override | ||
public Stream<Sequence> rhs() { | ||
return rhs.stream(); | ||
} | ||
|
||
@Override | ||
public Variable lhs() { | ||
return lhs; | ||
} | ||
|
||
public Rule map(final Function<? super Term, ? extends Term> mapper) { | ||
return new Derivation(lhs, rhs.stream().map(seq -> seq.map(mapper)).toList()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj == this || | ||
(obj instanceof Derivation that && | ||
Objects.equals(this.lhs, that.lhs) && | ||
Objects.equals(this.rhs, that.rhs)); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(lhs, rhs); | ||
public Rule recMap(final Function<? super Term, ? extends Term> mapper) { | ||
return new Derivation(lhs, rhs.recMap(mapper)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "%s -> %s".formatted( | ||
lhs.name(), | ||
rhs.stream().map(terms -> terms.stream().map(Term::toString).collect(joining(" "))) | ||
.collect(joining(" | "))); | ||
return "%s -> %s".formatted(lhs.name(), rhs); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
platform-eql-grammar/src/main/java/fielden/platform/bnf/Quantifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package fielden.platform.bnf; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public sealed interface Quantifier extends Notation permits OneOrMore, Optional, ZeroOrMore { | ||
|
||
Term term(); | ||
|
||
@Override | ||
default Stream<Term> flatten() { | ||
return term().flatten(); | ||
} | ||
|
||
} |
9 changes: 1 addition & 8 deletions
9
platform-eql-grammar/src/main/java/fielden/platform/bnf/Rule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,9 @@ | ||
package fielden.platform.bnf; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Stream; | ||
|
||
public sealed interface Rule permits Derivation, Specialization { | ||
|
||
Variable lhs(); | ||
|
||
Stream<Sequence> rhs(); | ||
|
||
default Stream<Term> rhsTerms() { | ||
return rhs().flatMap(Collection::stream); | ||
} | ||
Alternation rhs(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
platform-eql-grammar/src/main/java/fielden/platform/bnf/Specialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
platform-eql-grammar/src/main/java/fielden/platform/bnf/Symbol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package fielden.platform.bnf; | ||
|
||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
public sealed interface Symbol extends Term permits Variable, Terminal { | ||
|
||
String name(); | ||
|
||
@Override | ||
default Stream<Term> flatten() { | ||
return Stream.of(this); | ||
} | ||
|
||
@Override | ||
default Term recMap(Function<? super Term, ? extends Term> mapper) { | ||
return mapper.apply(this); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.