2

I'm trying to debug an undefined parser in petitparser.

See the code without debug first:

import "package:petitparser/petitparser.dart";

main() {
  var mynum = undefined();
  var parser = string("abc").map((s) {
    mynum.set(string("888"));
    return s;
  }) & mynum;

  var result = parser.parse("abc888");
  print(result);
}

It prints:

Success[1:7]: [abc, 888]

But if I add debug:

import "package:petitparser/petitparser.dart";

main() {
  var mynum = undefined();
  var parser = string("abc").map((s) {
    mynum.set(string("888"));
    return s;
  }) & mynum;

  var result = debug(parser).parse("abc888");
  print(result);
}

It will fail:

Instance of 'SequenceParser'
  Instance of 'ActionParser'
    Instance of 'PredicateParser'[abc expected]
    Success[1:4]: abc
  Success[1:4]: abc
  Instance of 'SetableParser'
    Instance of 'FailureParser'[undefined parser]
    Failure[1:4]: undefined parser
  Failure[1:4]: undefined parser
Failure[1:4]: undefined parser
Failure[1:4]: undefined parser

Is there anything wrong in my usage?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

1

The current implementation of Parser debug(Parser) doesn't work well with self-modifying parsers. Its implementation uses Parser transformParser(Parser, Function) that creates a transformed copy of the complete grammar graph. Thus, you action function doesn't have any effect on the running code as it modifies the unused old version of the grammar.

I can think of a way to fix the problem (debug could modify the parser in-place), but that seems to have other disadvantages. Care to file and issue on GitHub to discuss this further?

Lukas Renggli
  • 8,754
  • 23
  • 46