use of suite.ebnf.Grammar in project suite by stupidsing.
the class ReduceHeadRecursion method getHeadRecursionForm.
private HeadRecursionForm getHeadRecursionForm(Grammar en0, String entity) {
List<Grammar> empty = List.of();
Grammar en = expand(en0);
HeadRecursionForm hrf;
if (en.type == GrammarType.AND___ && en.children.isEmpty())
hrf = new HeadRecursionForm(empty, empty);
else if (en.type == GrammarType.AND___) {
HeadRecursionForm hrf0 = getHeadRecursionForm(en.children.get(0), entity);
List<Grammar> tail = List_.right(en.children, 1);
Fun<List<Grammar>, List<Grammar>> fun = list -> Read.from(list).map(en_ -> {
List<Grammar> ens1 = new ArrayList<>();
ens1.add(en_);
ens1.addAll(tail);
return new Grammar(GrammarType.AND___, ens1);
}).toList();
hrf = new HeadRecursionForm(fun.apply(hrf0.listb), fun.apply(hrf0.listc));
} else if (en.type == GrammarType.NAMED_ && String_.equals(en.content, entity))
hrf = new HeadRecursionForm(empty, List.of(new Grammar(GrammarType.AND___)));
else if (en.type == GrammarType.OR____) {
List<HeadRecursionForm> hrfs = Read.from(en.children).map(en_ -> getHeadRecursionForm(en_, entity)).toList();
List<Grammar> listb = Read.from(hrfs).flatMap(hrf_ -> hrf_.listb).toList();
List<Grammar> listc = Read.from(hrfs).flatMap(hrf_ -> hrf_.listc).toList();
hrf = new HeadRecursionForm(listb, listc);
} else
hrf = new HeadRecursionForm(List.of(en), empty);
return hrf;
}
Aggregations