Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backtracking support through &try/backtrack() #606

Merged
merged 3 commits into from
Dec 10, 2020

Conversation

rsmmr
Copy link
Member

@rsmmr rsmmr commented Dec 2, 2020

No description provided.

@rsmmr rsmmr changed the title Topic/robin/gh 25 backtrack Backtracking support through &try/backtrack() Dec 2, 2020
@rsmmr rsmmr requested a review from bbannier December 3, 2020 08:09
Copy link
Member

@bbannier bbannier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great tool so users can build their own tooling to parse conditionally. At the same time, I could see this being used to implement error handling for which this patch doesn't seem to go all the way or at least is missing tests (e.g., can backtrack be caught by on %error handlers; guaranteed interaction of plain old parse errors and &try fields; no access to particular error when &try field fails to parse). I left a few comments along those lines below. Like discussed offline we probably still need a better idea how we want error handling to finally look like.

tests/spicy/types/vector/parse-eod.spicy Outdated Show resolved Hide resolved
tests/spicy/types/unit/backtrack.spicy Show resolved Hide resolved
doc/programming/parsing.rst Outdated Show resolved Hide resolved
spicy/runtime/src/parser.cc Show resolved Hide resolved
spicy/toolchain/include/ast/operators/unit.h Show resolved Hide resolved
tests/spicy/types/unit/backtrack-lah.spicy Show resolved Hide resolved
tests/spicy/types/unit/backtrack-lah.spicy Show resolved Hide resolved
That tells a vector explicitly to parse until it hits the end of the
input, without having to use the look-ahead machinery to figure out when
to stop.
@rsmmr rsmmr force-pushed the topic/robin/gh-25-backtrack branch from 8f1a3aa to f4b9f4b Compare December 8, 2020 08:30
bbannier
bbannier previously approved these changes Dec 9, 2020
If a field is marked with ``&try``, a later call to ``self.backtrack()``
anywhere down in the parse tree will return to that position and
continue there. (This is ported over the old prototype).

Example:

    export type test = unit {
        a: bytes &length=4;
        foo: Foo &try;
        bar: Bar;
        b: bytes &length=6;

        on %done { print self; }
    };

    type Foo = unit {
        a: int8 { print "Foo.a", self; }
        b: int8 { print "Backtracking"; self.backtrack(); }
        c: int8 { print "Foo.c", self; }
    };

    type Bar = unit {
        a: int8 { print "Bar.a", self; }
        b: int8 { print "Bar.b", self; }
        c: int8 { print "Bar.c", self; }
    };

    # printf '1234\001\002\003567890' | pac-driver backtrack.pac2
    Foo.a <a=1, b=(not set), c=(not set)>
    Backtracking
    Bar.a <a=1, b=(not set), c=(not set)>
    Bar.b <a=1, b=2, c=(not set)>
    Bar.c <a=1, b=2, c=3>
    <a=1234, foo=(Null), bar=<a=1, b=2, c=3>, b=567890>

Closes #25.
When an example contained multiple commands separated by semicolons,
we'd execute them separately but still include all of them into each of
the command lines emitted into the docs.

(Couple of doc outputs show slightly changed content, including updates
into this commit.)
@rsmmr rsmmr force-pushed the topic/robin/gh-25-backtrack branch from f4b9f4b to 2038055 Compare December 9, 2020 10:05
@rsmmr rsmmr self-assigned this Dec 10, 2020
@rsmmr rsmmr merged commit f6bce51 into master Dec 10, 2020
@rsmmr rsmmr deleted the topic/robin/gh-25-backtrack branch December 10, 2020 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants