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

Not (not) capturing #66

Closed
kans opened this issue Nov 8, 2023 · 2 comments
Closed

Not (not) capturing #66

kans opened this issue Nov 8, 2023 · 2 comments
Assignees

Comments

@kans
Copy link

kans commented Nov 8, 2023

I'd like match any char that isn't a newline.

This works as expected:

> newline().pipe(not()).parse('\n')
ParjsFailure {
  trace: {
    userState: ParserUserState {},
    position: 0,
    reason: 'not expecting: expecting newline',
    input: '\n',
    location: [Getter],
    stackTrace: [ [Not] ],
    kind: 'Soft'
  }
}

In the following example, no input is consumed:

> newline().pipe(not()).parse('a')
ParjsFailure {
  trace: {
    userState: ParserUserState {},
    position: 0,
    reason: 'parsers did not consume all input',
    input: 'a',
    location: [Getter],
    stackTrace: [],
    kind: 'Soft'
  }
}

Is the intended way to write something like this using then?

>  newline().pipe(not()).pipe(then(anyChar())).parse('a')
@barona-mika-vilpas barona-mika-vilpas self-assigned this Nov 18, 2023
@barona-mika-vilpas
Copy link
Collaborator

barona-mika-vilpas commented Nov 21, 2023

Hi 👋

I think you're right. Your second example creates a parser that cannot parse a newline (but does not parse anything else), and then that parser succeeds at failing to parse the newline. However, there is still text to parse, so parjs throws an error (you must handle all input by default).

You have two options:

it("can parse with anyChar()", () => {
    const p = newline().pipe(not(), qthen(anyChar()));
    expect(p.parse("a")).toBeSuccessful("a");
    expect(p.parse("\n")).toBeFailure(ResultKind.SoftFail);
});

it("can parse with must", () => {
    const p = anyChar().pipe(must(c => c !== "\n" || { kind: "Soft" }));
    expect(p.parse("a")).toBeSuccessful("a");
    expect(p.parse("\n")).toBeFailure(ResultKind.SoftFail);
});

@barona-mika-vilpas
Copy link
Collaborator

Closing this as it's been inactive for some weeks now. Feel free to reopen if needed.

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

No branches or pull requests

2 participants