petitparser / dart-petitparser

Dynamic parser combinators in Dart.
https://pub.dartlang.org/packages/petitparser
MIT License
453 stars 47 forks source link

Getting line and colone in the code. #140

Closed Oualitsen closed 1 year ago

Oualitsen commented 1 year ago

Hello, I have a side project that is a parser for graphql. the main goal of it is to generate classes from a graphql schema. I cannot seem to find a way to get the line and the column of the code that is being parsed. I want to throw some ParseException with a good description and the where (namely the line : column) the issue came from. I have already used JavaCC for parsing a subset of the ECMAScript language as a school project and I remember that JavaCC has that. I wonder if petitParser has that or should we implement something to track the lines and the columns. Thank you, Ramdane.

renggli commented 1 year ago

The Token class provides such a helper: https://github.com/petitparser/dart-petitparser/blob/fc676d01b1854c32abc928960f09653ca1b0fe0c/lib/src/core/token.dart#L89

You can either use that static method, or build your own similar helper.

Example usage:

final result = parser.parse(input);
if (result.isFailure) {
  final lineAndColumn = Token.lineAndColumnOf(input, result.position);
  print('Error ${result.message} at ${lineAndColumn.join(':')}');
  ...
Oualitsen commented 1 year ago

Thank you very much for your fast response and Good Job by the way Lukas :)