E602 A grammar takes itself inΒΆ

A grammar extends or imports a module that takes the grammar in again, through the grammars it extends and imports in turn, so that joining them would not end. Only modules given with a grammar can do so, since those that come with fpeg take in none of their own, so this page shows Python, where modules are given.

import fpeg

modules = {
    "a": '%import b (y)\nx: "x" y',
    "b": '%import a (x)\ny: "y"',
}

try:
    fpeg.Grammar("%import a (x)\nstart: x", modules=modules)
except fpeg.GrammarError as error:
    (problem,) = error.diagnostics
    assert problem.code == "E602"
    assert str(problem) == (
        "<grammar>:1:9: error[E602]: the grammar a takes itself in through the grammars it "
        "extends and imports"
    )

Move the rules that both need into a module of their own, which both import.

modules = {
    "a": '%import b (y)\nx: "x" y',
    "b": 'y: "y"',
}
grammar = fpeg.Grammar("%import a (x)\nstart: x", modules=modules)
assert grammar.parse("xy").data == "start"