Skip to content

Reporting parsing erros #184

@dubik

Description

@dubik

I saw previous question about this topic #98 but unfortunately it doesn't seem to work, or at least I don't understand what I'm doing wrong.
I'm trying to parse protobuf like grammar:

class ProtobufGrammar extends GrammarDefinition {
  @override
  Parser start() => ref0(file).end();

  Parser file() => ref0(definition).star();

  Parser definition() => ref0(message);

  Parser message() =>
      string('message').trim() &
      ref0(identifier).trim() &
      char('{').trim() &
      char('}').trim();

  Parser identifier() => (letter() & word().star())
      .flatten("identifier is expected");
}

void main() {
  final parser = ProtobufGrammar().build();
  test('message without name', () {
    final input = 'message ';
    final result = parser.parse(input);
    // Would be great to have an error: identifier is expected
    expect(result, isSuccess());
  });

  test('message with name', () {
    final input = 'message Empty';
    final result = parser.parse(input);
    // Would be great to have an error: '{' is expected
    expect(result, isSuccess());
  });

  test('Bad identifier', () {
    final input = 'message f123';
    final result = parser.parse(input);
    expect(result, isSuccess());
  });
}

All test cases fail (that's correct, since input is malformed), with the same error message: <Failure[1:1]: end of input expected>
After reading #98 I would expected Bad identifer to report identifier is expected but it's not. I tried to use .or(failure("identifier is expected") but it didn't do anything.

I think it goes into:

  Parser message() =>
      string('message').trim() &

and since it can't satisfy ref0(identifier).trim() it bails out, and tries another parser. But since there is no alternative with message (there is single parser anyway), I would like it to fail with something else than generic end of input expected. Is there a way, thanks?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions