2

I'm trying out PetitParser for parsing a simple integer list delimited by commas. For example: "1, 2, 3, 4"

I tried creating a integer parser and then use the delimitedBy method.

Parser integerParser = digit().plus().flatten().trim().map((String value) -> Integer.parseInt(value));
Parser listParser = integerParser.delimitedBy(of(','));

List<Integer> list = listParser.parse(input).get();

This returns a list with the parsed integers but also the delimiters. For example: [1, ,, 2, ,, 3, ,, 4]

Is there a way to exclude the delimiters from the result?

  • Do you need this `Parser` for it? You can do it without any external library... – deHaar Aug 08 '19 at 13:31
  • Try this: List integerList = Arrays.stream("1, 2, 3, 4".split(",")) .map(i -> Integer.parseInt(i.trim())) .collect(Collectors.toList()); – arifng Aug 08 '19 at 13:55
  • I'm aware that this is simple enough to implement with pure java. I'm trying a simple parse because I'm new to PetitParser. – Miguel Figueiredo Aug 08 '19 at 18:30

3 Answers3

2

Yes, there is:

Parser listParser = integerParser
    .delimitedBy(of(','))
    .map(withoutSeparators());

To get withoutSeparators() import import static org.petitparser.utils.Functions.withoutSeparators;.

Lukas Renggli
  • 8,754
  • 23
  • 46
0

Here is an example of how to do it without any external library:

public static void main(String args[]) {
    // source String
    String delimitedNumbers = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10";
    // split this String by its delimiter, a comma here
    String[] delimitedNumbersSplit = delimitedNumbers.split(",");

    // provide a data structure that holds numbers (integers) only
    List<Integer> numberList = new ArrayList<>();

    // for each part of the split list
    for (String num : delimitedNumbersSplit) {
        // remove all whitespaces and parse the number
        int n = Integer.parseInt(num.trim());
        // add the number to the list of numbers
        numberList.add(n);
    }

    // then create the representation you want to print
    StringBuilder sb = new StringBuilder();
    // [Java 8] concatenate the numbers to a String that delimits by whitespace
    numberList.forEach(number -> sb.append(number).append(" "));
    // then remove the trailing whitespace
    String numbersWithoutCommas = sb.toString();
    numbersWithoutCommas = numbersWithoutCommas.substring(0, numbersWithoutCommas.length() - 1);
    // and print the result
    System.out.println(numbersWithoutCommas);
}

Note that you don't need to trim() the results of the split String if you have a list without whitespaces.

In case you need the PetitParser library, you will have to look up how to use it in its docs.

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Thanks for the reply. The goal here is to try out PetitParser with simple examples. Unfortunately the documentation is not detailed. I have to dig in the source code ;) – Miguel Figueiredo Aug 08 '19 at 19:27
0

With a bit more code I got the result without the delimiters:

Parser number = digit().plus().flatten().trim()
        .map((String value) -> Integer.parseInt(value));

Parser next = of(',').seq(number).map((List<?> input) -> input.get(1)).star();

Parser parser = number.seq(next).map((List<List<?>> input) -> {
    List<Object> result = new ArrayList<>();
    result.add(input.get(0));
    result.addAll(input.get(1));
    return result;
});