|
| 1 | +module gamma.grammar.hyper.HyperGrammarBuilder; |
| 2 | + |
| 3 | +import gamma.grammar.affixes.Signature; |
| 4 | +import gamma.grammar.Alternative; |
| 5 | +import gamma.grammar.GrammarBuilder; |
| 6 | +import gamma.grammar.hyper.AnonymousNonterminal; |
| 7 | +import gamma.grammar.hyper.Group; |
| 8 | +import gamma.grammar.hyper.HyperLhsNode; |
| 9 | +import gamma.grammar.hyper.Operator; |
| 10 | +import gamma.grammar.hyper.Option; |
| 11 | +import gamma.grammar.hyper.Repetition; |
| 12 | +import gamma.grammar.Node; |
| 13 | +import gamma.grammar.Nonterminal; |
| 14 | +import gamma.grammar.Rule; |
| 15 | +import std.range; |
| 16 | + |
| 17 | +public struct HyperGrammarBuilder |
| 18 | +{ |
| 19 | + GrammarBuilder builder; |
| 20 | + |
| 21 | + alias builder this; |
| 22 | + |
| 23 | + public AnonymousNonterminal buildAnonymousNonterminal() |
| 24 | + { |
| 25 | + import std.exception : enforce; |
| 26 | + import std.format : format; |
| 27 | + |
| 28 | + const index = builder.nonterminals.length; |
| 29 | + auto nonterminal = new AnonymousNonterminal(index); |
| 30 | + |
| 31 | + enforce(nonterminal.toString !in builder.nonterminalMap, |
| 32 | + format!"generated nonterminal name already defined by the user: %s"(nonterminal)); |
| 33 | + |
| 34 | + builder.nonterminalMap[nonterminal.toString] = nonterminal; |
| 35 | + builder.nonterminals ~= nonterminal; |
| 36 | + builder.alternativesMap ~= null; |
| 37 | + |
| 38 | + return nonterminal; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Replaces each anonymous nonterminal whose EBNF operator appears exclusively on the RHS |
| 43 | + * of a single alternative with the nonterminal from the LHS. |
| 44 | + * |
| 45 | + * Returns: a map of nonterminal → signature for each nonterminal that was inlined |
| 46 | + * and whose operator carried formal parameters. |
| 47 | + */ |
| 48 | + public Signature[Nonterminal] inlineSingleOperators() |
| 49 | + { |
| 50 | + import std.algorithm : map; |
| 51 | + import std.array : array; |
| 52 | + |
| 53 | + Signature[Nonterminal] signatureByNonterminal; |
| 54 | + |
| 55 | + foreach (nonterminal; builder.nonterminals) |
| 56 | + { |
| 57 | + if (cast(AnonymousNonterminal) nonterminal) |
| 58 | + continue; |
| 59 | + |
| 60 | + auto alternatives = builder.alternativesMap[nonterminal.index]; |
| 61 | + |
| 62 | + if (alternatives.length != 1) |
| 63 | + continue; |
| 64 | + |
| 65 | + auto alternative = alternatives.front; |
| 66 | + |
| 67 | + if (alternative.rhs.length != 1) |
| 68 | + continue; |
| 69 | + |
| 70 | + auto operator = cast(Operator) alternative.rhs.front; |
| 71 | + |
| 72 | + if (operator is null) |
| 73 | + continue; |
| 74 | + |
| 75 | + auto alternativeLhs = cast(HyperLhsNode) alternative.lhs; |
| 76 | + |
| 77 | + if (alternativeLhs !is null && alternativeLhs.params !is null) |
| 78 | + continue; |
| 79 | + |
| 80 | + auto rule = new Rule(operator.rule.alternatives.map!(a => a.rebuild(nonterminal)).array); |
| 81 | + Node rebuiltOperator = rebuildOperator(operator, rule); |
| 82 | + auto rebuiltAlternative = new Alternative(alternative.lhs, [rebuiltOperator], alternative.position); |
| 83 | + |
| 84 | + builder.alternativesMap[nonterminal.index][0] = rebuiltAlternative; |
| 85 | + |
| 86 | + if (auto lhsNode = cast(HyperLhsNode) rule.lhs) |
| 87 | + if (lhsNode.signature !is null) |
| 88 | + signatureByNonterminal[nonterminal] = lhsNode.signature; |
| 89 | + } |
| 90 | + return signatureByNonterminal; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +private Operator rebuildOperator(Operator operator, Rule rule) |
| 95 | +{ |
| 96 | + if (auto group = cast(Group) operator) |
| 97 | + return new Group(group.params, rule, group.position); |
| 98 | + if (auto option = cast(Option) operator) |
| 99 | + return new Option(option.params, rule, option.endParams, option.position); |
| 100 | + if (auto repetition = cast(Repetition) operator) |
| 101 | + return new Repetition(repetition.params, rule, repetition.endParams, repetition.position); |
| 102 | + |
| 103 | + assert(false, "unknown operator type"); |
| 104 | +} |
| 105 | + |
| 106 | +private Alternative rebuild(Alternative alternative, Nonterminal nonterminal) |
| 107 | +{ |
| 108 | + import gamma.grammar.hyper.RepetitionAlternative : RepetitionAlternative; |
| 109 | + |
| 110 | + auto alternativeLhs = cast(HyperLhsNode) alternative.lhs; |
| 111 | + auto lhs = new HyperLhsNode(nonterminal, alternativeLhs.signature, alternativeLhs.params, alternativeLhs.position); |
| 112 | + |
| 113 | + if (auto repetitionAlt = cast(RepetitionAlternative) alternative) |
| 114 | + return new RepetitionAlternative(lhs, alternative.rhs, repetitionAlt.params, alternative.position); |
| 115 | + return new Alternative(lhs, alternative.rhs, alternative.position); |
| 116 | +} |
0 commit comments